Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cplex.cc « lemon « lemon-1.3.1 « 3rd « quadriflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 029a3efc5006ef6eb6f88922f64cd154cac71f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2013
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#include <iostream>
#include <vector>
#include <cstring>

#include <lemon/cplex.h>

extern "C" {
#include <ilcplex/cplex.h>
}


///\file
///\brief Implementation of the LEMON-CPLEX lp solver interface.
namespace lemon {

  CplexEnv::LicenseError::LicenseError(int status) {
    if (!CPXgeterrorstring(0, status, _message)) {
      std::strcpy(_message, "Cplex unknown error");
    }
  }

  CplexEnv::CplexEnv() {
    int status;
    _cnt = new int;
    (*_cnt) = 1;
    _env = CPXopenCPLEX(&status);
    if (_env == 0) {
      delete _cnt;
      _cnt = 0;
      throw LicenseError(status);
    }
  }

  CplexEnv::CplexEnv(const CplexEnv& other) {
    _env = other._env;
    _cnt = other._cnt;
    ++(*_cnt);
  }

  CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
    _env = other._env;
    _cnt = other._cnt;
    ++(*_cnt);
    return *this;
  }

  CplexEnv::~CplexEnv() {
    --(*_cnt);
    if (*_cnt == 0) {
      delete _cnt;
      CPXcloseCPLEX(&_env);
    }
  }

  CplexBase::CplexBase() : LpBase() {
    int status;
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
    messageLevel(MESSAGE_NOTHING);
  }

  CplexBase::CplexBase(const CplexEnv& env)
    : LpBase(), _env(env) {
    int status;
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
    messageLevel(MESSAGE_NOTHING);
  }

  CplexBase::CplexBase(const CplexBase& cplex)
    : LpBase() {
    int status;
    _prob = CPXcloneprob(cplexEnv(), cplex._prob, &status);
    rows = cplex.rows;
    cols = cplex.cols;
    messageLevel(MESSAGE_NOTHING);
  }

  CplexBase::~CplexBase() {
    CPXfreeprob(cplexEnv(),&_prob);
  }

  int CplexBase::_addCol() {
    int i = CPXgetnumcols(cplexEnv(), _prob);
    double lb = -INF, ub = INF;
    CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0);
    return i;
  }


  int CplexBase::_addRow() {
    int i = CPXgetnumrows(cplexEnv(), _prob);
    const double ub = INF;
    const char s = 'L';
    CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
    return i;
  }

  int CplexBase::_addRow(Value lb, ExprIterator b,
                         ExprIterator e, Value ub) {
    int i = CPXgetnumrows(cplexEnv(), _prob);
    if (lb == -INF) {
      const char s = 'L';
      CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
    } else if (ub == INF) {
      const char s = 'G';
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
    } else if (lb == ub){
      const char s = 'E';
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
    } else {
      const char s = 'R';
      double len = ub - lb;
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0);
    }

    std::vector<int> indices;
    std::vector<int> rowlist;
    std::vector<Value> values;

    for(ExprIterator it=b; it!=e; ++it) {
      indices.push_back(it->first);
      values.push_back(it->second);
      rowlist.push_back(i);
    }

    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
                   &rowlist.front(), &indices.front(), &values.front());

    return i;
  }

  void CplexBase::_eraseCol(int i) {
    CPXdelcols(cplexEnv(), _prob, i, i);
  }

  void CplexBase::_eraseRow(int i) {
    CPXdelrows(cplexEnv(), _prob, i, i);
  }

  void CplexBase::_eraseColId(int i) {
    cols.eraseIndex(i);
    cols.shiftIndices(i);
  }
  void CplexBase::_eraseRowId(int i) {
    rows.eraseIndex(i);
    rows.shiftIndices(i);
  }

  void CplexBase::_getColName(int col, std::string &name) const {
    int size;
    CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col);
    if (size == 0) {
      name.clear();
      return;
    }

    size *= -1;
    std::vector<char> buf(size);
    char *cname;
    int tmp;
    CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size,
                  &tmp, col, col);
    name = cname;
  }

  void CplexBase::_setColName(int col, const std::string &name) {
    char *cname;
    cname = const_cast<char*>(name.c_str());
    CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname);
  }

  int CplexBase::_colByName(const std::string& name) const {
    int index;
    if (CPXgetcolindex(cplexEnv(), _prob,
                       const_cast<char*>(name.c_str()), &index) == 0) {
      return index;
    }
    return -1;
  }

  void CplexBase::_getRowName(int row, std::string &name) const {
    int size;
    CPXgetrowname(cplexEnv(), _prob, 0, 0, 0, &size, row, row);
    if (size == 0) {
      name.clear();
      return;
    }

    size *= -1;
    std::vector<char> buf(size);
    char *cname;
    int tmp;
    CPXgetrowname(cplexEnv(), _prob, &cname, &buf.front(), size,
                  &tmp, row, row);
    name = cname;
  }

  void CplexBase::_setRowName(int row, const std::string &name) {
    char *cname;
    cname = const_cast<char*>(name.c_str());
    CPXchgrowname(cplexEnv(), _prob, 1, &row, &cname);
  }

  int CplexBase::_rowByName(const std::string& name) const {
    int index;
    if (CPXgetrowindex(cplexEnv(), _prob,
                       const_cast<char*>(name.c_str()), &index) == 0) {
      return index;
    }
    return -1;
  }

  void CplexBase::_setRowCoeffs(int i, ExprIterator b,
                                      ExprIterator e)
  {
    std::vector<int> indices;
    std::vector<int> rowlist;
    std::vector<Value> values;

    for(ExprIterator it=b; it!=e; ++it) {
      indices.push_back(it->first);
      values.push_back(it->second);
      rowlist.push_back(i);
    }

    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
                   &rowlist.front(), &indices.front(), &values.front());
  }

  void CplexBase::_getRowCoeffs(int i, InsertIterator b) const {
    int tmp1, tmp2, tmp3, length;
    CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);

    length = -length;
    std::vector<int> indices(length);
    std::vector<double> values(length);

    CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2,
               &indices.front(), &values.front(),
               length, &tmp3, i, i);

    for (int i = 0; i < length; ++i) {
      *b = std::make_pair(indices[i], values[i]);
      ++b;
    }
  }

  void CplexBase::_setColCoeffs(int i, ExprIterator b, ExprIterator e) {
    std::vector<int> indices;
    std::vector<int> collist;
    std::vector<Value> values;

    for(ExprIterator it=b; it!=e; ++it) {
      indices.push_back(it->first);
      values.push_back(it->second);
      collist.push_back(i);
    }

    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
                   &indices.front(), &collist.front(), &values.front());
  }

  void CplexBase::_getColCoeffs(int i, InsertIterator b) const {

    int tmp1, tmp2, tmp3, length;
    CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);

    length = -length;
    std::vector<int> indices(length);
    std::vector<double> values(length);

    CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2,
               &indices.front(), &values.front(),
               length, &tmp3, i, i);

    for (int i = 0; i < length; ++i) {
      *b = std::make_pair(indices[i], values[i]);
      ++b;
    }

  }

  void CplexBase::_setCoeff(int row, int col, Value value) {
    CPXchgcoef(cplexEnv(), _prob, row, col, value);
  }

  CplexBase::Value CplexBase::_getCoeff(int row, int col) const {
    CplexBase::Value value;
    CPXgetcoef(cplexEnv(), _prob, row, col, &value);
    return value;
  }

  void CplexBase::_setColLowerBound(int i, Value value) {
    const char s = 'L';
    CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
  }

  CplexBase::Value CplexBase::_getColLowerBound(int i) const {
    CplexBase::Value res;
    CPXgetlb(cplexEnv(), _prob, &res, i, i);
    return res <= -CPX_INFBOUND ? -INF : res;
  }

  void CplexBase::_setColUpperBound(int i, Value value)
  {
    const char s = 'U';
    CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
  }

  CplexBase::Value CplexBase::_getColUpperBound(int i) const {
    CplexBase::Value res;
    CPXgetub(cplexEnv(), _prob, &res, i, i);
    return res >= CPX_INFBOUND ? INF : res;
  }

  CplexBase::Value CplexBase::_getRowLowerBound(int i) const {
    char s;
    CPXgetsense(cplexEnv(), _prob, &s, i, i);
    CplexBase::Value res;

    switch (s) {
    case 'G':
    case 'R':
    case 'E':
      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
      return res <= -CPX_INFBOUND ? -INF : res;
    default:
      return -INF;
    }
  }

  CplexBase::Value CplexBase::_getRowUpperBound(int i) const {
    char s;
    CPXgetsense(cplexEnv(), _prob, &s, i, i);
    CplexBase::Value res;

    switch (s) {
    case 'L':
    case 'E':
      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
      return res >= CPX_INFBOUND ? INF : res;
    case 'R':
      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
      {
        double rng;
        CPXgetrngval(cplexEnv(), _prob, &rng, i, i);
        res += rng;
      }
      return res >= CPX_INFBOUND ? INF : res;
    default:
      return INF;
    }
  }

  //This is easier to implement
  void CplexBase::_set_row_bounds(int i, Value lb, Value ub) {
    if (lb == -INF) {
      const char s = 'L';
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &ub);
    } else if (ub == INF) {
      const char s = 'G';
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
    } else if (lb == ub){
      const char s = 'E';
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
    } else {
      const char s = 'R';
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
      double len = ub - lb;
      CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
    }
  }

  void CplexBase::_setRowLowerBound(int i, Value lb)
  {
    LEMON_ASSERT(lb != INF, "Invalid bound");
    _set_row_bounds(i, lb, CplexBase::_getRowUpperBound(i));
  }

  void CplexBase::_setRowUpperBound(int i, Value ub)
  {

    LEMON_ASSERT(ub != -INF, "Invalid bound");
    _set_row_bounds(i, CplexBase::_getRowLowerBound(i), ub);
  }

  void CplexBase::_setObjCoeffs(ExprIterator b, ExprIterator e)
  {
    std::vector<int> indices;
    std::vector<Value> values;
    for(ExprIterator it=b; it!=e; ++it) {
      indices.push_back(it->first);
      values.push_back(it->second);
    }
    CPXchgobj(cplexEnv(), _prob, values.size(),
              &indices.front(), &values.front());

  }

  void CplexBase::_getObjCoeffs(InsertIterator b) const
  {
    int num = CPXgetnumcols(cplexEnv(), _prob);
    std::vector<Value> x(num);

    CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1);
    for (int i = 0; i < num; ++i) {
      if (x[i] != 0.0) {
        *b = std::make_pair(i, x[i]);
        ++b;
      }
    }
  }

  void CplexBase::_setObjCoeff(int i, Value obj_coef)
  {
    CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef);
  }

  CplexBase::Value CplexBase::_getObjCoeff(int i) const
  {
    Value x;
    CPXgetobj(cplexEnv(), _prob, &x, i, i);
    return x;
  }

  void CplexBase::_setSense(CplexBase::Sense sense) {
    switch (sense) {
    case MIN:
      CPXchgobjsen(cplexEnv(), _prob, CPX_MIN);
      break;
    case MAX:
      CPXchgobjsen(cplexEnv(), _prob, CPX_MAX);
      break;
    }
  }

  CplexBase::Sense CplexBase::_getSense() const {
    switch (CPXgetobjsen(cplexEnv(), _prob)) {
    case CPX_MIN:
      return MIN;
    case CPX_MAX:
      return MAX;
    default:
      LEMON_ASSERT(false, "Invalid sense");
      return CplexBase::Sense();
    }
  }

  void CplexBase::_clear() {
    CPXfreeprob(cplexEnv(),&_prob);
    int status;
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
  }

  void CplexBase::_messageLevel(MessageLevel level) {
    switch (level) {
    case MESSAGE_NOTHING:
      _message_enabled = false;
      break;
    case MESSAGE_ERROR:
    case MESSAGE_WARNING:
    case MESSAGE_NORMAL:
    case MESSAGE_VERBOSE:
      _message_enabled = true;
      break;
    }
  }

  void CplexBase::_applyMessageLevel() {
    CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND,
                   _message_enabled ? CPX_ON : CPX_OFF);
  }

  void CplexBase::_write(std::string file, std::string format) const
  {
    if(format == "MPS" || format == "LP")
      CPXwriteprob(cplexEnv(), cplexLp(), file.c_str(), format.c_str());
    else if(format == "SOL")
      CPXsolwrite(cplexEnv(), cplexLp(), file.c_str());
    else throw UnsupportedFormatError(format);
  }



  // CplexLp members

  CplexLp::CplexLp()
    : LpBase(), LpSolver(), CplexBase() {}

  CplexLp::CplexLp(const CplexEnv& env)
    : LpBase(), LpSolver(), CplexBase(env) {}

  CplexLp::CplexLp(const CplexLp& other)
    : LpBase(), LpSolver(), CplexBase(other) {}

  CplexLp::~CplexLp() {}

  CplexLp* CplexLp::newSolver() const { return new CplexLp; }
  CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }

  const char* CplexLp::_solverName() const { return "CplexLp"; }

  void CplexLp::_clear_temporals() {
    _col_status.clear();
    _row_status.clear();
    _primal_ray.clear();
    _dual_ray.clear();
  }

  // The routine returns zero unless an error occurred during the
  // optimization. Examples of errors include exhausting available
  // memory (CPXERR_NO_MEMORY) or encountering invalid data in the
  // CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a
  // user-specified CPLEX limit, or proving the model infeasible or
  // unbounded, are not considered errors. Note that a zero return
  // value does not necessarily mean that a solution exists. Use query
  // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
  // further information about the status of the optimization.
  CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
