Question

I need to write a cplex program using the callable library in visual c++.

I need to use numrows and numcols in this way.

I only put the beginning of my program since my problem is at the beginning. My program crash and I find where. It happened after the loop to increment the numrows. It seems like it can't go out of the loop and std::cout << NUMROWS << "and "; never appear. If I write it in the loop I will see the value but not after. I can't find the reason. Do you know why?

Thanks

//subfunction pk, xik, yjk
int P(int k){
return k-1;
}

int X(int i, int k){
int p, n;
return p + (i-1)*(n-1) + (k-1);
}

int Y(int j, int k){
int p, n, m;
return p + n*p + (j-1)*(m-1) + (k-1);
}

int main (int argc, char **argv){

srand(time(0));
int status = 0;

int project = 4; 
int employee = 5; 
int time = 5; 

int empl [] = {2, 2, 2, 3};
int meet [] = {2, 4, 3, 3};

int n, m, k, p;
n=5;
m=5;
p=4;
int NUMCOLS=n+m;

CPXENVptr env = NULL;
CPXLPptr  lp  = NULL;

double *obj = new double [NUMCOLS]; 

//Objective function
int profit [] = {10, 20, 5, 15};
for (int i=0; i<project; i++){
        obj[i]=profit[i];
}

int      solstat;
double   objval;

double *lb = new double [NUMCOLS];
double *ub = new double [NUMCOLS];
double *x  = new double [NUMCOLS];
int    *matbeg = new int [NUMCOLS];
int    *matcnt = new int [NUMCOLS];
char   *ctype = new char [NUMCOLS];

int **F = new int*[employee]; 

for(int a = 0; a < employee; a++){      
    F[a] = new int [time];
    for(int b = 0; b < time; b++){
        F[a][b]=rand()%2;
        std::cout << F[a][b] << ", ";
    }
}

int NUMROWS=0;  

for(int i=1; i<=n; i++){ //Each xi      
    for(int j=1; j<=m; j++){ //Each yj
        if(F[i][j] ==0){
            NUMROWS++;
        } 
    }
}std::cout << NUMROWS << "and ";

double *rhs = new double [NUMROWS+1];
char   *sense = new char [NUMROWS+1];

for(int i=0; i < NUMROWS; i++) { //Each row
    rhs[i]=1;
    sense[i]='L';
}

int num_entries=-1;
for(int i=0; i < n; i++) {
    for(int j=0; j < m; j++) {
        if(F[i+1][j+1] ==0) { //1st constraint (pk, xik, yjk)
            num_entries++;
            num_entries++;
            num_entries++;
            std::cout << "try ";

        }
    }
}

int NUMNZ = NUMROWS*NUMCOLS;
int    *matind = new int [2*NUMROWS+m];
double *matval  = new double [2*NUMROWS+m];
matrix_entry *M = new matrix_entry  [num_entries+1+m];

num_entries=-1;
int row=-1;
for(int i=0; i < n; i++) {//1st constraint
    for(int j=0; j < m; j++) {
        if(F[i+1][j+1] ==0) {
            row++;
            num_entries++;
            M[num_entries].col=P(project); //pk
            M[num_entries].row=row;
            M[num_entries].val=-1;
            num_entries++;
            M[num_entries].col=X(i,project); //xik
            M[num_entries].row=row;
            M[num_entries].val=1;
            num_entries++;
            M[num_entries].col=Y(j,project); //yik
            M[num_entries].row=row;
            M[num_entries].val=1;
        }
    }
}
Was it helpful?

Solution

You have the following declarations.

int employee = 5; 
int time = 5; 
n=5;
m=5;

Then you initialize F array using--

for(int a = 0; a < employee; a++){      
    F[a] = new int [time];
    for(int b = 0; b < time; b++){
         F[a][b]=rand()%2;

Then you access it using--

for(int i=1; i<=n; i++){ //Each xi      
    for(int j=1; j<=m; j++){ //Each yj
        if(F[i][j] ==0){

So you are out of bounds by 1. Your loop should be

for(int i=0 i<n; i++){ //Each xi      
    for(int j=0; j<m; j++){ //Each yj

Suggestions:

  1. Please use std::vector or some other container (suggested by Basile Starynkevitch)

  2. Please name your variables better (which are easy to understand and remember)

  3. Please be consistent in the variables you use in the loops, you will forget less about bounds.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top