Search code examples
schedulingcplex

How to add new constraints in the scheduling problem at CPLEX?


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.


Solution

  • 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];