COIN-OR::LEMON - Graph Library

source: lemon/test/mip_test.cc @ 735:1f08e846df29

Last change on this file since 735:1f08e846df29 was 678:d21b38647e53, checked in by Akos Ladanyi <ladanyi@…>, 15 years ago

Remove superfluous HAVE_CONFIG_H (#278)

File size: 3.0 KB
RevLine 
[481]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
[598]5 * Copyright (C) 2003-2009
[481]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#include <lemon/config.h>
22
[674]23#ifdef LEMON_HAVE_CPLEX
[484]24#include <lemon/cplex.h>
[481]25#endif
26
[674]27#ifdef LEMON_HAVE_GLPK
[484]28#include <lemon/glpk.h>
[481]29#endif
30
[674]31#ifdef LEMON_HAVE_CBC
[614]32#include <lemon/cbc.h>
33#endif
34
[481]35
36using namespace lemon;
37
[482]38void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
[481]39                   double exp_opt) {
40  using std::string;
41
[482]42  mip.solve();
[481]43  //int decimal,sign;
44  std::ostringstream buf;
[482]45  buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
[481]46
47
48  //  itoa(stat,buf1, 10);
[482]49  check(mip.type()==stat, buf.str());
[481]50
[482]51  if (stat ==  MipSolver::OPTIMAL) {
[481]52    std::ostringstream sbuf;
53    buf << "Wrong optimal value: the right optimum is " << exp_opt;
[482]54    check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
[481]55    //+ecvt(exp_opt,2)
56  }
57}
58
[482]59void aTest(MipSolver& mip)
[481]60{
[614]61  //The following example is very simple
[481]62
63
[482]64  typedef MipSolver::Row Row;
65  typedef MipSolver::Col Col;
[481]66
67
68  Col x1 = mip.addCol();
69  Col x2 = mip.addCol();
70
71
72  //Objective function
73  mip.obj(x1);
74
75  mip.max();
76
77  //Unconstrained optimization
78  mip.solve();
79  //Check it out!
80
81  //Constraints
[614]82  mip.addRow(2 * x1 + x2 <= 2);
83  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
[481]84
85  //Nonnegativity of the variable x1
86  mip.colLowerBound(x1, 0);
87
[614]88
[481]89  //Maximization of x1
90  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
91  double expected_opt=4.0/5.0;
[482]92  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
[481]93
[614]94
[481]95  //Restrict x2 to integer
[482]96  mip.colType(x2,MipSolver::INTEGER);
[481]97  expected_opt=1.0/2.0;
[482]98  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
[481]99
100
101  //Restrict both to integer
[482]102  mip.colType(x1,MipSolver::INTEGER);
[481]103  expected_opt=0;
[482]104  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
[481]105
[614]106  //Erase a variable
107  mip.erase(x2);
108  mip.rowUpperBound(y2, 8);
109  expected_opt=1;
110  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
[481]111
112}
113
[614]114
[587]115template<class MIP>
116void cloneTest()
117{
[598]118
[587]119  MIP* mip = new MIP();
120  MIP* mipnew = mip->newSolver();
121  MIP* mipclone = mip->cloneSolver();
122  delete mip;
123  delete mipnew;
124  delete mipclone;
125}
[481]126
127int main()
128{
129
[674]130#ifdef LEMON_HAVE_GLPK
[482]131  {
[485]132    GlpkMip mip1;
[482]133    aTest(mip1);
[587]134    cloneTest<GlpkMip>();
[482]135  }
[481]136#endif
137
[674]138#ifdef LEMON_HAVE_CPLEX
[482]139  try {
[485]140    CplexMip mip2;
[482]141    aTest(mip2);
[598]142    cloneTest<CplexMip>();
[482]143  } catch (CplexEnv::LicenseError& error) {
144    check(false, error.what());
145  }
[481]146#endif
147
[674]148#ifdef LEMON_HAVE_CBC
[614]149  {
150    CbcMip mip1;
151    aTest(mip1);
152    cloneTest<CbcMip>();
153  }
154#endif
155
[481]156  return 0;
157
158}
Note: See TracBrowser for help on using the repository browser.