Bi-level Optimization Problem
이전 댓글 표시
Hello all
I have to solve a bi-level optimization problem that is described as follows:
The upper layer problem is needed to be solved by using 'ga' solver.
and the lower problem is a MILP that should be solved by 'intlinprog'
The procedure of the solving the problem is as follows (in general):
1st ) the upper decision variables should be initialized and passed to lower problem as fixed values to start solving it .
2nd ) the lower problem is run and solved and produce the solutions ( the lower decision variables)
3rd ) the lower problem solutions should be returned to upper problem ( as values ) to start solving the upper problem and producing the solutions.
4th) the resultant upper solutions should be paassed to lower problem again until the maximum number of ga's generation is reached
The figures attached below clarify the structure of the mentioned problem



These figures are taken from this paper (https://www.sciencedirect.com/science/article/abs/pii/S0960148119303799)
My questions are 1) how can I pass the intial populations (initial values of upper decision variables) to start the lower porblem ?
2)Also, how can I pass the solution of the upper problem to lower problem after each generation of ga ?
답변 (1개)
Matt J
2023년 4월 13일
0 개 추천
Sine intlinprog is called by by ga's fitness function, you will have the variables in the upper problem as input to the lower problem there.
댓글 수: 18
You don't need to care about passing variables or something like this.
The interface where upper and lower problem meet is the function "nonlcon" called by the upper problem where you define its constraints. Here, the solution variables of the upper problem so far are available, and you have to use them to solve the lower problem. With this solution, you can define the constraints of the upper problem.
If it's additionally necessary to get the solution of the lower problem to define the objective of the upper problem, you have to do the same in the function where you define this objective for ga. If this is the case, you might be able to save optimizations with intlinprog following the procedure described here:
Maybe it's possible also to use the problem-based approach to solve this problem, but I'd prefer the solver-based approach in this complicated case.
Test2(1)
function [UpperSol,Upperfval,exitflag_Upper,LowerSol] = Test2(t)
options = optimoptions(@ga,...
'PlotFcn',{@gaplotbestf,@gaplotmaxconstr},...
'Display','iter');
[p, Upperfval,exitflag_Upper] = ...
ga(@cam, 3, [],[],[],[],[0 0 -inf], [1,20,+inf],@(p)nonlcon(p,t),1:2,options);
UpperSol.x=p(1);
UpperSol.y=p(2);
UpperSol.f=p(3);
[~,~,LowerSol,Lowerfval,exitflag_Lower] = nonlcon(UpperSol,t);
end
function out=cam(p)
[x,y,f]=deal(p(1),p(2),p(3));
out=(4 - 2.1.*x.^2 + x.^4./3).*x.^2 + x.*y + (-4 + 4.*y.^2).*y.^2 + f
end
function [cineq,ceq,LowerSol,Lowerfval,exitflag_Lower] = nonlcon(p,t)
[x,y,f]=deal(p(1),p(2),p(3));
%%%Sub-problem (START)
a = optimvar('a','LowerBound',0);
b = optimvar('b','LowerBound',0);
c = optimvar('c','LowerBound',0,'UpperBound',1,'Type','integer');
lowerProb = optimproblem('Objective',-3*a*x-2*b-c);
lowerProb.Constraints.cons1 = a*y + b + c <= 7;
lowerProb.Constraints.cons2 = 4*a + 2*b + c == 12;
[LowerSol,Lowerfval,exitflag_Lower] = solve(lowerProb);
%%%Sub-problem (END)
cineq(1) = t*x*y + x - y + 1.5 ;
cineq(2) = 10 - x*y ;
ceq= f - Lowerfval ;
if exitflag_Lower<0
ceq=inf;
end
end
Torsten
2023년 4월 13일
ga restriction makes it impossible to solve the test problem.
Bashar
2023년 4월 13일
Here's an alternative formulation of the example problem that avoids the previously encountered errors, though for t=1 a solution may not exist.
Test2(1)
function [UpperSol,Upperfval,exitflag_Upper,LowerSol] = Test2(t)
options = optimoptions(@ga,...
'PlotFcn',{@gaplotbestf,@gaplotmaxconstr},...
'Display','iter');
[p, Upperfval,exitflag_Upper] = ...
ga(@cam, 3, [],[],[],[],[0 0 -inf], [1,20,+inf],@(p)nonlcon(p,t),1:2,options);
UpperSol.x=p(1);
UpperSol.y=p(2);
UpperSol.f=p(3);
[~,LowerSol,Lowerfval,exitflag_Lower] = cam(p);
end
function [out,LowerSol,Lowerfval,exitflag_Lower]=cam(p)
[x,y,f]=deal(p(1),p(2),p(3));
%%%Sub-problem (START)
a = optimvar('a','LowerBound',0);
b = optimvar('b','LowerBound',0);
c = optimvar('c','LowerBound',0,'UpperBound',1,'Type','integer');
lowerProb = optimproblem('Objective',-3*a*x-2*b-c);
lowerProb.Constraints.cons1 = a*y + b + c <= 7;
lowerProb.Constraints.cons2 = 4*a + 2*b + c == 12;
[LowerSol,Lowerfval,exitflag_Lower] = solve(lowerProb);
%%%Sub-problem (END)
if exitflag_Lower>=0
out=(4 - 2.1.*x.^2 + x.^4./3).*x.^2 + x.*y + (-4 + 4.*y.^2).*y.^2 + Lowerfval;
else
out=inf;
end
end
function [cineq,ceq] = nonlcon(p,t)
[x,y,f]=deal(p(1),p(2),p(3));
cineq(1) = t*x*y + x - y + 1.5 ;
cineq(2) = 10 - x*y ;
ceq=[];
end
Torsten
2023년 4월 14일
So, I think the problem is the combination of these two examples is inappropriate (in my opinion )
No. The problem was that "ga" does not accept nonlinear equality constraints if the problem also has integer constraints:
There might be potential to save time in exchanging solutions of your lower problem between "cam" and "nonlcon" with the help of the link I already gave you:
Adding the constraint to the lower problem needs less changes to the code from above than adding it to the upper problem (as you want).
I hope you are aware that with this constraint, your problem becomes infeasible.
Test2(1)
function [UpperSol,Upperfval,exitflag_Upper,LowerSol] = Test2(t)
options = optimoptions(@ga,...
'PlotFcn',{@gaplotbestf,@gaplotmaxconstr},...
'Display','iter');
[p, Upperfval,exitflag_Upper] = ...
ga(@cam, 3, [],[],[],[],[0 0 -inf], [1,20,+inf],@(p)nonlcon(p,t),1:2,options);
UpperSol.x=p(1);
UpperSol.y=p(2);
UpperSol.f=p(3);
[~,LowerSol,Lowerfval,exitflag_Lower] = cam(p);
end
function [out,LowerSol,Lowerfval,exitflag_Lower]=cam(p)
[x,y,f]=deal(p(1),p(2),p(3));
%%%Sub-problem (START)
a = optimvar('a','LowerBound',0);
b = optimvar('b','LowerBound',0);
c = optimvar('c','LowerBound',0,'UpperBound',1,'Type','integer');
lowerProb = optimproblem('Objective',-3*a*x-2*b-c);
lowerProb.Constraints.cons1 = a*y + b + c <= 7;
lowerProb.Constraints.cons2 = 4*a + 2*b + c == 12;
[LowerSol,Lowerfval,exitflag_Lower] = solve(lowerProb);
%%%Sub-problem (END)
if exitflag_Lower>=0
out=(4 - 2.1.*x.^2 + x.^4./3).*x.^2 + x.*y + (-4 + 4.*y.^2).*y.^2 + Lowerfval;
else
out=inf;
end
end
function [cineq,ceq] = nonlcon(p,t)
[x,y,f]=deal(p(1),p(2),p(3));
[x,y,f]=deal(p(1),p(2),p(3));
%%%Sub-problem (START)
a = optimvar('a','LowerBound',0);
b = optimvar('b','LowerBound',0);
c = optimvar('c','LowerBound',0,'UpperBound',1,'Type','integer');
lowerProb = optimproblem('Objective',-3*a*x-2*b-c);
lowerProb.Constraints.cons1 = a*y + b + c <= 7;
lowerProb.Constraints.cons2 = 4*a + 2*b + c == 12;
[LowerSol,Lowerfval,exitflag_Lower] = solve(lowerProb);
%%%Sub-problem (END)
cineq(1) = t*x*y + x - y + 1.5 ;
cineq(2) = 10 - x*y ;
cineq(3) = LowerSol.a*x + LowerSol.b*y;
ceq=[];
end
Bashar
2023년 4월 15일
Bashar
2023년 4월 16일
Torsten
2023년 4월 17일
Kindly, Could anyone help me with this hypothetical case in the above test code ?
the case is the following: If I want to add a forth variable (z) (vector (state) variable with lenght of N) to the upper problem, and this variable is only incorporated in the upper problem as (state) updating constraint as follows
z(t+1) = z(t) + a(t)/b; for t=1:N-1 and a and b are the lower problem variables ( as you know)
this is required that the lower variable (a) should be redfined in lower problem as a vector with length of N
With regardless of the modification in the lower problem, I need just how can I define the variable (z) in the problem, especially in nonlcon function where the constraints are defined.
I searched in Mathworks documentation for longtime and I didn't find anything may help me; in particular, how to define vector variable in the solver based appraoch.
Actually, I introduce this case to simulate challenges will appear in my actual problem ( which I can't share it here).
I appreciate your valued help
Kind regards
Everything about the direct call of ga with many examples is explained here:
At the moment, you call ga as
[p, Upperfval,exitflag_Upper] = ...
ga(@cam, 3, [],[],[],[],[0 0 -inf], [1,20,+inf],@(p)nonlcon(p,t),1:2,options);
So you could change the 3 to N+3,
the upper and lower limits to [0 0 -inf zlb] resp. [1,20,+inf,zub] where zlb and zub are row vectors of length N which contain the lower and upper bounds for the z components
and maybe
1:2 to a modified index vector with the possible z components that are integer variables.
But note that this way will not work because you cannot define z as optimization variables and the relation for z in the upper problem because you had to define the relations as nonlinear equality constraints. But as you might remember from your first question here, ga does not accept nonlinear equality constraints together with integer constraints.
I suggest you don't stick to your test problem too strong because it only shows you how in principle a coupling of two optimizations works. You should now concentrate on your real problem and plan which variables to solve in the upper, which in the lower problem, which constraints are to be set in the upper and which in the lower problem and if this can be done with the MATLAB solvers you plan to use.
Bashar
2023년 4월 18일
Torsten
2023년 4월 18일
I'm also a fan of "work from easy to difficult". But in your case, all the preparatory work has been done, you know how to couple two optimizations and it doesn't make sense to extend the test problem. You will have to start anew with your real problem making use of the method, but not the equations of the test problem.
Bashar
2023년 4월 18일
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
