-
Notifications
You must be signed in to change notification settings - Fork 1
/
experiment_with_constant_marginal_costs.py
109 lines (93 loc) · 3.82 KB
/
experiment_with_constant_marginal_costs.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""# Description of the experiment:
#
# - We generate the costs to up to 5.000 tasks for 10 and 100 resources.
# - All costs follow linear functions (i.e., constant marginal costs)
# with RNG seeds [100..199].
# - We schedule from 1.000 to 5.000 tasks in increments of 100.
# - We run (MC)^2MKP, MarIn, MarCo, MarDec, and FedAvg.
# - All resources have a lower limit of 5.
# - The first half of the resources have no upper limit.
# - The second half has an upper limit of 2*(tasks/resources).
# - Every result is verified and logged to a CSV file.
"""
import numpy as np
import code.support as support
import code.schedulers as schedulers
import code.devices as devices
# File containing the results
logger = support.Logger('results_with_constant_marginal_costs.csv')
rng_seed_resources = 100
min_tasks = 1000
max_tasks = 5001
step_tasks = 100
def run_constant_marginal_costs():
# Stores the description of the experiments
logger.header(__doc__)
# Header of the CSV file
logger.store('Scheduler,Tasks,Resources,Total Cost')
# Runs experiments for 10 resources
run_for_n_resources(10)
# Runs experiments for 100 resources
run_for_n_resources(100)
# Finishes logging
logger.finish()
def run_for_n_resources(resources):
"""
Runs experiments for a number of resources.
Parameters
----------
resources : int
Number of resources
"""
print(f'- Running experiment for {resources} resources.')
# Initializes the cost matrix with zeros
cost = np.zeros(shape=(resources, max_tasks+1))
# Fills the cost matrix with costs based on a linear function
base_seed = rng_seed_resources
for i in range(resources):
devices.create_linear_costs(base_seed, cost, i, max_tasks)
base_seed += 1
# Prepares the lower limit array
lower_limit = np.full(shape=resources, fill_value=5, dtype=int)
# Iterates over the number of tasks running all schedulers
for tasks in range(min_tasks, max_tasks, step_tasks):
if tasks % 1000 == 0:
print(f'-- Running with {tasks} tasks.')
# Prepares the upper limit array
upper_limit = np.full(shape=resources, fill_value=tasks, dtype=int)
np.put(upper_limit, np.arange(resources//2, resources), 2*(tasks//resources))
# Runs the algorithms
a = schedulers.mc2mkp(tasks, resources, cost, lower_limit, upper_limit)
check_and_store('(MC)2MKP', tasks, resources, a, cost)
a = schedulers.marin(tasks, resources, cost, lower_limit, upper_limit)
check_and_store('MarIn', tasks, resources, a, cost)
a = schedulers.marco(tasks, resources, cost, lower_limit, upper_limit)
check_and_store('MarCo', tasks, resources, a, cost)
a = schedulers.mardec(tasks, resources, cost, lower_limit, upper_limit)
check_and_store('MarDec', tasks, resources, a, cost)
a = schedulers.fedavg(tasks, resources)
check_and_store('FedAvg', tasks, resources, a, cost)
def check_and_store(name, tasks, resources, assignment, cost):
"""
Checks if the results are correct and stores them in the logger.
Parameters
----------
name : string
Name of the scheduler
tasks : int
Number of tasks (tau)
resources : int
Number of resources (R)
assignment : np.array(shape=(resources))
Assignment of tasks to resources
cost : np.array(shape=(resources, tasks+1))
Cost functions per resource (C)
"""
total_cost = support.get_total_cost(cost, assignment)
if support.check_total_assigned(tasks, assignment) == False:
print(f'-- {name} failed to assign {tasks} tasks to' +
f' {resources} resources ({np.sum(assignment)}' +
' were assigned).')
logger.store(f'{name},{tasks},{resources},{total_cost}')
if __name__ == '__main__':
run_constant_marginal_costs()