Minimize a function with special constraints
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Imanitxka imane
el 10 de Feb. de 2023
Comentada: Imanitxka imane
el 10 de Feb. de 2023
Hello community, can anyone please help me to get the solution of this problem.
I have to minimize the following objective function:
OF= (x1+x2)*0.2 – (x3+x4)*0.1+ (x5+x2-x6-x4)*0.005+(x7+x5+x3)*0.006
With respect to the following constraints:
x7+x5+x3= 12
x7+x1+x6=10
-3 <= (x5+x2)*0.9-(x6+x4)/0.9 <= 3
0<=x5+x2<=4
0<=x6+x4<=4
The code must return the x1,x2,x3,x4,x5,x6,and x7
1 comentario
Sarvesh Kale
el 10 de Feb. de 2023
I tried the following code
x1=optimvar('x1');
x2=optimvar('x2');
x3=optimvar('x3');
x4=optimvar('x4');
x5=optimvar('x5');
x6=optimvar('x6');
x7=optimvar('x7');
prob = optimproblem ;
prob.Objective = -(x1+x2)*0.2 +(x3+x4)*0.1-(x5+x2-x6-x4)*0.005-(x7+x5+x3)*0.006;
prob.Constraints.cons1= x7+x5+x3 == 12;
prob.Constraints.cons2= x7+x1+x6 == 10;
prob.Constraints.cons3= x5+x2 <= 4;
prob.Constraints.cons4= x5+x2 >= 0;
prob.Constraints.cons5= x6+x4 <= 4;
prob.Constraints.cons6= x6+x4 >= 0;
prob.Constraints.cons7= (x5+x2)*0.9-(x6+x4)/0.9 <= 3;
prob.Constraints.cons8= (x5+x2)*0.9-(x6+x4)/0.9 >= 0;
sol=solve(prob)
This means that linprog stopped because there is no solution to linear programming problem. You might have to check for your contraints, this statement says that there is no value of x1 .. x7 such that every constraint provided by you satisfies
Respuesta aceptada
John D'Errico
el 10 de Feb. de 2023
Editada: John D'Errico
el 10 de Feb. de 2023
If linprog tells you the problem is unbounded, then it is. What does that mean?
Unbounded means there is some direction you can move infinitely far out, and it can keep in decreasing the objective function. FOREVER. And this means the function has no minimum.
However, first it is important to see if you have actually written the linprog call properly. You have NOT done so. You wrote this:
Aeq=[0 0 1 0 1 0 7; 1 0 0 0 0 1 1];
beq=[12 10];
The first line of Aeq says that
x3 + x5 + 7*x7 = 12
That is clearly incorrect, or, at least, is inconsistent with the equations you have written. It is most likely a typo, made when you transcribed the first equation. You need to be more careful in implementing your equations into code.
It might be easier to use a problem formulation to solve your linear programming problem. I'll do that here. I say that because now your equations can be written directly, pretty much as you wrote them. The only thing I needed to do was convert the scalar variables into a vector of variables. That is something you need to learn to do anyway.
x = optimvar('x',[1,7]);
pr = optimproblem;
pr.Objective = (x(1)+x(2))*0.2-(x(3)+x(4))*0.1+(x(5)+x(2)-x(6)-x(4))*0.005+(x(7)+x(5)+x(3))*0.006;
pr.Constraints.eq1 = x(7)+x(5)+x(3) == 12;
pr.Constraints.eq2 = x(7)+x(1)+x(6) == 10;
pr.Constraints.ineq1 = -3 <= (x(5)+x(2))*0.9-(x(6)+x(4))/0.9;
pr.Constraints.ineq2 = 0<=x(5)+x(2);
pr.Constraints.ineq3 = 0<=x(6)+x(4);
pr.Constraints.ineq4 = (x(5)+x(2))*0.9-(x(6)+x(4))/0.9 <= 3;
pr.Constraints.ineq5 = x(5)+x(2)<=4;
pr.Constraints.ineq6 = x(6)+x(4)<=4;
solve(pr)
Even so, the problem is still reported as being unbounded.
You might consider reading this post I made recently explaining the situation, where I explain some common mistakes and problems people have when solving linear programming problems.
The third answer to that question explains the issues of unboundedness.
There is a reasonable chance that you really need to just assign lower and possibly upper bounds to the variables. That is, does it make sense if the variables can be negative? If not, then you need bounds on those variables. For example, you might do something as simple as this:
x.LowerBound = zeros(1,7)
x0 = solve(pr)
And now your problem has a unique solution. I don't know if this is reasonable for your problem, but something like that is necessary for a solution to exist.
Can we learn where the solver wants to go, such that the problem is unbounded? Probably yes. For example, try this next, since a lower bound solved the problem.
x.LowerBound = -100*ones(1,7);
x100 = solve(pr)
Do you see what it wants to do? It wants to move in this direction:
xdir = x100.x - x0.x; xdir = xdir/norm(xdir,inf)
And that tells me I could have solved the problem by creating upper bounds for the variables instead. I might even have been able to solve it by bounding only ONE of those variables. For example, I might have done this instead.
x.LowerBound = -inf(1,7);
x.LowerBound(1) = 0;
x.UpperBound(5) = 10
solve(pr)
That was also sufficient to create a bounded solution, but I did need to adjust two of the bounds for it to succeed. Anyway, you need to consider why your problem does not have a solution as you wrote it.
Más respuestas (1)
Torsten
el 10 de Feb. de 2023
Use "linprog".
There are a lot of examples on how to use the solver under
Ver también
Categorías
Más información sobre Linear Programming and Mixed-Integer Linear Programming en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!