test/mip_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 20 Jan 2021 16:17:21 +0100
changeset 1205 57abff252556
parent 1105 62dba6c90f35
permissions -rw-r--r--
Bugfixes in CplexBase and ClpLp (#639)
deba@458
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@458
     2
 *
deba@458
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@458
     4
 *
deba@551
     5
 * Copyright (C) 2003-2009
deba@458
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@458
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@458
     8
 *
deba@458
     9
 * Permission to use, modify and distribute this software is granted
deba@458
    10
 * provided that this copyright notice appears in all copies. For
deba@458
    11
 * precise terms see the accompanying LICENSE file.
deba@458
    12
 *
deba@458
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@458
    14
 * express or implied, and with no claim as to its suitability for any
deba@458
    15
 * purpose.
deba@458
    16
 *
deba@458
    17
 */
deba@458
    18
deba@458
    19
#include "test_tools.h"
deba@458
    20
deba@458
    21
#include <lemon/config.h>
deba@458
    22
ladanyi@627
    23
#ifdef LEMON_HAVE_CPLEX
alpar@461
    24
#include <lemon/cplex.h>
deba@458
    25
#endif
deba@458
    26
ladanyi@627
    27
#ifdef LEMON_HAVE_GLPK
alpar@461
    28
#include <lemon/glpk.h>
deba@458
    29
#endif
deba@458
    30
ladanyi@627
    31
#ifdef LEMON_HAVE_CBC
deba@567
    32
#include <lemon/cbc.h>
deba@567
    33
#endif
deba@567
    34
alpar@1105
    35
#ifdef LEMON_HAVE_MIP
alpar@1105
    36
#include <lemon/lp.h>
alpar@1105
    37
#endif
alpar@1105
    38
deba@458
    39
deba@458
    40
using namespace lemon;
deba@458
    41
deba@459
    42
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
deba@458
    43
                   double exp_opt) {
deba@458
    44
  using std::string;
deba@458
    45
deba@459
    46
  mip.solve();
deba@458
    47
  //int decimal,sign;
deba@458
    48
  std::ostringstream buf;
deba@459
    49
  buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
deba@458
    50
deba@458
    51
deba@458
    52
  //  itoa(stat,buf1, 10);
deba@459
    53
  check(mip.type()==stat, buf.str());
deba@458
    54
deba@459
    55
  if (stat ==  MipSolver::OPTIMAL) {
deba@458
    56
    std::ostringstream sbuf;
alpar@748
    57
    sbuf << "Wrong optimal value ("<< mip.solValue()
alpar@748
    58
         <<" instead of " << exp_opt << ")";
deba@459
    59
    check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
deba@458
    60
    //+ecvt(exp_opt,2)
deba@458
    61
  }
deba@458
    62
}
deba@458
    63
alpar@1205
    64
void aTest(MipSolver& mip, bool solve_empty=true)
deba@458
    65
{
deba@567
    66
  //The following example is very simple
deba@458
    67
deba@458
    68
deba@459
    69
  typedef MipSolver::Row Row;
deba@459
    70
  typedef MipSolver::Col Col;
deba@458
    71
deba@458
    72
deba@458
    73
  Col x1 = mip.addCol();
deba@458
    74
  Col x2 = mip.addCol();
deba@458
    75
deba@458
    76
deba@458
    77
  //Objective function
deba@458
    78
  mip.obj(x1);
deba@458
    79
deba@458
    80
  mip.max();
deba@458
    81
deba@458
    82
  //Unconstrained optimization
alpar@1205
    83
  if(solve_empty)
alpar@1205
    84
    mip.solve();
deba@458
    85
  //Check it out!
deba@458
    86
deba@458
    87
  //Constraints
deba@567
    88
  mip.addRow(2 * x1 + x2 <= 2);
deba@567
    89
  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
deba@458
    90
deba@458
    91
  //Nonnegativity of the variable x1
deba@458
    92
  mip.colLowerBound(x1, 0);
deba@458
    93
deba@567
    94
deba@458
    95
  //Maximization of x1
deba@458
    96
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
deba@458
    97
  double expected_opt=4.0/5.0;
deba@459
    98
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@458
    99
deba@567
   100
deba@458
   101
  //Restrict x2 to integer
deba@459
   102
  mip.colType(x2,MipSolver::INTEGER);
deba@458
   103
  expected_opt=1.0/2.0;
deba@459
   104
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@458
   105
deba@458
   106
deba@458
   107
  //Restrict both to integer
deba@459
   108
  mip.colType(x1,MipSolver::INTEGER);
deba@458
   109
  expected_opt=0;
deba@459
   110
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@458
   111
deba@567
   112
  //Erase a variable
deba@567
   113
  mip.erase(x2);
deba@567
   114
  mip.rowUpperBound(y2, 8);
deba@567
   115
  expected_opt=1;
deba@567
   116
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
deba@458
   117
deba@458
   118
}
deba@458
   119
deba@567
   120
alpar@540
   121
template<class MIP>
alpar@540
   122
void cloneTest()
alpar@540
   123
{
deba@551
   124
alpar@540
   125
  MIP* mip = new MIP();
alpar@540
   126
  MIP* mipnew = mip->newSolver();
alpar@540
   127
  MIP* mipclone = mip->cloneSolver();
alpar@540
   128
  delete mip;
alpar@540
   129
  delete mipnew;
alpar@540
   130
  delete mipclone;
alpar@540
   131
}
deba@458
   132
deba@458
   133
int main()
deba@458
   134
{
deba@458
   135
alpar@1105
   136
#ifdef LEMON_HAVE_MIP
alpar@1105
   137
  {
alpar@1105
   138
    Mip mip1;
alpar@1205
   139
#if LEMON_DEFAULT_MIP==LEMON_CBC_
alpar@1205
   140
    aTest(mip1, false);
alpar@1205
   141
#else
alpar@1105
   142
    aTest(mip1);
alpar@1205
   143
#endif
alpar@1105
   144
    cloneTest<Mip>();
alpar@1105
   145
  }
alpar@1105
   146
#endif
alpar@1105
   147
ladanyi@627
   148
#ifdef LEMON_HAVE_GLPK
deba@459
   149
  {
alpar@462
   150
    GlpkMip mip1;
deba@459
   151
    aTest(mip1);
alpar@540
   152
    cloneTest<GlpkMip>();
deba@459
   153
  }
deba@458
   154
#endif
deba@458
   155
ladanyi@627
   156
#ifdef LEMON_HAVE_CPLEX
deba@459
   157
  try {
alpar@462
   158
    CplexMip mip2;
deba@459
   159
    aTest(mip2);
deba@551
   160
    cloneTest<CplexMip>();
deba@459
   161
  } catch (CplexEnv::LicenseError& error) {
deba@459
   162
    check(false, error.what());
deba@459
   163
  }
deba@458
   164
#endif
deba@458
   165
ladanyi@627
   166
#ifdef LEMON_HAVE_CBC
deba@567
   167
  {
deba@567
   168
    CbcMip mip1;
alpar@1205
   169
    aTest(mip1, false);
deba@567
   170
    cloneTest<CbcMip>();
deba@567
   171
  }
deba@567
   172
#endif
deba@567
   173
deba@458
   174
  return 0;
deba@458
   175
deba@458
   176
}