How can I define a constraint to my optimization problem?

Hi,
I am trying to write my optimization model by following the way of Factory, Warehouse, Sales Allocation Model: Solver-Based example. Unfortunately, I have this error:
% Unable to perform assignment because the size of the left side is 90-by-1 and the size of the right side is 5-by-3.
%
% Error in Constraint (line 28)
% xtemp(:,:,ii) = d_ik;
The optimization model:
This is the code I tried to write:
i = 5; k = 3; t = 6;
d_ik = [1,5,6; 3,6,3; 5,7,0; 6,3,9; 2,8,8];
TR_t = [1000, 1000, 1000, 1000, 1000, 1000];
X_ikt = zeros(i,k,t); % Allocate arrays
for ii = 1:i
for jj = 1:k
for kk = 1:t
obj(ii,jj,kk) = 1;
end
end
end
obj = X_ikt(:);
matwid = length(obj);
Aineq = spalloc(t,matwid,i*k*t);
bineq = zeros(t,1);
clearer1 = zeros(size(obj));
clearer12 = clearer1(:);
counter = 1;
for ii = 1:t
xtemp = clearer1;
xtemp(:,:,ii) = d_ik;
xtemp = sparse(xtemp(:)); % Convert to sparse
Aineq(counter,:) = xtemp'; % Fill in the row
bineq(counter) = TR(ii)';
counter = counter + 1;
end
intcon = t+1:length(obj);
lb = zeros(length(obj),1);
ub = Inf(length(obj),1);
opts = optimoptions('intlinprog','Display','off','PlotFcn',@optimplotmilp);
[solution,fval,exitflag,output] = intlinprog(obj,intcon,Aineq,bineq,lb,ub,opts);
PS: I am intended to write my model like Factory, Warehouse, Sales Allocation Model: Solver-Based example to obtain computational efficiency for large scale models and to solve using different techniques such as Genetic Algorithm.

2 Comments

OK, well you need to tell us which information in the error message contradicts what you intended to happen? If you didn't intend the left hand side xtemp(:,:,ii) at line 22 to be 90x1 then what did you intend? Similarly if you didn't intend the right hand side d_ik to be 5x3 what did you intend for that?
I want to obtain Aineq matrix corretly to run intlinprog function. It must be (t, i*k*t) sized matrix. I am not very familiar with coding using Matlab but please let me demonstrate it with an example below:
Aineq = [2 3 1 6 7 5 0 --------------------------------------------------- 0 % for t = 1
0 --------- 0 3 5 1 5 6 8 0 ------------------------------------- 0 % for t = 2
0 ------------------------0 3 6 8 4 2 4 0 ----------------------- 0 % for t = 3
0 --------------------------------------0 1 4 3 2 5 5 0 --------- 0 % for t = 4
0 --------------------------------------------------- 0 2 4 5 6 2 5]
Row size must be t = 6, column size equal to i*k*t = 90 and each coefficient size (non-zero elements in a row) is i*k = 15. Coefficients are coming from d_ik. For instance the coefficient for d_ik(1,2)*X(1,2,5) = 5*X(1,2,5).
I was following Factory, Warehouse, Sales Allocation Model: Solver-Based example. I just couldn't figure out what part I am missing.
Thank you.

Sign in to comment.

 Accepted Answer

Aineq=kron(speye(t),d_ik(:).');
bineq=TR;
Note also that your call to intlinprog is the wrong syntax. You haven't specified the equality constraints (or indicated that there are none),
intlinprog(-obj,intcon,Aineq,bineq,[],[],lb,ub,opts)

3 Comments

Yes, it works! I appreciate your help. Here is the code I run:
P.S: Inf ub resulted with an unbounded solution and I defined 500 as ub.
i = 5; k = 3; t = 6;
d_ik = [1,5,6; 3,6,3; 5,7,0; 6,3,9; 2,8,8];
TR_t = [1000, 1000, 1000, 1000, 1000, 1000];
obj = zeros(i,k,t); % X_ikt
for ii = 1:i
for jj = 1:k
for kk = 1:t
obj(ii,jj,kk) = 1;
end
end
end
obj = obj(:);
matwid = length(obj);
Aineq=kron(speye(t),d_ik(:).');
bineq=TR_t;
intcon = t+1:length(obj);
lb = zeros(length(obj),1);
ub = repmat(500,length(obj),1);
opts = optimoptions('intlinprog','Display','off','PlotFcn',@optimplotmilp);
[solution,fval,exitflag,output] = intlinprog(-obj,intcon,Aineq,bineq,[],[],lb,ub,opts)
Note that instead of this part
obj = zeros(i,k,t); % X_ikt
for ii = 1:i
for jj = 1:k
for kk = 1:t
obj(ii,jj,kk) = 1;
end
end
end
obj = obj(:);
you could just do,
obj=ones(i,t,k);
Good simplification! Thank you.

Sign in to comment.

More Answers (1)

Perhaps you would find it easier to follow the problem-based version of the example. It is indeed difficult to get the constraint matrices straight using the solver-based approach.
If you eventually have to convert the problem to a solver-based approach, you can first write the model in the problem-based approach and then convert it using prob2struct.
Alan Weiss
MATLAB mathematical toolbox documentation

Categories

Community Treasure Hunt

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

Start Hunting!