-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulator.py
83 lines (75 loc) · 3.44 KB
/
emulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Author: DURUII
Date: 2023/12/23
Ref:
1. https://github.com/DURUII/Replica-AUCB/blob/main/emulator.py
"""
from algorithms.euwr import EUWR
from algorithms.first import EpsilonFirst
from algorithms.opt import Opt
from algorithms.rand import Random
from algorithms.uwr import UWR
from generator import EasyGenerator, GeoGenerator
from config import Config as config
import time
class Emulator:
algorithms = ['UWR', 'opt', '0.05-first', '0.1-first', 'random',
'extended-EUWR', 'extended-opt', 'extended-0.05-first', 'extended-0.1-first', 'extended-random',
]
def __init__(self, n_tasks: int = config.M,
n_workers: int = config.N,
r_selected: float = config.rK,
n_options: int = config.L,
budget: float = config.B,
f=config.f,
easy_generator=config.easy_generator,
extended=config.extended):
self.N = n_workers
self.M = n_tasks
self.B = budget
self.K = int(r_selected * self.N)
self.L = n_options
self.f = f
self.extended = extended
if easy_generator:
generator = EasyGenerator(n_tasks=self.M, n_workers=self.N, n_options=self.L, f=lambda x: x)
else:
generator = GeoGenerator(n_tasks=self.M, n_workers=self.N, n_options=self.L, f=lambda x: x)
self.tasks, self.workers = generator.generate()
self.name2sol = {}
def build(self):
for algo in Emulator.algorithms:
if self.extended:
if algo.startswith('extended'):
algo = algo[9:][:]
if algo == 'EUWR':
self.name2sol[algo] = EUWR(self.workers, self.tasks, self.K, self.B, self.f)
elif algo.endswith('-first'):
self.name2sol[algo] = EpsilonFirst(self.workers, self.tasks, self.K, self.B, self.f,
float(algo[:-6]), extended=True)
elif algo == 'random':
self.name2sol[algo] = Random(self.workers, self.tasks, self.K, self.B, self.f, extended=True)
elif algo == 'opt':
self.name2sol[algo] = Opt(self.workers, self.tasks, self.K, self.B, self.f, extended=True)
elif not algo.startswith('extended'):
if algo == 'UWR':
self.name2sol[algo] = UWR(self.workers, self.tasks, self.K, self.B, self.f)
elif algo.endswith('-first'):
self.name2sol[algo] = EpsilonFirst(self.workers, self.tasks, self.K, self.B, self.f,
float(algo[:-6]))
elif algo == 'random':
self.name2sol[algo] = Random(self.workers, self.tasks, self.K, self.B, self.f)
elif algo == 'opt':
self.name2sol[algo] = Opt(self.workers, self.tasks, self.K, self.B, self.f)
def simulate(self):
self.build()
name2res = {name: None for name in self.name2sol.keys()}
for name in name2res:
tic = time.perf_counter()
# instance of an algorithm
solver = self.name2sol[name]
solver.initialize()
name2res[name] = solver.run()
toc = time.perf_counter()
print(f'algo={name}, res={name2res[name]}, time={toc-tic}')
return name2res