我需要使用 Visual c++ 中的可调用库编写一个 cplex 程序。
我需要以这种方式使用 numrows 和 numcols 。
我只放了程序的开头,因为我的问题在开头。我的程序崩溃了,我找到了哪里。它发生在循环增加numrows之后。似乎它不能跳出循环和 std::cout << NUMROWS << "and "; 永远不会出现。如果我把它写在循环中,我会看到这个值,但不是之后。我找不到原因。你知道为什么吗?
谢谢
//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;
}
}
}