Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
365 lines (317 loc) · 11.6 KB
/
Copy pathtest.cpp
File metadata and controls
365 lines (317 loc) · 11.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*--------------------------------------------------------------------------*/
/*----------------------------- File test.cpp ------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Simple test on TwoStageStochasticBlock.
*
* \author Benoît Tran \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Benoît Tran
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "TwoStageStochasticBlock.h"
#include "AbstractBlock.h"
#include "FRealObjective.h"
#include "LinearFunction.h"
#include "PolyhedralFunction.h"
#include <cstdio>
#include <iostream>
#include <set>
#include <typeinfo>
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace std;
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/// Custom terminate function to print the exception message
void smspp_terminate( void ) {
std::cerr << "Uncaught exception in executing SMS++:\n";
try {
std::rethrow_exception( std::current_exception() );
}
catch( const std::exception & e ) {
std::cerr << "\tException type: " << typeid( e ).name() << "\n";
std::cerr << "\tException message: " << e.what() << "\n";
} catch( ... ) {
std::cerr << "\tUnknown exception" << std::endl;
}
std::abort();
}
/*--------------------------------------------------------------------------*/
/*----------------------------- TEST HELPERS -------------------------------*/
/*--------------------------------------------------------------------------*/
using Index = Block::Index;
/// a TwoStageStochasticBlock whose scenarios and paths are given in C++
class TestTSSB : public TwoStageStochasticBlock {
public:
using TwoStageStochasticBlock::here_and_now_const;
/// adds a scenario holding n ColVariable x, minimising sum_j x_j if
/// linear, max_j x_j otherwise; returns x
std::vector< ColVariable > * add_scenario( Index n , bool linear ) {
auto b = new AbstractBlock( this );
auto x = new std::vector< ColVariable >( n );
b->add_static_variable( *x , "x" );
Function * f;
if( linear ) {
LinearFunction::v_coeff_pair cp;
for( auto & xj : *x )
cp.push_back( { & xj , 1.0 } );
f = new LinearFunction( std::move( cp ) );
}
else {
PolyhedralFunction::VarVector vx;
PolyhedralFunction::MultiVector A;
for( Index j = 0 ; j < n ; ++j ) {
vx.push_back( & ( *x )[ j ] );
A.push_back( PolyhedralFunction::RealVector( n , 0.0 ) );
A.back()[ j ] = 1.0;
}
f = new PolyhedralFunction( std::move( vx ) , std::move( A ) ,
PolyhedralFunction::RealVector( n , 0.0 ) );
}
b->set_objective( new FRealObjective( b , f ) , eNoMod );
v_Block.push_back( b );
++f_number_scenarios;
return( x );
}
/// the here-and-now Variable are the given elements of x in each scenario
void set_here_and_now( std::vector< ColVariable > * x0 ,
std::vector< Index > subset ) {
auto path = new AbstractPath( & ( *x0 )[ 0 ] , v_Block[ 0 ] );
path->set_last_node_subset( std::move( subset ) );
v_paths_to_static_vars.emplace_back( path );
}
/// the scenarios have the given probabilities
void set_probabilities( const std::vector< double > & p ) {
auto dss = new DiscreteScenarioSet();
dss->load_from_memory( std::vector< std::vector< double > >( p.size() ,
{ 0.0 } ) , p );
set_scenario_generator( dss );
}
};
/*--------------------------------------------------------------------------*/
/// a first-stage path on the non-contiguous subset { 0 , 2 } of x must tie,
/// return, read and write x[ 0 ] and x[ 2 ] of each scenario, and nothing else
bool test_path_subset( void ) {
const std::vector< Index > subset = { 0 , 2 };
const Index NS = 3;
TestTSSB tssb;
std::vector< std::vector< ColVariable > * > x;
for( Index s = 0 ; s < NS ; ++s )
x.push_back( tssb.add_scenario( 4 , true ) );
tssb.set_here_and_now( x[ 0 ] , subset );
tssb.generate_abstract_variables();
tssb.generate_abstract_constraints();
// the first-stage Variable are x[ 0 ] and x[ 2 ] of scenario 0
auto fsv = tssb.get_first_stage_variables();
if( fsv.size() != subset.size() )
return( false );
for( Index j = 0 ; j < subset.size() ; ++j )
if( fsv[ j ] != & ( *x[ 0 ] )[ subset[ j ] ] )
return( false );
// the j-th non-anticipativity constraint of each pair of scenarios ties
// x[ subset[ j ] ] of the two, and nothing else
const auto & hnc = tssb.here_and_now_const;
if( ( hnc.shape()[ 0 ] != NS - 1 ) || ( hnc.shape()[ 1 ] != 1 ) )
return( false );
std::set< Variable * > tied;
for( Index t = 0 ; t < NS - 1 ; ++t ) {
if( hnc[ t ][ 0 ].size() != subset.size() )
return( false );
for( Index j = 0 ; j < subset.size() ; ++j ) {
auto lf = dynamic_cast< LinearFunction * >(
hnc[ t ][ 0 ][ j ].get_function() );
if( ( ! lf ) || ( lf->get_num_active_var() != 2 ) )
return( false );
for( Index k = 0 ; k < 2 ; ++k ) {
auto v = lf->get_active_var( k );
bool ok = false;
for( Index s = 0 ; s < NS ; ++s )
ok |= ( v == & ( *x[ s ] )[ subset[ j ] ] );
if( ! ok )
return( false );
tied.insert( v );
}
}
}
if( tied.size() != NS * subset.size() )
return( false );
// the Solution reads the values of x[ 0 ] and x[ 2 ] of scenario 0 ...
for( Index j = 0 ; j < 4 ; ++j )
( *x[ 0 ] )[ j ].set_value( j + 1 );
const char * fn = "TSSB_test_solution.nc4";
bool ok = true;
{
netCDF::NcFile file( fn , netCDF::NcFile::replace );
auto sol = tssb.get_Solution();
auto read = file.addGroup( "Read" );
sol->serialize( read );
delete sol;
std::vector< double > han( subset.size() );
read.getVar( "HereAndNow" ).getVar( han.data() );
ok = ( han[ 0 ] == 1 ) && ( han[ 1 ] == 3 );
// ... and writes them back there
auto write = file.addGroup( "Write" );
auto dim = write.addDim( "TotalNumberHereAndNow" , subset.size() );
std::vector< double > val = { 10 , 30 };
write.addVar( "HereAndNow" , netCDF::NcDouble() , dim ).putVar(
val.data() );
TwoStageStochasticBlockSolution wsol;
wsol.deserialize( write );
for( auto & xj : *x[ 0 ] )
xj.set_value( 0 );
wsol.write( & tssb );
const std::vector< double > exp = { 10 , 0 , 30 , 0 };
for( Index j = 0 ; j < 4 ; ++j )
ok &= ( ( *x[ 0 ] )[ j ].get_value() == exp[ j ] );
}
std::remove( fn );
return( ok );
}
/*--------------------------------------------------------------------------*/
/// an Objective that cannot be weighted by the probability of its scenario
/// must throw rather than count with weight 1, while a linear one is weighted
/// and a probability of 1 needs no weighting
bool test_unweighted_objective( void ) {
// a linear Objective is weighted
{
TestTSSB tssb;
tssb.add_scenario( 2 , true );
tssb.add_scenario( 2 , true );
tssb.set_probabilities( { 0.5 , 0.5 } );
tssb.generate_objective();
for( Index s = 0 ; s < 2 ; ++s ) {
auto obj = static_cast< FRealObjective * >(
tssb.get_sub_Block( s )->get_objective() );
auto lf = static_cast< LinearFunction * >( obj->get_function() );
if( lf->get_coefficient( 0 ) != 0.5 )
return( false );
}
}
// a polyhedral one cannot be, with a probability that is not 1 ...
{
TestTSSB tssb;
tssb.add_scenario( 2 , false );
tssb.add_scenario( 2 , false );
tssb.set_probabilities( { 0.5 , 0.5 } );
bool thrown = false;
try { tssb.generate_objective(); }
catch( const std::invalid_argument & ) { thrown = true; }
if( ! thrown )
return( false );
}
// ... while with a probability of 1 there is nothing to weight
{
TestTSSB tssb;
tssb.add_scenario( 2 , false );
tssb.set_probabilities( { 1.0 } );
try { tssb.generate_objective(); }
catch( const std::exception & ) { return( false ); }
}
return( true );
}
/*--------------------------------------------------------------------------*/
/// a first-stage path to an element that does not exist, here past the end
/// of x, must throw rather than tie a Variable that is not there
bool test_path_past_end( void ) {
TestTSSB tssb;
auto x0 = tssb.add_scenario( 4 , true );
tssb.add_scenario( 4 , true );
tssb.set_here_and_now( x0 , { 0 , 4 } );
tssb.generate_abstract_variables();
try { tssb.generate_abstract_constraints(); }
catch( const std::invalid_argument & ) { return( true ); }
return( false );
}
/*--------------------------------------------------------------------------*/
/*--------------------------------- MAIN -----------------------------------*/
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
// override the default terminate handler to print the exception message
std::set_terminate( smspp_terminate );
cout << "=== TwoStageStochasticBlock Factory Test ===" << endl << endl;
// Test 1: Direct construction
cout << "Test 1: Direct construction...";
auto * tssb1 = new TwoStageStochasticBlock();
if( tssb1 ) {
cout << " SUCCESS" << endl;
delete tssb1;
} else {
cout << " FAILED" << endl;
return( 1 );
}
// Test 2: Factory registration check
cout << "Test 2: Factory registration...";
try {
auto * tssb2 = dynamic_cast< TwoStageStochasticBlock * >(
Block::new_Block( "TwoStageStochasticBlock" , nullptr ) );
if( tssb2 ) {
cout << " SUCCESS" << endl;
delete tssb2;
} else {
cout << " FAILED: wrong type" << endl;
return( 1 );
}
} catch( const exception & e ) {
cout << " FAILED: " << e.what() << endl;
return( 1 );
}
// Test 3: first-stage path on a non-contiguous subset
cout << "Test 3: first-stage path on a subset...";
if( test_path_subset() )
cout << " SUCCESS" << endl;
else {
cout << " FAILED" << endl;
return( 1 );
}
// Test 4: Objective that cannot be weighted
cout << "Test 4: Objective that cannot be weighted...";
if( test_unweighted_objective() )
cout << " SUCCESS" << endl;
else {
cout << " FAILED" << endl;
return( 1 );
}
// Test 5: first-stage path past the end of its group
cout << "Test 5: first-stage path past the end...";
if( test_path_past_end() )
cout << " SUCCESS" << endl;
else {
cout << " FAILED" << endl;
return( 1 );
}
// Test 6: Load from nc4 file if provided
if( argc > 1 ) {
cout << "Test 6: Loading from " << argv[ 1 ] << "...";
try {
netCDF::NcFile dataFile( argv[ 1 ] , netCDF::NcFile::read );
auto * block = Block::new_Block( dataFile , nullptr );
if( block ) {
auto * tssb3 = dynamic_cast< TwoStageStochasticBlock * >( block );
if( tssb3 ) {
cout << " SUCCESS (scenarios: " << tssb3->get_number_scenarios() << ")" << endl;
} else {
cout << " FAILED: not a TwoStageStochasticBlock" << endl;
}
delete block;
} else {
cout << " FAILED: new_Block returned nullptr" << endl;
return( 1 );
}
} catch( const exception & e ) {
cout << " FAILED: " << e.what() << endl;
return( 1 );
}
}
cout << endl << "=== All tests passed ===" << endl;
return( 0 );
}
/*--------------------------------------------------------------------------*/
/*--------------------------- End File test.cpp ----------------------------*/
/*--------------------------------------------------------------------------*/