COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/lp_cplex.cc @ 1435:8e85e6bbefdf

Last change on this file since 1435:8e85e6bbefdf was 1435:8e85e6bbefdf, checked in by Akos Ladanyi, 19 years ago

trunk/src/* move to trunk/

File size: 9.7 KB
Line 
1/* -*- C++ -*-
2 * lemon/lp_cplex.cc - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16#include <iostream>
17#include<lemon/lp_cplex.h>
18
19///\file
20///\brief Implementation of the LEMON-CPLEX lp solver interface.
21namespace lemon {
22 
23  LpCplex::LpCplex() : LpSolverBase() {
24    env = NULL;
25    lp = NULL;
26    env = CPXopenCPLEXdevelop(&status);     
27//     if (Env == NULL)
28//     {
29//          fprintf(stderr,"A CPLEX környezet megnyitása sikertelen.\n");
30//       CPXgeterrorstring(Env, Status, ErrorMsg);
31//       fprintf(stderr,"%s",ErrorMsg);
32//       goto Terminate;
33//     }
34   
35    // *** A problema létrehozása ***
36    lp = CPXcreateprob(env, &status, "LP problem");
37   
38    //    if (Problem == NULL)
39//     {
40//      fprintf(stderr,"Az LP létrehozása sikertelen");
41//      goto Terminate;
42//     }
43   
44  }
45 
46  LpCplex::~LpCplex() {
47    status = CPXfreeprob(env,&lp);
48    //       if (Status != 0)
49    //  {
50//        fprintf(stderr,"A CPLEX feladat törlése sikertelen.\n");
51//        CPXgeterrorstring(Env, Status, ErrorMsg);
52//        fprintf(stderr,"%s",ErrorMsg);
53//        goto Terminate;
54//      }
55       
56    status = CPXcloseCPLEX(&env);
57    //       if (Status != 0)
58    //  {
59    //    fprintf(stderr,"A CPLEX környezet bezárása sikertelen.\n");
60//        CPXgeterrorstring(Env, Status, ErrorMsg);
61//        fprintf(stderr,"%s",ErrorMsg);
62//        goto Terminate;
63//      }
64     
65  }
66 
67  LpSolverBase &LpCplex::_newLp()
68  {
69    return *(LpSolverBase*)0;
70  }
71  LpSolverBase &LpCplex::_copyLp() {
72    return *(LpSolverBase*)0;
73    //Ez lesz majd CPXcloneprob (env, lp, &status);
74  }
75
76  int LpCplex::_addCol()
77  {
78    int i = CPXgetnumcols (env, lp);
79    Value lb[1],ub[1];
80    lb[0]=-INF;//-CPX_INFBOUND;
81    ub[0]=INF;//CPX_INFBOUND;
82    status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
83    return i;
84  }
85 
86  int LpCplex::_addRow()
87  {
88    //We want a row that is not constrained
89    char sense[1];
90    sense[0]='L';//<= constraint
91    Value rhs[1];
92    rhs[0]=INF;
93    int i = CPXgetnumrows (env, lp);
94    status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
95    return i;
96  }
97
98
99  void LpCplex::_eraseCol(int i) {
100    ///\todo Not implemented yet
101  }
102 
103  void LpCplex::_eraseRow(int i) {
104    ///\todo Not implemented yet
105  }
106
107 
108  ///\warning Data at index 0 is ignored in the arrays.
109  void LpCplex::_setRowCoeffs(int i,
110                              int length,
111                              int  const * indices,
112                              Value  const * values )
113  {
114    int rowlist[length+1];
115    int* p=rowlist;
116    for (int k=1;k<=length;++k){
117      rowlist[k]=i;
118    }
119    status = CPXchgcoeflist(env, lp,
120                            length,
121                            p+1,
122                            const_cast<int * >(indices+1),
123                            const_cast<Value * >(values+1));
124  }
125 
126  void LpCplex::_setColCoeffs(int i,
127                              int length,
128                              int  const * indices,
129                              Value  const * values)
130  {
131    int collist[length+1];
132    int* p=collist;
133    for (int k=1;k<=length;++k){
134      collist[k]=i;
135    }
136    status = CPXchgcoeflist(env, lp,
137                            length,
138                            const_cast<int * >(indices+1),
139                            p+1,
140                            const_cast<Value * >(values+1));
141  }
142 
143  void LpCplex::_setCoeff(int row, int col, Value value)
144  {
145    CPXchgcoef (env, lp, row, col, value);
146  }
147
148  void LpCplex::_setColLowerBound(int i, Value value)
149  {
150    int indices[1];
151    indices[0]=i;
152    char lu[1];
153    lu[0]='L';
154    Value bd[1];
155    bd[0]=value;
156    status = CPXchgbds (env, lp, 1, indices, lu, bd);
157 
158  }
159 
160  void LpCplex::_setColUpperBound(int i, Value value)
161  {
162    int indices[1];
163    indices[0]=i;
164    char lu[1];
165    lu[0]='U';
166    Value bd[1];
167    bd[0]=value;
168    status = CPXchgbds (env, lp, 1, indices, lu, bd);
169  }
170
171  //This will be easier to implement
172  void LpCplex::_setRowBounds(int i, Value lb, Value ub)
173  {
174    //Bad parameter
175    if (lb==INF || ub==-INF) {
176      //FIXME error
177    }
178   
179    int cnt=1;
180    int indices[1];
181    indices[0]=i;
182    char sense[1];
183
184    if (lb==-INF){
185      sense[0]='L';
186      CPXchgsense (env, lp, cnt, indices, sense);
187      CPXchgcoef (env, lp, i, -1, ub);
188     
189    }
190    else{
191      if (ub==INF){
192        sense[0]='G';
193        CPXchgsense (env, lp, cnt, indices, sense);
194        CPXchgcoef (env, lp, i, -1, lb);
195      }
196      else{
197        if (lb == ub){
198          sense[0]='E';
199          CPXchgsense (env, lp, cnt, indices, sense);
200          CPXchgcoef (env, lp, i, -1, lb);
201        }
202        else{
203          sense[0]='R';
204          CPXchgsense (env, lp, cnt, indices, sense);
205          CPXchgcoef (env, lp, i, -1, lb);
206          CPXchgcoef (env, lp, i, -2, ub-lb);     
207        }
208      }
209    }
210  }
211
212//   void LpCplex::_setRowLowerBound(int i, Value value)
213//   {
214//     //Not implemented, obsolete
215//   }
216 
217//   void LpCplex::_setRowUpperBound(int i, Value value)
218//   {
219//     //Not implemented, obsolete
220// //     //TODO Ezt kell meg megirni
221// //     //type of the problem
222// //     char sense[1];
223// //     status = CPXgetsense (env, lp, sense, i, i);
224// //     Value rhs[1];
225// //     status = CPXgetrhs (env, lp, rhs, i, i);
226
227// //     switch (sense[0]) {
228// //     case 'L'://<= constraint
229// //       break;
230// //     case 'E'://= constraint
231// //       break;
232// //     case 'G'://>= constraint
233// //       break;
234// //     case 'R'://ranged constraint
235// //       break;
236// //     default: ;
237// //       //FIXME error
238// //     }
239
240// //     status = CPXchgcoef (env, lp, i, -2, value_rng);
241//   }
242 
243  void LpCplex::_setObjCoeff(int i, Value obj_coef)
244  {
245    CPXchgcoef (env, lp, -1, i, obj_coef);
246  }
247
248  void LpCplex::_clearObj()
249  {
250    for (int i=0;i< CPXgetnumcols (env, lp);++i){
251      CPXchgcoef (env, lp, -1, i, 0);
252    }
253   
254  }
255
256  LpCplex::SolveExitStatus LpCplex::_solve()
257  {
258   
259    status = CPXlpopt (env, lp);
260    if (status == 0){
261      return SOLVED;
262    }
263    else{
264      return UNSOLVED;
265    }
266//     int i=  lpx_simplex(lp);
267//     switch (i) {
268//     case LPX_E_OK:
269//       return SOLVED;
270//       break;
271//     default:
272//       return UNSOLVED;
273//     }
274  }
275
276  LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
277  {
278//7.5-os cplex statusai
279// #define CPX_OPTIMAL                      1
280// #define CPX_INFEASIBLE                   2
281// #define CPX_UNBOUNDED                    3
282// #define CPX_OBJ_LIM                      4
283// #define CPX_IT_LIM_FEAS                  5
284// #define CPX_IT_LIM_INFEAS                6
285// #define CPX_TIME_LIM_FEAS                7
286// #define CPX_TIME_LIM_INFEAS              8
287// #define CPX_NUM_BEST_FEAS                9
288// #define CPX_NUM_BEST_INFEAS             10
289// #define CPX_OPTIMAL_INFEAS              11
290// #define CPX_ABORT_FEAS                  12
291// #define CPX_ABORT_INFEAS                13
292// #define CPX_ABORT_DUAL_INFEAS           14
293// #define CPX_ABORT_PRIM_INFEAS           15
294// #define CPX_ABORT_PRIM_DUAL_INFEAS      16
295// #define CPX_ABORT_PRIM_DUAL_FEAS        17
296// #define CPX_ABORT_CROSSOVER             18
297// #define CPX_INForUNBD                   19
298// #define CPX_PIVOT                       20
299
300//     Ezeket hova tegyem:
301// ??case CPX_ABORT_DUAL_INFEAS           
302// ??case CPX_ABORT_CROSSOVER             
303// ??case CPX_INForUNBD                   
304// ??case CPX_PIVOT                       
305
306    int stat = CPXgetstat (env, lp);
307    switch (stat) {
308    case 0:
309      return UNDEFINED; //Undefined
310      break;     
311    case CPX_OPTIMAL://Optimal
312      return OPTIMAL;
313      break;
314    case CPX_UNBOUNDED://Unbounded
315      return INFINITE;
316      break;
317    case CPX_INFEASIBLE://Infeasible
318    case CPX_IT_LIM_INFEAS:
319    case CPX_TIME_LIM_INFEAS:
320    case CPX_NUM_BEST_INFEAS:             
321    case CPX_OPTIMAL_INFEAS:             
322    case CPX_ABORT_INFEAS:               
323    case CPX_ABORT_PRIM_INFEAS:           
324    case CPX_ABORT_PRIM_DUAL_INFEAS:     
325      return INFEASIBLE;
326      break;
327    case CPX_OBJ_LIM:                   
328    case CPX_IT_LIM_FEAS:             
329    case CPX_TIME_LIM_FEAS:               
330    case CPX_NUM_BEST_FEAS:               
331    case CPX_ABORT_FEAS:                 
332    case CPX_ABORT_PRIM_DUAL_FEAS:       
333      return FEASIBLE;
334      break;
335    default:
336      return UNDEFINED; //Everything else comes here
337      //FIXME error
338    }
339
340
341    //Nem tudom, hanyas cplex verzio statusai
342// CPX_STAT_ABORT_DUAL_OBJ_LIM
343// CPX_STAT_ABORT_IT_LIM
344// CPX_STAT_ABORT_OBJ_LIM
345// CPX_STAT_ABORT_PRIM_OBJ_LIM
346// CPX_STAT_ABORT_TIME_LIM
347// CPX_STAT_ABORT_USER
348// CPX_STAT_FEASIBLE_RELAXED
349// CPX_STAT_INFEASIBLE
350// CPX_STAT_INForUNBD
351// CPX_STAT_NUM_BEST
352// CPX_STAT_OPTIMAL
353// CPX_STAT_OPTIMAL_FACE_UNBOUNDED
354// CPX_STAT_OPTIMAL_INFEAS
355// CPX_STAT_OPTIMAL_RELAXED
356// CPX_STAT_UNBOUNDED
357
358//     int stat = CPXgetstat (env, lp);
359//     switch (stat) {
360//     case CPX_STAT_OPTIMAL://Optimal
361//       return OPTIMAL;
362//       break;
363//     case CPX_STAT_INFEASIBLE://Infeasible
364//       return INFEASIBLE;
365//       break;
366//     case CPX_STAT_UNBOUNDED://Unbounded
367//       return INFINITE;
368//       break;
369//     case CPX_STAT_NUM_BEST://Feasible
370//       return FEASIBLE;
371//       break;
372//     default:
373//       return UNDEFINED; //Everything else comes here
374//       //FIXME error
375//     }
376
377  }
378
379  LpCplex::Value LpCplex::_getPrimal(int i)
380  {
381    Value x;
382    CPXgetx (env, lp, &x, i, i);
383    return x;
384  }
385 
386  LpCplex::Value LpCplex::_getPrimalValue()
387  {
388    Value objval;
389    //method = CPXgetmethod (env, lp);
390    status = CPXgetobjval (env, lp, &objval);
391    return objval;
392  }
393 
394 
395
396
397  void LpCplex::_setMax()
398  {
399    CPXchgobjsen (env, lp, CPX_MAX);
400   }
401  void LpCplex::_setMin()
402  {
403    CPXchgobjsen (env, lp, CPX_MIN);
404   }
405 
406} //namespace lemon
407
Note: See TracBrowser for help on using the repository browser.