Search code examples
mathlinear-programmingcplexbin-packing

Error Type Not an array type in constrain defined in Cplex


I wrote an simple Binpacking problem in Cplex from here. My code is:

//Parameters

using CP;

int n=...;//Num item

range Item = 1..n;
range Bin = 1..n;

float c=...; //Volume Bin
float w_j=...; //Volume Item

//Decision variable

dvar boolean x[Bin][Item];
dvar boolean y[Bin];

// Objective


minimize sum (i in Bin) y[i];

// Constrains

subject to{
  
  forall(i in Bin)
    constrain_1:
    sum(j in Item) w_j[j]*x[i][j] <= c*y[i];

  forall(j in Item)
    constrain_2:
    sum(i in Bin) (x[i][j]) == 1;
 }

execute{
  if(cplex.getCplexStatus()==1){
    writeln("Item are placed in Bin as:", x.solutionValue);
    }
    
   else{
     writeln("Error. solution not found");
   }
    
}

For reading data:

n=10;

SheetConnection sheetData("data1.xlsx");
 
c from SheetRead(sheetData, "Sheet1!D2");
w_j from SheetRead(sheetData, "Sheet1!B2:B11");

Which n is the number of Item from 1 to 10 , c is bin volume and is 10, and w_j is the volume of items which are [7,9,2,8,4,6,7,8,3,6] and they are saved in a data1.xlsx.

The error is in (w_j[j]*x[i][j]):

Description Resource Path Location Type Not an array type.

I am sure there is no problem with accessing data because one time I removed that line and the code run correctly. Do you know where is the problem?


Solution

  • w_j is not a float but an array of float!

    .mod

    //using CP;
    
    
    int n=...;
    range Item = 1..n;
    range Bin = 1..n;
    
    float c[Bin]=...; //Volume Bin
    float w_j[Item]=...; //Volume Item
    
    //Decision variable
    
    dvar boolean x[Bin][Item];
    dvar boolean y[Bin];
    
    // Objective
    
    
    minimize sum (i in Bin) y[i];
    
    // Constrains
    
    subject to{
      
      forall(i in Bin)
        constrain_1:
        sum(j in Item) w_j[j]*x[i][j] <= c[i]*y[i];
    
      forall(j in Item)
        constrain_2:
        sum(i in Bin) (x[i][j]) == 1;
     }
    

    .dat

    n=3;
    
    
    c=[1,2,3]; //Volume Bin
    w_j=[1,2,3]; //Volume Item
    

    work fine