-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
323 lines (262 loc) · 11.1 KB
/
Copy pathtest.cpp
File metadata and controls
323 lines (262 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*--------------------------------------------------------------------------*/
/*-------------------------- File test.cpp ---------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Main for testing the capacity of an AbstractBlock to solve a quadratic
* problem.
*
* \author Enrico Calandrini \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Enrico Calandrini
*/
/*--------------------------------------------------------------------------*/
/*-------------------------------- MACROS ----------------------------------*/
/*--------------------------------------------------------------------------*/
#define LOG1( x ) cout << x
#define CLOG1( y , x ) if( y ) cout << x
#define LOG_LEVEL 0
// 0 = only final result of the model
// 1 = + save LP file
/*--------------------------------------------------------------------------*/
#define PANIC( x ) if( ! ( x ) ) PANICMSG
#define USECOLORS 1
#if( USECOLORS )
#define RED( x ) "\x1B[31m" #x "\033[0m"
#define GREEN( x ) "\x1B[32m" #x "\033[0m"
#else
#define RED( x ) #x
#define GREEN( x ) #x
#endif
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <chrono>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "common_utils.h"
#include "AbstractBlock.h"
#include "MILPSolver.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace std;
using namespace SMSpp_di_unipi_it;
using FunctionValue = Function::FunctionValue;
/*--------------------------------------------------------------------------*/
/*-------------------------------- TYPES -----------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*------------------------------- CONSTANTS --------------------------------*/
/*--------------------------------------------------------------------------*/
const FunctionValue INF = SMSpp_di_unipi_it::Inf< FunctionValue >();
/*--------------------------------------------------------------------------*/
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------------------------------------------------*/
AbstractBlock * LPBlock; // the problem expressed as an LP
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
static bool search_opt( std::string file_name , double* opt_value , char type )
{
// Read all the available optimal solution from the file list_problems.txt
std::ifstream file;
file.open( "available_instances.txt" );
auto max = std::numeric_limits< std::streamsize >::max();
/*---------------------------------------*/
/* HERE WE ARE STARTING TO READ THE FILE */
/*---------------------------------------*/
// Eat initial comments
while( file.peek() == file.widen( '\\' ) ||
file.peek() == '\n' ) {
file.ignore( max, '\n' );
}
std::string word;
file >> word;
bool opt_found = false;
// Now we should be reading lp files name until we reach the END word
while( word != "END" ) {
if( word == file_name ) {
// We found the right file
// If we want the optimal value of the continuous relaxation, skip the
// first entry (solution of integer version)
if( type == 'C')
file >> word;
std::string value;
file >> value;
if( value == "?" )
// The instance is available but the optimal value is unknown
return( false );
*opt_value = std::stod( value );
opt_found = true;
word = "END";
}
else{
file.ignore( max, '\n' );
file >> word;
}
}
return( opt_found );
}
/*--------------------------------------------------------------------------*/
static bool SolveModel( bool is_found , double opt_value )
{
try {
// solve the LPBlock- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Solver * slvrLP = ( LPBlock->get_registered_solvers() ).front();
auto start = std::chrono::system_clock::now();
int rtrnLP = slvrLP->compute( false );
auto end = std::chrono::system_clock::now();
double t = std::chrono::duration< double >( end - start ).count();
bool hsLP = ( ( rtrnLP >= Solver::kOK ) && ( rtrnLP < Solver::kError ) )
|| ( rtrnLP == Solver::kLowPrecision );
double foLP = hsLP ? ( slvrLP->get_ub() ) : ( INF );
if( is_found )
LOG1( "The known optimal solution of the model is : " << opt_value << endl );
else
LOG1( "No known optimal solution available" << endl );
// determine the value token, the pass/fail verdict and (for the uniform
// per-instance line) the reference, keeping the original semantics
bool ok;
std::string tok;
double ref = is_found ? opt_value
: std::numeric_limits< double >::quiet_NaN();
if( rtrnLP == Solver::kStopTime ) {
double boundLP = hsLP ? ( slvrLP->get_lb() ) : ( INF );
LOG1( "Optimization stopped due to time limit." << endl );
tok = fmt_obj( foLP );
if( opt_value == 0 && abs(foLP) <= 1e-4 ) {
LOG1( "The best solution found is " << foLP << endl );
ok = true;
}
else {
LOG1( "The best solution found is " << foLP << " and the best bound is "
<< boundLP << endl );
ok = ( ( opt_value <= foLP + abs(foLP)*1e-4 && opt_value >= boundLP ) ||
( opt_value >= foLP - abs(foLP)*1e-4 && opt_value <= boundLP ) );
}
}
else if( hsLP ) {
LOG1( "The returned value of the solution found is : " << foLP << endl );
tok = fmt_obj( foLP );
ok = ! ( is_found && abs( foLP - opt_value ) >= 1e-3 * std::max( double( 1 ) ,
std::max( abs( foLP ) , abs( opt_value ) ) ) );
}
else if( rtrnLP == Solver::kInfeasible ) {
LOG1( "The model hs been proven to be infeasible" << endl );
tok = "Unfeas";
ok = ! is_found;
}
else if( rtrnLP == Solver::kUnbounded ) {
LOG1( "The model hs been proven to be unbounded" << endl );
tok = "Unbounded";
ok = ! is_found;
}
else {
tok = "Error!";
ok = false;
}
print_instance_line( { t } , { tok } , ref , ok ? "OK" : "KO" );
return( ok );
}
catch( exception &e ) {
cerr << e.what() << endl;
exit( 1 );
}
catch(...) {
cerr << "Error: unknown exception thrown" << endl;
exit( 1 );
}
}
int main( int argc , char **argv )
{
// override the default terminate handler to print the exception message
std::set_terminate( smspp_terminate );
// reading command line parameters - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cout.setf( ios::scientific, ios::floatfield );
cout << setprecision( 10 );
std::string file_name;
double opt_value = 0;
char type = 'I';
bool is_opt_found = false;
// construction and loading of the objects - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
switch( argc ) {
case( 4 ): Str2Sthg( argv[ 3 ] , opt_value );
is_opt_found = true;
case( 3 ): Str2Sthg( argv[ 2 ] , type );
case( 2 ): Str2Sthg( argv[ 1 ] , file_name );
break;
default: cerr << "Usage: " << argv[ 0 ] <<
" file_name : name of the lp file to be loaded"
<< std::endl <<
" type: solve the Integer version or the Continuous Relaxation [I]"
<< std::endl <<
" opt_value: optimal known value of the model [0]"
<< std::endl;
return( 1 );
}
if( argc < 4 ) {
// We have to retrieve optimal value, if available
is_opt_found = search_opt( file_name , &opt_value , type );
}
// construct the LP problem by reading the mps file specified - - - - - - -
{
LPBlock = new AbstractBlock();
std::ifstream file;
file.open( file_name );
LPBlock->load( file , 'L' );
}
// attach the Solver to the Block- - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// for both Block do this by reading an appropriate BlockSolverConfig from
// file and apply() it to the Block; note that the BlockSolverConfig are
// clear()-ed and kept to do the cleanup at the end.
// BSC may be a plain BlockSolverConfig or a meta-config
// SimpleConfiguration< std::map< std::string , Configuration * > >;
// s_config_Block() dispatches on the runtime type and clears the config(s)
// for final cleanup.
std::string lpbsc_fn = ( type == 'C' ) ? "LPPar_C.txt" : "LPPar_I.txt";
Configuration * lpbsc = Configuration::deserialize( lpbsc_fn );
if( ! lpbsc ) {
cerr << "Error: cannot load BSC from " << lpbsc_fn << endl;
exit( 1 );
}
s_config_Block( LPBlock , lpbsc , lpbsc_fn );
// Solve the continuous relaxation, if required
if( type == 'C' )
( ( LPBlock->get_registered_solvers() ).front() )->set_par(
MILPSolver::intRelaxIntVars , 1 );
// open log-file - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// write the .lp of the model loaded in order to compare it with the
// previous one
#if( LOG_LEVEL >= 1 )
( ( LPBlock->get_registered_solvers() ).front() )->set_par(
MILPSolver::strOutputFile , "Read_Model.lp" );
#endif
// first solver call - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool is_solved = SolveModel( is_opt_found , opt_value );
if( is_solved )
cout << GREEN( Test passed!! ) << endl;
else
cout << RED( Shit happened!! ) << endl;
// destroy the Block - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// apply() the clear()-ed BlockSolverConfig (or meta-config) to cleanup Solver
s_config_Block( LPBlock , lpbsc );
// then delete the BlockSolverConfig
delete( lpbsc );
// delete the Blocks
delete( LPBlock );
// terminate - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return( is_solved ? 0 : 1 );
} // end( main )
/*--------------------------------------------------------------------------*/
/*------------------------- End File test.cpp ------------------------------*/
/*--------------------------------------------------------------------------*/