logical expression in objective function

can i use logical expression inside the objective function of an optimization problem?
...
prob.Objective = (x(1)+x(2)+x(3)>= 0.0001)+(x(6)+x(7)+x(8)>= 0.0001);
....

 Respuesta aceptada

Matt J
Matt J el 23 de Jun. de 2019
Editada: Matt J el 23 de Jun. de 2019
The way you would do this is to introduce additional binary variables,
b1=optimvar('boolean1','Type','integer','LowerBound',0,'UpperBound',1);
b2=optimvar('boolean2','Type','integer','LowerBound',0,'UpperBound',1);
and linear constraints,
prob.Constraint.Boolean1_Upper = b1>=(x(1)+x(2)+x(3)-0.0001)/U1;
prob.Constraint.Boolean1_Lower = b1<=(x(1)+x(2)+x(3))/0.0001;
prob.Constraint.Boolean2_Upper = b2>=(x(6)+x(7)+x(8)-0.0001)/U2;
prob.Constraint.Boolean2_Lower = b2<=(x(6)+x(7)+x(8))/0.0001;
where U1 and U2 are constant bounds large enough that the right hand sides can never be greater than 1. For example, if you have positive upper bounds on the x(i), the U can be the sum of those bounds.
These contraints combined with the binariness of b1 ensure that b1=(x(1)+x(2)+x(3)>=0.0001), and analogously for b2.
And now your objective would just be,
prob.Objective=b1+b2;

7 comentarios

Walter Roberson
Walter Roberson el 23 de Jun. de 2019
And you would need to ask to maximize rather than minimize.
Matt J
Matt J el 23 de Jun. de 2019
Editada: Matt J el 23 de Jun. de 2019
mohammad's reply moved here
Hi matt thanks for your assistance.
i tried to run the code using the commands you explained above, the code is as the following: (note: this is a part of my code)
.
.
.
prob = optimproblem('ObjectiveSense','max');
x = optimvar('x',10,1,'LowerBound',0,'UpperBound',1);
b1=optimvar('boolean1','Type','integer','LowerBound',0,'UpperBound',1);
b2=optimvar('boolean1','Type','integer','LowerBound',0,'UpperBound',1);
prob.Constraints.Boolean1_Upper = b1>=(x(1)+x(2)+x(3)-0.0001)/10;
prob.Constraints.Boolean1_Lower = b1<=(x(1)+x(2)+x(3)-0.0001);
prob.Constraints.Boolean2_Upper = b2>=(x(6)+x(7)+x(8)-0.0001)/10;
prob.Constraints.Boolean2_Lower = b2<=(x(6)+x(7)+x(8)-0.0001);
prob.Objective=b1+b2;
cons1 = A1*x <= RD ;
cons2 = A2*x <= Lx;
cons3 = A3*x <= ones(M,1);
cons5 = A5*x == beq;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons5 = cons5;
.
.
.
but i got the following error
OptimizationVariables appearing in the same problem, constraint, or expression must have distinct "Name" properties.
Make a new variable with a different "Name" property, or retrieve the original variable using the Variables property.
And Walter's reply moved here:
Change
b2=optimvar('boolean1','Type','integer','LowerBound',0,'UpperBound',1);
to
b2=optimvar('boolean2','Type','integer','LowerBound',0,'UpperBound',1);
Matt J
Matt J el 23 de Jun. de 2019
Editada: Matt J el 25 de Jun. de 2019
In addition to what Walter said, the constraints should really be
prob.Constraint.Boolean1_Upper = b1>=(x(1)+x(2)+x(3)-0.0001)/U1;
prob.Constraint.Boolean1_Lower = b1<=(x(1)+x(2)+x(3))/0.0001;
prob.Constraint.Boolean2_Upper = b2>=(x(6)+x(7)+x(8)-0.0001)/U2;
prob.Constraint.Boolean2_Lower = b2<=(x(6)+x(7)+x(8))/0.0001;
and here I assume, for simplicity that all x(i) are non-negative, which I infer is the case from elsewhere in the thread. I've made necessary edits to my original answer.
mohammad alquraan
mohammad alquraan el 25 de Jun. de 2019
Editada: mohammad alquraan el 25 de Jun. de 2019
i tried this code but i got empty result. what is the problem?
(note: x values are binary)
Matt J
Matt J el 25 de Jun. de 2019
Editada: Matt J el 25 de Jun. de 2019
The problem is infeasible, apparently. We would need to see what exactly you have implemented. So I suggest you show us your code, and maybe also the output of showproblem(prob).
i changed the constraints like the following:
x = optimvar('x',N*(M+2),'Type','integer','LowerBound',0,'UpperBound',1);
b1=optimvar('b1','Type','integer','LowerBound',0,'UpperBound',1);
b2=optimvar('b2','Type','integer','LowerBound',0,'UpperBound',1);
prob.Constraints.b1_Upper = b1>=(x(1)+x(2)+x(3))/M;
prob.Constraints.b1_Lower = b1<=(x(1)+x(2)+x(3));
prob.Constraints.b2_Upper = b2>=(x(6)+x(7)+x(8))/M;
prob.Constraints.b2_Lower = b2<=(x(6)+x(7)+x(8));
prob.Objective=b1+b2;
this code gave me correct result.
Thanks so much Mr. Matt J for your assistance.
mohammad alquraan
mohammad alquraan el 25 de Jun. de 2019
the above code is a simple example.
how i can write general code were:
x=x(1),x(2),x(3),....,x(M)
and b=b1,b2,b3,....,bN

