test/mip_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 17 Apr 2009 09:54:14 +0200
changeset 640 7ac52d6a268e
parent 614 3314f58e7b25
child 674 20dac2104519
permissions -rw-r--r--
Extend and modify the interface of matching algorithms (#265)

- Rename decomposition() to status() in MaxMatching.
- Add a new query function statusMap() to MaxMatching.
- Add a new query function matchingMap() to all the three classes.
- Rename matchingValue() to matchingWeight() in the weighted
matching classes.
deba@481
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@481
     2
 *
deba@481
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@481
     4
 *
deba@598
     5
 * Copyright (C) 2003-2009
deba@481
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@481
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@481
     8
 *
deba@481
     9
 * Permission to use, modify and distribute this software is granted
deba@481
    10
 * provided that this copyright notice appears in all copies. For
deba@481
    11
 * precise terms see the accompanying LICENSE file.
deba@481
    12
 *
deba@481
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@481
    14
 * express or implied, and with no claim as to its suitability for any
deba@481
    15
 * purpose.
deba@481
    16
 *
deba@481
    17
 */
deba@481
    18
deba@481
    19
#include "test_tools.h"
deba@481
    20
deba@481
    21
#ifdef HAVE_CONFIG_H
deba@481
    22
#include <lemon/config.h>
deba@481
    23
#endif
deba@481
    24
deba@481
    25
#ifdef HAVE_CPLEX
alpar@484
    26
#include <lemon/cplex.h>
deba@481
    27
#endif
deba@481
    28
deba@481
    29
#ifdef HAVE_GLPK
alpar@484
    30
#include <lemon/glpk.h>
deba@481
    31
#endif
deba@481
    32
deba@614
    33
#ifdef HAVE_CBC
deba@614
    34
#include <lemon/cbc.h>
deba@614
    35
#endif
deba@614
    36
deba@481
    37
deba@481
    38
using namespace lemon;
deba@481
    39
deba@482
    40
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
deba@481
    41
                   double exp_opt) {
deba@481
    42
  using std::string;
deba@481
    43
deba@482
    44
  mip.solve();
deba@481
    45
  //int decimal,sign;
deba@481
    46
  std::ostringstream buf;
deba@482
    47
  buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
deba@481
    48
deba@481
    49
deba@481
    50
  //  itoa(stat,buf1, 10);
deba@482
    51
  check(mip.type()==stat, buf.str());
deba@481
    52
deba@482
    53
  if (stat ==  MipSolver::OPTIMAL) {
deba@481
    54
    std::ostringstream sbuf;
deba@481
    55
    buf << "Wrong optimal value: the right optimum is " << exp_opt;
deba@482
    56
    check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
deba@481
    57
    //+ecvt(exp_opt,2)
deba@481
    58
  }
deba@481
    59
}
deba@481
    60
deba@482
    61
void aTest(MipSolver& mip)
deba@481
    62
{
deba@614
    63
  //The following example is very simple
deba@481
    64
deba@481
    65
deba@482
    66
  typedef MipSolver::Row Row;
deba@482
    67
  typedef MipSolver::Col Col;
deba@481
    68
deba@481
    69
deba@481
    70
  Col x1 = mip.addCol();
deba@481
    71
  Col x2 = mip.addCol();
deba@481
    72
deba@481
    73
deba@481
    74
  //Objective function
deba@481
    75
  mip.obj(x1);
deba@481
    76
deba@481
    77
  mip.max();
deba@481
    78
deba@481
    79
  //Unconstrained optimization
deba@481
    80
  mip.solve();
deba@481
    81
  //Check it out!
deba@481
    82
deba@481
    83
  //Constraints
deba@614
    84
  mip.addRow(2 * x1 + x2 <= 2);
deba@614
    85
  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
deba@481
    86
deba@481
    87
  //Nonnegativity of the variable x1
deba@481
    88
  mip.colLowerBound(x1, 0);
deba@481
    89
deba@614
    90
deba@481
    91
  //Maximization of x1
deba@481
    92
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
deba@481
    93
  double expected_opt=4.0/5.0;
deba@482
    94
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@481
    95
deba@614
    96
deba@481
    97
  //Restrict x2 to integer
deba@482
    98
  mip.colType(x2,MipSolver::INTEGER);
deba@481
    99
  expected_opt=1.0/2.0;
deba@482
   100
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@481
   101
deba@481
   102
deba@481
   103
  //Restrict both to integer
deba@482
   104
  mip.colType(x1,MipSolver::INTEGER);
deba@481
   105
  expected_opt=0;
deba@482
   106
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@481
   107
deba@614
   108
  //Erase a variable
deba@614
   109
  mip.erase(x2);
deba@614
   110
  mip.rowUpperBound(y2, 8);
deba@614
   111
  expected_opt=1;
deba@614
   112
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@481
   113
deba@481
   114
}
deba@481
   115
deba@614
   116
alpar@587
   117
template<class MIP>
alpar@587
   118
void cloneTest()
alpar@587
   119
{
deba@598
   120
alpar@587
   121
  MIP* mip = new MIP();
alpar@587
   122
  MIP* mipnew = mip->newSolver();
alpar@587
   123
  MIP* mipclone = mip->cloneSolver();
alpar@587
   124
  delete mip;
alpar@587
   125
  delete mipnew;
alpar@587
   126
  delete mipclone;
alpar@587
   127
}
deba@481
   128
deba@481
   129
int main()
deba@481
   130
{
deba@481
   131
deba@481
   132
#ifdef HAVE_GLPK
deba@482
   133
  {
alpar@485
   134
    GlpkMip mip1;
deba@482
   135
    aTest(mip1);
alpar@587
   136
    cloneTest<GlpkMip>();
deba@482
   137
  }
deba@481
   138
#endif
deba@481
   139
deba@481
   140
#ifdef HAVE_CPLEX
deba@482
   141
  try {
alpar@485
   142
    CplexMip mip2;
deba@482
   143
    aTest(mip2);
deba@598
   144
    cloneTest<CplexMip>();
deba@482
   145
  } catch (CplexEnv::LicenseError& error) {
deba@482
   146
    check(false, error.what());
deba@482
   147
  }
deba@481
   148
#endif
deba@481
   149
deba@614
   150
#ifdef HAVE_CBC
deba@614
   151
  {
deba@614
   152
    CbcMip mip1;
deba@614
   153
    aTest(mip1);
deba@614
   154
    cloneTest<CbcMip>();
deba@614
   155
  }
deba@614
   156
#endif
deba@614
   157
deba@481
   158
  return 0;
deba@481
   159
deba@481
   160
}