COIN-OR::LEMON - Graph Library

source: lemon-main/test/mip_test.cc @ 778:a143f19f465b

Last change on this file since 778:a143f19f465b was 631:d21b38647e53, checked in by Akos Ladanyi <ladanyi@…>, 16 years ago

Remove superfluous HAVE_CONFIG_H (#278)

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