0
3
0
| 1 | 1 |
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) |
| 2 | 2 |
|
| 3 | 3 |
IF(EXISTS ${CMAKE_SOURCE_DIR}/cmake/version.cmake)
|
| 4 | 4 |
INCLUDE(${CMAKE_SOURCE_DIR}/cmake/version.cmake)
|
| 5 | 5 |
ELSE(EXISTS ${CMAKE_SOURCE_DIR}/cmake/version.cmake)
|
| 6 | 6 |
SET(PROJECT_NAME "LEMON") |
| 7 | 7 |
SET(PROJECT_VERSION "hg-tip" CACHE STRING "LEMON version string.") |
| 8 | 8 |
ENDIF(EXISTS ${CMAKE_SOURCE_DIR}/cmake/version.cmake)
|
| 9 | 9 |
|
| 10 | 10 |
PROJECT(${PROJECT_NAME})
|
| 11 | 11 |
|
| 12 | 12 |
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
| 13 | 13 |
|
| 14 | 14 |
INCLUDE(FindDoxygen) |
| 15 | 15 |
INCLUDE(FindGhostscript) |
| 16 | 16 |
FIND_PACKAGE(GLPK 4.33) |
| 17 | 17 |
FIND_PACKAGE(CPLEX) |
| 18 | 18 |
FIND_PACKAGE(COIN) |
| 19 | 19 |
|
| 20 |
ADD_DEFINITIONS(-DHAVE_CONFIG_H) |
|
| 21 |
|
|
| 22 | 20 |
IF(MSVC) |
| 23 | 21 |
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250 /wd4355 /wd4800 /wd4996")
|
| 24 | 22 |
# Suppressed warnings: |
| 25 | 23 |
# C4250: 'class1' : inherits 'class2::member' via dominance |
| 26 | 24 |
# C4355: 'this' : used in base member initializer list |
| 27 | 25 |
# C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) |
| 28 | 26 |
# C4996: 'function': was declared deprecated |
| 29 | 27 |
ENDIF(MSVC) |
| 30 | 28 |
|
| 31 |
ADD_DEFINITIONS(-DHAVE_CONFIG_H) |
|
| 32 |
|
|
| 33 | 29 |
INCLUDE(CheckTypeSize) |
| 34 | 30 |
CHECK_TYPE_SIZE("long long" LEMON_LONG_LONG)
|
| 35 | 31 |
|
| 36 | 32 |
ENABLE_TESTING() |
| 37 | 33 |
|
| 38 | 34 |
ADD_SUBDIRECTORY(lemon) |
| 39 | 35 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
|
| 40 | 36 |
ADD_SUBDIRECTORY(demo) |
| 41 | 37 |
ADD_SUBDIRECTORY(tools) |
| 42 | 38 |
ADD_SUBDIRECTORY(doc) |
| 43 | 39 |
ADD_SUBDIRECTORY(test) |
| 44 | 40 |
ENDIF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
|
| 45 | 41 |
|
| 46 | 42 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
|
| 47 | 43 |
IF(WIN32) |
| 48 | 44 |
SET(CPACK_PACKAGE_NAME ${PROJECT_NAME})
|
| 49 | 45 |
SET(CPACK_PACKAGE_VENDOR "EGRES") |
| 50 | 46 |
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY |
| 51 | 47 |
"LEMON - Library of Efficient Models and Optimization in Networks") |
| 52 | 48 |
SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
|
| 53 | 49 |
|
| 54 | 50 |
SET(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
|
| 55 | 51 |
|
| 56 | 52 |
SET(CPACK_PACKAGE_INSTALL_DIRECTORY |
| 57 | 53 |
"${PROJECT_NAME} ${PROJECT_VERSION}")
|
| 58 | 54 |
SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY |
| 59 | 55 |
"${PROJECT_NAME} ${PROJECT_VERSION}")
|
| 60 | 56 |
|
| 61 | 57 |
SET(CPACK_COMPONENTS_ALL headers library html_documentation bin) |
| 62 | 58 |
|
| 63 | 59 |
SET(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ headers") |
| 64 | 60 |
SET(CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "Dynamic-link library") |
| 65 | 61 |
SET(CPACK_COMPONENT_BIN_DISPLAY_NAME "Command line utilities") |
| 66 | 62 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DISPLAY_NAME "HTML documentation") |
| 67 | 63 |
|
| 68 | 64 |
SET(CPACK_COMPONENT_HEADERS_DESCRIPTION |
| 69 | 65 |
"C++ header files") |
| 70 | 66 |
SET(CPACK_COMPONENT_LIBRARY_DESCRIPTION |
| 71 | 67 |
"DLL and import library") |
| 72 | 68 |
SET(CPACK_COMPONENT_BIN_DESCRIPTION |
| 73 | 69 |
"Command line utilities") |
| 74 | 70 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DESCRIPTION |
| 75 | 71 |
"Doxygen generated documentation") |
| 76 | 72 |
|
| 77 | 73 |
SET(CPACK_COMPONENT_HEADERS_DEPENDS library) |
| 78 | 74 |
|
| 79 | 75 |
SET(CPACK_COMPONENT_HEADERS_GROUP "Development") |
| 80 | 76 |
SET(CPACK_COMPONENT_LIBRARY_GROUP "Development") |
| 81 | 77 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_GROUP "Documentation") |
| 82 | 78 |
|
| 83 | 79 |
SET(CPACK_COMPONENT_GROUP_DEVELOPMENT_DESCRIPTION |
| 84 | 80 |
"Components needed to develop software using LEMON") |
| 85 | 81 |
SET(CPACK_COMPONENT_GROUP_DOCUMENTATION_DESCRIPTION |
| 86 | 82 |
"Documentation of LEMON") |
| 87 | 83 |
|
| 88 | 84 |
SET(CPACK_ALL_INSTALL_TYPES Full Developer) |
| 89 | 85 |
|
| 90 | 86 |
SET(CPACK_COMPONENT_HEADERS_INSTALL_TYPES Developer Full) |
| 91 | 87 |
SET(CPACK_COMPONENT_LIBRARY_INSTALL_TYPES Developer Full) |
| 92 | 88 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_INSTALL_TYPES Full) |
| 93 | 89 |
|
| 94 | 90 |
SET(CPACK_GENERATOR "NSIS") |
| 95 | 91 |
SET(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis/lemon.ico")
|
| 96 | 92 |
SET(CPACK_NSIS_MUI_UNIICON "${PROJECT_SOURCE_DIR}/cmake/nsis/uninstall.ico")
|
| 97 | 93 |
#SET(CPACK_PACKAGE_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis\\\\installer.bmp")
|
| 98 | 94 |
SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\lemon.ico") |
| 99 | 95 |
SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY} ${PROJECT_NAME}")
|
| 100 | 96 |
SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\lemon.cs.elte.hu") |
| 101 | 97 |
SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\lemon.cs.elte.hu") |
| 102 | 98 |
SET(CPACK_NSIS_CONTACT "lemon-user@lemon.cs.elte.hu") |
| 103 | 99 |
SET(CPACK_NSIS_CREATE_ICONS_EXTRA " |
| 104 | 100 |
CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation.lnk\\\" \\\"$INSTDIR\\\\share\\\\doc\\\\index.html\\\" |
| 105 | 101 |
") |
| 106 | 102 |
SET(CPACK_NSIS_DELETE_ICONS_EXTRA " |
| 107 | 103 |
!insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP |
| 108 | 104 |
Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation.lnk\\\" |
| 109 | 105 |
") |
| 110 | 106 |
|
| 111 | 107 |
INCLUDE(CPack) |
| 112 | 108 |
ENDIF(WIN32) |
| 113 | 109 |
ENDIF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <sstream> |
| 20 | 20 |
#include <lemon/lp_skeleton.h> |
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
|
| 25 | 24 |
#include <lemon/config.h> |
| 26 |
#endif |
|
| 27 | 25 |
|
| 28 | 26 |
#ifdef LEMON_HAVE_GLPK |
| 29 | 27 |
#include <lemon/glpk.h> |
| 30 | 28 |
#endif |
| 31 | 29 |
|
| 32 | 30 |
#ifdef LEMON_HAVE_CPLEX |
| 33 | 31 |
#include <lemon/cplex.h> |
| 34 | 32 |
#endif |
| 35 | 33 |
|
| 36 | 34 |
#ifdef LEMON_HAVE_SOPLEX |
| 37 | 35 |
#include <lemon/soplex.h> |
| 38 | 36 |
#endif |
| 39 | 37 |
|
| 40 | 38 |
#ifdef LEMON_HAVE_CLP |
| 41 | 39 |
#include <lemon/clp.h> |
| 42 | 40 |
#endif |
| 43 | 41 |
|
| 44 | 42 |
using namespace lemon; |
| 45 | 43 |
|
| 46 | 44 |
void lpTest(LpSolver& lp) |
| 47 | 45 |
{
|
| 48 | 46 |
|
| 49 | 47 |
typedef LpSolver LP; |
| 50 | 48 |
|
| 51 | 49 |
std::vector<LP::Col> x(10); |
| 52 | 50 |
// for(int i=0;i<10;i++) x.push_back(lp.addCol()); |
| 53 | 51 |
lp.addColSet(x); |
| 54 | 52 |
lp.colLowerBound(x,1); |
| 55 | 53 |
lp.colUpperBound(x,1); |
| 56 | 54 |
lp.colBounds(x,1,2); |
| 57 | 55 |
|
| 58 | 56 |
std::vector<LP::Col> y(10); |
| 59 | 57 |
lp.addColSet(y); |
| 60 | 58 |
|
| 61 | 59 |
lp.colLowerBound(y,1); |
| 62 | 60 |
lp.colUpperBound(y,1); |
| 63 | 61 |
lp.colBounds(y,1,2); |
| 64 | 62 |
|
| 65 | 63 |
std::map<int,LP::Col> z; |
| 66 | 64 |
|
| 67 | 65 |
z.insert(std::make_pair(12,INVALID)); |
| 68 | 66 |
z.insert(std::make_pair(2,INVALID)); |
| 69 | 67 |
z.insert(std::make_pair(7,INVALID)); |
| 70 | 68 |
z.insert(std::make_pair(5,INVALID)); |
| 71 | 69 |
|
| 72 | 70 |
lp.addColSet(z); |
| 73 | 71 |
|
| 74 | 72 |
lp.colLowerBound(z,1); |
| 75 | 73 |
lp.colUpperBound(z,1); |
| 76 | 74 |
lp.colBounds(z,1,2); |
| 77 | 75 |
|
| 78 | 76 |
{
|
| 79 | 77 |
LP::Expr e,f,g; |
| 80 | 78 |
LP::Col p1,p2,p3,p4,p5; |
| 81 | 79 |
LP::Constr c; |
| 82 | 80 |
|
| 83 | 81 |
p1=lp.addCol(); |
| 84 | 82 |
p2=lp.addCol(); |
| 85 | 83 |
p3=lp.addCol(); |
| 86 | 84 |
p4=lp.addCol(); |
| 87 | 85 |
p5=lp.addCol(); |
| 88 | 86 |
|
| 89 | 87 |
e[p1]=2; |
| 90 | 88 |
*e=12; |
| 91 | 89 |
e[p1]+=2; |
| 92 | 90 |
*e+=12; |
| 93 | 91 |
e[p1]-=2; |
| 94 | 92 |
*e-=12; |
| 95 | 93 |
|
| 96 | 94 |
e=2; |
| 97 | 95 |
e=2.2; |
| 98 | 96 |
e=p1; |
| 99 | 97 |
e=f; |
| 100 | 98 |
|
| 101 | 99 |
e+=2; |
| 102 | 100 |
e+=2.2; |
| 103 | 101 |
e+=p1; |
| 104 | 102 |
e+=f; |
| 105 | 103 |
|
| 106 | 104 |
e-=2; |
| 107 | 105 |
e-=2.2; |
| 108 | 106 |
e-=p1; |
| 109 | 107 |
e-=f; |
| 110 | 108 |
|
| 111 | 109 |
e*=2; |
| 112 | 110 |
e*=2.2; |
| 113 | 111 |
e/=2; |
| 114 | 112 |
e/=2.2; |
| 115 | 113 |
|
| 116 | 114 |
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ |
| 117 | 115 |
(f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ |
| 118 | 116 |
(f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ |
| 119 | 117 |
2.2*f+f*2.2+f/2.2+ |
| 120 | 118 |
2*f+f*2+f/2+ |
| 121 | 119 |
2.2*p1+p1*2.2+p1/2.2+ |
| 122 | 120 |
2*p1+p1*2+p1/2 |
| 123 | 121 |
); |
| 124 | 122 |
|
| 125 | 123 |
|
| 126 | 124 |
c = (e <= f ); |
| 127 | 125 |
c = (e <= 2.2); |
| 128 | 126 |
c = (e <= 2 ); |
| 129 | 127 |
c = (e <= p1 ); |
| 130 | 128 |
c = (2.2<= f ); |
| 131 | 129 |
c = (2 <= f ); |
| 132 | 130 |
c = (p1 <= f ); |
| 133 | 131 |
c = (p1 <= p2 ); |
| 134 | 132 |
c = (p1 <= 2.2); |
| 135 | 133 |
c = (p1 <= 2 ); |
| 136 | 134 |
c = (2.2<= p2 ); |
| 137 | 135 |
c = (2 <= p2 ); |
| 138 | 136 |
|
| 139 | 137 |
c = (e >= f ); |
| 140 | 138 |
c = (e >= 2.2); |
| 141 | 139 |
c = (e >= 2 ); |
| 142 | 140 |
c = (e >= p1 ); |
| 143 | 141 |
c = (2.2>= f ); |
| 144 | 142 |
c = (2 >= f ); |
| 145 | 143 |
c = (p1 >= f ); |
| 146 | 144 |
c = (p1 >= p2 ); |
| 147 | 145 |
c = (p1 >= 2.2); |
| 148 | 146 |
c = (p1 >= 2 ); |
| 149 | 147 |
c = (2.2>= p2 ); |
| 150 | 148 |
c = (2 >= p2 ); |
| 151 | 149 |
|
| 152 | 150 |
c = (e == f ); |
| 153 | 151 |
c = (e == 2.2); |
| 154 | 152 |
c = (e == 2 ); |
| 155 | 153 |
c = (e == p1 ); |
| 156 | 154 |
c = (2.2== f ); |
| 157 | 155 |
c = (2 == f ); |
| 158 | 156 |
c = (p1 == f ); |
| 159 | 157 |
//c = (p1 == p2 ); |
| 160 | 158 |
c = (p1 == 2.2); |
| 161 | 159 |
c = (p1 == 2 ); |
| 162 | 160 |
c = (2.2== p2 ); |
| 163 | 161 |
c = (2 == p2 ); |
| 164 | 162 |
|
| 165 | 163 |
c = ((2 <= e) <= 3); |
| 166 | 164 |
c = ((2 <= p1) <= 3); |
| 167 | 165 |
|
| 168 | 166 |
c = ((2 >= e) >= 3); |
| 169 | 167 |
c = ((2 >= p1) >= 3); |
| 170 | 168 |
|
| 171 | 169 |
e[x[3]]=2; |
| 172 | 170 |
e[x[3]]=4; |
| 173 | 171 |
e[x[3]]=1; |
| 174 | 172 |
*e=12; |
| 175 | 173 |
|
| 176 | 174 |
lp.addRow(-LP::INF,e,23); |
| 177 | 175 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); |
| 178 | 176 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); |
| 179 | 177 |
|
| 180 | 178 |
lp.addRow(x[1]+x[3]<=x[5]-3); |
| 181 | 179 |
lp.addRow((-7<=x[1]+x[3]-12)<=3); |
| 182 | 180 |
lp.addRow(x[1]<=x[5]); |
| 183 | 181 |
|
| 184 | 182 |
std::ostringstream buf; |
| 185 | 183 |
|
| 186 | 184 |
|
| 187 | 185 |
e=((p1+p2)+(p1-0.99*p2)); |
| 188 | 186 |
//e.prettyPrint(std::cout); |
| 189 | 187 |
//(e<=2).prettyPrint(std::cout); |
| 190 | 188 |
double tolerance=0.001; |
| 191 | 189 |
e.simplify(tolerance); |
| 192 | 190 |
buf << "Coeff. of p2 should be 0.01"; |
| 193 | 191 |
check(e[p2]>0, buf.str()); |
| 194 | 192 |
|
| 195 | 193 |
tolerance=0.02; |
| 196 | 194 |
e.simplify(tolerance); |
| 197 | 195 |
buf << "Coeff. of p2 should be 0"; |
| 198 | 196 |
check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str()); |
| 199 | 197 |
|
| 200 | 198 |
//Test for clone/new |
| 201 | 199 |
LP* lpnew = lp.newSolver(); |
| 202 | 200 |
LP* lpclone = lp.cloneSolver(); |
| 203 | 201 |
delete lpnew; |
| 204 | 202 |
delete lpclone; |
| 205 | 203 |
|
| 206 | 204 |
} |
| 207 | 205 |
|
| 208 | 206 |
{
|
| 209 | 207 |
LP::DualExpr e,f,g; |
| 210 | 208 |
LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID, |
| 211 | 209 |
p4 = INVALID, p5 = INVALID; |
| 212 | 210 |
|
| 213 | 211 |
e[p1]=2; |
| 214 | 212 |
e[p1]+=2; |
| 215 | 213 |
e[p1]-=2; |
| 216 | 214 |
|
| 217 | 215 |
e=p1; |
| 218 | 216 |
e=f; |
| 219 | 217 |
|
| 220 | 218 |
e+=p1; |
| 221 | 219 |
e+=f; |
| 222 | 220 |
|
| 223 | 221 |
e-=p1; |
| 224 | 222 |
e-=f; |
| 225 | 223 |
|
| 226 | 224 |
e*=2; |
| 227 | 225 |
e*=2.2; |
| 228 | 226 |
e/=2; |
| 229 | 227 |
e/=2.2; |
| 230 | 228 |
|
| 231 | 229 |
e=((p1+p2)+(p1-p2)+ |
| 232 | 230 |
(p1+f)+(f+p1)+(f+g)+ |
| 233 | 231 |
(p1-f)+(f-p1)+(f-g)+ |
| 234 | 232 |
2.2*f+f*2.2+f/2.2+ |
| 235 | 233 |
2*f+f*2+f/2+ |
| 236 | 234 |
2.2*p1+p1*2.2+p1/2.2+ |
| 237 | 235 |
2*p1+p1*2+p1/2 |
| 238 | 236 |
); |
| 239 | 237 |
} |
| 240 | 238 |
|
| 241 | 239 |
} |
| 242 | 240 |
|
| 243 | 241 |
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat, |
| 244 | 242 |
double exp_opt) {
|
| 245 | 243 |
using std::string; |
| 246 | 244 |
lp.solve(); |
| 247 | 245 |
|
| 248 | 246 |
std::ostringstream buf; |
| 249 | 247 |
buf << "PrimalType should be: " << int(stat) << int(lp.primalType()); |
| 250 | 248 |
|
| 251 | 249 |
check(lp.primalType()==stat, buf.str()); |
| 252 | 250 |
|
| 253 | 251 |
if (stat == LpSolver::OPTIMAL) {
|
| 254 | 252 |
std::ostringstream sbuf; |
| 255 | 253 |
sbuf << "Wrong optimal value (" << lp.primal() <<") with "
|
| 256 | 254 |
<< lp.solverName() <<"\n the right optimum is " << exp_opt; |
| 257 | 255 |
check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str()); |
| 258 | 256 |
} |
| 259 | 257 |
} |
| 260 | 258 |
|
| 261 | 259 |
void aTest(LpSolver & lp) |
| 262 | 260 |
{
|
| 263 | 261 |
typedef LpSolver LP; |
| 264 | 262 |
|
| 265 | 263 |
//The following example is very simple |
| 266 | 264 |
|
| 267 | 265 |
typedef LpSolver::Row Row; |
| 268 | 266 |
typedef LpSolver::Col Col; |
| 269 | 267 |
|
| 270 | 268 |
|
| 271 | 269 |
Col x1 = lp.addCol(); |
| 272 | 270 |
Col x2 = lp.addCol(); |
| 273 | 271 |
|
| 274 | 272 |
|
| 275 | 273 |
//Constraints |
| 276 | 274 |
Row upright=lp.addRow(x1+2*x2 <=1); |
| 277 | 275 |
lp.addRow(x1+x2 >=-1); |
| 278 | 276 |
lp.addRow(x1-x2 <=1); |
| 279 | 277 |
lp.addRow(x1-x2 >=-1); |
| 280 | 278 |
//Nonnegativity of the variables |
| 281 | 279 |
lp.colLowerBound(x1, 0); |
| 282 | 280 |
lp.colLowerBound(x2, 0); |
| 283 | 281 |
//Objective function |
| 284 | 282 |
lp.obj(x1+x2); |
| 285 | 283 |
|
| 286 | 284 |
lp.sense(lp.MAX); |
| 287 | 285 |
|
| 288 | 286 |
//Testing the problem retrieving routines |
| 289 | 287 |
check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
| 290 | 288 |
check(lp.sense() == lp.MAX,"This is a maximization!"); |
| 291 | 289 |
check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); |
| 292 | 290 |
check(lp.colLowerBound(x1)==0, |
| 293 | 291 |
"The lower bound for variable x1 should be 0."); |
| 294 | 292 |
check(lp.colUpperBound(x1)==LpSolver::INF, |
| 295 | 293 |
"The upper bound for variable x1 should be infty."); |
| 296 | 294 |
check(lp.rowLowerBound(upright) == -LpSolver::INF, |
| 297 | 295 |
"The lower bound for the first row should be -infty."); |
| 298 | 296 |
check(lp.rowUpperBound(upright)==1, |
| 299 | 297 |
"The upper bound for the first row should be 1."); |
| 300 | 298 |
LpSolver::Expr e = lp.row(upright); |
| 301 | 299 |
check(e[x1] == 1, "The first coefficient should 1."); |
| 302 | 300 |
check(e[x2] == 2, "The second coefficient should 1."); |
| 303 | 301 |
|
| 304 | 302 |
lp.row(upright, x1+x2 <=1); |
| 305 | 303 |
e = lp.row(upright); |
| 306 | 304 |
check(e[x1] == 1, "The first coefficient should 1."); |
| 307 | 305 |
check(e[x2] == 1, "The second coefficient should 1."); |
| 308 | 306 |
|
| 309 | 307 |
LpSolver::DualExpr de = lp.col(x1); |
| 310 | 308 |
check( de[upright] == 1, "The first coefficient should 1."); |
| 311 | 309 |
|
| 312 | 310 |
LpSolver* clp = lp.cloneSolver(); |
| 313 | 311 |
|
| 314 | 312 |
//Testing the problem retrieving routines |
| 315 | 313 |
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
| 316 | 314 |
check(clp->sense() == clp->MAX,"This is a maximization!"); |
| 317 | 315 |
check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!"); |
| 318 | 316 |
// std::cout<<lp.colLowerBound(x1)<<std::endl; |
| 319 | 317 |
check(clp->colLowerBound(x1)==0, |
| 320 | 318 |
"The lower bound for variable x1 should be 0."); |
| 321 | 319 |
check(clp->colUpperBound(x1)==LpSolver::INF, |
| 322 | 320 |
"The upper bound for variable x1 should be infty."); |
| 323 | 321 |
|
| 324 | 322 |
check(lp.rowLowerBound(upright)==-LpSolver::INF, |
| 325 | 323 |
"The lower bound for the first row should be -infty."); |
| 326 | 324 |
check(lp.rowUpperBound(upright)==1, |
| 327 | 325 |
"The upper bound for the first row should be 1."); |
| 328 | 326 |
e = clp->row(upright); |
| 329 | 327 |
check(e[x1] == 1, "The first coefficient should 1."); |
| 330 | 328 |
check(e[x2] == 1, "The second coefficient should 1."); |
| 331 | 329 |
|
| 332 | 330 |
de = clp->col(x1); |
| 333 | 331 |
check(de[upright] == 1, "The first coefficient should 1."); |
| 334 | 332 |
|
| 335 | 333 |
delete clp; |
| 336 | 334 |
|
| 337 | 335 |
//Maximization of x1+x2 |
| 338 | 336 |
//over the triangle with vertices (0,0) (0,1) (1,0) |
| 339 | 337 |
double expected_opt=1; |
| 340 | 338 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 341 | 339 |
|
| 342 | 340 |
//Minimization |
| 343 | 341 |
lp.sense(lp.MIN); |
| 344 | 342 |
expected_opt=0; |
| 345 | 343 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 346 | 344 |
|
| 347 | 345 |
//Vertex (-1,0) instead of (0,0) |
| 348 | 346 |
lp.colLowerBound(x1, -LpSolver::INF); |
| 349 | 347 |
expected_opt=-1; |
| 350 | 348 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 351 | 349 |
|
| 352 | 350 |
//Erase one constraint and return to maximization |
| 353 | 351 |
lp.erase(upright); |
| 354 | 352 |
lp.sense(lp.MAX); |
| 355 | 353 |
expected_opt=LpSolver::INF; |
| 356 | 354 |
solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt); |
| 357 | 355 |
|
| 358 | 356 |
//Infeasibilty |
| 359 | 357 |
lp.addRow(x1+x2 <=-2); |
| 360 | 358 |
solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt); |
| 361 | 359 |
|
| 362 | 360 |
} |
| 363 | 361 |
|
| 364 | 362 |
template<class LP> |
| 365 | 363 |
void cloneTest() |
| 366 | 364 |
{
|
| 367 | 365 |
//Test for clone/new |
| 368 | 366 |
|
| 369 | 367 |
LP* lp = new LP(); |
| 370 | 368 |
LP* lpnew = lp->newSolver(); |
| 371 | 369 |
LP* lpclone = lp->cloneSolver(); |
| 372 | 370 |
delete lp; |
| 373 | 371 |
delete lpnew; |
| 374 | 372 |
delete lpclone; |
| 375 | 373 |
} |
| 376 | 374 |
|
| 377 | 375 |
int main() |
| 378 | 376 |
{
|
| 379 | 377 |
LpSkeleton lp_skel; |
| 380 | 378 |
lpTest(lp_skel); |
| 381 | 379 |
|
| 382 | 380 |
#ifdef LEMON_HAVE_GLPK |
| 383 | 381 |
{
|
| 384 | 382 |
GlpkLp lp_glpk1,lp_glpk2; |
| 385 | 383 |
lpTest(lp_glpk1); |
| 386 | 384 |
aTest(lp_glpk2); |
| 387 | 385 |
cloneTest<GlpkLp>(); |
| 388 | 386 |
} |
| 389 | 387 |
#endif |
| 390 | 388 |
|
| 391 | 389 |
#ifdef LEMON_HAVE_CPLEX |
| 392 | 390 |
try {
|
| 393 | 391 |
CplexLp lp_cplex1,lp_cplex2; |
| 394 | 392 |
lpTest(lp_cplex1); |
| 395 | 393 |
aTest(lp_cplex2); |
| 396 | 394 |
cloneTest<CplexLp>(); |
| 397 | 395 |
} catch (CplexEnv::LicenseError& error) {
|
| 398 | 396 |
check(false, error.what()); |
| 399 | 397 |
} |
| 400 | 398 |
#endif |
| 401 | 399 |
|
| 402 | 400 |
#ifdef LEMON_HAVE_SOPLEX |
| 403 | 401 |
{
|
| 404 | 402 |
SoplexLp lp_soplex1,lp_soplex2; |
| 405 | 403 |
lpTest(lp_soplex1); |
| 406 | 404 |
aTest(lp_soplex2); |
| 407 | 405 |
cloneTest<SoplexLp>(); |
| 408 | 406 |
} |
| 409 | 407 |
#endif |
| 410 | 408 |
|
| 411 | 409 |
#ifdef LEMON_HAVE_CLP |
| 412 | 410 |
{
|
| 413 | 411 |
ClpLp lp_clp1,lp_clp2; |
| 414 | 412 |
lpTest(lp_clp1); |
| 415 | 413 |
aTest(lp_clp2); |
| 416 | 414 |
cloneTest<ClpLp>(); |
| 417 | 415 |
} |
| 418 | 416 |
#endif |
| 419 | 417 |
|
| 420 | 418 |
return 0; |
| 421 | 419 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include "test_tools.h" |
| 20 | 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
|
| 22 | 21 |
#include <lemon/config.h> |
| 23 |
#endif |
|
| 24 | 22 |
|
| 25 | 23 |
#ifdef LEMON_HAVE_CPLEX |
| 26 | 24 |
#include <lemon/cplex.h> |
| 27 | 25 |
#endif |
| 28 | 26 |
|
| 29 | 27 |
#ifdef LEMON_HAVE_GLPK |
| 30 | 28 |
#include <lemon/glpk.h> |
| 31 | 29 |
#endif |
| 32 | 30 |
|
| 33 | 31 |
#ifdef LEMON_HAVE_CBC |
| 34 | 32 |
#include <lemon/cbc.h> |
| 35 | 33 |
#endif |
| 36 | 34 |
|
| 37 | 35 |
|
| 38 | 36 |
using namespace lemon; |
| 39 | 37 |
|
| 40 | 38 |
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat, |
| 41 | 39 |
double exp_opt) {
|
| 42 | 40 |
using std::string; |
| 43 | 41 |
|
| 44 | 42 |
mip.solve(); |
| 45 | 43 |
//int decimal,sign; |
| 46 | 44 |
std::ostringstream buf; |
| 47 | 45 |
buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type()); |
| 48 | 46 |
|
| 49 | 47 |
|
| 50 | 48 |
// itoa(stat,buf1, 10); |
| 51 | 49 |
check(mip.type()==stat, buf.str()); |
| 52 | 50 |
|
| 53 | 51 |
if (stat == MipSolver::OPTIMAL) {
|
| 54 | 52 |
std::ostringstream sbuf; |
| 55 | 53 |
buf << "Wrong optimal value: the right optimum is " << exp_opt; |
| 56 | 54 |
check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); |
| 57 | 55 |
//+ecvt(exp_opt,2) |
| 58 | 56 |
} |
| 59 | 57 |
} |
| 60 | 58 |
|
| 61 | 59 |
void aTest(MipSolver& mip) |
| 62 | 60 |
{
|
| 63 | 61 |
//The following example is very simple |
| 64 | 62 |
|
| 65 | 63 |
|
| 66 | 64 |
typedef MipSolver::Row Row; |
| 67 | 65 |
typedef MipSolver::Col Col; |
| 68 | 66 |
|
| 69 | 67 |
|
| 70 | 68 |
Col x1 = mip.addCol(); |
| 71 | 69 |
Col x2 = mip.addCol(); |
| 72 | 70 |
|
| 73 | 71 |
|
| 74 | 72 |
//Objective function |
| 75 | 73 |
mip.obj(x1); |
| 76 | 74 |
|
| 77 | 75 |
mip.max(); |
| 78 | 76 |
|
| 79 | 77 |
//Unconstrained optimization |
| 80 | 78 |
mip.solve(); |
| 81 | 79 |
//Check it out! |
| 82 | 80 |
|
| 83 | 81 |
//Constraints |
| 84 | 82 |
mip.addRow(2 * x1 + x2 <= 2); |
| 85 | 83 |
Row y2 = mip.addRow(x1 - 2 * x2 <= 0); |
| 86 | 84 |
|
| 87 | 85 |
//Nonnegativity of the variable x1 |
| 88 | 86 |
mip.colLowerBound(x1, 0); |
| 89 | 87 |
|
| 90 | 88 |
|
| 91 | 89 |
//Maximization of x1 |
| 92 | 90 |
//over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
| 93 | 91 |
double expected_opt=4.0/5.0; |
| 94 | 92 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 95 | 93 |
|
| 96 | 94 |
|
| 97 | 95 |
//Restrict x2 to integer |
| 98 | 96 |
mip.colType(x2,MipSolver::INTEGER); |
| 99 | 97 |
expected_opt=1.0/2.0; |
| 100 | 98 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 101 | 99 |
|
| 102 | 100 |
|
| 103 | 101 |
//Restrict both to integer |
| 104 | 102 |
mip.colType(x1,MipSolver::INTEGER); |
| 105 | 103 |
expected_opt=0; |
| 106 | 104 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 107 | 105 |
|
| 108 | 106 |
//Erase a variable |
| 109 | 107 |
mip.erase(x2); |
| 110 | 108 |
mip.rowUpperBound(y2, 8); |
| 111 | 109 |
expected_opt=1; |
| 112 | 110 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 113 | 111 |
|
| 114 | 112 |
} |
| 115 | 113 |
|
| 116 | 114 |
|
| 117 | 115 |
template<class MIP> |
| 118 | 116 |
void cloneTest() |
| 119 | 117 |
{
|
| 120 | 118 |
|
| 121 | 119 |
MIP* mip = new MIP(); |
| 122 | 120 |
MIP* mipnew = mip->newSolver(); |
| 123 | 121 |
MIP* mipclone = mip->cloneSolver(); |
| 124 | 122 |
delete mip; |
| 125 | 123 |
delete mipnew; |
| 126 | 124 |
delete mipclone; |
| 127 | 125 |
} |
| 128 | 126 |
|
| 129 | 127 |
int main() |
| 130 | 128 |
{
|
| 131 | 129 |
|
| 132 | 130 |
#ifdef LEMON_HAVE_GLPK |
| 133 | 131 |
{
|
| 134 | 132 |
GlpkMip mip1; |
| 135 | 133 |
aTest(mip1); |
| 136 | 134 |
cloneTest<GlpkMip>(); |
| 137 | 135 |
} |
| 138 | 136 |
#endif |
| 139 | 137 |
|
| 140 | 138 |
#ifdef LEMON_HAVE_CPLEX |
| 141 | 139 |
try {
|
| 142 | 140 |
CplexMip mip2; |
| 143 | 141 |
aTest(mip2); |
| 144 | 142 |
cloneTest<CplexMip>(); |
| 145 | 143 |
} catch (CplexEnv::LicenseError& error) {
|
| 146 | 144 |
check(false, error.what()); |
| 147 | 145 |
} |
| 148 | 146 |
#endif |
| 149 | 147 |
|
| 150 | 148 |
#ifdef LEMON_HAVE_CBC |
| 151 | 149 |
{
|
| 152 | 150 |
CbcMip mip1; |
| 153 | 151 |
aTest(mip1); |
| 154 | 152 |
cloneTest<CbcMip>(); |
| 155 | 153 |
} |
| 156 | 154 |
#endif |
| 157 | 155 |
|
| 158 | 156 |
return 0; |
| 159 | 157 |
|
| 160 | 158 |
} |
0 comments (0 inline)