-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
1455 lines (1166 loc) · 47.4 KB
/
Copy pathtest.cpp
File metadata and controls
1455 lines (1166 loc) · 47.4 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------*/
/*------------------------------ File test.cpp -----------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
*
* This is a convenient tool for solving the investment problem defined by an
* InvestmentBlock. The description of the InvestmentBlock must be given in a
* netCDF file. This tool can be executed as follows:
*
* ./test [-s] [-r] [-B FILE] [-p PATH] [-c PATH] [-x FILE ]
* -S FILE <nc4-file>
*
* The only mandatory arguments are the netCDF file containing the description
* of the InvestmentBlock and the solver configuration file indicated by the
* -S option. This netCDF file can be either a BlockFile or a ProbFile. The
* BlockFile can contain any number of child groups, each one describing an
* InvestmentBlock. Every InvestmentBlock is then solved. The ProbFile can
* also contain any number of child groups, each one having the description of
* an InvestmentBlock alongside the description of a BlockConfig and a
* BlockSolverConfig for the InvestmentBlock. Also in this case, every
* InvestmentBlock is solved.
*
* The -c option specifies the prefix to the paths to all configuration
* files. This means that if PATH is the value passed to the -c option, then
* the name (or path) to each configuration file will be prepended by
* PATH. The -p option specifies the prefix to the paths to all files
* specified by the attribute "filename" in the input netCDF file.
*
* It is possible to provide an initial point (initial solution or initial
* investment) through the -x option. This option must be followed by a file
* containing the initial point. If there are N assets subject to investment,
* then this file must contain N numbers, where the i-th number is the initial
* value for the investment in the i-th asset. If this option is not used,
* then the initial value x_i for the investment in the i-th asset is
* determined as follows. If the lower bound l_i on the i-th investment is
* finite, then x_i = l_i. Otherwise, if the upper bound u_i on the i-th
* investment is finite, then x_i = u_i. Otherwise, if both bounds are not
* finite, then x_i = 0.
*
* The -r option indicates that the integrality constraints over the variables
* must be relaxed.
*
* To simulate a given investment, i.e., to compute the investment function at
* a given point, the -s option must be used. The investment to be simulated
* is given by the initial point as described above: a given point provided by
* the -x option or the default initial point.
*
* The -B and -S options are only considered if the given netCDF file is a
* BlockFile. The -B option specifies a BlockConfig file to be applied to
* every InvestmentBlock; while the -S option specifies a BlockSolverConfig
* file for every InvestmentBlock. If the -B option is not provided when the
* given netCDF file is a BlockFile, then a default configuration is
* considered.
*
* \author Rafael Durbano Lobato \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Rafael Durbano Lobato, Antonio Frangioni, Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*-------------------------------- MACROS ----------------------------------*/
/*--------------------------------------------------------------------------*/
#define LOG_LEVEL 2
// -1 = no log at all, not even pass/fail
// 0 = only pass/fail
// 1 = result of each test
// 2 = + solver log
// 3 = reserved
// 4 = reserved
#if( LOG_LEVEL >= 1 )
#define LOG1( x ) std::cout << x
#define CLOG1( y , x ) if( y ) std::cout << x
#if( LOG_LEVEL >= 2 )
#define LOG_ON_COUT 1
#endif
#else
#define LOG1( x )
#define CLOG1( y , x )
#endif
// USECOLORS / RED / GREEN: in common_utils.h
#include "common_utils.h"
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <getopt.h>
#include <filesystem>
#include <cerrno>
#include <cstdlib>
#include <exception>
#include <typeinfo>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <map>
#include <iomanip>
#include <iostream>
#include <queue>
#include <chrono>
#include <cmath>
#include <limits>
#include <list>
#include <Block.h>
#include <BlockSolverConfig.h>
#include <CDASolver.h>
#include <Solution.h>
#include <BatteryUnitBlock.h>
#include <BendersBlock.h>
#include <HydroSystemUnitBlock.h>
#include <IntermittentUnitBlock.h>
#include <NetworkBlock.h>
#include <SDDPBlock.h>
#include <StochasticBlock.h>
#include <SDDPSolver.h>
#include <SlackUnitBlock.h>
#include <ThermalUnitBlock.h>
#include <UCBlock.h>
#include "InvestmentBlock.h"
#include "InvestmentFunction.h"
#ifdef USE_MPI
#include <boost/mpi/environment.hpp>
#endif
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------- Investment-local CLI extensions ----------------------*/
/*--------------------------------------------------------------------------*/
// Globals + getopt extensions that go beyond the test baseline in
// tests/common_utils.h. Kept local because they replicate the tools/
// state-save / solution-I/O machinery, which is not relevant to other tests.
std::string state_in_file; ///< State to be loaded into the Solver (-b)
std::string state_out_file; ///< final State of the Solver (-a)
std::string sol_input; ///< filename of input Solution (-I)
std::string sol_output; ///< filename of output Solution (-O)
std::string sol_cfg_file; ///< filename of output Solution Config (-C)
bool output_solution = false; ///< true if solution has to be output (-o)
bool writeprob = false; ///< if the problem should be written back (-n)
/// parse the current optarg as a long int, returning -1 on parse error
inline long get_long_option( char * end = nullptr )
{
errno = 0;
long option = std::strtol( optarg , &end , 10 );
if( ( ! optarg ) || ( ( option = std::strtol( optarg , &end , 10 ) ) ,
( errno || ( end && *end ) ) ) )
option = -1;
return( option );
}
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
void get_initial_Solution( Block * block )
{
if( sol_input.empty() )
return;
if( auto initsol = Solution::deserialize( sol_input ) ) {
initsol->write( block );
delete initsol;
}
else
std::cout << "Warning: input Solution " << sol_input << " invalid"
<< std::endl;
}
/*--------------------------------------------------------------------------*/
void get_initial_State( Solver * solver )
{
if( state_in_file.empty() )
return;
try {
auto state = State::deserialize( state_in_file );
solver->put_State( *state );
delete state;
}
catch( netCDF::exceptions::NcException & ) {
std::cout << "Warning: State file " << state_in_file
<< " could not be loaded" << std::endl;
}
catch( const std::exception & e ) {
std::cout << "Warning: error " << e.what()
<< " occurred while loading the Solver State" << std::endl;
}
}
/*--------------------------------------------------------------------------*/
void write_final_Solution( Block * block , Configuration * cfg = nullptr ,
bool replace = false )
{
if( sol_output.empty() )
return;
Configuration * outsolcfg = cfg;
if( ( ! outsolcfg ) && ( ! sol_cfg_file.empty() ) )
if( ! ( outsolcfg = Configuration::deserialize(
resolve_with_prefix( conf_prefix , sol_cfg_file ) ) ) )
std::cout << "Warning: output Solution Configuration "
<< sol_cfg_file << " invalid" << std::endl;
if( auto sol = block->get_Solution( outsolcfg , false ) ) {
sol->serialize( sol_output , replace );
delete sol;
}
else
std::cout << "Warning: output Solution empty" << std::endl;
if( ! cfg )
delete outsolcfg;
}
/*--------------------------------------------------------------------------*/
void write_final_State( Solver * solver , bool replace = false )
{
if( state_out_file.empty() )
return;
try {
solver->serialize_State( state_out_file , replace );
}
catch( netCDF::exceptions::NcException & ) {
std::cout << "Warning: State file " << state_out_file
<< " could not be opened" << std::endl;
}
catch( const std::exception & e ) {
std::cout << "Warning: error " << e.what()
<< " occurred while saving the Solver State" << std::endl;
}
}
/*--------------------------------------------------------------------------*/
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------------------------------------------------*/
bool AllPassed = true;
std::string cuts_filename{};
std::string initial_point_filename{};
// State to be loaded into the InvestmentBlock Solver
std::string solver_state_input_filename{};
// Prefix to the name of the file that will store the State of the
// InvestmentBlock Solver
std::string solver_state_output_filename{};
long num_sub_blocks_per_stage = 1;
bool relax_integrality = false;
bool simulate_investment = false;
bool single_scenario = false;
// Since BundleSolver cannot currently handle general bounds on the variables
// of the form l <= x <= u, these constraints must be reformulated by
// replacing them by 0 <= x <= u - l.
const bool reformulate_variable_bounds = true;
// This variable indicates whether negative prices may occur
const bool negative_prices = false;
// It indicates whether the investment function is based on simulation only
// (true) or SDDP (false).
bool simulation_based_function = true;
std::vector< double > initial_point;
/*--------------------------------------------------------------------------*/
const std::string my_short_opts = "l:n:rso:x:";
const std::vector< option > my_long_opts = {
{ "load-cuts" , required_argument , nullptr , 'l' } ,
{ "num-blocks" , required_argument , nullptr , 'n' } ,
{ "relax" , no_argument , nullptr , 'r' } ,
{ "simulate" , no_argument , nullptr , 's' } ,
{ "ref-objective" , required_argument , nullptr , 'o' } ,
{ "initial-investment" , required_argument , nullptr , 'x' }
};
const std::string my_help =
" -l, --load-cuts <file> load cuts from a file\n"
" -n, --num-blocks <number> number of sub-Blocks per stage\n"
" -r, --relax relax integer variables\n"
" -s, --simulate simulate the given investment\n"
" -o, --ref-objective <value> compare the first solver to a reference\n"
" -x, --initial-investment <file> initial investment\n";
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
static double get_solver_objective_value( Solver * solver ) {
if( solver->has_var_solution() )
return( solver->get_var_value() );
return( solver->get_lb() );
}
static bool test_investment_solvers( InvestmentBlock * investment_block ) {
try {
auto investment_function = static_cast< InvestmentFunction * >(
investment_block->get_function() );
auto & solvers = investment_block->get_registered_solvers();
if( solvers.empty() )
throw( std::logic_error( "No solver has been registered." ) );
if( sol_verbose )
for( auto solver : solvers )
if( solver )
solver->set_log( &std::cout );
// Output the variable and function values at each iteration
investment_function->set_par( InvestmentFunction::strOutputFilename ,
"investment_candidates.txt" );
// set initial Solution, if provided - - - - - - - - - - - - - - - - - - - -
get_initial_Solution( investment_block );
// load the given State, if provided - - - - - - - - - - - - - - - - - - - -
auto first_solver = solvers.front();
get_initial_State( first_solver );
#if( LOG_LEVEL >= 1 )
auto start = std::chrono::system_clock::now();
#endif
int rtrn1st = Solver::kOK;
if( ! dryrun )
rtrn1st = first_solver->compute();
#if( LOG_LEVEL >= 1 )
auto end = std::chrono::system_clock::now();
std::chrono::duration< double > elapsed = end - start;
double time1 = elapsed.count();
#endif
bool hs1st = ( ( ( rtrn1st >= Solver::kOK ) && ( rtrn1st < Solver::kError )
&& ( rtrn1st != Solver::kUnbounded )
&& ( rtrn1st != Solver::kInfeasible ) )
|| ( rtrn1st == Solver::kLowPrecision ) );
double fo1st = hs1st
? get_solver_objective_value( first_solver )
: -Inf< double >();
#if( LOG_LEVEL >= 1 )
long it1 = first_solver->get_elapsed_iterations();
#endif
bool all_passed = hs1st;
// build readings for every registered Solver (each an exact optimum read
// via get_solver_objective_value); Solver 0 was already solved above, the
// rest are solved here. The cross-check verdict and the uniform per-instance
// line, including the optional RefObjective, are produced by common_utils
std::vector< Solver * > S( solvers.begin() , solvers.end() );
const std::size_t M = S.size();
std::vector< double > times( M , 0.0 );
std::vector< std::string > toks( M );
std::vector< SolverReading > rd( M );
std::vector< bool > hsv( M , false );
std::vector< int > statusv( M , Solver::kOK );
auto tok = []( bool h , int rtrn , const SolverReading & r ) -> std::string {
if( h ) return( reading_token( r ) );
if( rtrn == Solver::kInfeasible ) return( "Unfeas" );
if( rtrn == Solver::kUnbounded ) return( "Unbounded" );
return( "Error!" );
};
times[ 0 ] = time1; hsv[ 0 ] = hs1st; statusv[ 0 ] = rtrn1st;
if( hs1st ) rd[ 0 ] = SolverReading::exact( fo1st , eps_of( 0 , S[ 0 ] ) );
toks[ 0 ] = tok( hs1st , rtrn1st , rd[ 0 ] );
for( std::size_t k = 1 ; k < M ; ++k ) {
auto st = std::chrono::system_clock::now();
if( ! dryrun )
statusv[ k ] = S[ k ]->compute();
auto en = std::chrono::system_clock::now();
times[ k ] = std::chrono::duration< double >( en - st ).count();
hsv[ k ] = ( ( ( statusv[ k ] >= Solver::kOK ) &&
( statusv[ k ] < Solver::kError ) &&
( statusv[ k ] != Solver::kUnbounded ) &&
( statusv[ k ] != Solver::kInfeasible ) )
|| ( statusv[ k ] == Solver::kLowPrecision ) );
if( hsv[ k ] )
rd[ k ] = SolverReading::exact( get_solver_objective_value( S[ k ] ) ,
eps_of( k , S[ k ] ) );
toks[ k ] = tok( hsv[ k ] , statusv[ k ] , rd[ k ] );
}
std::string verdict;
double diff;
all_passed = cross_check( rd , hsv , statusv , RefObjective , 1e-5 ,
verdict , diff );
print_instance_line( times , toks , RefObjective , verdict , diff );
(void) it1;
#if( LOG_LEVEL >= 0 )
if( all_passed )
std::cout << GREEN( All tests passed!! ) << std::endl;
else
std::cout << RED( Shit happened!! ) << std::endl;
#endif
// write final Solution, if required - - - - - - - - - - - - - - - - - - - -
write_final_Solution( investment_block );
// write final State, if required- - - - - - - - - - - - - - - - - - - - - -
write_final_State( first_solver );
if( output_solution ) { // display the solution
if( first_solver->has_var_solution() ) {
const auto solution_value = first_solver->get_var_value();
std::cout << "Solution value: " << std::setprecision( 20 )
<< solution_value << std::endl;
first_solver->get_var_solution();
std::cout << "Solution: " << std::endl;
const auto & variables = investment_block->get_variables();
const auto & var_lb = investment_block->get_variable_lower_bound();
const auto width = std::to_string( variables.size() ).size();
for( Index i = 0 ; i < variables.size() ; ++i ) {
auto value = variables[ i ].get_value();
if( reformulate_variable_bounds && ( i < var_lb.size() ) &&
( var_lb[ i ] > -Inf< double >() ) )
value += var_lb[ i ];
std::cout << std::setw( width ) << i << " " << value << std::endl;
}
}
else
std::cout << "No solution has been found" << std::endl;
}
return( all_passed );
}
catch( std::exception & e ) {
std::cerr << e.what() << std::endl;
throw;
}
}
/*--------------------------------------------------------------------------*/
void process_my_args( int argc , char ** argv ) {
exe = get_filename( argv[ 0 ] );
if( argc < 2 ) {
std::cout << exe << ": no input file\n"
<< "Try " << exe << "' --help' for more information.\n";
exit( 1 );
}
// Support the batch invocation:
// test <nc-file> <block-config> <solver-config> 0 <ref-objective>
//
// Semantics:
// - load the InvestmentBlock from <nc-file>
// - apply <block-config> as external BlockConfig
// - apply <solver-config> as external BlockSolverConfig
// - if a reference objective is provided, compare the first solver to it
//
// This branch is taken only when the first argument is not an option.
if( argv[ 1 ][ 0 ] != '-' ) {
filename = std::string( argv[ 1 ] );
if( argc >= 3 )
bconf_file = std::string( argv[ 2 ] );
if( argc >= 4 )
sconf_file = std::string( argv[ 3 ] );
if( argc >= 6 )
RefObjective = std::stod( argv[ 5 ] );
bconf_file = resolve_with_prefix( conf_prefix , bconf_file );
sconf_file = resolve_with_prefix( conf_prefix , sconf_file );
return;
}
while( true ) { // options
auto opt = getopt_long( argc , argv , short_opts.data() ,
long_opts.data() , nullptr );
if( opt == -1 ) break;
if( process_standard_arg( opt ) ) // if it is a standard one
continue; // next
switch( opt ) { // non-standard options
case 'l' : cuts_filename = std::string( optarg );
break;
case 'n' : {
num_sub_blocks_per_stage = get_long_option();
if( num_sub_blocks_per_stage <= 0 ) {
std::cout << "The number of sub-Blocks per stage must be a "
<< "positive integer." << std::endl;
exit( 1 );
}
break;
}
case 'r' : relax_integrality = true;
break;
case 's' : simulate_investment = true;
break;
case 'o' : RefObjective = std::stod( optarg );
break;
case 'x' : initial_point_filename = std::string( optarg );
break;
case '?' : // Unrecognized option
default : std::cout << "Try " << exe << "' --help' for more information"
<< std::endl;
exit( 1 );
}
} // end( while( true ) )
if( optind < argc ) // last argument == [InvestmentBlock] filename
filename = std::string( argv[ optind ] );
else {
std::cout << exe << ": no input file" << std::endl
<< "Try " << exe << "' --help' for more information" << std::endl;
exit( 1 );
}
bconf_file = resolve_with_prefix( conf_prefix , bconf_file );
sconf_file = resolve_with_prefix( conf_prefix , sconf_file );
} // end( process_my_args )
/*--------------------------------------------------------------------------*/
Block * get_uc_block( const SDDPBlock * sddp_block , Index stage ,
Index sub_block_index ) {
auto benders_block = static_cast< BendersBlock * >(
sddp_block->get_sub_Block( stage , sub_block_index )->get_inner_block() );
auto objective = static_cast< FRealObjective * >(
benders_block->get_objective() );
auto benders_function = static_cast< BendersBFunction * >(
objective->get_function() );
return( benders_function->get_inner_block() );
}
/*--------------------------------------------------------------------------*/
bool update_hydro_unit( Block * previous_block , Block * block ,
Index stage ) {
auto unit = dynamic_cast< HydroUnitBlock * >( block );
auto previous_unit = dynamic_cast< HydroUnitBlock * >( previous_block );
if( ( ! unit ) && ( ! previous_unit ) )
return( false );
if( ( ! unit ) || ( ! previous_unit ) )
throw( std::logic_error( "test: UCBlocks at stages " +
std::to_string( stage - 1 ) + " and " +
std::to_string( stage ) +
" do not have the same structure" ) );
auto number_generators = previous_unit->get_number_generators();
if( number_generators != unit->get_number_generators() )
throw( std::logic_error( "test: HydroUnitBlock at stage " +
std::to_string( stage - 1 ) + " has " +
std::to_string( number_generators ) +
", but corresponding HydroUnitBlock at stage " +
std::to_string( stage ) + " has " +
std::to_string( unit->get_number_generators() )
) );
const auto time_horizon = previous_unit->get_time_horizon();
std::vector< double > flow_rate( number_generators );
for( Index g = 0 ; g < number_generators ; ++g )
flow_rate[ g ] =
previous_unit->get_flow_rate( g , time_horizon - 1 )->get_value();
unit->set_initial_flow_rate( flow_rate.cbegin() );
return( true );
}
/*--------------------------------------------------------------------------*/
bool update_battery_unit( Block * previous_block , Block * block ,
Index stage ) {
auto unit = dynamic_cast< BatteryUnitBlock * >( block );
auto previous_unit = dynamic_cast< BatteryUnitBlock * >( previous_block );
if( ( ! unit ) && ( ! previous_unit ) )
return( false );
if( ( ! unit ) || ( ! previous_unit ) )
throw( std::logic_error( "test: UCBlocks at stages " +
std::to_string( stage - 1 ) +
" and " + std::to_string( stage ) +
" do not have the same structure" ) );
const auto time_horizon = previous_unit->get_time_horizon();
std::vector< double > initial_power_data = {
( previous_unit->get_active_power( 0 ) + time_horizon - 1 )->get_value()
};
unit->set_initial_power( initial_power_data.cbegin() );
std::vector< double > initial_storage_data = {
previous_unit->get_storage_level()[ time_horizon - 1 ].get_value()
};
unit->set_initial_storage( initial_storage_data.cbegin() );
return( true );
}
/*--------------------------------------------------------------------------*/
int compute_init_up_down_time( const SDDPBlock * sddp_block ,
ThermalUnitBlock * previous_unit ,
ThermalUnitBlock * unit , Index stage ,
Index sub_block_index ) {
auto time_horizon = previous_unit->get_time_horizon();
auto commitment = previous_unit->get_commitment( 0 ) + time_horizon - 1;
auto shutdown = previous_unit->get_shut_down( time_horizon - 1 );
if( shutdown && shutdown->get_value() >= 0.5 )
return( 0 );
int init_up_down_time = 0;
const bool on = commitment->get_value() >= 0.5;
if( on ) init_up_down_time = 1;
else init_up_down_time = -1;
AbstractPath path;
for( Index outer_t = 0 ; outer_t < stage ; ++outer_t ) {
for( Index t = 1 ; t < time_horizon ; ++t, --commitment ) {
if( std::abs( commitment->get_value() -
( commitment - 1 )->get_value() ) > 0.5 )
return( init_up_down_time );
if( on ) ++init_up_down_time;
else --init_up_down_time;
}
if( outer_t == stage - 1 )
break;
if( path.empty() ) {
auto uc_block = get_uc_block( sddp_block , stage , sub_block_index );
path.build( unit , uc_block );
}
auto previous_uc_block = get_uc_block( sddp_block , stage - outer_t - 2 ,
sub_block_index );
previous_unit = dynamic_cast< ThermalUnitBlock * >(
path.get_element< Block >( previous_uc_block ) );
time_horizon = previous_unit->get_time_horizon();
if( ! previous_unit )
throw( std::logic_error(
"sddp_solver::update_thermal_block: ThermalUnitBlock not found "
"at stage " + std::to_string( stage - outer_t - 2 ) ) );
commitment = previous_unit->get_commitment( 0 ) + time_horizon - 1;
if( on ) {
if( commitment->get_value() >= 0.5 ) ++init_up_down_time;
else break;
}
else {
if( commitment->get_value() < 0.5 ) --init_up_down_time;
else break;
}
}
return( init_up_down_time );
}
/*--------------------------------------------------------------------------*/
bool update_thermal_unit( const SDDPBlock * sddp_block ,
Block * previous_block , Block * block ,
Index stage , Index sub_block_index ) {
auto previous_unit = dynamic_cast< ThermalUnitBlock * >( previous_block );
auto unit = dynamic_cast< ThermalUnitBlock * >( block );
if( ! unit && ! previous_unit )
return( false );
if( ! unit || ! previous_unit )
throw( std::logic_error(
"test: UCBlocks at stages " + std::to_string( stage - 1 ) +
" and " + std::to_string( stage ) +
" do not have the same structure." ) );
if( single_scenario ) {
// The only way to update the initial up and down time is when there is a
// single scenario.
auto init_up_down_time = compute_init_up_down_time( sddp_block ,
previous_unit , unit , stage , sub_block_index );
std::vector< int > init_up_down_time_data = { init_up_down_time };
unit->set_init_updown_time( init_up_down_time_data.cbegin() );
}
const auto time_horizon = previous_unit->get_time_horizon();
std::vector< double > active_power_data = {
( previous_unit->get_active_power( 0 ) + time_horizon - 1 )->get_value()
};
unit->set_initial_power( active_power_data.cbegin() );
return( true );
}
/*--------------------------------------------------------------------------*/
void callback( SDDPBlock * sddp_block , Index stage , Index sub_block_index ) {
if( stage == 0 )
return;
auto previous_uc_block = get_uc_block( sddp_block , stage - 1 ,
sub_block_index );
auto uc_block = get_uc_block( sddp_block , stage , sub_block_index );
std::queue< Block * > blocks;
blocks.push( uc_block );
std::queue< Block * > previous_blocks;
previous_blocks.push( previous_uc_block );
while( ! blocks.empty() ) {
auto block = blocks.front();
blocks.pop();
auto previous_block = previous_blocks.front();
previous_blocks.pop();
auto n = block->get_number_nested_Blocks();
if( n != previous_block->get_number_nested_Blocks() )
throw( std::logic_error( "test: UCBlocks at stages " +
std::to_string( stage - 1 ) +
" and " + std::to_string( stage ) +
" do not have the same structure" ) );
for( decltype( n ) i = 0 ; i < n ; ++i ) {
blocks.push( block->get_nested_Block( i ) );
previous_blocks.push( previous_block->get_nested_Block( i ) );
}
if( ( ! update_hydro_unit( previous_block , block , stage ) ) &&
simulation_based_function ) {
// In SDDP, only the reservoir volumes (of the hydro units) are transmitted
// from one stage to the next. In simulation, on the other hand, data from
// thermal and battery units are also passed from one stage to the
// next. Thefore, initial states of thermal and battery units should only
// be updated when the simulation-based function is considered.
update_thermal_unit( sddp_block , previous_block , block , stage ,
sub_block_index )
|| update_battery_unit( previous_block , block , stage );
}
}
}
/*--------------------------------------------------------------------------*/
std::vector< double > get_default_initial_point( InvestmentBlock * block ) {
block->generate_abstract_constraints();
const auto & box_constraints = block->get_constraints();
std::vector< double > initial_point( box_constraints.size() );
for( Index i = 0 ; i < box_constraints.size() ; ++i )
if( box_constraints[ i ].get_lhs() > -Inf< double >() )
initial_point[ i ] = box_constraints[ i ].get_lhs();
else if( box_constraints[ i ].get_rhs() < Inf< double >() )
initial_point[ i ] = box_constraints[ i ].get_rhs();
else
initial_point[ i ] = 0;
return( initial_point );
}
/*--------------------------------------------------------------------------*/
std::vector< double > load_initial_point( void ) {
if( initial_point_filename.empty() )
return {};
std::ifstream file( initial_point_filename );
// Make sure the file is open
if( ! file.is_open() )
throw( std::runtime_error( "It was not possible to open the file " +
initial_point_filename ) );
std::vector< double > initial_point;
double component;
while( file >> component )
initial_point.push_back( component );
return( initial_point );
}
/*--------------------------------------------------------------------------*/
void set_initial_point( InvestmentBlock * investment_block ) {
// Generate the abstract variables so that we can set their values.
investment_block->generate_abstract_variables();
// Possibly load a given initial point.
initial_point = load_initial_point();
if( ! initial_point.empty() ) {
// An initial point has been provided.
const auto num_variables = investment_block->get_number_variables();
if( initial_point.size() != num_variables )
throw( std::logic_error( "The initial point has size " +
std::to_string( initial_point.size() ) + ", but "
"there are " + std::to_string( num_variables ) +
" variables." ) );
if( reformulate_variable_bounds ) {
// If variable bounds have been reformulated, the initial point must be
// adjusted.
const auto & var_lower_bound =
investment_block->get_variable_lower_bound();
for( Index i = 0 ; i < initial_point.size() ; ++i ) {
if( ( i < var_lower_bound.size() ) &&
( var_lower_bound[ i ] > -Inf< double >() ) )
initial_point[ i ] -= var_lower_bound[ i ];
}
}
}
else // Since no initial point has been provided, we use the default one.
initial_point = get_default_initial_point( investment_block );
// Finally, set the initial point.
investment_block->set_variable_values( initial_point );
}
/*--------------------------------------------------------------------------*/
void load_cuts( SDDPBlock * sddp_block ) {
if( cuts_filename.empty() )
return;
std::ifstream cuts_file( cuts_filename );
// Make sure the file is open
if( ! cuts_file.is_open() )
throw( std::runtime_error( "It was not possible to open the file " +
cuts_filename ) );
const auto time_horizon = sddp_block->get_time_horizon();
std::vector< PolyhedralFunction::MultiVector > A( time_horizon ,
PolyhedralFunction::MultiVector
{} );
std::vector< PolyhedralFunction::RealVector > b( time_horizon ,
PolyhedralFunction::RealVector
{} );
std::string line;
if( cuts_file.good() )
// Skip the first line containing the header
std::getline( cuts_file , line );
int line_number = 0;
// Read the cuts
while( std::getline( cuts_file , line ) ) {
++line_number;
std::stringstream line_stream( line );
// Try to read the stage
Index stage;
if( ! ( line_stream >> stage ) )
break;
if( stage >= time_horizon )
throw( std::logic_error( "File " + cuts_filename + "contains invalid"
" stage " + std::to_string( stage ) ) );
if( line_stream.peek() != ',' )
throw( std::logic_error( "File " + cuts_filename +
" has an invalid format." ) );
line_stream.ignore();
// Read the cut
const auto polyhedral_function =
sddp_block->get_polyhedral_function( stage );
const auto num_active_var = polyhedral_function->get_num_active_var();
PolyhedralFunction::RealVector a( num_active_var );
Index i = 0;
double value;
while( line_stream >> value ) {
if( i > num_active_var )
throw( std::logic_error( "File " + cuts_filename + " contains an invalid"
" cut at line " + std::to_string( line_number )
) );
if( i < num_active_var )
a[ i ] = value;
else
b[ stage ].push_back( value );
++i;
if( line_stream.peek() == ',' )
line_stream.ignore();
}
if( i < num_active_var )
throw( std::logic_error( "File " + cuts_filename + " contains an invalid"
" cut at line " + std::to_string( line_number )
) );
A[ stage ].push_back( a );
}
cuts_file.close();
// Now, add the cuts to all PolyhedralFunctions
for( Index stage = 0 ; stage < time_horizon ; ++stage ) {
for( Index sub_block_index = 0 ;
sub_block_index < sddp_block->get_num_sub_blocks_per_stage() ;
++sub_block_index ) {
if( b[ stage ].empty() )
continue; // no cut for this stage
// We assume that there is only one PolyhedralFunction per stage
auto polyhedral_function =
sddp_block->get_polyhedral_function( stage , 0 , sub_block_index );
// Copy the A matrix for this stage so that it can be moved
auto A_stage = A[ stage ];
polyhedral_function->add_rows( std::move( A_stage ) , b[ stage ] );
}
}
}
/*--------------------------------------------------------------------------*/
void configure_Blocks( UCBlock * ucblock , bool relax_binary_variables ,
bool add_reserve_variables_to_objective ) {
std::queue< Block * > blocks;
blocks.push( ucblock );
while( ! blocks.empty() ) {
auto block = blocks.front();
blocks.pop();
auto n = block->get_number_nested_Blocks();
for( decltype( n ) i = 0 ; i < n ; ++i ) {
blocks.push( block->get_nested_Block( i ) );
}
int var_type = 0;
if( relax_binary_variables ) var_type = 1;