-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga_experiment.py
101 lines (78 loc) · 3.13 KB
/
ga_experiment.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
import sys, getopt
sys.path.append("./")
import numpy as np
from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_sampling, get_crossover, get_mutation
import matplotlib.pyplot as plt
from pymoo.performance_indicator.hv import Hypervolume
import json
from pymoo.factory import get_termination
from pymoo.optimize import minimize
from ga.problem import ScheduleProblem
from pymoo.visualization.scatter import Scatter
def run(countryname, capacity):
problem = ScheduleProblem(country_name=countryname, critical_capacity=capacity, record_all=True)
algorithm = NSGA2(
pop_size=100,
n_offsprings=100,
sampling=get_sampling("int_random"),
crossover=get_crossover("int_sbx", prob=0.9, eta=15),
mutation=get_mutation("int_pm", eta=20),
eliminate_duplicates=True
)
termination = get_termination("n_gen", 100)
res = minimize(problem,
algorithm,
termination,
seed=1,
pf=problem.pareto_front(use_cache=False),
save_history=True,
verbose=True)
# create the performance indicator object with reference point (4,4)
metric = Hypervolume(ref_point=np.array([1.0, 1.0]))
# collect the population in each generation
pop_each_gen = [a.pop for a in res.history]
with open("./experiments/ga_{}_lastpop.json".format(countryname), 'w') as f:
json.dump( {"df":[e.to_dict() for e in problem.last[0]],"x":problem.last[1].tolist()}, f)
with open("./experiments/ga_{}_lastobj.json".format(countryname), 'w') as f:
json.dump( {"deaths": problem.last_objectives[0].tolist(), "activity":problem.last_objectives[1].tolist()} , f)
# Objective Space
fig = plt.figure()
plot = Scatter(title = "Objective Space")
plot.add(res.F)
plt.savefig("./experiments/ga_{}_objective.png".format(countryname))
# receive the population in each generation
obj_and_feasible_each_gen = [pop[pop.get("feasible")[:,0]].get("F") for pop in pop_each_gen]
# calculate for each generation the HV metric
hv = [metric.calc(f) for f in obj_and_feasible_each_gen]
# function evaluations at each snapshot
n_evals = np.array([a.evaluator.n_eval for a in res.history])
# visualize the convergence curve
fig = plt.figure()
plt.plot(n_evals, hv, '-o')
plt.title("Convergence")
plt.xlabel("Function Evaluations")
plt.ylabel("Hypervolume")
plt.savefig("./experiments/ga_{}_hypervolume.png".format(countryname))
plt.show()
def main(argv):
country = 'luxembourg'
try:
opts, args = getopt.getopt(argv,"c:",["country="])
except getopt.GetoptError:
print ('ga_experiment.py -c <country> -h <critical hospital capacity>')
sys.exit(2)
for opt, arg in opts:
if opt in ("-c", "--country"):
country = arg
#www.covid19.healthdata/org for sources
if country=="luxembourg":
run("Luxembourg", 42)
elif country=="france":
run("France", 1980)
elif country=="japan":
run("Japan", 2054)
elif country=="italy":
run("Italy", 1822)
if __name__ == "__main__":
main(sys.argv[1:])