#if CPX_VERSION >= 800
    if (status == 0) {
      switch (CPXgetstat(cplexEnv(), _prob)) {
      case CPX_STAT_OPTIMAL:
      case CPX_STAT_INFEASIBLE:
      case CPX_STAT_UNBOUNDED:
        return SOLVED;
      default:
        return UNSOLVED;
      }
    } else {
      return UNSOLVED;
    }
#else
    if (status == 0) {
      //We want to exclude some cases
      switch (CPXgetstat(cplexEnv(), _prob)) {
      case CPX_OBJ_LIM:
      case CPX_IT_LIM_FEAS:
      case CPX_IT_LIM_INFEAS:
      case CPX_TIME_LIM_FEAS:
      case CPX_TIME_LIM_INFEAS:
        return UNSOLVED;
      default:
        return SOLVED;
      }
    } else {
      return UNSOLVED;
    }
#endif
  }

  CplexLp::SolveExitStatus CplexLp::_solve() {
    _clear_temporals();
    _applyMessageLevel();
    return convertStatus(CPXlpopt(cplexEnv(), _prob));
  }

  CplexLp::SolveExitStatus CplexLp::solvePrimal() {
    _clear_temporals();
    _applyMessageLevel();
    return convertStatus(CPXprimopt(cplexEnv(), _prob));
  }

  CplexLp::SolveExitStatus CplexLp::solveDual() {
    _clear_temporals();
    _applyMessageLevel();
    return convertStatus(CPXdualopt(cplexEnv(), _prob));
  }

  CplexLp::SolveExitStatus CplexLp::solveBarrier() {
    _clear_temporals();
    _applyMessageLevel();
    return convertStatus(CPXbaropt(cplexEnv(), _prob));
  }

  CplexLp::Value CplexLp::_getPrimal(int i) const {
    Value x;
    CPXgetx(cplexEnv(), _prob, &x, i, i);
    return x;
  }

  CplexLp::Value CplexLp::_getDual(int i) const {
    Value y;
    CPXgetpi(cplexEnv(), _prob, &y, i, i);
    return y;
  }

  CplexLp::Value CplexLp::_getPrimalValue() const {
    Value objval;
    CPXgetobjval(cplexEnv(), _prob, &objval);
    return objval;
  }

  CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
    if (_col_status.empty()) {
      _col_status.resize(CPXgetnumcols(cplexEnv(), _prob));
      CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0);
    }
    switch (_col_status[i]) {
    case CPX_BASIC:
      return BASIC;
    case CPX_FREE_SUPER:
      return FREE;
    case CPX_AT_LOWER:
      return LOWER;
    case CPX_AT_UPPER:
      return UPPER;
    default:
      LEMON_ASSERT(false, "Wrong column status");
      return CplexLp::VarStatus();
    }
  }

  CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
    if (_row_status.empty()) {
      _row_status.resize(CPXgetnumrows(cplexEnv(), _prob));
      CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front());
    }
    switch (_row_status[i]) {
    case CPX_BASIC:
      return BASIC;
    case CPX_AT_LOWER:
      {
        char s;
        CPXgetsense(cplexEnv(), _prob, &s, i, i);
        return s != 'L' ? LOWER : UPPER;
      }
    case CPX_AT_UPPER:
      return UPPER;
    default:
      LEMON_ASSERT(false, "Wrong row status");
      return CplexLp::VarStatus();
    }
  }

  CplexLp::Value CplexLp::_getPrimalRay(int i) const {
    if (_primal_ray.empty()) {
      _primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob));
      CPXgetray(cplexEnv(), _prob, &_primal_ray.front());
    }
    return _primal_ray[i];
  }

  CplexLp::Value CplexLp::_getDualRay(int i) const {
    if (_dual_ray.empty()) {

    }
    return _dual_ray[i];
  }

  // Cplex 7.0 status values
  // This table lists the statuses, returned by the CPXgetstat()
  // routine, for solutions to LP problems or mixed integer problems. If
  // no solution exists, the return value is zero.

  // For Simplex, Barrier
  // 1          CPX_OPTIMAL
  //          Optimal solution found
  // 2          CPX_INFEASIBLE
  //          Problem infeasible
  // 3    CPX_UNBOUNDED
  //          Problem unbounded
  // 4          CPX_OBJ_LIM
  //          Objective limit exceeded in Phase II
  // 5          CPX_IT_LIM_FEAS
  //          Iteration limit exceeded in Phase II
  // 6          CPX_IT_LIM_INFEAS
  //          Iteration limit exceeded in Phase I
  // 7          CPX_TIME_LIM_FEAS
  //          Time limit exceeded in Phase II
  // 8          CPX_TIME_LIM_INFEAS
  //          Time limit exceeded in Phase I
  // 9          CPX_NUM_BEST_FEAS
  //          Problem non-optimal, singularities in Phase II
  // 10         CPX_NUM_BEST_INFEAS
  //          Problem non-optimal, singularities in Phase I
  // 11         CPX_OPTIMAL_INFEAS
  //          Optimal solution found, unscaled infeasibilities
  // 12         CPX_ABORT_FEAS
  //          Aborted in Phase II
  // 13         CPX_ABORT_INFEAS
  //          Aborted in Phase I
  // 14          CPX_ABORT_DUAL_INFEAS
  //          Aborted in barrier, dual infeasible
  // 15          CPX_ABORT_PRIM_INFEAS
  //          Aborted in barrier, primal infeasible
  // 16          CPX_ABORT_PRIM_DUAL_INFEAS
  //          Aborted in barrier, primal and dual infeasible
  // 17          CPX_ABORT_PRIM_DUAL_FEAS
  //          Aborted in barrier, primal and dual feasible
  // 18          CPX_ABORT_CROSSOVER
  //          Aborted in crossover
  // 19          CPX_INForUNBD
  //          Infeasible or unbounded
  // 20   CPX_PIVOT
  //       User pivot used
  //
  // Pending return values
  // ??case CPX_ABORT_DUAL_INFEAS
  // ??case CPX_ABORT_CROSSOVER
  // ??case CPX_INForUNBD
  // ??case CPX_PIVOT

  //Some more interesting stuff:

  // CPX_PARAM_PROBMETHOD  1062  int  LPMETHOD
  // 0 Automatic
  // 1 Primal Simplex
  // 2 Dual Simplex
  // 3 Network Simplex
  // 4 Standard Barrier
  // Default: 0
  // Description: Method for linear optimization.
  // Determines which algorithm is used when CPXlpopt() (or "optimize"
  // in the Interactive Optimizer) is called. Currently the behavior of
  // the "Automatic" setting is that CPLEX simply invokes the dual
  // simplex method, but this capability may be expanded in the future
  // so that CPLEX chooses the method based on problem characteristics
