COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/mip_test.cc @ 2213:2c094dfa176d

Last change on this file since 2213:2c094dfa176d was 2213:2c094dfa176d, checked in by athos, 18 years ago

Some corrections.

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