Azzera filtri
Azzera filtri

using the linprog function for two constraints?

10 visualizzazioni (ultimi 30 giorni)
Hi! I have a problem which we have sorted out practically but need to have an interface to make it useful to the laymen.
We are considering 3 types of bulbs used in a household and assuming that a small house or section of the house will be requiring 9 bulbs.Considering x, y and z to be types of bulbs, this gives the equation: x+y+z=9
The power consumption for each bulb and optimum power consumption has been verified practically and the following equation was established: 60.44x+13.8y+8.4z = 154.8
For electrical total harmonic distortions, the following constraints were established: 0.4x+4.3y+1.85z<=32 0.377x+0.43y+0.5z<=5
I want to use linprog to get the optimum solution for x, y and z. I have used the equation: 60.44x+13.8y+8.4z = 154.8 as both the constraint equation and objective function as well. Ideally I should get the answer x=1. y=4 and z=4.
Here is my code:
f = [60.44 13.8 8.4];
A = [1 1 1; 0.4 4.3 1.85; 0.377 0.43 0.5];
b = [9 32 5];
Aeq = [60.44 13.8 8.4];
beq = [154.8];
lb = [1 1 1];
ub = [9 9 9];
[X,Z]=linprog(f,A,b,Aeq,beq,lb,ub)
I do not get the practical answer. Any way that my code needs improvement? What I notice is that my problem has two constraints and two inequalities with three variables and I think Im not able to incorporate this theory in the algorithm properly.

Risposta accettata

Torsten
Torsten il 24 Ago 2017
If you are in the stage of optimization, you don't know the 154.8 in the equation 60.44x+13.8y+8.4z = 154.8, but only the power consumption of each bulb. So you will have to remove the equality constraint from your problem formulation.
One more thing is that your solution variables must be integers - so you will have to use "intlinprog" instead of "linprog".
Best wishes
Torsten.
  2 Commenti
Ojaswita
Ojaswita il 24 Ago 2017
Thanks alot Torsten!
I have actually tried it that way too, but not getting the optimal solution that I got from practicals... Some formulation in the algorithm is messing up... Any ideas on how to sort it?
Torsten
Torsten il 24 Ago 2017
What is the solution you get from
f = [60.44 13.8 8.4];
A = [1 1 1; 0.4 4.3 1.85; 0.377 0.43 0.5];
b = [9 32 5];
lb = [0 0 0];
ub = [9 9 9];
X=linprog(f,A,b,[],[],lb,ub)
and what is the solution practitioner suggest ?
Best wishes
Torsten.

Accedi per commentare.

Più risposte (1)

