test/mip_test.cc
author deba
Tue, 06 Mar 2007 17:04:16 +0000
changeset 2395 fe5966d8acfc
parent 2386 81b47fc5c444
child 2441 d8d6ab871608
permissions -rw-r--r--
Correction of urls and email addresses
alpar@2391
     1
/* -*- C++ -*-
alpar@2391
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2391
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2391
     8
 *
alpar@2391
     9
 * Permission to use, modify and distribute this software is granted
alpar@2391
    10
 * provided that this copyright notice appears in all copies. For
alpar@2391
    11
 * precise terms see the accompanying LICENSE file.
alpar@2391
    12
 *
alpar@2391
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2391
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2391
    15
 * purpose.
alpar@2391
    16
 *
alpar@2391
    17
 */
alpar@2391
    18
athos@2149
    19
#include "test_tools.h"
athos@2146
    20
athos@2218
    21
athos@2221
    22
#ifdef HAVE_CONFIG_H
athos@2221
    23
#include <config.h>
athos@2221
    24
#endif
athos@2221
    25
deba@2227
    26
#ifdef HAVE_CPLEX
deba@2227
    27
#include <lemon/mip_cplex.h>
deba@2227
    28
#endif
deba@2227
    29
deba@2227
    30
#ifdef HAVE_GLPK
deba@2227
    31
#include <lemon/mip_glpk.h>
deba@2227
    32
#endif
athos@2221
    33
athos@2218
    34
athos@2146
    35
using namespace lemon;
athos@2146
    36
athos@2218
    37
void solveAndCheck(MipSolverBase& lp, MipSolverBase::SolutionStatus stat, 
athos@2149
    38
		   double exp_opt) {
athos@2149
    39
  using std::string;
athos@2218
    40
athos@2149
    41
  lp.solve();
athos@2149
    42
  //int decimal,sign;
athos@2149
    43
  std::ostringstream buf;
athos@2149
    44
  buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
athos@2146
    45
athos@2218
    46
athos@2149
    47
  //  itoa(stat,buf1, 10);
athos@2213
    48
  check(lp.mipStatus()==stat, buf.str());
athos@2149
    49
athos@2218
    50
  if (stat ==  MipSolverBase::OPTIMAL) {
deba@2386
    51
    std::ostringstream sbuf;
athos@2149
    52
    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
deba@2386
    53
    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
athos@2149
    54
    //+ecvt(exp_opt,2)
athos@2149
    55
  }
athos@2149
    56
}
athos@2149
    57
athos@2218
    58
void aTest(MipSolverBase& mip)
athos@2149
    59
{
athos@2149
    60
 //The following example is very simple
athos@2149
    61
athos@2218
    62
athos@2218
    63
  typedef MipSolverBase::Row Row;
athos@2218
    64
  typedef MipSolverBase::Col Col;
athos@2218
    65
athos@2149
    66
athos@2149
    67
athos@2149
    68
  Col x1 = mip.addCol();
athos@2149
    69
  Col x2 = mip.addCol();
athos@2149
    70
athos@2149
    71
athos@2213
    72
  //Objective function
deba@2369
    73
  mip.obj(x1);
athos@2213
    74
athos@2213
    75
  mip.max();
athos@2213
    76
athos@2213
    77
athos@2213
    78
  //Unconstrained optimization
athos@2213
    79
  mip.solve();
athos@2213
    80
  //Check it out!
athos@2213
    81
athos@2149
    82
  //Constraints
athos@2149
    83
  mip.addRow(2*x1+x2 <=2);  
athos@2149
    84
  mip.addRow(x1-2*x2 <=0);  
athos@2149
    85
athos@2149
    86
  //Nonnegativity of the variable x1
athos@2149
    87
  mip.colLowerBound(x1, 0);
athos@2149
    88
athos@2149
    89
athos@2149
    90
athos@2149
    91
  //Maximization of x1
athos@2221
    92
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
athos@2149
    93
  double expected_opt=4.0/5.0;
athos@2218
    94
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
athos@2149
    95
athos@2149
    96
  //Restrict x2 to integer
athos@2267
    97
  mip.colType(x2,MipSolverBase::INT);  
athos@2149
    98
  expected_opt=1.0/2.0;
athos@2218
    99
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
athos@2149
   100
athos@2149
   101
athos@2149
   102
  //Restrict both to integer
athos@2267
   103
  mip.colType(x1,MipSolverBase::INT);  
athos@2149
   104
  expected_opt=0;
athos@2218
   105
  solveAndCheck(mip, MipSolverBase::OPTIMAL, expected_opt);
athos@2149
   106
athos@2149
   107
 
athos@2149
   108
athos@2149
   109
}
athos@2149
   110
athos@2149
   111
athos@2149
   112
int main() 
athos@2149
   113
{
athos@2146
   114
athos@2147
   115
#ifdef HAVE_GLPK
athos@2149
   116
  MipGlpk mip1;
athos@2149
   117
  aTest(mip1);
athos@2147
   118
#endif
athos@2146
   119
athos@2218
   120
#ifdef HAVE_CPLEX
athos@2218
   121
  MipCplex mip2;
athos@2218
   122
  aTest(mip2);
athos@2218
   123
#endif
athos@2218
   124
athos@2147
   125
  return 0;
athos@2146
   126
athos@2146
   127
}