Linear programming with conditional constraints
Mostrar comentarios más antiguos
Hello,
I'm trying to figure out a way to formulate a linear program to solve an optimization problem with the following constraints:
x1 = 10*e1 if e1>=0
x1 = 0,1*e1 if e1<0
x1 and e1 are variables of the problem.
All the other constraints are regular linear expressions.
Is it possible to model with a linear programming solution?
Is it possible to solve using linprog or intlinprog? If so, how can I do this?
Thanks
Respuestas (3)
Alan Weiss
el 15 de Dic. de 2020
I think that you can do this with mixed-integer linear programming. Create auxiliary binary variables y1 and y2. These variables track whether e1 is positive or not. Assume that M is an upper bound on abs(e1). Include the constraints
e1 <= M*y1; % This enforces y1 = 1 whenever e1 > 0
e1 + M*y2 >= 0; % This enforces y2 = 1 whenever e1 < 0
y1 + y2 = 1;
So now you have y1 as the indicator that e1 > 0 and y2 as the indicator that e1 < 0.
I am not sure at this point how to include y1 and y2 into your problem formulation, but maybe you can figure it out from here.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
14 comentarios
NN
el 4 de Oct. de 2021
Hi Sir,
Can i kknow if there is a solution to inlcude y1 and y2 in problem
Alan Weiss
el 4 de Oct. de 2021
The documentation section Integer and Logical Modeling contains information on how to do this kind of modeling. In particular, see the section Combine Logical Constraints to Create New Formulas. That section shows how to make
where z is binary. You can extend that formulation to
where
and
are constants (10 and 1/10 in your case) and
and
are the binary variables from the first part of my answer..
Alan Weiss
MATLAB mathematical toolbox documentation
NN
el 5 de Oct. de 2021
Thank you very much
Matt J
el 5 de Oct. de 2021
It looks like a good way out!
I'll try to formulate from there and I'll let you know if it works.
Thanks a lot
NN
el 5 de Oct. de 2021
I have used this below code
y1V = optimvar('y1V',N,'Type','integer','LowerBound',0,'UpperBound',1);
y2V = optimvar('y2V',N,'Type','integer','LowerBound',0,'UpperBound',1);
MV = optimvar('MV',N,'LowerBound',0,'UpperBound',16.5e3);
prob.Objective = dt*(y1V.*Nb1+y2V.*Nb2)'*PbattV;
prob.constraints.batt1= PbattEVregV <= MV*y1V; % This enforces y1 = 1 whenever PbattEVregV > 0
prob.constraints.batt2= PbattEVregV + MV*y2V >= 0; % This enforces y2 = 1 whenever e1 < 0
prob.constraints.batt3= y1V + y2V == 1;
It gives below comment for dimesnion error , please advice
An error occurred while running the simulation and the simulation was terminated
Caused by:
- Unable to perform assignment because the left and right sides have a different number of elements.
Alan Weiss
el 5 de Oct. de 2021
I see two issues here. One is that your stated objective function is not linear in the optimization variables. Please follow the documentation more closely, looking carefully at how the examples take extraordinary steps to keep the expressions linear.
dt*(y1V.*Nb1+y2V.*Nb2)'*PbattV; % I'm not sure about this one
PbattEVregV <= MV*y1V; % This one for sure.
% MV should not be an optimization variable if you are trying to use
% a Big-M formulation
Alan Weiss
MATLAB mathematical toolbox documentation
NN
el 5 de Oct. de 2021
dt*(y1V.*Nb1+y2V.*Nb2)'*PbattV; % I'm not sure about this one
PbattEVregV <= MV*y1V; % This one for sure.--->this is PbattV(not PbattEVregV)
% MV should not be an optimization variable if you are trying to use
% a Big-M formulation
NN
el 5 de Oct. de 2021
Assume that M is an upper bound on abs(e1).
If M is not an optimization variable , how can i define it ?Please explain
Alan Weiss
el 5 de Oct. de 2021
Give it a value. 1e4, 1e5, whatever you think is good. Larger values can lead to numerical instability, so take the smallest value you can.
Alan Weiss
MATLAB mathematical toolbox documentation
I am not sure at this point how to include y1 and y2 into your problem formulation, but maybe you can figure it out from here.
Complete the constraints as follows:
e1 <= M*y1; % This enforces y1 = 1 whenever e1 > 0
e1 + M*y2 >= 0; % This enforces y2 = 1 whenever e1 < 0
y1 + y2 = 1;
10*e1<= x1 <= 10*e1 + 100*M*y2 %equivalent to x1=10*e1 when e1>0
0.1*e1<= x1 <= 0.1*e1 + 100*M*y1 %equivalent to x1=0.1*e1 when e1<0
NN
el 5 de Oct. de 2021
Yes, i did, but with the below expression
PbattV = optimvar('PbattV',N,'LowerBound',-16500,'UpperBound',16500);
y1V = optimvar('y1V',N,'Type','integer','LowerBound',0,'UpperBound',1);
y2V = optimvar('y2V',N,'Type','integer','LowerBound',0,'UpperBound',1);
prob.Objective = dt*(y1V.*Nb1+y2V.*Nb2)'*PbattV;
prob.constraints.batt1= PbattV <= 16500*y1V; % This enforces y1 = 1 whenever PbattV > 0
prob.constraints.batt2= PbattV + 16500V*y2V >= 0; % This enforces y2 = 1 whenever PbattV< 0
prob.constraints.batt3= y1V + y2V == 1;
Here Nb1 and Nb2 are two struct arrays of 1x1 with subfields time(288 x1 double ) and signals(1 x 1 struct ).Signal ha s two subfields dimesions (241) and values( 288*241 double)
I am getting the error ,
Unable to perform assignment because the left and right sides have a different number of elements.
Dimension problem i am unable to solve.Please help
Alan Weiss
el 5 de Oct. de 2021
Your objective needs to be a scalar. You might need to rewrite your objective expression as explicit matrix multiplication rather than using a structure. Use the debugger to see what the sizes of your variables are. Ensure that the objective expression is a scalar.
Alan Weiss
MATLAB mathematical toolbox documentation
NN
el 6 de Oct. de 2021
Ok
NN
el 6 de Oct. de 2021
I have used debugger and getting this error for the line
prob.constraints.batt1= PbattV <= 16500*y1V; % This enforces y1 = 1 whenever PbattV > 0
prob.constraints.batt2= PbattV + 16500V*y2V >= 0; % This enforces y2 = 1 whenever PbattV< 0
prob.constraints.batt3= y1V + y2V == 1;
- Unrecognized method, property, or field 'constraints' for class 'optim.problemdef.OptimizationProblem'.
Is there any formatting problem ?
Thanks
Matt J
el 5 de Oct. de 2021
All the other constraints are regular linear expressions.
If that's true then just divide the problem into two subproblems, one with the constraints,
x1 = 10*e1
e1>=0
and the other with the constraints,
x1 = 0.1*e1
e1<=0
Whichever of these two linear sub-programs gives you the most optimal objective value is the solution that you accept.
Asume a bound e1<=M, and introduce additional binary variables b1 and b2 with the constraints.
%(1)
e1 <= M*b1; %This enforces b1 = 1 whenever e1 > 0
e1 + M*b2 >= 0; % This enforces b2 = 1 whenever e1 < 0
b1 + b2 = 1;
10*e1 <= x1, x1 <= 10*e1 + 9.9*M*b2 %(2)
0.1*e1 <= x1, x1 <= 0.1*e1 + 9.9*M*b1 %(3)
When 0<=e1<=M, the above reduces to,
- b1=1, b2=0,
- x1=10*e1,
- 0.1*e<=x1<=0.1*e1 +9.9*M
Note that (2) is what is required and (3) does not conflict with (2) on 0<=e1<=M
Similarly when -M<=e1<=0,
- b1=0, b2=1,
- 10*e1 <= x1 <= 10*e1 + 9.9*M
- x1=0.1*e1
Here, (3) is the required condition and (2) does not conflict with (3) on the interval -M<=e1<=0.
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!