-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
78 lines (65 loc) · 2.01 KB
/
main.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
# -*- coding: utf-8 -*-
import numpy as np
import tqdm
import crossover
import mutation
import selection
import preprocs
def main(**kw):
"""
Return:
The best solutions in each iteration
"""
config = preprocs.procs_params(kw)
procs_model(config)
# evolutionary algorithm
# stores the best solution in every iteration
ret = []
pop = config["pop"]
if config["info_flag"]:
pbar = tqdm.tqdm(range(config["max_iter"]))
pbar.set_description(f'{config["f"].__name__} {config["d"]}d - {config["run"]}/{config["max_run"]}')
else:
pbar = range(config["max_iter"])
for i in pbar:
config["iter"] = i + 1
pop1 = crossover.sbx(
pop=pop,
lower_bound=config["lower_bound"],
upper_bound=config["upper_bound"],
pc=config["pc"],
eta_c=config["eta_c"]
)
pop2 = mutation.poly_mutation(
pop=np.vstack((pop, pop1)),
lower_bound=config["lower_bound"],
upper_bound=config["upper_bound"],
pm=config["pm"],
eta_m=config["eta_m"]
)
pop = np.vstack((pop, pop1, pop2))
pop_idx = selection.select(
pop=pop,
config=config
)
# update the population
pop = pop[pop_idx]
# store the best individual
ret.append(pop[0])
return np.array(ret)
def procs_model(config):
import time
from cmp import CmpModel
if config["info_flag"]:
time1 = time.time()
# train the comparison model
samples_x, samples_y = config["samples"]
config["cmp_model"] = CmpModel()
config["cmp_model"].fit(
samples_x,
samples_y
)
if config["info_flag"]:
time2 = time.time()
print("{run}: Compare model build time = ".format(run=config["run"]), time2 - time1)
time.sleep(1) # In order to be compatible with the output of tqdm