COIN-OR::LEMON - Graph Library

source: lemon/test/mip_test.cc @ 481:7afc121e0689

Last change on this file since 481:7afc121e0689 was 481:7afc121e0689, checked in by Balazs Dezso <deba@…>, 15 years ago

Port LP and MIP solvers from SVN -r3509 (#44)

File size: 2.4 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19#include "test_tools.h"
20
21
22#ifdef HAVE_CONFIG_H
23#include <lemon/config.h>
24#endif
25
26#ifdef HAVE_CPLEX
27#include <lemon/mip_cplex.h>
28#endif
29
30#ifdef HAVE_GLPK
31#include <lemon/mip_glpk.h>
32#endif
33
34
35using namespace lemon;
36
37void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat,
38                   double exp_opt) {
39  using std::string;
40
41  lp.solve();
42  //int decimal,sign;
43  std::ostringstream buf;
44  buf << "Primalstatus should be: " << int(stat)
45      <<" and it is "<<int(lp.mipStatus());
46
47
48  //  itoa(stat,buf1, 10);
49  check(lp.mipStatus()==stat, buf.str());
50
51  if (stat ==  MipSolverBase::OPTIMAL) {
52    std::ostringstream sbuf;
53    buf << "Wrong optimal value: the right optimum is " << exp_opt;
54    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
55    //+ecvt(exp_opt,2)
56  }
57}
58
59void aTest(MipSolverBase& mip)
60{
61 //The following example is very simple
62
63
64  typedef MipSolverBase::Row Row;
65  typedef MipSolverBase::Col Col;
66
67
68
69  Col x1 = mip.addCol();
70  Col x2 = mip.addCol();
71
72
73  //Objective function
74  mip.obj(x1);
75
76  mip.max();
77
78
79  //Unconstrained optimization
80  mip.solve();
81  //Check it out!
82
83  //Constraints
84  mip.addRow(2*x1+x2 <=2);
85  mip.addRow(x1-2*x2 <=0);
86
87  //Nonnegativity of the variable x1
88  mip.colLowerBound(x1, 0);
89
90  //Maximization of x1
91  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
92  double expected_opt=4.0/5.0;
93  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
94
95  //Restrict x2 to integer
96  mip.colType(x2,MipSolverBase::INT);
97  expected_opt=1.0/2.0;
98  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
99
100
101  //Restrict both to integer
102  mip.colType(x1,MipSolverBase::INT);
103  expected_opt=0;
104  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
105
106
107
108}
109
110
111int main()
112{
113
114#ifdef HAVE_GLPK
115  MipGlpk mip1;
116  aTest(mip1);
117#endif
118
119#ifdef HAVE_CPLEX
120  MipCplex mip2;
121  aTest(mip2);
122#endif
123
124  return 0;
125
126}
Note: See TracBrowser for help on using the repository browser.