-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_AbstractBlock.cpp
More file actions
1664 lines (1421 loc) · 58.7 KB
/
Copy pathtests_AbstractBlock.cpp
File metadata and controls
1664 lines (1421 loc) · 58.7 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
* Unit tests for Block and AbstractBlock.
*
* \author Niccolo' Iardella \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Niccolo' Iardella, Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "AbstractBlock.h"
#include "FRowConstraint.h"
#include "ColVariable.h"
#include "ColVariableSolution.h"
#include "BlockInspection.h"
#include "FRealObjective.h"
#include "DQuadFunction.h"
#include "LinearFunction.h"
#include "OneVarConstraint.h"
#include <netcdf>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <vector>
// last, so that the headers above are read as the library was compiled
#include "TestAssert.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
void TearDown( AbstractBlock * block )
{
block->reset_static_constraints();
assert( block->get_static_constraint_groups().empty() );
block->reset_static_variables();
assert( block->get_static_variable_groups().empty() );
block->reset_dynamic_constraints();
assert( block->get_dynamic_constraint_groups().empty() );
block->reset_dynamic_variables();
assert( block->get_dynamic_variable_groups().empty() );
delete block;
}
/*--------------------------------------------------------------------------*/
void runAllTests()
{
// test Sets_UpperBound
double ub = 1.0;
AbstractBlock * block = new AbstractBlock(); // SetUp
assert( block->get_valid_upper_bound() == Inf< double >() );
block->set_valid_upper_bound( ub , true );
assert( block->get_valid_upper_bound( true ) == ub );
TearDown( block ); // TearDown
// test Sets_LowerBound
double lb = -1.0;
block = new AbstractBlock(); // SetUp
assert( block->get_valid_lower_bound() == -Inf< double >() );
block->set_valid_lower_bound( lb , true );
assert( block->get_valid_lower_bound( true ) == lb );
TearDown( block ); // TearDown
// test Adds_StaticConstraints_One
block = new AbstractBlock(); // SetUp
auto c = new FRowConstraint();
block->add_static_constraint( *c );
assert( block->get_static_constraint< FRowConstraint >( 0 ) == c );
assert(
block->get_static_constraint< FRowConstraint >( 0 )->get_Block() == block );
TearDown( block ); // TearDown
// test Adds_StaticConstraints_Vector: the group is the vector, and each of
// its elements knows the Block and where it sits in the group
block = new AbstractBlock(); // SetUp
auto v_c = new std::vector< FRowConstraint >( 5 );
block->add_static_constraint( *v_c , "v_c" );
assert( block->get_static_constraint_v< FRowConstraint >( 0 ) == v_c );
assert( block->get_number_static_constraints() == 1 );
assert( block->get_static_constraint_groups()[ 0 ]->get_num_elements() == 5 );
assert( block->get_static_constraint_groups()[ 0 ]->get_rank() == 1 );
for( Block::Index i = 0 ; i < v_c->size() ; ++i ) {
assert( ( *v_c )[ i ].get_Block() == block );
assert( inspection::get_element< FRowConstraint >( block , true , 0 , i )
== & ( *v_c )[ i ] );
assert( std::get< 2 >( inspection::get_element_index( & ( *v_c )[ i ] ) )
== i );
}
Constraint::clear( *v_c );
TearDown( block ); // TearDown
// test Adds_StaticConstraints_MultiArray
block = new AbstractBlock(); // SetUp
auto m_c = new boost::multi_array< FRowConstraint , 2 >;
m_c->resize( boost::extents[ 2 ][ 2 ] );
block->add_static_constraint( *m_c );
assert( ( block->get_static_constraint< FRowConstraint , 2 >( 0 ) ) == m_c );
for( auto i = m_c->data() ; i < ( m_c->data() + m_c->num_elements() ) ; ++i )
assert( i->get_Block() == block );
Constraint::clear( *m_c );
TearDown( block ); // TearDown
// test Adds_StaticVariables_One
block = new AbstractBlock(); // SetUp
auto v = new ColVariable();
block->add_static_variable( *v );
assert( block->get_static_variable< ColVariable >( 0 ) == v );
assert( block->get_static_variable< ColVariable >( 0 )->get_Block() == block );
TearDown( block ); // TearDown
// test Adds_StaticVariables_Vector
block = new AbstractBlock(); // SetUp
auto v_v = new std::vector< ColVariable >( 5 );
block->add_static_variable( *v_v );
assert( block->get_static_variable_v< ColVariable >( 0 ) == v_v );
for( const auto & i : *v_v )
assert( i.get_Block() == block );
assert( ColVariable::is_feasible( *v_v ) );
TearDown( block ); // TearDown
// test Adds_StaticVariables_MultiArray
block = new AbstractBlock(); // SetUp
auto m_v = new boost::multi_array< ColVariable , 2 >;
m_v->resize( boost::extents[ 2 ][ 2 ] );
block->add_static_variable( *m_v );
assert( ( block->get_static_variable< ColVariable , 2 >( 0 ) ) == m_v );
for( auto i = m_v->data() ; i < ( m_v->data() + m_v->num_elements() ) ; ++i )
assert( i->get_Block() == block );
assert( ColVariable::is_feasible( *m_v ) );
TearDown( block ); // TearDown
// test Adds_DynamicConstraints_List
block = new AbstractBlock(); // SetUp
auto l_c = new std::list< FRowConstraint >( 5 );
block->add_dynamic_constraint( *l_c );
assert( block->get_dynamic_constraint< FRowConstraint >( 0 ) == l_c );
for( const auto & i : *l_c )
assert( i.get_Block() == block );
Constraint::clear( *l_c );
TearDown( block ); // TearDown
// test Adds_DynamicConstraints_Vector
block = new AbstractBlock(); // SetUp
auto v_l_c = new std::vector< std::list< FRowConstraint > >( 5 );
for( auto & i : *v_l_c )
i.resize( 3 );
block->add_dynamic_constraint( *v_l_c );
assert( block->get_dynamic_constraint_v< FRowConstraint >( 0 ) == v_l_c );
for( auto & i : *v_l_c )
for( auto & j : i )
assert( j.get_Block() == block );
Constraint::clear( *v_l_c );
TearDown( block ); // TearDown
// test Adds_DynamicConstraints_MultiArray
block = new AbstractBlock(); // SetUp
auto m_l_c = new boost::multi_array< std::list< FRowConstraint > , 2 >;
m_l_c->resize( boost::extents[ 2 ][ 2 ] );
for( auto i = m_l_c->data() ;
i < ( m_l_c->data() + m_l_c->num_elements() ) ; ++i )
i->resize( 3 );
block->add_dynamic_constraint( *m_l_c );
assert(
( block->get_dynamic_constraint< FRowConstraint , 2 >( 0 ) ) == m_l_c );
for( auto i = m_l_c->data() ;
i < ( m_l_c->data() + m_l_c->num_elements() ) ; ++i )
for( auto & j : *i )
assert( j.get_Block() == block );
Constraint::clear( *m_l_c );
TearDown( block ); // TearDown
// test Adds_DynamicVariables_List
block = new AbstractBlock(); // SetUp
auto l_v = new std::list< ColVariable >( 5 );
block->add_dynamic_variable( *l_v );
assert( block->get_dynamic_variable< ColVariable >( 0 ) == l_v );
for( const auto & i : *l_v )
assert( i.get_Block() == block );
assert( ColVariable::is_feasible( *l_v ) );
TearDown( block ); // TearDown
// test Adds_DynamicVariables_Vector
block = new AbstractBlock(); // SetUp
auto v_l_v = new std::vector< std::list< ColVariable > >( 5 );
for( auto & i : *v_l_v )
i.resize( 3 );
block->add_dynamic_variable( *v_l_v );
assert( block->get_dynamic_variable_v< ColVariable >( 0 ) == v_l_v );
for( auto & i : *v_l_v )
for( auto & j : i )
assert( j.get_Block() == block );
assert( ColVariable::is_feasible( *v_l_v ) );
TearDown( block ); // TearDown
// test Adds_DynamicVariables_MultiArray
block = new AbstractBlock(); // SetUp
auto m_l_v = new boost::multi_array< std::list< ColVariable > , 2 >;
m_l_v->resize( boost::extents[ 2 ][ 2 ] );
for( auto i = m_l_v->data() ;
i < ( m_l_v->data() + m_l_v->num_elements() ) ; ++i )
i->resize( 3 );
block->add_dynamic_variable( *m_l_v );
assert( ( block->get_dynamic_variable< ColVariable , 2 >( 0 ) ) == m_l_v );
for( auto i = m_l_v->data() ;
i < ( m_l_v->data() + m_l_v->num_elements() ) ; ++i )
for( auto & j : *i )
assert( j.get_Block() == block );
assert( ColVariable::is_feasible( *m_l_v ) );
TearDown( block ); // TearDown
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* A Solution written to a netCDF group and read back from it holds what it
* held: the values of the static Variable, group by group, and those of the
* dynamic ones, group by group and cell by cell. */
static void test_solution_round_trip( void )
{
AbstractBlock block;
std::vector< ColVariable > v( 3 );
for( int i = 0 ; i < 3 ; ++i )
v[ i ].set_value( 1.0 + i );
block.add_static_variable( v , "x" );
auto cells = new std::vector< std::list< ColVariable > >( 2 );
( *cells )[ 0 ].resize( 2 );
( *cells )[ 1 ].resize( 1 );
double k = 10;
for( auto & cell : *cells )
for( auto & element : cell )
element.set_value( k++ );
block.add_dynamic_variable( *cells , "y" );
ColVariableSolution written;
written.read( & block );
const char * const name = "tests_AbstractBlock_solution.nc4";
{
netCDF::NcFile file( name , netCDF::NcFile::replace );
auto g = file.addGroup( "Solution" );
written.serialize( g );
}
ColVariableSolution read;
{
netCDF::NcFile file( name , netCDF::NcFile::read );
read.deserialize( file.getGroup( "Solution" ) );
}
std::remove( name );
assert( read.get_static_variable_values() ==
written.get_static_variable_values() );
assert( read.get_dynamic_variable_values() ==
written.get_dynamic_variable_values() );
// the shape survives too: one group of 3, and one of 2 cells of 2 and 1
assert( read.get_static_variable_values().size() == 1 );
assert( read.get_static_variable_values()[ 0 ].size() == 3 );
assert( read.get_dynamic_variable_values().size() == 1 );
assert( read.get_dynamic_variable_values()[ 0 ].size() == 2 );
assert( read.get_dynamic_variable_values()[ 0 ][ 0 ].size() == 2 );
assert( read.get_dynamic_variable_values()[ 0 ][ 1 ].size() == 1 );
delete cells;
}
/*--------------------------------------------------------------------------*/
/* A Block written as an LP file and read back from it is the same model: the
* same rows, the same bounds, the same columns declared integer. What is not
* the same is the way it is grouped, since an LP file has no notion of
* groups and read_lp() builds one group of columns and one of rows. */
static void test_lp_round_trip( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 3 );
( *cols )[ 0 ].set_type( ColVariable::kNatural );
( *cols )[ 1 ].is_positive( true , eNoMod );
block.add_static_variable( *cols , "x" );
auto rows = new std::vector< FRowConstraint >( 2 );
{
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 0 ] , 1.0 } );
p.push_back( { & ( *cols )[ 1 ] , 2.0 } );
( *rows )[ 0 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 0 ].set_lhs( 1 ); // both sides finite: the LP file says it
( *rows )[ 0 ].set_rhs( 4 ); // in two rows, since it has no two-sided one
}
{
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 2 ] , -3.0 } );
( *rows )[ 1 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 1 ].set_lhs( - Inf< double >() );
( *rows )[ 1 ].set_rhs( 7 );
}
block.add_static_constraint( *rows , "r" );
LinearFunction::v_coeff_pair o;
o.push_back( { & ( *cols )[ 0 ] , 5.0 } );
o.push_back( { & ( *cols )[ 2 ] , -1.0 } );
auto obj = new FRealObjective( & block , new LinearFunction( std::move( o ) ) );
obj->set_sense( Objective::eMin , eNoMod );
block.set_objective( obj , eNoMod );
std::ostringstream written;
block.write_lp( written );
// the file says what the model is: the three columns are named after the
// group they sit in, the row with both sides is there twice, and the one
// integer column is in the Generals section
const auto lp = written.str();
assert( lp.find( "Minimize" ) != std::string::npos );
assert( lp.find( "x_0" ) != std::string::npos );
assert( lp.find( "r_0_up" ) != std::string::npos );
assert( lp.find( "r_0_lo" ) != std::string::npos );
assert( lp.find( "Generals" ) != std::string::npos );
assert( lp.find( "End" ) != std::string::npos );
// and what is written is read back into the same model
const char * const name = "tests_AbstractBlock_model.nc4";
block.Block::serialize( name , eBlockFile );
auto read = dynamic_cast< AbstractBlock * >( Block::deserialize( name ) );
std::remove( name );
assert( read );
std::ostringstream again;
read->write_lp( again );
delete read;
// the LP of the copy has the same rows and the same bounds; the names of
// the groups are those read_lp() gives, so the two files are not compared
// character by character
const auto lp2 = again.str();
assert( lp2.find( "Minimize" ) != std::string::npos );
assert( lp2.find( "Generals" ) != std::string::npos );
assert( std::count( lp2.begin() , lp2.end() , '\n' ) ==
std::count( lp.begin() , lp.end() , '\n' ) );
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* The same model written as an MPS file and read back from it. The format
* says a two-sided row once, with its second side in RANGES, so the file has
* one row per Constraint and not two as the LP one has. */
static void test_mps_round_trip( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 3 );
( *cols )[ 0 ].set_type( ColVariable::kNatural );
( *cols )[ 1 ].is_positive( true , eNoMod );
block.add_static_variable( *cols , "x" );
auto rows = new std::vector< FRowConstraint >( 2 );
{
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 0 ] , 1.0 } );
p.push_back( { & ( *cols )[ 1 ] , 2.0 } );
( *rows )[ 0 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 0 ].set_lhs( 1 ); // both sides finite: RANGES says the
( *rows )[ 0 ].set_rhs( 4 ); // second one
}
{
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 2 ] , -3.0 } );
( *rows )[ 1 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 1 ].set_lhs( - Inf< double >() );
( *rows )[ 1 ].set_rhs( 7 );
}
block.add_static_constraint( *rows , "r" );
LinearFunction::v_coeff_pair o;
o.push_back( { & ( *cols )[ 0 ] , 5.0 } );
o.push_back( { & ( *cols )[ 2 ] , -1.0 } );
auto obj = new FRealObjective( & block , new LinearFunction( std::move( o ) ) );
obj->set_sense( Objective::eMin , eNoMod );
block.set_objective( obj , eNoMod );
std::ostringstream written;
block.write_mps( written );
const auto mps = written.str();
assert( mps.find( "OBJSENSE" ) != std::string::npos );
assert( mps.find( " L r_0" ) != std::string::npos );
assert( mps.find( "RANGES" ) != std::string::npos );
assert( mps.find( "'INTORG'" ) != std::string::npos );
assert( mps.find( "ENDATA" ) != std::string::npos );
// read back: the same rows, the same bounds, the same integer column
AbstractBlock again;
std::istringstream in( mps );
again.load( in , 'M' );
const auto & gv = again.get_static_variable_groups();
const auto & gc = again.get_static_constraint_groups();
assert( gv.size() == 1 );
assert( gc.size() == 2 ); // the rows, and the box of the columns
assert( gv[ 0 ]->get_num_elements() == 3 );
assert( gc[ 0 ]->get_num_elements() == 2 ); // one row each, not two
// and what it says is what was written
std::vector< double > lhs , rhs;
gc[ 0 ]->for_each_as< FRowConstraint >(
[ & lhs , & rhs ]( FRowConstraint & c ) {
lhs.push_back( c.get_lhs() ); rhs.push_back( c.get_rhs() ); } );
assert( lhs.size() == 2 );
assert( lhs[ 0 ] == 1 );
assert( rhs[ 0 ] == 4 );
assert( lhs[ 1 ] <= - Inf< double >() );
assert( rhs[ 1 ] == 7 );
bool first = true;
gv[ 0 ]->for_each_as< ColVariable >(
[ & first ]( ColVariable & v ) {
if( first ) { assert( v.is_integer() ); first = false; } } );
/* Writing the copy does NOT give the first file back: an MPS file has no
* notion of groups, so the copy has the one group of columns and the one of
* rows read_mps() builds, and its elements are named after those. What does
* hold is that from there on the file is a fixed point, which is what says
* that nothing is lost or added at each trip. */
std::ostringstream twice;
again.write_mps( twice );
assert( twice.str() != mps );
AbstractBlock third;
std::istringstream in2( twice.str() );
third.load( in2 , 'M' );
std::ostringstream thrice;
third.write_mps( thrice );
assert( thrice.str() == twice.str() );
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* The rows a dual ray names. What a Solver does when it proves a model
* unfeasible is writing the ray into the Block, one multiplier per
* Constraint, so the ray is put there by hand here and what is checked is
* that the printer names the rows that carry one and leaves the others
* alone. The model is x >= 2 and x <= 1, which cannot both hold. */
static void test_is( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 2 );
block.add_static_variable( *cols , "x" );
auto rows = new std::vector< FRowConstraint >( 3 );
for( int i = 0 ; i < 3 ; ++i ) {
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ i == 2 ? 1 : 0 ] , 1.0 } );
( *rows )[ i ].set_function( new LinearFunction( std::move( p ) ) );
}
( *rows )[ 0 ].set_lhs( 2 ); // x_0 >= 2
( *rows )[ 0 ].set_rhs( Inf< double >() );
( *rows )[ 1 ].set_lhs( - Inf< double >() ); // x_0 <= 1
( *rows )[ 1 ].set_rhs( 1 );
( *rows )[ 2 ].set_lhs( - Inf< double >() ); // and one that has nothing
( *rows )[ 2 ].set_rhs( 9 ); // to do with it
block.add_static_constraint( *rows , "r" );
// with no ray in the Block the printer says so rather than naming rows
{
std::ostringstream none;
block.write_is( none );
assert( none.str().find( "no multiplier" ) != std::string::npos );
}
// the ray a Solver would have written: the two rows that fight, and not
// the third one
( *rows )[ 0 ].set_dual( 1 );
( *rows )[ 1 ].set_dual( -1 );
( *rows )[ 2 ].set_dual( 0 );
std::ostringstream is;
block.write_is( is );
const auto said = is.str();
assert( said.find( "r_2" ) == std::string::npos );
assert( said.find( "no multiplier" ) == std::string::npos );
// the line is read by somebody, so it is the whole line that is checked:
// the multiplier, the name, and the row with its side where it belongs
assert( said.find( " 1 * ( r_0: 2 <= x_0 )" ) != std::string::npos );
assert( said.find( " -1 * ( r_1: x_0 <= 1 )" ) != std::string::npos );
// eps leaves out what is smaller than it
( *rows )[ 1 ].set_dual( 1e-12 );
std::ostringstream cut;
block.write_is( cut , 1e-9 );
assert( cut.str().find( "r_0" ) != std::string::npos );
assert( cut.str().find( "r_1" ) == std::string::npos );
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* What the two writers do with what is degenerate: no Objective at all, a
* row every coefficient of which is zero, a free column, a fixed one, a
* column that is in no row and in no Objective, an equality row, a row whose
* two sides are both infinite, a group with no name, a coefficient of 1 and
* one of -1, a number that wants all of its digits, and a Block that is
* maximizing. Each of these is a branch of the two writers that the plain
* model does not go through. */
static void test_writers_edge_cases( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 5 );
// x_0 free, x_1 fixed, x_2 in nothing, x_3 and x_4 ordinary
( *cols )[ 0 ].is_positive( false , eNoMod );
( *cols )[ 0 ].is_negative( false , eNoMod );
( *cols )[ 1 ].set_value( 2.5 );
( *cols )[ 1 ].is_fixed( true , eNoMod );
block.add_static_variable( *cols , "x" );
// a group with no name: its elements are named after its index
auto more = new std::vector< ColVariable >( 1 );
block.add_static_variable( *more );
auto rows = new std::vector< FRowConstraint >( 4 );
{ // every coefficient zero
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 3 ] , 0.0 } );
( *rows )[ 0 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 0 ].set_lhs( - Inf< double >() );
( *rows )[ 0 ].set_rhs( 1 );
}
{ // an equality, and a 1 and a -1
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 3 ] , 1.0 } );
p.push_back( { & ( *cols )[ 4 ] , -1.0 } );
( *rows )[ 1 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 1 ].set_both( 3 );
}
{ // both sides infinite
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 4 ] , 2.0 } );
( *rows )[ 2 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 2 ].set_lhs( - Inf< double >() );
( *rows )[ 2 ].set_rhs( Inf< double >() );
}
{ // a number that wants its digits
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *more )[ 0 ] , 0.1 } );
( *rows )[ 3 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 3 ].set_lhs( 1.0 / 3.0 );
( *rows )[ 3 ].set_rhs( Inf< double >() );
}
block.add_static_constraint( *rows , "r" );
// ---- with no Objective at all -------------------------------------------
std::ostringstream noobj;
block.write_lp( noobj );
assert( noobj.str().find( "Minimize" ) != std::string::npos );
assert( noobj.str().find( " obj: 0" ) != std::string::npos );
// ---- the LP file --------------------------------------------------------
LinearFunction::v_coeff_pair o;
o.push_back( { & ( *cols )[ 3 ] , 1.0 } );
auto obj = new FRealObjective( & block ,
new LinearFunction( std::move( o ) ) );
obj->set_sense( Objective::eMax , eNoMod );
block.set_objective( obj , eNoMod );
std::ostringstream lps;
block.write_lp( lps );
const auto lp = lps.str();
assert( lp.find( "Maximize" ) != std::string::npos );
// a row with no coefficient left is still a row, and says 0
assert( lp.find( " r_0_up: 0 <= 1" ) != std::string::npos );
// an equality is one row, and a coefficient of 1 or -1 is not written
assert( lp.find( " r_1: x_3 - x_4 = 3" ) != std::string::npos );
// the free column says so, the fixed one is a point, and the group with
// no name is named after its index
assert( lp.find( " x_0 free" ) != std::string::npos );
assert( lp.find( " v1_0" ) != std::string::npos );
// the number is there whole
assert( lp.find( "0.3333333333333333" ) != std::string::npos );
assert( lp.find( "0.1 v1_0" ) != std::string::npos );
// ---- the MPS file -------------------------------------------------------
std::ostringstream mpss;
block.write_mps( mpss );
const auto mps = mpss.str();
assert( mps.find( "OBJSENSE" ) != std::string::npos );
assert( mps.find( " MAX" ) != std::string::npos );
assert( mps.find( " E r_1" ) != std::string::npos );
// the row with both sides infinite is not in the file at all
assert( mps.find( "r_2" ) == std::string::npos );
// the column that is in no row is, with a zero cost
assert( mps.find( " x_2 obj 0" ) != std::string::npos );
// the fixed column is a point and the free one is free
assert( mps.find( " FX BND x_1 2.5" ) != std::string::npos );
assert( mps.find( " FR BND x_0" ) != std::string::npos );
// no INTORG marker, nothing here being integer
assert( mps.find( "INTORG" ) == std::string::npos );
// and it still makes the trip: reading it and writing it again is a fixed
// point, which is what says that none of these was lost on the way
AbstractBlock again;
std::istringstream in( mps );
again.load( in , 'M' );
std::ostringstream twice , thrice;
again.write_mps( twice );
/* The copy has no named group at all, and its group of columns and its
* group of rows both have index 0: the two markers have to differ, or the
* file would call a row and a column by the same name and no reader could
* tell which of them a name means. */
assert( twice.str().find( " E c0_1" ) != std::string::npos );
assert( twice.str().find( " v0_1 obj" ) != std::string::npos );
AbstractBlock third;
std::istringstream in2( twice.str() );
third.load( in2 , 'M' );
third.write_mps( thrice );
assert( thrice.str() == twice.str() );
// ---- a Function that is not linear is refused, not written wrong --------
auto quad = new std::vector< FRowConstraint >( 1 );
{
DQuadFunction::v_coeff_triple t;
t.push_back( { & ( *cols )[ 3 ] , 1.0 , 1.0 } );
( *quad )[ 0 ].set_function( new DQuadFunction( std::move( t ) ) );
( *quad )[ 0 ].set_rhs( 1 );
}
block.add_static_constraint( *quad , "q" );
bool refused = false;
try { std::ostringstream no; block.write_lp( no ); }
catch( const std::invalid_argument & ) { refused = true; }
assert( refused );
refused = false;
try { std::ostringstream no; block.write_mps( no ); }
catch( const std::invalid_argument & ) { refused = true; }
assert( refused );
}
/*--------------------------------------------------------------------------*/
/* What write_is() does at its own edges: a multiplier exactly equal to eps,
* which the comparison leaves out; an equality row, which is written with
* its one side; a row that is not linear, which has no place in a
* certificate written this way; and a ray that names nothing at all. */
static void test_is_edge_cases( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 2 );
block.add_static_variable( *cols , "y" );
auto rows = new std::vector< FRowConstraint >( 2 );
{
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 0 ] , 1.0 } );
( *rows )[ 0 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 0 ].set_both( 4 ); // an equality
( *rows )[ 0 ].set_dual( 1e-9 );
}
{
LinearFunction::v_coeff_pair p;
p.push_back( { & ( *cols )[ 1 ] , 1.0 } );
( *rows )[ 1 ].set_function( new LinearFunction( std::move( p ) ) );
( *rows )[ 1 ].set_lhs( - Inf< double >() );
( *rows )[ 1 ].set_rhs( 2 );
( *rows )[ 1 ].set_dual( -1 );
}
block.add_static_constraint( *rows , "e" );
// a multiplier exactly equal to eps is left out: the comparison is <=, so
// the row on the threshold does not enter the certificate
std::ostringstream at;
block.write_is( at , 1e-9 );
assert( at.str().find( "e_0" ) == std::string::npos );
assert( at.str().find( "e_1" ) != std::string::npos );
// just below eps it does enter, and an equality is written with its side
std::ostringstream under;
block.write_is( under , 1e-10 );
assert( under.str().find( " 1e-09 * ( e_0: y_0 = 4 )" ) != std::string::npos );
// a row that is not linear carries no multiplier into the certificate
auto quad = new std::vector< FRowConstraint >( 1 );
{
DQuadFunction::v_coeff_triple t;
t.push_back( { & ( *cols )[ 0 ] , 1.0 , 1.0 } );
( *quad )[ 0 ].set_function( new DQuadFunction( std::move( t ) ) );
( *quad )[ 0 ].set_rhs( 1 );
( *quad )[ 0 ].set_dual( 7 );
}
block.add_static_constraint( *quad , "q" );
std::ostringstream withq;
block.write_is( withq );
assert( withq.str().find( "q_0" ) == std::string::npos );
assert( withq.str().find( "e_1" ) != std::string::npos );
// a ray that names nothing says so rather than writing an empty answer
( *rows )[ 0 ].set_dual( 0 );
( *rows )[ 1 ].set_dual( 0 );
std::ostringstream none;
block.write_is( none );
assert( none.str().find( "no multiplier" ) != std::string::npos );
}
/*--------------------------------------------------------------------------*/
/*------------------ SERIALIZATION, Objective AND mirror() -----------------*/
/*--------------------------------------------------------------------------*/
/// writes the Block into a netCDF file and reads it back, nullptr if it fails
static Block * through_a_file( const Block & block )
{
const char * const name = "tests_AbstractBlock_round_trip.nc4";
block.Block::serialize( name , eBlockFile );
auto read = Block::deserialize( name );
std::remove( name );
return( read );
}
/*--------------------------------------------------------------------------*/
/// a LinearFunction with coefficient 1 for each of the given Variable
static LinearFunction * sum_of( const std::vector< ColVariable * > & vars )
{
LinearFunction::v_coeff_pair p;
for( auto var : vars )
p.push_back( { var , 1.0 } );
return( new LinearFunction( std::move( p ) ) );
}
/*--------------------------------------------------------------------------*/
/* An empty Block goes through a netCDF file and comes back as an empty
* AbstractBlock: no group, no Objective, no nested Block. */
static void test_serialize_empty( void )
{
AbstractBlock block;
auto read = through_a_file( block );
assert( read );
assert( dynamic_cast< AbstractBlock * >( read ) );
assert( read->get_static_variable_groups().empty() );
assert( read->get_dynamic_variable_groups().empty() );
assert( read->get_static_constraint_groups().empty() );
assert( read->get_dynamic_constraint_groups().empty() );
assert( ! read->get_objective() );
assert( read->get_nested_Blocks().empty() );
delete read;
std::cout << "serialize an empty Block: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The dynamic groups travel as the model they are part of [see
* AbstractBlock::serialize()]: what comes back has the same columns and the
* same rows, in the static groups read_lp() builds. */
static void test_serialize_dynamic_groups( void )
{
AbstractBlock block;
auto cols = new std::list< ColVariable >( 3 );
block.add_dynamic_variable( *cols , "y" );
std::vector< ColVariable * > y;
for( auto & col : *cols )
y.push_back( & col );
auto rows = new std::list< FRowConstraint >( 2 );
double rhs = 5;
for( auto & row : *rows ) {
row.set_function( sum_of( { y[ 0 ] , y[ 1 ] } ) );
row.set_lhs( - Inf< double >() );
row.set_rhs( rhs++ );
}
block.add_dynamic_constraint( *rows , "d" );
block.set_objective( new FRealObjective( & block , sum_of( y ) ) , eNoMod );
auto read = through_a_file( block );
assert( read );
assert( read->get_dynamic_variable_groups().empty() );
assert( read->get_dynamic_constraint_groups().empty() );
const auto & sv = read->get_static_variable_groups();
const auto & sc = read->get_static_constraint_groups();
assert( sv.size() == 1 );
assert( sv[ 0 ]->get_num_elements() == 3 );
assert( sc.size() == 2 ); // the rows, and the box of columns
assert( sc[ 0 ]->get_num_elements() == 2 );
std::vector< double > got;
sc[ 0 ]->for_each_as< FRowConstraint >( [ & got ]( FRowConstraint & c ) {
assert( c.get_lhs() <= - Inf< double >() );
got.push_back( c.get_rhs() ); } );
assert( ( got == std::vector< double >( { 5 , 6 } ) ) );
assert( read->get_objective() );
delete read;
std::cout << "serialize dynamic groups: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The bounds a :OneVarConstraint puts on a column travel as bounds of that
* column; the Objective as it is. */
static void test_serialize_one_var_constraint( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 2 );
block.add_static_variable( *cols , "x" );
auto box = new std::vector< BoxConstraint >( 1 );
( *box )[ 0 ].set_variable( & ( *cols )[ 1 ] );
( *box )[ 0 ].set_lhs( 1 );
( *box )[ 0 ].set_rhs( 3 );
block.add_static_constraint( *box , "b" );
// both columns in the Objective, so that each of them is in the model
// somewhere else than in the Bounds [see test_serialize_column_in_no_row()]
block.set_objective( new FRealObjective( & block ,
sum_of( { & ( *cols )[ 0 ] , & ( *cols )[ 1 ] } ) ) ,
eNoMod );
auto read = through_a_file( block );
assert( read );
// the bounds of the columns read back: x_0 is free, x_1 is in [ 1 , 3 ]
std::vector< std::pair< double , double > > bounds;
for( const auto & group : read->get_static_constraint_groups() )
group->for_each_as< BoxConstraint >( [ & bounds ]( BoxConstraint & c ) {
bounds.emplace_back( c.get_lhs() , c.get_rhs() ); } );
assert( bounds.size() == 2 );
assert( bounds[ 0 ].first <= - Inf< double >() );
assert( bounds[ 0 ].second >= Inf< double >() );
assert( ( bounds[ 1 ] == std::pair< double , double >( 1 , 3 ) ) );
auto obj = dynamic_cast< FRealObjective * >( read->get_objective() );
assert( obj );
auto lf = dynamic_cast< LinearFunction * >( obj->get_function() );
assert( lf && ( lf->get_num_active_var() == 2 ) );
delete read;
std::cout << "serialize a OneVarConstraint: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A column that is in no row and not in the Objective is still a column of
* the model, and write_lp() writes it in the Bounds section: read_lp() has
* to take it from there, or the Block cannot be read back at all. */
static void test_serialize_column_in_no_row( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 2 );
block.add_static_variable( *cols , "x" );
block.set_objective( new FRealObjective( & block ,
sum_of( { & ( *cols )[ 0 ] } ) ) ,
eNoMod );
auto read = through_a_file( block );
assert( read );
const auto & sv = read->get_static_variable_groups();
assert( sv.size() == 1 );
assert( sv[ 0 ]->get_num_elements() == 2 );
delete read;
std::cout << "serialize a column in no row: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* An Objective that is not linear cannot be written as the LP file the
* AbstractBlock is serialized into: it is refused, not written wrong. */
static void test_serialize_quadratic_objective( void )
{
AbstractBlock block;
auto cols = new std::vector< ColVariable >( 1 );
block.add_static_variable( *cols , "x" );
DQuadFunction::v_coeff_triple t;
t.push_back( { & ( *cols )[ 0 ] , 1.0 , 2.0 } );
block.set_objective( new FRealObjective( & block ,
new DQuadFunction( std::move( t ) ) ) ,
eNoMod );
const char * const name = "tests_AbstractBlock_quadratic.nc4";
bool refused = false;
try {
block.Block::serialize( name , eBlockFile );
}
catch( const std::invalid_argument & ) {
refused = true;
}
std::remove( name );
assert( refused );
std::cout << "serialize a quadratic Objective: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The nested Block travel each in a group of its own, and come back nested
* in the Block they were nested in, with their own model. */
static void test_serialize_nested( void )
{
auto block = new AbstractBlock;
auto x = new std::vector< ColVariable >( 1 );
block->add_static_variable( *x , "x" );
block->set_objective( new FRealObjective( block ,
sum_of( { & ( *x )[ 0 ] } ) ) , eNoMod );
for( int k = 0 ; k < 2 ; ++k ) {
auto inner = new AbstractBlock( block );
auto z = new std::vector< ColVariable >( 2 );
inner->add_static_variable( *z , "z" );
auto rows = new std::vector< FRowConstraint >( 1 + k );
for( auto & row : *rows ) {
row.set_function( sum_of( { & ( *z )[ 0 ] , & ( *z )[ 1 ] } ) );
row.set_lhs( - Inf< double >() ); // one side, hence one row of the LP
row.set_rhs( 4 );
}
inner->add_static_constraint( *rows , "r" );
inner->set_objective( new FRealObjective( inner ,
sum_of( { & ( *z )[ 1 ] } ) ) ,
eNoMod );
block->add_nested_Block( inner );
}
auto read = through_a_file( *block );
assert( read );
const auto & nested = read->get_nested_Blocks();
assert( nested.size() == 2 );
for( int k = 0 ; k < 2 ; ++k ) {
assert( nested[ k ] );
assert( nested[ k ]->get_static_variable_groups().size() == 1 );
assert( nested[ k ]->get_static_variable_groups()[ 0 ]->
get_num_elements() == 2 );
assert( nested[ k ]->get_static_constraint_groups()[ 0 ]->
get_num_elements() == Block::Index( 1 + k ) );
assert( nested[ k ]->get_f_Block() == read );
}