lemon/lp_cplex.cc
changeset 1529 c914e7ec2b7b
parent 1473 876c7b7f4dae
child 1542 0219ee65ffcc
equal deleted inserted replaced
4:7edcda98ddad 5:4d3e4f0595e2
    43     //The first approach opens a new environment
    43     //The first approach opens a new environment
    44     LpCplex* newlp=new LpCplex();
    44     LpCplex* newlp=new LpCplex();
    45     //The routine CPXcloneprob can be used to create a new CPLEX problem 
    45     //The routine CPXcloneprob can be used to create a new CPLEX problem 
    46     //object and copy all the problem data from an existing problem 
    46     //object and copy all the problem data from an existing problem 
    47     //object to it. Solution and starting information is not copied.
    47     //object to it. Solution and starting information is not copied.
    48     newlp->lp = CPXcloneprob (env, lp, &status);
    48     newlp->lp = CPXcloneprob(env, lp, &status);
    49     return *newlp;
    49     return *newlp;
    50   }
    50   }
    51 
    51 
    52   int LpCplex::_addCol()
    52   int LpCplex::_addCol()
    53   {
    53   {
    54     int i = CPXgetnumcols (env, lp);
    54     int i = CPXgetnumcols(env, lp);
    55     Value lb[1],ub[1];
    55     Value lb[1],ub[1];
    56     lb[0]=-INF;//-CPX_INFBOUND;
    56     lb[0]=-INF;//-CPX_INFBOUND;
    57     ub[0]=INF;//CPX_INFBOUND;
    57     ub[0]=INF;//CPX_INFBOUND;
    58     status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
    58     status = CPXnewcols(env, lp, 1, NULL, lb, ub, NULL, NULL);
    59     return i;
    59     return i;
    60   }
    60   }
    61 
    61 
    62   
    62   
    63   int LpCplex::_addRow() 
    63   int LpCplex::_addRow() 
    65     //We want a row that is not constrained
    65     //We want a row that is not constrained
    66     char sense[1];
    66     char sense[1];
    67     sense[0]='L';//<= constraint
    67     sense[0]='L';//<= constraint
    68     Value rhs[1];
    68     Value rhs[1];
    69     rhs[0]=INF;
    69     rhs[0]=INF;
    70     int i = CPXgetnumrows (env, lp);
    70     int i = CPXgetnumrows(env, lp);
    71     status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
    71     status = CPXnewrows(env, lp, 1, rhs, sense, NULL, NULL);
    72     return i;
    72     return i;
    73   }
    73   }
    74 
    74 
    75 
    75 
    76   void LpCplex::_eraseCol(int i) {
    76   void LpCplex::_eraseCol(int i) {
    77     CPXdelcols (env, lp, i, i);
    77     CPXdelcols(env, lp, i, i);
    78   }
    78   }
    79   
    79   
    80   void LpCplex::_eraseRow(int i) {
    80   void LpCplex::_eraseRow(int i) {
    81     CPXdelrows (env, lp, i, i);
    81     CPXdelrows(env, lp, i, i);
    82   }
    82   }
    83 
    83 
    84   
    84   
    85   ///\warning Data at index 0 is ignored in the arrays.
    85   ///\warning Data at index 0 is ignored in the arrays.
    86   void LpCplex::_setRowCoeffs(int i, 
    86   void LpCplex::_setRowCoeffs(int i, 
   117 			    const_cast<Value * >(values+1));
   117 			    const_cast<Value * >(values+1));
   118   }
   118   }
   119   
   119   
   120   void LpCplex::_setCoeff(int row, int col, Value value) 
   120   void LpCplex::_setCoeff(int row, int col, Value value) 
   121   {
   121   {
   122     CPXchgcoef (env, lp, row, col, value);
   122     CPXchgcoef(env, lp, row, col, value);
   123   }
   123   }
   124 
   124 
   125   void LpCplex::_setColLowerBound(int i, Value value)
   125   void LpCplex::_setColLowerBound(int i, Value value)
   126   {
   126   {
   127     int indices[1];
   127     int indices[1];
   128     indices[0]=i;
   128     indices[0]=i;
   129     char lu[1];
   129     char lu[1];
   130     lu[0]='L';
   130     lu[0]='L';
   131     Value bd[1];
   131     Value bd[1];
   132     bd[0]=value;
   132     bd[0]=value;
   133     status = CPXchgbds (env, lp, 1, indices, lu, bd);
   133     status = CPXchgbds(env, lp, 1, indices, lu, bd);
   134  
   134  
   135   }
   135   }
   136   
   136   
   137   void LpCplex::_setColUpperBound(int i, Value value)
   137   void LpCplex::_setColUpperBound(int i, Value value)
   138   {
   138   {
   140     indices[0]=i;
   140     indices[0]=i;
   141     char lu[1];
   141     char lu[1];
   142     lu[0]='U';
   142     lu[0]='U';
   143     Value bd[1];
   143     Value bd[1];
   144     bd[0]=value;
   144     bd[0]=value;
   145     status = CPXchgbds (env, lp, 1, indices, lu, bd);
   145     status = CPXchgbds(env, lp, 1, indices, lu, bd);
   146   }
   146   }
   147 
   147 
   148   //This will be easier to implement
   148   //This will be easier to implement
   149   void LpCplex::_setRowBounds(int i, Value lb, Value ub)
   149   void LpCplex::_setRowBounds(int i, Value lb, Value ub)
   150   {
   150   {
   158     indices[0]=i;
   158     indices[0]=i;
   159     char sense[1];
   159     char sense[1];
   160 
   160 
   161     if (lb==-INF){
   161     if (lb==-INF){
   162       sense[0]='L';
   162       sense[0]='L';
   163       CPXchgsense (env, lp, cnt, indices, sense);
   163       CPXchgsense(env, lp, cnt, indices, sense);
   164       CPXchgcoef (env, lp, i, -1, ub);
   164       CPXchgcoef(env, lp, i, -1, ub);
   165       
   165       
   166     }
   166     }
   167     else{
   167     else{
   168       if (ub==INF){
   168       if (ub==INF){
   169 	sense[0]='G';
   169 	sense[0]='G';
   170 	CPXchgsense (env, lp, cnt, indices, sense);
   170 	CPXchgsense(env, lp, cnt, indices, sense);
   171 	CPXchgcoef (env, lp, i, -1, lb);
   171 	CPXchgcoef(env, lp, i, -1, lb);
   172       }
   172       }
   173       else{
   173       else{
   174 	if (lb == ub){
   174 	if (lb == ub){
   175 	  sense[0]='E';
   175 	  sense[0]='E';
   176 	  CPXchgsense (env, lp, cnt, indices, sense);
   176 	  CPXchgsense(env, lp, cnt, indices, sense);
   177 	  CPXchgcoef (env, lp, i, -1, lb);
   177 	  CPXchgcoef(env, lp, i, -1, lb);
   178 	}
   178 	}
   179 	else{
   179 	else{
   180 	  sense[0]='R';
   180 	  sense[0]='R';
   181 	  CPXchgsense (env, lp, cnt, indices, sense);
   181 	  CPXchgsense(env, lp, cnt, indices, sense);
   182 	  CPXchgcoef (env, lp, i, -1, lb);
   182 	  CPXchgcoef(env, lp, i, -1, lb);
   183 	  CPXchgcoef (env, lp, i, -2, ub-lb);	  
   183 	  CPXchgcoef(env, lp, i, -2, ub-lb);	  
   184 	}
   184 	}
   185       }
   185       }
   186     }
   186     }
   187   }
   187   }
   188 
   188 
   195 //   {
   195 //   {
   196 //     //Not implemented, obsolete
   196 //     //Not implemented, obsolete
   197 // //     //TODO Ezt kell meg megirni
   197 // //     //TODO Ezt kell meg megirni
   198 // //     //type of the problem
   198 // //     //type of the problem
   199 // //     char sense[1];
   199 // //     char sense[1];
   200 // //     status = CPXgetsense (env, lp, sense, i, i);
   200 // //     status = CPXgetsense(env, lp, sense, i, i);
   201 // //     Value rhs[1];
   201 // //     Value rhs[1];
   202 // //     status = CPXgetrhs (env, lp, rhs, i, i);
   202 // //     status = CPXgetrhs(env, lp, rhs, i, i);
   203 
   203 
   204 // //     switch (sense[0]) {
   204 // //     switch (sense[0]) {
   205 // //     case 'L'://<= constraint
   205 // //     case 'L'://<= constraint
   206 // //       break;
   206 // //       break;
   207 // //     case 'E'://= constraint
   207 // //     case 'E'://= constraint
   212 // //       break;
   212 // //       break;
   213 // //     default: ;
   213 // //     default: ;
   214 // //       //FIXME error
   214 // //       //FIXME error
   215 // //     }
   215 // //     }
   216 
   216 
   217 // //     status = CPXchgcoef (env, lp, i, -2, value_rng);
   217 // //     status = CPXchgcoef(env, lp, i, -2, value_rng);
   218 //   }
   218 //   }
   219   
   219   
   220   void LpCplex::_setObjCoeff(int i, Value obj_coef)
   220   void LpCplex::_setObjCoeff(int i, Value obj_coef)
   221   {
   221   {
   222     CPXchgcoef (env, lp, -1, i, obj_coef);
   222     CPXchgcoef(env, lp, -1, i, obj_coef);
   223   }
   223   }
   224 
   224 
   225   void LpCplex::_clearObj()
   225   void LpCplex::_clearObj()
   226   {
   226   {
   227     for (int i=0;i< CPXgetnumcols (env, lp);++i){
   227     for (int i=0;i< CPXgetnumcols(env, lp);++i){
   228       CPXchgcoef (env, lp, -1, i, 0);
   228       CPXchgcoef(env, lp, -1, i, 0);
   229     }
   229     }
   230     
   230     
   231   }
   231   }
   232   // The routine returns zero unless an error occurred during the
   232   // The routine returns zero unless an error occurred during the
   233   // optimization. Examples of errors include exhausting available
   233   // optimization. Examples of errors include exhausting available
   239   // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
   239   // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
   240   // further information about the status of the optimization.
   240   // further information about the status of the optimization.
   241   LpCplex::SolveExitStatus LpCplex::_solve()
   241   LpCplex::SolveExitStatus LpCplex::_solve()
   242   {
   242   {
   243     //CPX_PARAM_LPMETHOD 
   243     //CPX_PARAM_LPMETHOD 
   244     status = CPXlpopt (env, lp);
   244     status = CPXlpopt(env, lp);
   245     if (status == 0){
   245     if (status == 0){
   246       //We want to exclude some cases
   246       //We want to exclude some cases
   247       switch (CPXgetstat (env, lp)){
   247       switch (CPXgetstat(env, lp)){
   248       case CPX_OBJ_LIM:
   248       case CPX_OBJ_LIM:
   249       case CPX_IT_LIM_FEAS:
   249       case CPX_IT_LIM_FEAS:
   250       case CPX_IT_LIM_INFEAS:               
   250       case CPX_IT_LIM_INFEAS:               
   251       case CPX_TIME_LIM_FEAS:
   251       case CPX_TIME_LIM_FEAS:
   252       case CPX_TIME_LIM_INFEAS:
   252       case CPX_TIME_LIM_INFEAS:
   261   }
   261   }
   262 
   262 
   263   LpCplex::Value LpCplex::_getPrimal(int i)
   263   LpCplex::Value LpCplex::_getPrimal(int i)
   264   {
   264   {
   265     Value x;
   265     Value x;
   266     CPXgetx (env, lp, &x, i, i);
   266     CPXgetx(env, lp, &x, i, i);
   267     return x;
   267     return x;
   268   }
   268   }
   269   
   269   
   270   LpCplex::Value LpCplex::_getPrimalValue()
   270   LpCplex::Value LpCplex::_getPrimalValue()
   271   {
   271   {
   272     Value objval;
   272     Value objval;
   273     //method = CPXgetmethod (env, lp);
   273     //method = CPXgetmethod (env, lp);
   274     status = CPXgetobjval (env, lp, &objval);
   274     //printf("CPXgetprobtype %d \n",CPXgetprobtype(env,lp));
       
   275     status = CPXgetobjval(env, lp, &objval);
       
   276     //printf("Objective value: %g \n",objval);
   275     return objval;
   277     return objval;
   276   }
   278   }
   277   
   279   
   278 
   280 
   279 //7.5-os cplex statusai (Vigyazat: a 9.0-asei masok!)
   281 //7.5-os cplex statusai (Vigyazat: a 9.0-asei masok!)
   327 // ??case CPX_INForUNBD                   
   329 // ??case CPX_INForUNBD                   
   328 // ??case CPX_PIVOT                       
   330 // ??case CPX_PIVOT                       
   329 
   331 
   330   LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
   332   LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
   331   {
   333   {
   332     int stat = CPXgetstat (env, lp);
   334     int stat = CPXgetstat(env, lp);
       
   335     //printf("A primal status: %d, CPX_OPTIMAL=%d \n",stat,CPX_OPTIMAL);
   333     switch (stat) {
   336     switch (stat) {
   334     case 0:
   337     case 0:
   335       return UNDEFINED; //Undefined
   338       return UNDEFINED; //Undefined
   336     case CPX_OPTIMAL://Optimal
   339     case CPX_OPTIMAL://Optimal
   337       return OPTIMAL;
   340       return OPTIMAL;
   377 // CPX_STAT_OPTIMAL_RELAXED
   380 // CPX_STAT_OPTIMAL_RELAXED
   378 // CPX_STAT_UNBOUNDED
   381 // CPX_STAT_UNBOUNDED
   379 
   382 
   380   LpCplex::SolutionStatus LpCplex::_getDualStatus()
   383   LpCplex::SolutionStatus LpCplex::_getDualStatus()
   381   {
   384   {
   382     int stat = CPXgetstat (env, lp);
   385     int stat = CPXgetstat(env, lp);
   383     switch (stat) {
   386     switch (stat) {
   384     case 0:
   387     case 0:
   385       return UNDEFINED; //Undefined
   388       return UNDEFINED; //Undefined
   386     case CPX_OPTIMAL://Optimal
   389     case CPX_OPTIMAL://Optimal
   387       return OPTIMAL;
   390       return OPTIMAL;
   393     }
   396     }
   394   }
   397   }
   395 
   398 
   396   LpCplex::ProblemTypes LpCplex::_getProblemType()
   399   LpCplex::ProblemTypes LpCplex::_getProblemType()
   397   {
   400   {
   398     int stat = CPXgetstat (env, lp);
   401     int stat = CPXgetstat(env, lp);
   399     switch (stat) {
   402     switch (stat) {
   400     case CPX_OPTIMAL://Optimal
   403     case CPX_OPTIMAL://Optimal
   401 	return PRIMAL_DUAL_FEASIBLE;
   404 	return PRIMAL_DUAL_FEASIBLE;
   402     case CPX_UNBOUNDED:
   405     case CPX_UNBOUNDED:
   403  	return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
   406  	return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
   412     }
   415     }
   413   }
   416   }
   414 
   417 
   415   void LpCplex::_setMax()
   418   void LpCplex::_setMax()
   416   {
   419   {
   417     CPXchgobjsen (env, lp, CPX_MAX);
   420     CPXchgobjsen(env, lp, CPX_MAX);
   418    }
   421    }
   419   void LpCplex::_setMin()
   422   void LpCplex::_setMin()
   420   {
   423   {
   421     CPXchgobjsen (env, lp, CPX_MIN);
   424     CPXchgobjsen(env, lp, CPX_MIN);
   422    }
   425    }
   423   
   426   
   424 } //namespace lemon
   427 } //namespace lemon
   425 
   428