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