How can I solve the problem with linprog function described in post?

9 Ansichten (letzte 30 Tage)
Hello,
I have a problem with some linear programming problem. As in MS Excel software, I do not have problems with this issue and it works there, transfer the same to Matlab is too difficult for me. I have to include it in my script.
For example, we have matrix A, B and C: A = [1000 2000 5000 6000 200 300] B = [20 20 5 5 30 10] C = [5 5 30 25 15 10] The variables are "x" and "y".
We also include costs: for example, the unit of "x" costs 1000. the unit of "y" costs 200.
We create the matrix D, which we calculate in such a way like below: D = A-B*x-C*y Here we have to put a condition that if D (i) <0 then D (i) = 0 From this matrix, we count the sum of D. Restriction: the sum of (D) * 3/300 <= 85 "x" and "y" are integers, greater than or equal to 0. The function is to minimize costs, ie x * unit cost of x + y * unit cost of y must be minimized
I very kindly please help me, I'm in trouble ...
PS. If it will be helpful, for assumed data as above from MS excel solver: The optimal results are x = 0, y = 85 and the sum (D) * 3/300 = 84.75.
Best regards M

Akzeptierte Antwort

John D'Errico
John D'Errico am 12 Nov. 2017
Bearbeitet: John D'Errico am 12 Nov. 2017
You cannot use linprog. Not to solve a problem with integer variables. You will need intlinprog to solve that class of problem.
Next, you don't have two distinct variables. You have a vector, of length two, which corresponds to the unknowns. Until you start to think in terms of vectors and arrays, you cannot use MATLAB. You will still be just trying to use Excel, but in MATLAB. That will never work well, even if you do manage to cobble up a solution. Think in the language you are using. For example, if you try to speak in Spanish, but are still thinking in English, you will have a hell of a hard time to converse fluently in Spanish. (I can say this because over the years I have managed to attain fluency in multiple programming languages, but despite years of classes in several spoken languages while in school, I still thought in English.)
So, you have a vector of unknowns. I'll call it xy. Assume it has length 2, so we will make it a column vector, thus of shape 2x1.
You have a vector of costs. Call it costs.
costs = [1000;200];
You want to minimize the product costs*xy.
Next, you don't form the result D as you did. Remember, xy is a VECTOR.
A = [1000;2000;5000;6000;200;300];
BC = [20 20 5 5 30 10;5 5 30 25 15 10]';
So BC is a 6x2 array. The first column of BC looks like your B vector. The second column looks like what you gave as C. What would this result be?
D = A-BC*xy
What are your constraints? Again, think! But think in terms of matrices. Intlinprog won't work any other way. So
D >= 0
implies that
A - BC*xy >= 0
In the form that intlinprog wants to see, this means
BC*xy <= A
That gives a set of 6 linear inequality constraints. (With one more, described below.)
Do you have other constraints? Of course you do. both of x and y are integers, and non-negative. That means they both have a lower bound of 0, and an upper bound of inf.
lb = [0 0];
ub = [inf inf];
That they are integers is given as
intcon = [1 2];
So variables 1 and 2 are BOTH constrained to be positive integers.
There is one other inequality constraint to consider. It looked like this, as you described it:
sum(D)*3/300 <= 85
But how can you write the sum of D? Again, you need to think in terms of matrices. What is a sum? You can trivially form a sum of a vector by multiplying the vector with a vector of ones.
ones(1,6)*D*3/300 <= 85
So, expanding this, we have
ones(1,6)*A*3/300 - ones(1,6)*BC*3/300*xy <= 85
That becomes a single linear inequality constraint on the vector xy. Combined with those from above, we have
Aineq = [BC;-ones(1,6)*BC*3/300];
bineq = [A;85-ones(1,6)*A*3/300];
Finally, I think you had a typo in what you wrote. I think you missed a couple of zeros when you gave us A. You said the last two elements of A were 200 and 300. But think you may have intended them to be more like 2000 and 3000. I say this because intlinprog found no integer solutions to the problem you posed.
A = [1000;2000;5000;6000;2000;3000];
BC = [20 20 5 5 30 10;5 5 30 25 15 10]';
lb = [0 0];
ub = [inf inf];
intcon = [1 2];
Aineq = [BC;-ones(1,6)*BC*3/300];
bineq = [A;85-ones(1,6)*A*3/300];
xy = intlinprog(costs,intcon,Aineq,bineq,[],[],lb,ub)
If I try to pose the above problem, now I get a solution that is not inconsistent with the one you gave.
LP: Optimal objective value is 23333.333333.
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).
xy =
0
117
Finally, what is the sum of D*3/300?
sum(A-BC*xy)*3/300
ans =
84.7
So xy satisfies the constraints, and minimizes costs'*xy. Be careful though, as I see the output might confuse you. The output stated that
LP: Optimal objective value is 23333.333333.
But that is apparently the minimum of a LINEAR program, if I were to solve it without any integer constraint on xy. (The linear programming solution would have resulted in y=116.666666666667, not 117.) The actual minimum total cost to the ILP problem is 23400.
costs'*xy
ans =
23400
Anyway, you need to start thinking as if you were using MATLAB. If you think as if you are using Excel, you might as well keep on using Excel. Excel is a perfectly good tool, but you must think differently to use MATLAB.

Weitere Antworten (1)

Maciej Knapik
Maciej Knapik am 13 Nov. 2017
Thank You for your answer. It is a huge help, hint and advice for me. I am still learning Matlab more.
At this moment I can see that there I made mistake in assumptions.
A - BC*xy
can reach negative values... BUT to sum sum(A-BC*xy)*3/300 should take ONLY positive values.
I attached figure sample. It is sample of BC*xy values and at the end it should count only positve values, greater or equal than 0 and the sum of this positive values*3/300 should be less than 85.
It is possible to make it? I kindly ask for help again ...
Best regards M

Community Treasure Hunt

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

Start Hunting!

Translated by