#if CPX_VERSION < 900
  void statusSwitch(CPXENVptr cplexEnv(),int& stat){
    int lpmethod;
    CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod);
    if (lpmethod==2){
      if (stat==CPX_UNBOUNDED){
        stat=CPX_INFEASIBLE;
      }
      else{
        if (stat==CPX_INFEASIBLE)
          stat=CPX_UNBOUNDED;
      }
    }
  }
#else
  void statusSwitch(CPXENVptr,int&){}
#endif

  CplexLp::ProblemType CplexLp::_getPrimalType() const {
    // Unboundedness not treated well: the following is from cplex 9.0 doc
    // About Unboundedness

    // The treatment of models that are unbounded involves a few
    // subtleties. Specifically, a declaration of unboundedness means that
    // ILOG CPLEX has determined that the model has an unbounded
    // ray. Given any feasible solution x with objective z, a multiple of
    // the unbounded ray can be added to x to give a feasible solution
    // with objective z-1 (or z+1 for maximization models). Thus, if a
    // feasible solution exists, then the optimal objective is
    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
    // a feasible solution exists. Users can call the routine CPXsolninfo
    // to determine whether ILOG CPLEX has also concluded that the model
    // has a feasible solution.

    int stat = CPXgetstat(cplexEnv(), _prob);
#if CPX_VERSION >= 800
    switch (stat)
      {
      case CPX_STAT_OPTIMAL:
        return OPTIMAL;
      case CPX_STAT_UNBOUNDED:
        return UNBOUNDED;
      case CPX_STAT_INFEASIBLE:
        return INFEASIBLE;
      default:
        return UNDEFINED;
      }
#else
    statusSwitch(cplexEnv(),stat);
    //CPXgetstat(cplexEnv(), _prob);
    switch (stat) {
    case 0:
      return UNDEFINED; //Undefined
    case CPX_OPTIMAL://Optimal
      return OPTIMAL;
    case CPX_UNBOUNDED://Unbounded
      return INFEASIBLE;//In case of dual simplex
      //return UNBOUNDED;
    case CPX_INFEASIBLE://Infeasible
      //    case CPX_IT_LIM_INFEAS:
      //     case CPX_TIME_LIM_INFEAS:
      //     case CPX_NUM_BEST_INFEAS:
      //     case CPX_OPTIMAL_INFEAS:
      //     case CPX_ABORT_INFEAS:
      //     case CPX_ABORT_PRIM_INFEAS:
      //     case CPX_ABORT_PRIM_DUAL_INFEAS:
      return UNBOUNDED;//In case of dual simplex
      //return INFEASIBLE;
      //     case CPX_OBJ_LIM:
      //     case CPX_IT_LIM_FEAS:
      //     case CPX_TIME_LIM_FEAS:
      //     case CPX_NUM_BEST_FEAS:
      //     case CPX_ABORT_FEAS:
      //     case CPX_ABORT_PRIM_DUAL_FEAS:
      //       return FEASIBLE;
    default:
      return UNDEFINED; //Everything else comes here
      //FIXME error
    }
#endif
  }

  // Cplex 9.0 status values
  // CPX_STAT_ABORT_DUAL_OBJ_LIM
  // CPX_STAT_ABORT_IT_LIM
  // CPX_STAT_ABORT_OBJ_LIM
  // CPX_STAT_ABORT_PRIM_OBJ_LIM
  // CPX_STAT_ABORT_TIME_LIM
  // CPX_STAT_ABORT_USER
  // CPX_STAT_FEASIBLE_RELAXED
  // CPX_STAT_INFEASIBLE
  // CPX_STAT_INForUNBD
  // CPX_STAT_NUM_BEST
  // CPX_STAT_OPTIMAL
  // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
  // CPX_STAT_OPTIMAL_INFEAS
  // CPX_STAT_OPTIMAL_RELAXED
  // CPX_STAT_UNBOUNDED

  CplexLp::ProblemType CplexLp::_getDualType() const {
    int stat = CPXgetstat(cplexEnv(), _prob);
#if CPX_VERSION >= 800
    switch (stat) {
    case CPX_STAT_OPTIMAL:
      return OPTIMAL;
    case CPX_STAT_UNBOUNDED:
      return INFEASIBLE;
    default:
      return UNDEFINED;
    }
#else
    statusSwitch(cplexEnv(),stat);
    switch (stat) {
    case 0:
      return UNDEFINED; //Undefined
    case CPX_OPTIMAL://Optimal
      return OPTIMAL;
    case CPX_UNBOUNDED:
      return INFEASIBLE;
    default:
      return UNDEFINED; //Everything else comes here
      //FIXME error
    }
#endif
  }

  // CplexMip members

  CplexMip::CplexMip()
    : LpBase(), MipSolver(), CplexBase() {

#if CPX_VERSION < 800
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
#else
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
#endif
  }

  CplexMip::CplexMip(const CplexEnv& env)
    : LpBase(), MipSolver(), CplexBase(env) {

#if CPX_VERSION < 800
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
#else
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
#endif

  }

  CplexMip::CplexMip(const CplexMip& other)
    : LpBase(), MipSolver(), CplexBase(other) {}

  CplexMip::~CplexMip() {}

  CplexMip* CplexMip::newSolver() const { return new CplexMip; }
  CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }

  const char* CplexMip::_solverName() const { return "CplexMip"; }

  void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {

    // Note If a variable is to be changed to binary, a call to CPXchgbds
    // should also be made to change the bounds to 0 and 1.

    switch (col_type){
    case INTEGER: {
      const char t = 'I';
      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
    } break;
    case REAL: {
      const char t = 'C';
      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
    } break;
    default:
      break;
    }
  }

  CplexMip::ColTypes CplexMip::_getColType(int i) const {
    char t;
    CPXgetctype (cplexEnv(), _prob, &t, i, i);
    switch (t) {
    case 'I':
      return INTEGER;
    case 'C':
      return REAL;
    default:
      LEMON_ASSERT(false, "Invalid column type");
      return ColTypes();
    }

  }

  CplexMip::SolveExitStatus CplexMip::_solve() {
    int status;
    _applyMessageLevel();
    status = CPXmipopt (cplexEnv(), _prob);
    if (status==0)
      return SOLVED;
    else
      return UNSOLVED;

  }


  CplexMip::ProblemType CplexMip::_getType() const {

    int stat = CPXgetstat(cplexEnv(), _prob);

    //Fortunately, MIP statuses did not change for cplex 8.0
    switch (stat) {
    case CPXMIP_OPTIMAL:
      // Optimal integer solution has been found.
    case CPXMIP_OPTIMAL_TOL:
      // Optimal soluton with the tolerance defined by epgap or epagap has
      // been found.
      return OPTIMAL;
      //This also exists in later issues
      //    case CPXMIP_UNBOUNDED:
      //return UNBOUNDED;
      case CPXMIP_INFEASIBLE:
        return INFEASIBLE;
    default:
      return UNDEFINED;
    }
    //Unboundedness not treated well: the following is from cplex 9.0 doc
    // About Unboundedness

    // The treatment of models that are unbounded involves a few
    // subtleties. Specifically, a declaration of unboundedness means that
    // ILOG CPLEX has determined that the model has an unbounded
    // ray. Given any feasible solution x with objective z, a multiple of
    // the unbounded ray can be added to x to give a feasible solution
    // with objective z-1 (or z+1 for maximization models). Thus, if a
    // feasible solution exists, then the optimal objective is
    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
    // a feasible solution exists. Users can call the routine CPXsolninfo
    // to determine whether ILOG CPLEX has also concluded that the model
    // has a feasible solution.
  }

  CplexMip::Value CplexMip::_getSol(int i) const {
    Value x;
    CPXgetmipx(cplexEnv(), _prob, &x, i, i);
    return x;
  }

  CplexMip::Value CplexMip::_getSolValue() const {
    Value objval;
    CPXgetmipobjval(cplexEnv(), _prob, &objval);
    return objval;
  }

} //namespace lemon