test/mip_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Tue, 02 Dec 2008 21:40:33 +0100
changeset 481 7afc121e0689
child 482 ed54c0d13df0
permissions -rw-r--r--
Port LP and MIP solvers from SVN -r3509 (#44)
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@481
     5
 * Copyright (C) 2003-2008
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
deba@481
    22
#ifdef HAVE_CONFIG_H
deba@481
    23
#include <lemon/config.h>
deba@481
    24
#endif
deba@481
    25
deba@481
    26
#ifdef HAVE_CPLEX
deba@481
    27
#include <lemon/mip_cplex.h>
deba@481
    28
#endif
deba@481
    29
deba@481
    30
#ifdef HAVE_GLPK
deba@481
    31
#include <lemon/mip_glpk.h>
deba@481
    32
#endif
deba@481
    33
deba@481
    34
deba@481
    35
using namespace lemon;
deba@481
    36
deba@481
    37
void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat,
deba@481
    38
                   double exp_opt) {
deba@481
    39
  using std::string;
deba@481
    40
deba@481
    41
  lp.solve();
deba@481
    42
  //int decimal,sign;
deba@481
    43
  std::ostringstream buf;
deba@481
    44
  buf << "Primalstatus should be: " << int(stat)
deba@481
    45
      <<" and it is "<<int(lp.mipStatus());
deba@481
    46
deba@481
    47
deba@481
    48
  //  itoa(stat,buf1, 10);
deba@481
    49
  check(lp.mipStatus()==stat, buf.str());
deba@481
    50
deba@481
    51
  if (stat ==  MipSolverBase::OPTIMAL) {
deba@481
    52
    std::ostringstream sbuf;
deba@481
    53
    buf << "Wrong optimal value: the right optimum is " << exp_opt;
deba@481
    54
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
deba@481
    55
    //+ecvt(exp_opt,2)
deba@481
    56
  }
deba@481
    57
}
deba@481
    58
deba@481
    59
void aTest(MipSolverBase& mip)
deba@481
    60
{
deba@481
    61
 //The following example is very simple
deba@481
    62
deba@481
    63
deba@481
    64
  typedef MipSolverBase::Row Row;
deba@481
    65
  typedef MipSolverBase::Col Col;
deba@481
    66
deba@481
    67
deba@481
    68
deba@481
    69
  Col x1 = mip.addCol();
deba@481
    70
  Col x2 = mip.addCol();
deba@481
    71
deba@481
    72
deba@481
    73
  //Objective function
deba@481
    74
  mip.obj(x1);
deba@481
    75
deba@481
    76
  mip.max();
deba@481
    77
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@481
    84
  mip.addRow(2*x1+x2 <=2);
deba@481
    85
  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@481
    90
  //Maximization of x1
deba@481
    91
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
deba@481
    92
  double expected_opt=4.0/5.0;
deba@481
    93
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
deba@481
    94
deba@481
    95
  //Restrict x2 to integer
deba@481
    96
  mip.colType(x2,MipSolverBase::INT);
deba@481
    97
  expected_opt=1.0/2.0;
deba@481
    98
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
deba@481
    99
deba@481
   100
deba@481
   101
  //Restrict both to integer
deba@481
   102
  mip.colType(x1,MipSolverBase::INT);
deba@481
   103
  expected_opt=0;
deba@481
   104
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
deba@481
   105
deba@481
   106
deba@481
   107
deba@481
   108
}
deba@481
   109
deba@481
   110
deba@481
   111
int main()
deba@481
   112
{
deba@481
   113
deba@481
   114
#ifdef HAVE_GLPK
deba@481
   115
  MipGlpk mip1;
deba@481
   116
  aTest(mip1);
deba@481
   117
#endif
deba@481
   118
deba@481
   119
#ifdef HAVE_CPLEX
deba@481
   120
  MipCplex mip2;
deba@481
   121
  aTest(mip2);
deba@481
   122
#endif
deba@481
   123
deba@481
   124
  return 0;
deba@481
   125
deba@481
   126
}