COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/mip_test.cc @ 2218:50f1a780a5ff

Last change on this file since 2218:50f1a780a5ff was 2218:50f1a780a5ff, checked in by athos, 13 years ago

Interface to the cplex MIP solver: it is little, a bit sour but it is ours.

File size: 1.8 KB
Line 
1#include "test_tools.h"
2
3
4#include <lemon/mip_cplex.h>
5#include <lemon/mip_glpk.h>
6#include<lemon/config.h>
7
8using namespace lemon;
9
10void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat,
11                   double exp_opt) {
12  using std::string;
13
14  lp.solve();
15  //int decimal,sign;
16  std::ostringstream buf;
17  buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
18
19
20  //  itoa(stat,buf1, 10);
21  check(lp.mipStatus()==stat, buf.str());
22
23  if (stat ==  MipSolverBase::OPTIMAL) {
24    std::ostringstream buf;
25    buf << "Wrong optimal value: the right optimum is " << exp_opt;
26    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
27    //+ecvt(exp_opt,2)
28  }
29}
30
31void aTest(MipSolverBase& mip)
32{
33 //The following example is very simple
34
35
36  typedef MipSolverBase::Row Row;
37  typedef MipSolverBase::Col Col;
38
39
40
41  Col x1 = mip.addCol();
42  Col x2 = mip.addCol();
43
44
45  //Objective function
46  mip.setObj(x1);
47
48  mip.max();
49
50
51  //Unconstrained optimization
52  mip.solve();
53  //Check it out!
54
55  //Constraints
56  mip.addRow(2*x1+x2 <=2); 
57  mip.addRow(x1-2*x2 <=0); 
58
59  //Nonnegativity of the variable x1
60  mip.colLowerBound(x1, 0);
61
62
63
64  //Maximization of x1
65  //over the triangle with vertices
66  double expected_opt=4.0/5.0;
67  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
68
69  //Restrict x2 to integer
70  mip.colType(x2,MipSolverBase::LEMON_INTEGER); 
71  expected_opt=1.0/2.0;
72  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
73
74
75  //Restrict both to integer
76  mip.colType(x1,MipSolverBase::LEMON_INTEGER); 
77  expected_opt=0;
78  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
79
80 
81
82}
83
84
85int main()
86{
87
88#ifdef HAVE_GLPK
89  MipGlpk mip1;
90  aTest(mip1);
91#endif
92
93
94
95#ifdef HAVE_CPLEX
96  //std::cout<<ATTILA<<INTEGER;
97  MipCplex mip2;
98  aTest(mip2);
99#endif
100
101  return 0;
102
103}
Note: See TracBrowser for help on using the repository browser.