Cplex c++ loop for -


i need write cplex program using callable library in visual c++.

i need use numrows , numcols in way.

i put beginning of program since problem @ beginning. program crash , find where. happened after loop increment numrows. seems can't go out of loop , std::cout << numrows << "and "; never appear. if write in loop see value not after. can't find reason. 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}; (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 = 0; < 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; < numrows; i++) { //each row     rhs[i]=1;     sense[i]='l'; }  int num_entries=-1; for(int i=0; < 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; < 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 following declarations.

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

then initialize f array using--

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

then access 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 out of bounds 1. 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 other container (suggested basile starynkevitch)

  2. please name variables better (which easy understand , remember)

  3. please consistent in variables use in loops, forget less bounds.

hope helps.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -