How to write constraints for a linear programming problem shown below? [Unable to write constraints even after referring to matlab help guides]

3 views (last 30 days)
Hi all, I am new to optimization. I have a linear objective function that I'd like to minimize. It goes as such:
Min O.F= 1/3(−9x1 + 4y1) + 1/3(−9x2 + 4y2) + 1/3(−9x3 + 4y3)
Constraints:
  • 0 ≤ ED ≤ 150,
  • 100 − ED = x1 + y1
  • 125 - ED = x2 + y2
  • 150 - ED = x3 + y3
  • x1, x2, x3 ≤ 0
  • y1, y2, y3 >= 0
I tried the code shown below after referring to some optimization guides, but I am constantly getting errors.
e=[100;125;150];
p=[1/3;1/3;1/3];
x=zeros(3,1);
y=zeros(3,1);
variables={'eD','x','y'};
N = length(variables);
lb = zeros(size(variables));
lb([y(1),y(2),y(3)]) = [0,0,0];
ub = Inf(size(variables));
ub([x(1),x(2),x(3)]) = [0,0,0];
Aeq=zeros(3,4);
Aeq(1,[eD,x(1),y(1)]) = [1,1,1];
Aeq(2,[eD,x(2),y(2)]) = [1,1,1];
Aeq(3,[eD,x(3),y(3)]) = [1,1,1];
beq=[100;125;150];
f = zeros(size(variables));
f([x(1) y(1) x(2) y(2) x(3) y(3)]) = [p(1)*-9 p(1)*4 p(2)*-9 p(2)*4 p(3)*-9 p(3)*4];
[x,fval] = linprog(f,Aeq,beq,lb,ub);
Any help would be appreciated

Answers (1)

Alan Weiss
Alan Weiss on 2 Mar 2015
You would do yourself a favor to read an introductory example about linear programming in MATLAB. The main idea is that the control variables, meaning the variables that linprog moves around to try to find the optimum, are the ones that you need to have in a single variable, which is usually called x. For example, you could have
x = [x1,x2,x3,y1,y2,y3,ED]
Then it is easy to write all your conditions in terms of the one variable x. This is 7 dimensions, so each row of your A or Aeq matrices has to have 7 components.
Please look at the example for details.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Imran
Imran on 2 Mar 2015
Edited: Imran on 2 Mar 2015
Thanks. I came to know about this mistake I was making and changed my code. The problem now is my optimization is running smoothly but my results are not correct. I know this because I already have the solution from the book which has this problem. Here's my new code:
clc;
clear all;
close all;
variables={'eD','eup1','eup2','eup3','edw1','edw2','edw3'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
lb = zeros(size(variables));
lb([eD,edw1,edw2,edw3])=[0,0,0,0];
ub = Inf(size(variables));
ub([eD,eup1,eup2,eup3])=[150,0,0,0];
Aeq(1,[eD,eup1,edw1]) = [1,1,1];
Aeq(2,[eD,eup2,edw2]) = [1,1,1];
Aeq(3,[eD,eup3,edw3]) = [1,1,1];
beq=[100;125;150];
f = zeros(size(variables));
f([eup1 edw1 eup2 edw2 eup3 edw3]) = [-3 4/3 -3 4/3 -3 4/3];
[x,fval] = linprog(f,[],[],Aeq,beq,lb,ub);
disp('The optimal capacity to trade is:');
disp(x(1));
(I have replaced x and y with eup and edw for my own easy understanding). Could you please try and tell me if my constraints and OF are coded properly. Any advice would be appreciated.Thanks.
Alan Weiss
Alan Weiss on 3 Mar 2015
I think that you gave bounds incorrectly.
x1, x2, x3 ≤ 0
y1, y2, y3 >= 0
To me, this means that the lower bounds on the x1, x2, and x3 variables are -Inf, and the upper bounds on them are 0.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!