2

I am a beginner at CPLEX and I am struggling to add more constraints in my project. The script works well when I have multiple origin and destination, and just one product

I would like to do it with more products demand in each destination, and I do not know how to write the Constraints.

{string} Forest = {"A","B","C","D","E"};
{string} Destination = {"D1" , "D2"};
{string} Products = {"Pinus","Eucalyptus"};

float Demand [Destination][Products]= [[3,1],[4,5]];
float Distance [Forest][Destination]=[[21,52],[42,12],[25,15],[52,31],[9,42]]; 
float Stock [Forest][Products]= [[0.94,0],[0,8.62],[0,1.21],[2.6,0],[8.77,0]];` 

//Decision Variables
dvar float+ Delivered [Forest][Destination];

//Função Objetivo
minimize
sum (u in Forest, c in Destination) Distance[u][c] * Delivered[u][c];

//Constraints

subject to {
   forall (u in Forest)
      sum (c in Destination)
        Delivered[u][c] <= Stock [u];


   forall (c in Destination)
      sum (u in Forest) 
         Delivered[u][c] >= Demand[c];

}

I have cross-posted this question.

  • I have applied block formatting to your post - would you remove the backticks please? The [edit link is here](https://stackoverflow.com/posts/58428388/edit). – halfer Oct 18 '19 at 16:12
  • I have downvoted this question. If you could repair it as indicated, I shall undownvote. – halfer Oct 20 '19 at 12:19

2 Answers2

3

You shave to also expand your decision variable by Products (like you did for Demand and Stock) so that you can know how much of each product is delivered.

Then you can replicate each constraint for each product by adding a "forall (p in Products)".

dvar float+ Delivered [Forest][Destination][Products];

forall (p in Products)
  forall (u in Forest)
    sum (c in Destination)
      Delivered[u][c][p] <= Stock[u][p]; 
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
  • Thank you Daniel. I am really beginner. Thank for your help! – Marinna Gomes Oct 17 '19 at 22:55
  • If you want to offer meta-commentary, that is best separated out as comments under the question. That makes it easier to delete later on once the OP is aware of it. – halfer Oct 18 '19 at 16:24
0

you could try something like

{string} Forest = {"A","B","C","D","E"}; 

{string} Destination = {"D1" , "D2"};
{string} Products = {"Pinus","Eucalyptus"};

float Demand [Destination][Products]= [[3,1],[4,5]]; 

float Distance [Forest][Destination]=[[21,52],[42,12],[25,15],[52,31],[9,42]]; 

float Stock [Forest][Products]= [[0.94,0],[0,8.62],[0,1.21],[2.6,0],[8.77,0]]; 

//Decision Variables 
dvar float+ Delivered [Products][Forest][Destination];

//Função Objetivo 
minimize sum (p in Products,u in Forest, c in Destination) Distance[u][c] * Delivered[p][u][c];

//Constraints

subject to { 
forall (u in Forest,p in Products) sum (c in Destination) Delivered[p][u][c] <= Stock [u][p];

forall (p in Products,c in Destination) sum (u in Forest) Delivered[p][u][c] >= Demand[c][p];

}
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15