Clear Filters
Clear Filters

Minimize a function with special constraints

3 views (last 30 days)
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 Comment
Sarvesh Kale
Sarvesh Kale on 10 Feb 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)
Solving problem using linprog. Problem is unbounded.
sol = struct with fields:
x1: [] x2: [] x3: [] x4: [] x5: [] x6: [] x7: []
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

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 10 Feb 2023
Edited: John D'Errico on 10 Feb 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)
Solving problem using linprog. Problem is unbounded.
ans = struct with fields:
x: []
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)
x =
1×7 OptimizationVariable array with properties: Array-wide properties: Name: 'x' Type: 'continuous' IndexNames: {{} {}} Elementwise properties: LowerBound: [0 0 0 0 0 0 0] UpperBound: [Inf Inf Inf Inf Inf Inf Inf] See variables with show. See bounds with showbounds.
x0 = solve(pr)
Solving problem using linprog. Optimal solution found.
x0 = struct with fields:
x: [0 0 2.0000 2.7000 0 0 10.0000]
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)
Solving problem using linprog. Optimal solution found.
x100 = struct with fields:
x: [-100 -100 -100 -95.3000 100 98.0000 12.0000]
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)
xdir = 1×7
-0.9804 -0.9804 -1.0000 -0.9608 0.9804 0.9608 0.0196
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
x =
1×7 OptimizationVariable array with properties: Array-wide properties: Name: 'x' Type: 'continuous' IndexNames: {{} {}} Elementwise properties: LowerBound: [0 -Inf -Inf -Inf -Inf -Inf -Inf] UpperBound: [Inf Inf Inf Inf 10 Inf Inf] See variables with show. See bounds with showbounds.
solve(pr)
Solving problem using linprog. Optimal solution found.
ans = struct with fields:
x: [0 -10 -5.3000 0 10 2.7000 7.3000]
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.

More Answers (1)

Torsten
Torsten on 10 Feb 2023
Use "linprog".
There are a lot of examples on how to use the solver under
  1 Comment
Imanitxka imane
Imanitxka imane on 10 Feb 2023
Thanks so much,,
I have implemented the folowing code, but it returns '' Problem is unbounded ''
f=[0.2 0.205 -0.094 -0.105 0.011 -0.005 0.006];
Aeq=[0 0 1 0 1 0 7; 1 0 0 0 0 1 1];
beq=[12 10];
A=[0 0.9 0 -(1/0.9) 0.9 -(1/0.9) 0
0 -0.9 0 (1/0.9) -0.9 -(1/0.9) 0
0 1 0 0 1 0 0
0 0 0 1 0 1 0
0 -1 0 0 -1 0 0
0 0 0 -1 0 -1 0];
b=[3 3 4 4 0 0];
x = linprog(f,A,b,Aeq,beq)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!