athos@1299: /* -*- C++ -*-
athos@1299:  * src/lemon/lp_cplex.cc
athos@1299:  * - Part of LEMON, a generic C++ optimization library
athos@1299:  *
athos@1299:  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
athos@1299:  * (Egervary Combinatorial Optimization Research Group, EGRES).
athos@1299:  *
athos@1299:  * Permission to use, modify and distribute this software is granted
athos@1299:  * provided that this copyright notice appears in all copies. For
athos@1299:  * precise terms see the accompanying LICENSE file.
athos@1299:  *
athos@1299:  * This software is provided "AS IS" with no warranty of any kind,
athos@1299:  * express or implied, and with no claim as to its suitability for any
athos@1299:  * purpose.
athos@1299:  *
athos@1299:  */
athos@1299: 
athos@1299: #include"lp_cplex.h"
athos@1299: 
athos@1299: ///\file
athos@1299: ///\brief Implementation of the LEMON-CPLEX lp solver interface.
athos@1299: namespace lemon {
athos@1299:   
athos@1299:   int LpCplex::_addCol()
athos@1299:   {
athos@1299:     int i = CPXgetnumcols (env, lp);
athos@1319:     Value lb[1],ub[1];
athos@1299:     lb[0]=-INF;//-CPX_INFBOUND;
athos@1299:     ub[0]=INF;//CPX_INFBOUND;
athos@1299:     status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
athos@1299:     return i;
athos@1299:   }
athos@1299:   
athos@1299:   int LpCplex::_addRow() 
athos@1299:   {
athos@1319:     //We want a ranged row
athos@1319:     char sense[1];
athos@1319:     sense[0]='R';
athos@1319: 
athos@1299:     int i = CPXgetnumrows (env, lp);
athos@1319:     status = CPXnewrows (env, lp, 1, NULL, sense, NULL, NULL);
athos@1299:     return i;
athos@1299:   }
athos@1299:   
athos@1299:   ///\warning Data at index 0 is ignored iin the arrays.
athos@1299:   void LpCplex::_setRowCoeffs(int i, 
athos@1299: 			      int length,
athos@1299: 			      int  const * indices, 
athos@1299: 			      Value  const * values )
athos@1299:   {
athos@1299:     int rowlist[length+1];
athos@1319:     int* p=rowlist;
athos@1299:     for (int k=1;k<=length;++k){
athos@1299:       rowlist[k]=i;
athos@1299:     }
athos@1299:     status = CPXchgcoeflist(env, lp, 
athos@1299: 			    length, 
athos@1319: 			    p++, 
athos@1319: 			    const_cast<int * >(indices++), 
athos@1319: 			    const_cast<Value * >(values++));
athos@1299:   }
athos@1299:   
athos@1299:   void LpCplex::_setColCoeffs(int i, 
athos@1299: 			      int length,
athos@1299: 			      int  const * indices, 
athos@1299: 			      Value  const * values)
athos@1299:   {
athos@1299:     int collist[length+1];
athos@1319:     int* p=collist;
athos@1299:     for (int k=1;k<=length;++k){
athos@1299:       collist[k]=i;
athos@1299:     }
athos@1299:     status = CPXchgcoeflist(env, lp, 
athos@1299: 			    length, 
athos@1319: 			    const_cast<int * >(indices++), 
athos@1319: 			    p++, 
athos@1319: 			    const_cast<Value * >(values++));
athos@1299:   }
athos@1299:   
athos@1299:   void LpCplex::_setColLowerBound(int i, Value value)
athos@1299:   {
athos@1319:     int indices[1];
athos@1319:     indices[0]=i;
athos@1319:     char lu[1];
athos@1319:     lu[0]='L';
athos@1319:     Value bd[1];
athos@1319:     bd[0]=value;
athos@1319:     status = CPXchgbds (env, lp, 1, indices, lu, bd);
athos@1319:  
athos@1299:   }
athos@1299:   
athos@1299:   void LpCplex::_setColUpperBound(int i, Value value)
athos@1299:   {
athos@1319:     int indices[1];
athos@1319:     indices[0]=i;
athos@1319:     char lu[1];
athos@1319:     lu[0]='U';
athos@1319:     Value bd[1];
athos@1319:     bd[0]=value;
athos@1319:     status = CPXchgbds (env, lp, 1, indices, lu, bd);
athos@1299:   }
athos@1299:   
athos@1299:   void LpCplex::_setRowLowerBound(int i, Value value)
athos@1299:   {
athos@1319:     status = CPXchgcoef (env, lp, i, -1, value);
athos@1319: 
athos@1299:   }
athos@1299:   
athos@1299:   void LpCplex::_setRowUpperBound(int i, Value value)
athos@1299:   {
athos@1319:     //TODO Ezt kell meg megirni
athos@1339:     //type of the problem
athos@1339:     char sense[1];
athos@1339:     status = CPXgetsense (env, lp, sense, i, i);
athos@1339:     Value rhs[1];
athos@1339:     status = CPXgetrhs (env, lp, rhs, i, i);
athos@1339: 
athos@1339:     switch (sense[0]) {
athos@1339:     case 'L'://<= constraint
athos@1339:       break;
athos@1339:     case 'E'://= constraint
athos@1339:       break;
athos@1339:     case 'G'://>= constraint
athos@1339:       break;
athos@1339:     case 'R'://ranged constraint
athos@1339:       break;
athos@1339:     default: ;
athos@1339:       //FIXME error
athos@1339:     }
athos@1339: 
athos@1339:     status = CPXchgcoef (env, lp, i, -2, value_rng);
athos@1299:   }
athos@1299:   
athos@1299:   void LpCplex::_setObjCoeff(int i, Value obj_coef)
athos@1299:   {
athos@1319:     status = CPXchgcoef (env, lp, -1, i, obj_coef);
athos@1319:    }
athos@1319: 
athos@1319:   LpCplex::SolveExitStatus LpCplex::_solve()
athos@1319:   {
athos@1319:     return SOLVED;
athos@1319: //     int i=  lpx_simplex(lp);
athos@1319: //     switch (i) {
athos@1319: //     case LPX_E_OK: 
athos@1319: //       return SOLVED;
athos@1319: //       break;
athos@1319: //     default:
athos@1319: //       return UNSOLVED;
athos@1319: //     }
athos@1299:   }
athos@1299: 
athos@1319:   LpCplex::Value LpCplex::_getPrimal(int i)
athos@1299:   {
athos@1299:     return 0;
athos@1299:   }
athos@1299:   
athos@1319:   LpCplex::Value LpCplex::_getPrimalValue()
athos@1319:   {
athos@1319:     return 0;
athos@1319:   }
athos@1319:   
athos@1319:  
athos@1319:   LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
athos@1319:   {
athos@1319:     return OPTIMAL;
athos@1319: //     int stat=  lpx_get_status(lp);
athos@1319: //     switch (stat) {
athos@1319: //     case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1319: //       return UNDEFINED;
athos@1319: //       break;
athos@1319: //     case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1319: //     case LPX_INFEAS://Infeasible 
athos@1319: //       return INFEASIBLE;
athos@1319: //       break;
athos@1319: //     case LPX_UNBND://Unbounded
athos@1319: //       return INFINITE;
athos@1319: //       break;
athos@1319: //     case LPX_FEAS://Feasible
athos@1319: //       return FEASIBLE;
athos@1319: //       break;
athos@1319: //     case LPX_OPT://Feasible
athos@1319: //       return OPTIMAL;
athos@1319: //       break;
athos@1319: //     default:
athos@1319: //       return UNDEFINED; //to avoid gcc warning
athos@1319: //       //FIXME error
athos@1319: //     }
athos@1319:   }
athos@1319: 
athos@1319: 
athos@1319:   void LpCplex::_setMax()
athos@1319:   {
athos@1319:     CPXchgobjsen (env, lp, CPX_MAX);
athos@1319:    }
athos@1319:   void LpCplex::_setMin()
athos@1319:   {
athos@1319:     CPXchgobjsen (env, lp, CPX_MIN);
athos@1319:    }
athos@1319:   
athos@1299: } //namespace lemon
athos@1299: