forked from LiQianxiao/CloudPoint-MachineLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.py
150 lines (112 loc) · 4.86 KB
/
optimizer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'''
File: optimizer.py
File Created: Sunday, 4th November 2018 7:34:47 pm
Author: Qianxiao Li ([email protected])
Gonzalez Oyarce Anibal Lautaro ([email protected])
-----
License: MIT License
'''
import numpy as np
from pyswarm import pso
import pandas as pd
import time
import multiprocessing as mp
def unwrap_self(arg, **kwarg):
return PSO_Optimizer.optimisation(*arg, **kwarg)
class PSO_Optimizer(object):
def __init__(self, regressor, scaler, objective,
constraints, selection_criteria):
self.regressor = regressor
self.scaler = scaler
self.objective = lambda x, cp: objective(x, cp, regressor)
self.constraints = constraints
self.selection_criteria = selection_criteria
def optimisation(self, n_run, optimisation_options, manager_list):
print('Optimization No.:', n_run)
np.random.seed(n_run)
xopt, fopt = pso(
self.objective,
optimisation_options['lb'],
optimisation_options['ub'],
f_ieqcons=self.constraints,
args=[optimisation_options['target_CP']],
swarmsize=optimisation_options['swarmsize'],
omega=optimisation_options['omega'],
phip=optimisation_options['phip'],
phig=optimisation_options['phig'],
maxiter=optimisation_options['maxiter'],
debug=optimisation_options['debug_flag'])
manager_list.append([xopt, fopt])
return (xopt, fopt)
def optimisation_parallel(self, optimisation_options):
n_solutions = optimisation_options['n_solutions']
nprocessors = optimisation_options['nprocessors']
start = time.time()
print('Optimisation sequence started at time:')
print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(start)))
run_flag = True
round_count = 0
sols_per = []
sols_xopts = []
sols_pred = []
while run_flag:
seeds_list = np.arange(1, nprocessors+1) + nprocessors*round_count
round_count += 1
print('Working on round:', round_count)
manager = mp.Manager()
return_list = manager.list()
processes = []
for i in seeds_list:
p = mp.Process(
target=unwrap_self,
args=([self, i, optimisation_options, return_list], ))
processes.append(p)
[x.start() for x in processes]
[x.join() for x in processes]
results_optimisation = np.array(return_list)
xopts = results_optimisation[:, 0]
fopts = results_optimisation[:, 1]
preds_xopts = np.array(
[self.regressor.predict_transform(item) for item in xopts])
per_sol_preds = abs(
preds_xopts - optimisation_options['target_CP']) \
/ optimisation_options['target_CP']
for _xopt, _pred_xopt, _per in zip(
xopts, preds_xopts, per_sol_preds):
if _per <= optimisation_options['criteria']:
if self.selection_criteria:
if self.selection_criteria(_xopt):
sols_per.append(_per)
sols_xopts.append(_xopt)
sols_pred.append(_pred_xopt)
else:
sols_per.append(_per)
sols_xopts.append(_xopt)
sols_pred.append(_pred_xopt)
print('So far', len(sols_per), 'solutions have been found')
if len(sols_per) >= n_solutions:
run_flag = False
sols_per = np.array(sols_per)
sols_xopts = np.array(sols_xopts)
sols_pred = np.array(sols_pred)
sols_per_argsort = np.array(sols_per).argsort()
sols_per = sols_per[sols_per_argsort]
sols_xopts = sols_xopts[sols_per_argsort]
sols_pred = sols_pred[sols_per_argsort]
sols_per = sols_per[:n_solutions]
sols_xopts = sols_xopts[:n_solutions]
sols_pred = sols_pred[:n_solutions]
if round_count > optimisation_options['max_rounds']:
run_flag = False
sols_df = pd.DataFrame()
for _item in enumerate(optimisation_options['opt_vars']):
sols_df[optimisation_options['opt_vars'][_item[0]]] = \
sols_xopts[:, _item[0]]
sols_df['Cloud Point'] = sols_pred
sols_df['% Error'] = sols_per
end = time.time()
runtime = end - start
print('Optimisation finished in %.3f' % (runtime), '[s]')
sols_df.columns = ['Pred_'+item for item in sols_df.columns]
sols_df.loc[:, 'target_CP'] = optimisation_options['target_CP']
return sols_df, runtime