John D'Errico
John D'Errico il 24 Ago 2017
Modificato: John D'Errico il 24 Ago 2017
There are MULTIPLE problems here. Read carefully what I write, because it exposes a major problem with what you are trying to do.
First. You claim that x+y+z = 0.9 is a constraint, an EQUALITY constraint. Yet you provide it as a inequality constraint. Putting it in A does that. If it is an EQUALITY constraint, then you need to put it in Aeq, not in A.
Next, you seem to have integer constraints. Linprog has no understanding of what is an integer. You NEED to use intlinprog.
f = [60.44 13.8 8.4];
A = [0.4 4.3 1.85; 0.377 0.43 0.5];
b = [32 5];
Aeq = [1 1 1; 60.44 13.8 8.4];
beq = [9 154.8];
lb = [1 1 1];
ub = [9 9 9];
First, lets try it using linprog, so no integer constraint at all.
[XYZ,fval]=linprog(f,A,b,Aeq,beq,lb,ub)
Optimal solution found.
XYZ =
1.4181
1
6.5819
fval =
154.8
So linprog comes up with something completely different.
But let us look at your supposed practical solution. Does it work? Is it really a solution?
f*[1 4 4]'
ans =
149.24
A*[1 4 4]'
ans =
25
4.097
Aeq*[1 4 4]'
ans =
9
149.24
As you can see, while it has a lower objective function, it fails to satisfy the requirements. It is simply not true that
60.44x+13.8y+8.4z = 154.8
In fact, we see that:
[60.44 13.8 8.4]*[1 4 4]'
ans =
149.24
That fails the equality constraint! So your supposed "practical" solution is simply not a solution.
Using intlinprog will not help, because [1 4 4] is not any more of a solution if we require the solution to be integer. In fact, if we do use intlinprog, we will see that intlinprog finds no solution at all.
[XYZ,fval]=intlinprog(f,[1 2 3],A,b,Aeq,beq,lb,ub)
LP: Optimal objective value is 154.800000.
No feasible solution found.
Intlinprog stopped because no integer points satisfy the constraints.
XYZ =
[]
fval =
[]
>> f
f =
60.44 13.8 8.4
So, lets go back and understand why this fails.
You claim that f is your objective? You say this:
"The power consumption for each bulb and optimum power consumption has been verified practically and the following equation was established: 60.44x+13.8y+8.4z = 154.8"
So then why in the name of god and little green apples are you using
f = [60.44 13.8 8.4];
as both your objective AND your equality constraint? You are constraining the sum to be 154.8, and then trying to minimize that sum??????????
I'm sorry, but this makes no sense at all.
In fact, you even seem to realize what you are doing:
"I want to use linprog to get the optimum solution for x, y and z. I have used the equation: 60.44x+13.8y+8.4z = 154.8 as both the constraint equation and objective function as well. Ideally I should get the answer x=1. y=4 and z=4."
It makes no sense to minimize what is also being held constant. The solution can then be any set of values that satisfy the constraints. And this is your biggest problem. There are actually infinitely many solutions to your problem, as you have posed it to linprog. (Though there are NO integer solutions.) Let me pick 5 such "solutions".
v = null(Aeq)
v =
0.077044
-0.74247
0.66543
XYZhat = XYZ + v*linspace(0,-1,5)
XYZhat =
1.4181 1.3989 1.3796 1.3604 1.3411
1 1.1856 1.3712 1.5569 1.7425
6.5819 6.4155 6.2491 6.0828 5.9164
So each column of XYZhat is one such "solution".
Aeq*XYZhat
ans =
9 9 9 9 9
154.8 154.8 154.8 154.8 154.8
>> A*XYZhat
ans =
17.044 17.526 18.009 18.492 18.974
4.2556 4.2449 4.2343 4.2237 4.2131
>> f*XYZhat
ans =
154.8 154.8 154.8 154.8 154.8
You can see that each of those solutions satisfies ALL constraints. Of course, they all have the same objective value. So any of those possible solutions in XYZhat were equally good as a solution. But for no possible such solutions are all of them an integer.
So I'm sorry, but what you have done so far makes no sense at all. Your "practical" answer is not in fact an answer. And you can't use an optimizer to optimize a constant. If you do, you will get something arbitrary out, if you get anything at all out.
You need to re-think what you are doing. The basic problem formulation (as you have posed it) is not a well-posed optimization problem. What are you trying to minimize? If you want to minimize the power consumed, you cannot make the power consumed a constant!
If for example, I remove the constraint that the power consumed must be constant, then I get this:
f = [60.44 13.8 8.4];
A = [0.4 4.3 1.85; 0.377 0.43 0.5];
b = [32 5];
Aeq = [1 1 1];
beq = [9];
lb = [1 1 1];
ub = [9 9 9];
[XYZ,fval]=linprog(f,A,b,Aeq,beq,lb,ub)
Optimal solution found.
XYZ =
1
1
7
fval =
133.04
As you see, it shows a lower power consumption. In fact, that is also the best integer solution that exists.
[XYZ,fval]=intlinprog(f,[1 2 3],A,b,Aeq,beq,lb,ub)
LP: Optimal objective value is 133.040000.
Optimal solution found.
Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The
intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
XYZ =
1
1
7
fval =
133.04
What should you be doing? I have no clue there. You are the one building the model. You need to decide why a solution that has lower power consumed, that also satisfies all the constraints is not a better solution than your "practical" solution.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by