COIN-OR::LEMON - Graph Library

source: lemon-main/test/mip_test.cc @ 588:58f70400e139

Last change on this file since 588:58f70400e139 was 575:3cbddc293cf9, checked in by Balazs Dezso <deba@…>, 16 years ago

Force cplex checking (#260)

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