-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_multi_objective.py
106 lines (95 loc) · 5.51 KB
/
main_multi_objective.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
#!/usr/bin/env python
# ------------------------------------------------------------------------------------------------------%
# Created by "Thieu" at 14:18, 22/01/2021 %
# %
# Email: nguyenthieu2102@gmail.com %
# Homepage: https://www.researchgate.net/profile/Nguyen_Thieu2 %
# Github: https://github.com/thieu1995 %
# ------------------------------------------------------------------------------------------------------%
from sklearn.model_selection import ParameterGrid
import multiprocessing
from time import time
from copy import deepcopy
from numpy import zeros, ones
from config import OptExp, OptParas
import optimizer
from utils.io_util import load_tasks, load_nodes
from utils.experiment_util import *
def inside_loop(my_model, n_trials, n_timebound, epoch, fe, end_paras):
for n_tasks in OptExp.N_TASKS:
tasks = load_tasks(f'{Config.INPUT_DATA}/tasks_{n_tasks}.json')
problem = deepcopy(my_model['problem'])
problem["tasks"] = tasks
problem["n_tasks"] = n_tasks
problem["shape"] = n_tasks
problem["lb"] = zeros(n_tasks)
problem["ub"] = (problem["n_clouds"] + problem["n_fogs"]) * ones(n_tasks)
for pop_size in OptExp.POP_SIZE:
parameters_grid = list(ParameterGrid(my_model["param_grid"]))
for paras in parameters_grid:
opt = getattr(optimizer, my_model["class"])(problem=problem, pop_size=pop_size, epoch=epoch,
func_eval=fe, lb=problem["lb"], ub=problem["ub"], verbose=OptExp.VERBOSE, paras=paras)
solutions, g_best, g_best_dict, training_info = opt.train()
if Config.TIME_BOUND_KEY:
path_results = f'{Config.RESULTS_DATA}/{n_timebound}s/task_{n_tasks}/{Config.METRICS}/{my_model["name"]}/{n_trials}'
else:
path_results = f'{Config.RESULTS_DATA}/no_time_bound/task_{n_tasks}/{Config.METRICS}/{my_model["name"]}/{n_trials}'
Path(path_results).mkdir(parents=True, exist_ok=True)
name_paras = f'{epoch}_{pop_size}_{end_paras}'
save_experiment_results_multi(solutions, g_best, g_best_dict, training_info, name_paras, path_results)
if Config.VISUAL_SAVING:
save_visualization_results_multi(solutions, my_model["name"], name_paras, path_results)
def setting_and_running(my_model):
print(f'Start running: {my_model["name"]}')
for n_trials in range(OptExp.N_TRIALS):
if Config.TIME_BOUND_KEY:
for n_timebound in OptExp.TIME_BOUND_VALUES:
if Config.MODE == "epoch":
for epoch in OptExp.EPOCH:
end_paras = f"{epoch}"
inside_loop(my_model, n_trials, n_timebound, epoch, None, end_paras)
elif Config.MODE == "fe":
for fe in OptExp.FE:
end_paras = f"{fe}"
inside_loop(my_model, n_trials, n_timebound, None, fe, end_paras)
else:
if Config.MODE == "epoch":
for epoch in OptExp.EPOCH:
end_paras = f"{epoch}"
inside_loop(my_model, n_trials, None, epoch, None, end_paras)
elif Config.MODE == "fe":
for fe in OptExp.FE:
end_paras = f"{fe}"
inside_loop(my_model, n_trials, None, None, fe, end_paras)
if __name__ == '__main__':
starttime = time()
clouds, fogs, peers = load_nodes(f'{Config.INPUT_DATA}/nodes_4_10_7.json')
problem = {
"clouds": clouds,
"fogs": fogs,
"peers": peers,
"n_clouds": len(clouds),
"n_fogs": len(fogs),
"n_peers": len(peers),
}
models = [
# {"name": "MO_IBLA", "class": "MO_IBLA", "param_grid": OptParas.MO_IBLA, "problem": problem},
# {"name": "NSGA-II", "class": "BaseNSGA_II", "param_grid": OptParas.NSGA_II, "problem": problem},
# {"name": "NSGA-III", "class": "BaseNSGA_III", "param_grid": OptParas.NSGA_III, "problem": problem},
# {"name": "NSGAII_SDE", "class": "BaseNSGAII_SDE", "param_grid": OptParas.NSGAII_SDE, "problem": problem},
# {"name": "MO-PSO-RI", "class": "BaseMOPSORI", "param_grid": OptParas.MOPSORI, "problem": problem},
# {"name": "MO_ILCO", "class": "MO_ILCO", "param_grid": OptParas.MO_ILCO, "problem": problem},
{"name": "MO_ILCO_2", "class": "MO_ILCO_2", "param_grid": OptParas.MO_ILCO, "problem": problem},
# {"name": "MO-ALO", "class": "BaseMO_ALO", "param_grid": OptParas.MO_ALO, "problem": problem},
# {"name": "MO-SSA", "class": "BaseMO_SSA", "param_grid": OptParas.MO_SSA, "problem": problem},
# {"name": "NS-SSA", "class": "BaseNS_SSA", "param_grid": OptParas.MO_SSA, "problem": problem},
# {"name": "Improved-NSGA-III", "class": "Improved_NSGA_III", "param_grid": OptParas.IMPROVED_NSGA_III, "problem": problem},
]
processes = []
for my_md in models:
p = multiprocessing.Process(target=setting_and_running, args=(my_md,))
processes.append(p)
p.start()
for process in processes:
process.join()
print('That took: {} seconds'.format(time() - starttime))