简体   繁体   English

Cplex C ++循环

[英]Cplex c++ loop for

I need to write a cplex program using the callable library in visual c++. 我需要使用Visual c ++中的可调用库编写一个cplex程序。

I need to use numrows and numcols in this way. 我需要以这种方式使用numrows和numcols。

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 "; 似乎无法摆脱循环和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;
        }
    }
}

You have the following declarations. 您具有以下声明。

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

Then you initialize F array using-- 然后使用以下命令初始化F数组:

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 . 因此,您超出了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 ) 请使用std::vector或其他容器(由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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM