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