-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimiazation_examples.py
210 lines (166 loc) · 6.88 KB
/
optimiazation_examples.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import sys
import numpy as np
import array
from multiprocessing import Pool, Manager
import matplotlib.pyplot as plt
plt.ion()
from constants import END_AGE, RELIABILITY_DT, SERVICE_LIFE, FRP_DESIGN_YR
from constants.simpleCorrosionConstants import START_AGE, TIME_INTERVAL, END_AGE
from management.component import Component
from management.system import System
from management.performanceFuncs import evalFitness
import time
import datetime
import random
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
icorr_mean_list = [1,1,1]
year = 100
creator.create("FitnessMulti", base.Fitness, weights=(-1.0,-1.0))
creator.create("Individual", list, fitness=creator.FitnessMulti)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("attr_plan", random.randint, FRP_DESIGN_YR, SERVICE_LIFE-TIME_INTERVAL)
# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_plan, 3)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalFitness)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutUniformInt,
low=FRP_DESIGN_YR, up=SERVICE_LIFE-TIME_INTERVAL, indpb=0.05)
toolbox.register("select", tools.selNSGA2)
toolbox.register("sort", tools.sortNondominated)
#class MyManager(BaseManager):
# pass
#MyManager.register('pfkeeping', Component, exposed=('pfkeeping'))
#MyManager.register('costkeeping', Component, exposed=('costkeeping'))
def main_series():
Component.resetPfKeeping()
Component.resetCostKeeping()
print "MULTIOBJECTIVE OPTIMIZATION: series version"
start_delta_time = time.time()
# optimization
random.seed(64)
npop = 1000
ngen = 200
stats = tools.Statistics(key=lambda ind: ind.fitness.values)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
logbook = tools.Logbook()
logbook.header = "gen", "evals", "avg", "std", "min", "max"
pop = toolbox.population(n=npop)
fits = toolbox.map(toolbox.evaluate, pop)
for fit,ind in zip(fits, pop):
ind.fitness.values = fit
nevals = npop
allpop = []
for gen in range(ngen):
allpop = allpop+pop
record = stats.compile(pop)
logbook.record(gen=gen, evals=nevals, **record)
print(logbook.stream)
offspring = algorithms.varOr(pop, toolbox, lambda_=npop, cxpb=0.5, mutpb=0.1)
#invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
#invalid_ind = offspring
nevals = len(offspring)
fits = toolbox.map(toolbox.evaluate, offspring)
for fit,ind in zip(fits, offspring):
ind.fitness.values = fit
pop = toolbox.select(offspring+pop, k=npop)
front = toolbox.sort(allpop, k=int(ngen*npop), first_front_only=True)
delta_time = time.time() - start_delta_time
print 'DONE: {} s'.format(str(datetime.timedelta(seconds=delta_time)))
return pop, logbook, front
def main_parallel():
Component.resetPfKeeping()
Component.resetCostKeeping()
manager = Manager()
Component.pfkeeping = manager.dict(Component.pfkeeping)
Component.costkeeping = manager.dict(Component.costkeeping)
pool = Pool(processes=3)
toolbox.register("map", pool.map)
print "MULTIOBJECTIVE OPTIMIZATION: parallel version"
start_delta_time = time.time()
# optimization
random.seed(64)
npop = 100
ngen = 50
stats = tools.Statistics(key=lambda ind: ind.fitness.values)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
logbook = tools.Logbook()
logbook.header = "gen", "evals", "avg", "std", "min", "max"
pop = toolbox.population(n=npop)
fits = toolbox.map(toolbox.evaluate, pop)
for fit,ind in zip(fits, pop):
ind.fitness.values = fit
nevals = npop
allpop = []
for gen in range(ngen):
allpop = allpop+pop
record = stats.compile(pop)
logbook.record(gen=gen, evals=nevals, **record)
print(logbook.stream)
offspring = algorithms.varOr(pop, toolbox, lambda_=npop, cxpb=0.5, mutpb=0.1)
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
nevals = len(invalid_ind)
fits = toolbox.map(toolbox.evaluate, invalid_ind)
for fit,ind in zip(fits, invalid_ind):
ind.fitness.values = fit
pop = toolbox.select(offspring+pop, k=npop)
front = toolbox.sort(allpop, k=int(ngen*npop), first_front_only=True)
pool.close()
pool.join()
delta_time = time.time() - start_delta_time
print 'DONE: {} s'.format(str(datetime.timedelta(seconds=delta_time)))
return allpop, logbook, front
if __name__ == "__main__":
#pop,log,front_series = main_series()
#flex = Component.pfkeeping['flexure']
#indx = np.argsort(flex[0])
#flex = flex[:,indx]
#np.save('flex_series1.npy', flex)
#pop,log,front_series = main_series()
#flex = Component.pfkeeping['flexure']
#indx = np.argsort(flex[0])
#flex = flex[:,indx]
#np.save('flex_series2.npy', flex)
allpop,log,front_parallel = main_series()
flex = Component.pfkeeping['flexure']
indx = np.argsort(flex[0])
flex = flex[:,indx]
#np.save('flex_parallel.npy', flex)
allfits = [ind.fitness.values for ind in allpop]
frontfits = [ind.fitness.values for ind in front_parallel[0]]
#toolbox.register("map", map)
#pop_res = toolbox.map(toolbox.evaluate, pop)
#pop_res = np.array(pop_res)
#front_series_res = toolbox.map(toolbox.evaluate, front_series[0])
#front_parallel_res = toolbox.map(toolbox.evaluate, front_parallel[0])
#front_series_res = np.array(front_series_res)
#front_parallel_res = np.array(front_parallel_res)
plt.close('all')
plt.rc('font', family='serif', size=12)
#plt.scatter(pop_res[:,0], pop_res[:,1], facecolors='none')
#plt.scatter(front_series_res[:,0], front_series_res[:,1],
# marker='^', facecolors='b', edgecolors='b', label='series w/ bookkeeping')
#plt.scatter(front_parallel_res[:,0], front_parallel_res[:,1],
# marker='o', facecolors='r', edgecolors='r', label='parallel w/ bookkeeping and shared memory')
plt.scatter(np.array(allfits)[:,0], np.array(allfits)[:,1], facecolors='none')
plt.scatter(np.array(frontfits)[:,0], np.array(frontfits)[:,1],
marker='o', facecolors='r', edgecolors='r', label='parallel w/ bookkeeping and shared memory')
plt.xlabel('Surrogate failure probability')
plt.ylabel('Surrogate strengthening cost')
#plt.legend(loc='upper right', prop={'size':12})
for front in front_parallel[0]:
if front[0] == front[1] and front[1] == front[2]:
print '{}'.format(front)
else:
continue