-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreeobj.py
306 lines (283 loc) · 10.1 KB
/
threeobj.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from scipy.optimize import shgo
import numpy as np
import copy
import sys
## non-dominated sorting
#import pygmo as pg
#outputting
from matplotlib import pyplot as plt
nvar=2 # nr. of variables
nfun=3 # nr. of functions
## objective functions
def psi(x):
return x[0]*x[0]+x[1]*x[1]
def phi(x):
return psi(x)-np.exp(-50*psi(x))
def f1(x):
return phi(x)
def f2(x):
return phi([x[0],x[1]-1])
def f3(x):
return phi([x[0]-1,x[1]])
fvect=[f1,f2,f3]
def f(x): # the vector objective function
return np.array([f1(x),f2(x),f3(x)])
## ideal, nadir
ideal=np.array([-1,-1,-1])
nadir=np.array([1,2,2])
utopia=ideal-10**-5
## basic weights for Chebyshev
w0=1/(nadir-ideal)
## bounds
bnd=[(0,1) for i in range(nvar)]+[(-10,10)]
### PROBLEM FORMULATION
# decision vector xt = [x1_1,...,x_n,t]
## ASF objective: t + augmentation term
# *args = ( ref.point(list), rho(float), weights(list))
def rhosum_f(xt,*args):
return (
xt[-1]#+ # t
#args[1] * sum( args[2]*(f(xt[:-1])-args[0]) ) # augm. term
)
## l.h.s. fcuntions for ASF constraints: t >= w_i(f_i(x)-ref.point_i), i=1..n
t_constr=[ # *args = (ref.point_i,weight_i)
lambda xt,*args:
-args[1]*fvect[0](xt[:-1])+xt[-1]+args[1]*args[0] - 10**-8*sum(f(xt[:-1])*args[1]),
lambda xt,*args:
-args[1]*fvect[1](xt[:-1])+xt[-1]+args[1]*args[0] - 10**-8*sum(f(xt[:-1])),
lambda xt,*args:
-args[1]*fvect[2](xt[:-1])+xt[-1]+args[1]*args[0] - 10**-8*sum(f(xt[:-1])),
lambda xt: -fvect[0](xt[:-1]) + 1 # keep artificial upper bound
]
### PROBLEM SOLVING
# solving the scalarized problem
def solve_ref(refpoint,w,sampl_m='simplicial',itern=5,npoints=100,
subset=None, # subset of objectives to minimize
upbounds=None # vector of [(upper bound or None) for each objective]
):
for ishift in [3,5,10]:
# shifting the ref. point to exceed nadir+(nadir-ideal)
tadd=max(0,max(w0*(ishift*nadir-(ishift-1)*ideal-refpoint)))
refp=refpoint+tadd/w
# list of objectives to minimize
if subset is None:
subset=range(len(refpoint))
#creating constraints
constr_list=[
{ # ASF linearization constraint
"type": "ineq",
"fun": t_constr[i],
"args": (refp[i],w[i])
} for i in subset
]+[{ # problem-specifi constraint
"type": "ineq",
"fun": t_constr[3]
}]
if upbounds is not None:
for i,b in enumerate(upbounds):
if b is not None:
constr_list.append({
"type": "ineq",
"fun": lambda xt, *args: # function nr, constraint
-fvect[args[0]](xt[:-1]) + args[1],
"args":(i,b)
})
# calling the solver
sol = shgo(
rhosum_f, #obj. function
bounds=bnd, # variable bounds
args=(np.array(refp),10**-6,w), # parameters for obj. func.
constraints=constr_list,
sampling_method=sampl_m,
iters=itern,
n=npoints,
options={"minimize_every_iter":True,"local_iter":False}
)
# are ASF constraints tight?
if sol["x"] is not None:
constr=[
t_constr[i](sol["x"],refp[i],w[i])
for i in subset
]
if min(constr)>10**-6:
print("Wrong constraints on shift ",ishift, " for \nRefp: ",
refp,"\n Weights: ",w,"\nUpbounds: ",upbounds,
"\n Subset: ",subset,"\nWith slack:",constr)
sol["message"]="! Solved but ASF constraints are not tight"+ \
"x=" + str(sol["x"]) + ", y="+str(f(sol["x"][:-1]))
sol["x"]=None
sol["y"]=None
else: # if solved successfuly, enough shifting
break
else:
constr=None
# return
return {"message": sol["message"],
"x": sol["x"],
"fun": sol["fun"],
"nfev": sol["nfev"],
"nlfev":sol["nlfev"],
"constr":constr,
"y":f(sol["x"][:-1]) if sol["x"] is not None else None
}
## Deriving P.O. solutions for the reference point method
def solve_rpm(refpoint,w,
sampl_m='simplicial',itern=5,npoints=100):
p=[solve_ref(refpoint,w,
sampl_m=sampl_m,itern=itern,npoints=npoints)]
# Modified ASF solutions
normdif=np.linalg.norm(refpoint-p[0]["y"]) # perturbation value
for i in range(nfun):
pref1=copy.deepcopy(refpoint)
pref1[i]+=normdif
p.append(solve_ref(pref1,w,
sampl_m=sampl_m,itern=itern,npoints=npoints))
return p
## Deriving P.P. solutions for the Nimbus method
# p - current P.opt. solution
# tol - tolerance of assignment to classes "<", "=" and ">"
def solve_nimb(refpoint,w,y,tol=0.01,
sampl_m='simplicial',itern=5,npoints=100):
## assigning objectives to classes from "<" to ">"
s_l = []
s_leq = []
s_eq = []
s_geq = []
s_g = []
for i in range(len(refpoint)):
# absolute tolerance for this objective
itol=(nadir[i]-ideal[i])*tol
#assigning
if abs(ideal[i]-refpoint[i])<=itol:
s_l.append(i)
elif abs(nadir[i]-refpoint[i])<=itol:
s_g.append(i)
elif y[i]==refpoint[i]:
s_eq.append(i)
elif refpoint[i]<y[i]:
s_leq.append(i)
else:
s_geq.append(i)
if len(s_l)+len(s_leq)==0 or len(s_g)+len(s_geq)==0:
print("Nimbus preference error, ref.=",refpoint,", y=",y)
## creating the modified ref. point
ref1=copy.deepcopy(refpoint)
for i in s_l:
ref1[i]=ideal[i]
for i in s_g:
ref1[i]=nadir[i]
## solving scalarized problems
p=[] # collected solutions
# original Nimbus (3.1)
print("3.1", end=" ")
uporig=[None for i in range(len(refpoint))]
for i in s_l+s_leq+s_eq:
uporig[i]=y[i]
for i in s_geq:
uporig[i]=refpoint[i]
p.append(solve_ref(#!! itern changed to 6 here
ref1,w0,subset=s_l+s_leq,upbounds=uporig,
sampl_m=sampl_m,itern=6,npoints=npoints)
)
# from STOM (3.2)
print("3.2", end=" ")
wstom=1/(np.array(ref1)-utopia)
p.append(solve_ref(
utopia, wstom,
sampl_m=sampl_m,itern=itern,npoints=npoints)
)
# simple ASF (3.3)
print("3.3", end=" ")
p.append(solve_ref(
ref1, w0,
sampl_m=sampl_m,itern=itern,npoints=npoints)
)
# from GUESS (3.4)
print("3.4")
p.append(solve_ref(
nadir, 1/(nadir-np.array(ref1)),
subset = s_l + s_leq + s_eq + s_geq,
sampl_m=sampl_m,itern=itern,npoints=npoints)
)
return p
def solve_uf(uf,sampl_m='simplicial',itern=5,npoints=100):
sol = shgo(
lambda x: uf(f(x)), #obj. function
bounds=bnd[:nvar], # variable bounds
sampling_method=sampl_m,
iters=itern,
n=npoints,
options={"minimize_every_iter":True,"local_iter":False}
)
return[ sol["x"],f(sol["x"]), uf(f(sol["x"])),sol]
if __name__=="__main__":
rp=np.array([ 7.,12.68296,12.87776])
for i in [1,2,3]:
sol=solve_ref(
rp,
np.array([0.5,0.33333,0.33333]),
subset=[0,1],
upbounds=[-0.871937679931465, 0.9416768454172942, 1.013357076861016],itern=6)
print(sol["y"])
rp+=5/np.array([0.5,0.33333,0.33333])
pass
### generating and saving random reference points
# ref_l=np.array([
# [ideal[i]+np.random.random_sample()*(nadir[i]-ideal[i])
# for i in range(nfun)]
# for i in range(500)])
# np.savetxt("reflist.txt",ref_l)
### loading same saved reference points
# ref_l=np.genfromtxt("reflist.txt")
### collecting solution results
# x_l=[]
# y_l=[]
# for i in range(500):
# res=solve_ref(ref_l[i],w0,itern=5)
# x_l.append(res["x"])
# y_l.append(f(res["x"]))
### saving solution results
# np.savetxt("aug_add_10-6_x.txt",x_l)
# np.savetxt("aug_add_10-6_y.txt",y_l)
# ### Testing results of different ASF formulations on random points
# asfnames=["aug_10-6", # proper augmentation via constraints
# "noaugm", # no augmentation
# "aug_add_10-6" # augmented by adding term to the obj. function
# ]
# xx=[np.genfromtxt(s+"_x.txt") for s in asfnames]
# yy=[np.genfromtxt(s+"_y.txt") for s in asfnames]
#
# ### checking non-domination
# #np.set_printoptions(precision=5)
# #for i,s in enumerate(asfnames):
# # print("\n*******\n"+s)
# # for i1,y1 in enumerate(yy[i]):
# # for i2,y2 in enumerate(yy[i]):
# # if max(y1-y2)<=10**-6 and min(y1-y2)<0:
# # print("X: ",xx[i][i1][:-1],xx[i][i2][:-1])
# # print("Y: ",y1,y1,min(y1-y2),"\n")
#
# ## checking differences
# for i1,s1 in enumerate(asfnames[:-1]):
# for i2,s2 in enumerate(asfnames[i1+1:]):
# print("\n*******************")
# print(s1+" - "+s2)
# # difference between y values
# fig,ax=plt.subplots(figsize=(8,6))
# ax.set_title("Y")
# ax.hist([np.linalg.norm(yy[i1][j]-yy[i2][j])
# for j in range(len(yy[i1]))
# ],bins=20,range=(0,0.000004)
# )
# plt.show()
# # difference between x values
# fig,ax=plt.subplots(figsize=(8,6))
# ax.set_title("X")
# ax.hist([np.linalg.norm(xx[i1][j][:-1]-xx[i2][j][:-1])
# for j in range(len(xx[i1]))
# ],bins=20,range=(0,0.000004)
# )
# plt.show()
#
#
#