forked from ankurzing/bleaq2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialization.m
286 lines (237 loc) · 10.4 KB
/
initialization.m
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
function [ulPop] = initialization(ulDim, ulDimMin, ulDimMax, llDimMin, llDimMax, ulPopSize, testProblemName)
significantDistance = 1e-6;
data = createData(ulDimMin, ulDimMax, llDimMin, llDimMax, testProblemName, []);
if data.totalConstraints>0
display('Ensuring feasibility for the constraints')
constrModel = createModelConstraints(data);
end
for i=1:ulPopSize
if data.totalConstraints>0 && i<=ceil(ulPopSize/2)
%Ensuring relaxed feasibility
weights(1) = rand();
weights(2) = 1-rand();
ulPop(i,:) = ensureFeasibility(ulDimMin, ulDimMax, llDimMin, llDimMax, data, constrModel, weights, []);
for j=1:i-1
if computeDistance(ulPop(i,:), ulPop(j,:))<=significantDistance
ulPop(i,:) = mutation(ulPop(i,:),1);
ulPop(i,:) = checkLimitsReflection(ulPop(i,:),ulDimMin,ulDimMax);
break;
end
end
else
%Normal initialization
ulPop(i,:) = ulDimMin + rand(1, ulDim).*(ulDimMax-ulDimMin);
end
end
function data = createData(ulDimMin, ulDimMax, llDimMin, llDimMax, testProblemName, member)
%If the input parameter 'member' is empty then population is created in the entire
%box constraint set. If the input parameter 'member' is not empty then
%population is created around the 'member'
dimMin = [ulDimMin llDimMin];
dimMax = [ulDimMax llDimMax];
ulDim = size(ulDimMin,2);
llDim = size(llDimMin,2);
dim = ulDim + llDim;
popSize = (dim+1)*(dim+2)/2+5*dim;
if ~isempty(member)
for i=1:popSize-1
memberVariance = (dimMax - dimMin)/5;
pop(i,:) = member + (sqrt(memberVariance)).*randn(size(member));
end
pop(popSize,:) = member;
pop=checkLimitsReflection(pop, dimMin, dimMax);
else
pop=initialize(popSize,dim,dimMin,dimMax);
member = pop(popSize,:);
end
for i=1:popSize
[ulFunctionVals(i,:), ulEquality, ulInequality]=ulTestProblem(pop(i,1:ulDim), pop(i,ulDim+1:end), testProblemName);
if ~isempty(ulEquality)
ulEqualityConstrVals(i,:) = ulEquality;
else
ulEqualityConstrVals = [];
end
if ~isempty(ulInequality)
ulInequalityConstrVals(i,:) = ulInequality;
else
ulInequalityConstrVals = [];
end
[llFunctionVals(i,:), llEquality, llInequality]=llTestProblem(pop(i,ulDim+1:end), testProblemName, pop(i,1:ulDim));
if ~isempty(llEquality)
llEqualityConstrVals(i,:) = llEquality;
else
llEqualityConstrVals = [];
end
if ~isempty(llInequality)
llInequalityConstrVals(i,:) = llInequality;
else
llInequalityConstrVals = [];
end
end
data.totalConstraints = size(ulEqualityConstrVals,2)+size(ulInequalityConstrVals,2)+size(llEqualityConstrVals,2)+size(llInequalityConstrVals,2);
data.ul.pop = pop(:,1:ulDim);
data.ll.pop = pop(:,ulDim+1:end);
data.ul.functionVals = ulFunctionVals;
data.ul.equalityConstrVals = ulEqualityConstrVals;
data.ul.inequalityConstrVals = ulInequalityConstrVals;
data.ll.functionVals = llFunctionVals;
data.ll.equalityConstrVals = llEqualityConstrVals;
data.ll.inequalityConstrVals = llInequalityConstrVals;
function constrModel = createModelConstraints(data)
pop = [data.ul.pop data.ll.pop];
equalityConstrVals = [data.ul.equalityConstrVals, data.ll.equalityConstrVals];
inequalityConstrVals = [data.ul.inequalityConstrVals, data.ll.inequalityConstrVals];
numEqualityConstr = size(equalityConstrVals,2);
numInequalityConstr = size(inequalityConstrVals,2);
if numEqualityConstr ~=0
for i=1:numEqualityConstr
approx.equalityConstr{i} = quadApprox(equalityConstrVals(:,i), pop);
end
else
approx.equalityConstr = [];
end
if numInequalityConstr ~= 0
for i=1:numInequalityConstr
approx.inequalityConstr{i} = quadApprox(inequalityConstrVals(:,i), pop);
end
else
approx.inequalityConstr = [];
end
constrModel = approx;
function [ulMember, llMember] = ensureFeasibility(ulDimMin, ulDimMax, llDimMin, llDimMax, data, constrModel, weights, member)
%If input parameter 'member' is provided then the math program is
%solved with the 'member' as starting point otherwise any random point
%from data is chosen
epsilonZero = 1e-6;
dimMin = [ulDimMin llDimMin];
dimMax = [ulDimMax llDimMax];
ulDim = size(ulDimMin,2);
llDim = size(llDimMin,2);
dim = ulDim + llDim;
%Copying constraint model to approx
approx = constrModel;
%If weights are not provided then function to be optimized is taken as
%0, which means that only feasibility wrt constraints is needed
%otherwise a new function with upper and lower level weights is
%constructed
if isempty(weights)
functionVals = zeros(popSize,1);
else
functionVals = weights(1)*data.ul.functionVals + weights(2)*data.ll.functionVals;
end
pop = [data.ul.pop data.ll.pop];
popSize = size(pop,1);
if isempty(member)
r = ceil(rand*popSize);
member = pop(r,:);
else
functionVals = weights(1)*data.ul.functionVals + weights(2)*data.ll.functionVals;
end
%Adding function model to approx
approx.function = quadApprox(functionVals, pop);
options = optimset('Algorithm','interior-point','Display','off','TolX',1e-10,'TolFun',1e-10);
if isLinear(approx,epsilonZero)
if ~isempty(approx.equalityConstr)
for i=1:length(approx.equalityConstr)
A_equality(i,:) = approx.equalityConstr{i}.linear;
b_equality(i) = -approx.equalityConstr{i}.constant;
end
else
A_equality = [];
b_equality = [];
end
if ~isempty(approx.inequalityConstr)
for i=1:length(approx.inequalityConstr)
A_inequality(i,:) = approx.inequalityConstr{i}.linear;
b_inequality(i) = -approx.inequalityConstr{i}.constant;
end
else
A_inequality = [];
b_inequality = [];
end
optionsLinprog = optimset('Display','off');
[eliteIndiv] = linprog(-approx.function.linear,A_inequality,b_inequality',A_equality,b_equality',dimMin,dimMax,member,optionsLinprog);
eliteIndiv = eliteIndiv';
else
[eliteIndiv] = fmincon(@(x) -approximatedFunction(x,approx.function),member,[],[],[],[],dimMin,dimMax,@(x) approximatedConstraints(x,approx.equalityConstr,approx.inequalityConstr),options);
end
ulMember = eliteIndiv(1:ulDim);
llMember = eliteIndiv(ulDim+1:end);
function approxFunctionValue = approximatedFunction(pop, parameters)
approxFunctionValue = parameters.constant + pop*parameters.linear + pop*parameters.sqmatrix*pop';
function [c, ceq] = approximatedConstraints(pop, parametersEqualityConstr, parametersInequalityConstr)
if ~isempty(parametersEqualityConstr)
for i=1:length(parametersEqualityConstr)
ceq(i) = parametersEqualityConstr{i}.constant + pop*parametersEqualityConstr{i}.linear + pop*parametersEqualityConstr{i}.sqmatrix*pop';
end
else
ceq = [];
end
if ~isempty(parametersInequalityConstr)
for i=1:length(parametersInequalityConstr)
c(i) = parametersInequalityConstr{i}.constant + pop*parametersInequalityConstr{i}.linear + pop*parametersInequalityConstr{i}.sqmatrix*pop';
end
else
c = []; %Negative value suggests that there is no active inequality
end
function bool = isLinear(approximateFunctionParameters,epsilonZero)
bool = true;
if sum(sum(abs(approximateFunctionParameters.function.sqmatrix)>epsilonZero))>0
bool = false;
end
for i=1:length(approximateFunctionParameters.equalityConstr)
if sum(sum(abs(approximateFunctionParameters.equalityConstr{i}.sqmatrix)>epsilonZero))>0
bool = false;
end
end
for i=1:length(approximateFunctionParameters.inequalityConstr)
if sum(sum(abs(approximateFunctionParameters.inequalityConstr{i}.sqmatrix)>epsilonZero))>0
bool = false;
end
end
function pop=initialize(popSize,dim,dimMin,dimMax)
for i=1:popSize
pop(i,:) = dimMin + rand(1, dim).*(dimMax-dimMin);
end
function d = computeDistance(matrix1, matrix2)
%Computes pairwise distance between rows of matrix1 and matrix2
sz1 = size(matrix1, 1);
sz2 = size(matrix2, 1);
for i = 1:sz1
for j = 1:sz2
d(i,j) = sqrt(sum((matrix1(i,:)-matrix2(j,:)).^2));
end
end
function members = mutation(members, probMutation)
numOffsprings=size(members,1);
ulDim=size(members,2);
mum=20;
for i=1:numOffsprings
r = rand(1,ulDim);
index = r<0.5;
delta(index) = (2*r(index)).^(1/(mum+1)) - 1;
index = r>=0.5;
delta(index) = 1 - (2*(1 - r(index))).^(1/(mum+1));
% Generate the corresponding child element.
r = rand(1,ulDim);
if ~all(r>=probMutation)
members(i,r<probMutation) = members(i,r<probMutation) + delta(r<probMutation);
end
end
function members=checkLimitsReflection(members, dimMin, dimMax)
%This function reflects an infeasible point into the variable bounds. If the
%point lies far away, it assigns it a random position in the bounds.
numOfMembers = size(members,1);
dimMinMatrix = dimMin(ones(1,numOfMembers),:);
dimMaxMatrix = dimMax(ones(1,numOfMembers),:);
i = 0;
while sum(sum(members<dimMinMatrix)) || sum(sum(members>dimMaxMatrix))
I = members<dimMinMatrix-(dimMaxMatrix-dimMinMatrix);
J = members>dimMaxMatrix+(dimMaxMatrix-dimMinMatrix);
randI = rand(size(I));
randJ = rand(size(J));
members(I) = dimMinMatrix(I) + randI(I).*(dimMaxMatrix(I)-dimMinMatrix(I));
members(J) = dimMinMatrix(J) + randJ(J).*(dimMaxMatrix(J)-dimMinMatrix(J));
members(members<dimMinMatrix)=members(members<dimMinMatrix) + 2*(dimMinMatrix(members<dimMinMatrix)-members(members<dimMinMatrix));
members(members>dimMaxMatrix)=members(members>dimMaxMatrix) + 2*(dimMaxMatrix(members>dimMaxMatrix)-members(members>dimMaxMatrix));
end