Constraint with Loop

924 views
Skip to first unread message

Raouf

unread,
Apr 18, 2013, 6:37:43 AM4/18/13
to yal...@googlegroups.com
Hi,
I'm new with the yalmip, so i want to to solve a in binary problem (the fixed charge location problem/warehouse location problem see the annex for the cplex model of this problem) The question is : is it possible to use a loop on variable when constructing identical constraints to simplify the constraint construction ? (see constraint 3)

this example is my first try with yalmip i hope it's easy to understand...
All comments are welcome to improve my model
Example :

Parameters:
 fj = [5 6]; %Cost of server
 cj = [12 13];%capacity of server
 cj1 = diag(cj);%diagonal of server capacity 
 dij=[1 4;2 4;3 1;5 2];%distance from server  j to client i
 
 hi=[2;4;2;4];%client i demand
 hi1=diag(hi);%diagonal of the demand 
 alpha=1;%travel cost

a1=ones(1,2); 
b1=ones(4,1);

%variables 
y = binvar(4,2); %client i is served by server j 
x = binvar(2);  %locate a server at  j then  x=1 else 0 

%constraint
Contraints = [y*b1==b1, y'*hi1-cj1*x<=0, y(i,j)-x(j) <=0];
%constraint 1: y*b1==b1 cover all the point of demand
%constraint 2: y'*hi1-cj1*x<=0 contraint of capacity limitation
%constraint 3: y(i,j)-x(j) for each i and j 
obj=fj*cj+alpha*sum(sum((hi1*y)'*dij));
solvesdp(Contraints,obj);


Cplex version:
int Fixed = ...;
{string} Warehouses = ...;
int NbStores = ...;
range Stores = 0..NbStores-1;
int Capacity[Warehouses] = ...;
int SupplyCost[Stores][Warehouses] = ...;
dvar boolean Open[Warehouses];
dvar boolean Supply[Stores][Warehouses];


minimize
  sum( w in Warehouses ) 
    Fixed * Open[w] +
  sum( w in Warehouses , s in Stores ) 
    SupplyCost[s][w] * Supply[s][w];
    

subject to{
  forall( s in Stores )
    ctEachStoreHasOneWarehouse:
      sum( w in  Warehouses ) 
        Supply[s][w] == 1;
  forall( w in Warehouses, s in Stores )
    ctUseOpenWarehouses:
      Supply[s][w] <= Open[w];
  forall( w in Warehouses )
    ctMaxUseOfWarehouse:         
      sum( s in Stores ) 
        Supply[s][w] <= Capacity[w];
}


Johan Löfberg

unread,
Apr 18, 2013, 7:09:54 AM4/18/13
to yal...@googlegroups.com
First, I think you want x = binvar(2,2,'full'); As you have defined it now, it is symmetric
http://users.isy.liu.se/johanl/yalmip/pmwiki.php?n=Tutorials.Basics

Concerning loops, I am not sure what it is you want to do, but the general rule is that YALMIP is MATLAB consistent, so if if can do it in MATLAB with numerical variables, you can do it in YALMIP

Something like this, although there are some strange things here as your notation was unclear
%constraint
Constraints = [y*b1==b1];
obj
= 0;
for j = 1:2
 
for i = 1:4
 
Constraints = [Constraints, y(i,j)-x(j)<=0];
 
end
 Constraints = [Constraints, y'*hi1-cj1*x<=0];??
 obj=obj + f(j)*c(j)+alpha*sum(sum((hi1*y)'*d(i,j));??
end

Your code can probably be simplified a lot by using standard MATLAB code. For instance, your first constraint is simply

sum(y,2) == 1

In a similar way, you don't have to introduce those diagonal matrices you create etc.



Raouf

unread,
Apr 18, 2013, 7:43:01 AM4/18/13
to yal...@googlegroups.com
First, Thank for your fast answers, i'll try to simplify my model with your advices and repost it to see if am on the right direction.

Raouf

unread,
Apr 18, 2013, 11:41:57 AM4/18/13
to yal...@googlegroups.com
I have considered your comment and I changed my model. Here is the new version and the message listed by matlab (i used CPLEX and the default solver)

%Parameters:
 fj = [5;6]; %location cost j
 cj = [25;27];%capacity of server j

 dij=[1 4;2 4;3 1;5 2];%distance from server j to client i
 
 hi=[1 1;2 2;3 3;4 4];%demand of client i
 
 %alpha=1;%

%variables 
y = binvar(4,2); 
x = binvar(2,1); 
%x = binvar(2,1,'full');%what what a full bring to  a binary variable of
%2 lines and one collumn


%constraints
Constraints = [sum(y,2)==1,sum(y)'-x<=0];
for j = 1:2
 for i = 1:4
  Constraints = [Constraints, y(i,j)-x(j)<=0];
  
 end
end
obj=sum(fj.*x) + sum(sum((hi.*y.*dij))) ;
%solvesdp(Constraints,obj);

options = sdpsettings('verbose',1,'solver','cplex');
sol = solvesdp(Constraints,obj,options);
Row 'c1' infeasible, all entries at implied bounds.
Presolve time =    0.00 sec.

Johan Löfberg

unread,
Apr 18, 2013, 12:38:59 PM4/18/13
to yal...@googlegroups.com
cplex says the problem is infeasible, which it trivially is


Constraints = [sum(y,2)==1,sum(y)'-x<=0];

Here you say there has to be exactly one 1 on every row of y, and then that the sum of every column should be at most 1 (since x can be at most 1). That is totally inconsistent since at least one column will have at least 2 1s.

[1 0
 1 0
 1 0
 1 0]

one columns sums to 4, so obviously not feasible

[1 0
 0 1
 0 0
 0 1]

both columns sum to 2, so obviously not feasible

etc etc.



Johan Löfberg

unread,
Apr 18, 2013, 12:59:20 PM4/18/13
to yal...@googlegroups.com
Siunce you seem to struggle with vectorization of MATLAB code, it might be useful to go with a direct line-by-line translation of the code. Something like this

Open   = binvar(nWarehouses,1);
Supply = binvar(nStores, nWarehouses,'full');

obj
= 0;
for w = 1:nWarehouses
    obj
= obj + Fixed*Open(w);
   
for s = 1:nStores
        obj
= obj +  SupplyCost(s,w)*Supply(s,w);
   
end
end

Constraints = [];
for s = 1:nStores
   
Constraints = [Constraints, sum(Supply(s,;))==1];
end
for s = 1:nStores
   
for w = 1:nWarehouses
       
Constraints = [Constraints,Supply(s,w) <= Open(w)];
   
end
end
for w = 1:nWarehouses
   
Constraints = [Constraints, sum(Supply(:,w))<= Capacity(w)];
end





Raouf

unread,
Apr 19, 2013, 5:17:22 AM4/19/13
to yal...@googlegroups.com
Thank you for your help, i just realized the errors with my model :) so i  rebuild it with matlab vectorization and some of your advice and it's ok. I will pass YALMIP to my colleague and  the student of our lab.
Reply all
Reply to author
Forward
0 new messages