Iniciar sesión para comentar.

Más respuestas (2)

Stephan
Stephan el 22 de Jun. de 2019

1 voto

Hi,
use the inequality constraints A and b as input arguments for the solver.

4 comentarios

Walter Roberson
Walter Roberson el 22 de Jun. de 2019
Note that unless you have asked for maximization, then the minima would occur when both parts of the logical expression are false.
Your objective should almost always involve all of your inputs .
mohammad alquraan
mohammad alquraan el 22 de Jun. de 2019
Thanks for your assistance. My question is : can i use logical expression inside objective function of optimization problem? If yes, how ? (Example will help)
The code you posted is already an example of using logical expressions inside objective functions.
The difficulty is that only intlinprog and ga and gamultiobj are certain to handle discontinuities. patternsearch() might handle discontinuities; I would have to review how simannealbnd works to confirm whether it handles discontinuities or not.
fmincon and fminsearch and lsqnonlin do not handle discontinuities.
This does not mean that you definitely cannot use logical expressions for those functions, but you would have to be careful to retain continuity of the function, and continuity of the first derivative; you can violate continuity of the second and further derivatives.
You should probably be recoding
(x(1)+x(2)+x(3)>= 0.0001)+(x(6)+x(7)+x(8)>= 0.0001)
as a pair of linear constraints,
x(1)+x(2)+x(3) <= 0.0001*(1-eps)
x(6)+x(7)+x(8) <= 0.0001*(1-eps)
The 1-eps has to do with converting the >= to < form: if 0.0001 could be exactly represented in binary floating point, then a value of 0.0001 exactly would generate a logical value of true, which is numeric 1, which would greater than the logical value of false, which is numeric 0, so and for optimization you want minima, so you want x(1)+x(2)+x(3) == 0.0001 to be outside the constraint. And 0.0001 as a literal is the value 0.000100000000000000004792173602385929598312941379845142364501953125 in binary, so permitting equality would get you values that were larger than 0.0001 which you do not want. Permitting equality to 0.0001*(1-eps) is fine as that is 0.000099999999999999977687119290248318748126621358096599578857421875
mohammad alquraan
mohammad alquraan el 23 de Jun. de 2019
Editada: mohammad alquraan el 23 de Jun. de 2019
thanks alot Mr. Walter Roberson for your assistance again.
let me explain my problem in more details.
my optimization problem try to determine maximum number of users that can be served at a given time. (every logical expression will return 0 or 1, then it will sum them to determine number of served users).
my objective function is as the following:
....
prob.Objective = (x(1)+x(2)+x(3)>0)+(x(6)+x(7)+x(8)>0)+.......;
.....
this code is a simplified part of my optimization code. But when i try to run the whole code i got the following error:
"Objective must be a scalar OptimizationExpression or a struct containing a scalar OptimizationExpression."
could you help on this.
thanx in advance.

Iniciar sesión para comentar.

mohammad alquraan
mohammad alquraan el 23 de Jun. de 2019
i have tried to solve this problem using solver-based optimization problem. as the following:
[x,fval,exitflag] =bintprog(-f,A,b,Aeq,Beq)
my objective was: maximizing number of users as showing below
indicator.JPG
this is an indicator function.
how can i write it as objective function to be used in the matlab command above.
(f=?????)

11 comentarios

mohammad alquraan
mohammad alquraan el 23 de Jun. de 2019
i am using matlab releases that has bintprog. like R2013a and R2010a.
Walter Roberson
Walter Roberson el 23 de Jun. de 2019
optimvar is new as of R2017b. It is not possible that you were able to code with optimvar in R2013a.
Walter Roberson
Walter Roberson el 23 de Jun. de 2019
Does any x participate in more than one combination ? If not, then the number of users is the sum of the x, divided by 3.
mohammad alquraan
mohammad alquraan el 23 de Jun. de 2019
here i am not using optimvar, i am using solver-based optimization problem.
x: represent number of channels, and any user may assign more than one channel.
Matt J
Matt J el 23 de Jun. de 2019
Editada: Matt J el 23 de Jun. de 2019
There is nothing to be gained but pain by downgrading your solution tool to bintprog. Now, you have to take every one of your integer non-binary variables, x and express them as sums of binary variables.
x1=b11+b12+b13+...
x2=b21+b22+b23+...
Because this greatly increases the number of variables, the likelihood of a computationally efficient solution is also harder to guarantee.
But if you wish to go this route, then the same constraints and objective formulation as I outlined for the problem-based solver would be applicable with bintprog as well.
mohammad alquraan
mohammad alquraan el 23 de Jun. de 2019
Could you please provide me with more details about how to write the objective code for solver-based optimization problem.
The easiest would be to compose the problem first using the problem-based method and then convert it to solver form using,
problem = prob2struct(prob)
how i can convert the following command to let x take only binary values {0,1}?
x = optimvar('x',N*(M+2),1,'LowerBound',0,'UpperBound',1);
x = optimvar('x',N*(M+2),1,'Type','integer','LowerBound',0,'UpperBound',1);
mohammad alquraan
mohammad alquraan el 25 de Jun. de 2019
thank you matt J.

Iniciar sesión para comentar.

Categorías

Más información sobre Quadratic Programming and Cone Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 22 de Jun. de 2019

Comentada:

el 25 de Jun. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by