-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_Misc.cpp
More file actions
1161 lines (1006 loc) · 42.4 KB
/
Copy pathtests_Misc.cpp
File metadata and controls
1161 lines (1006 loc) · 42.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 tests_Misc.cpp --------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Unit tests for the smaller pieces of the core that have no test of their
* own: GlobalInformation and its Collection, SimpleDataMapping, Change and
* GroupChange, BoxSolver, UpdateSolver, and what the base Solver class does
* by itself (registration to a Block, the queue of Modification, the tables
* of the parameters).
*
* BoxSolver is taken over box LPs and separable box QPs whose optimum is
* known in closed form, in both senses, and over the two cases where there
* is no optimum, a box that is empty and a cost pushing towards an infinite
* bound, against what the Solver interface says has to be answered then.
*
* A check that fails because of a defect of the library prints what it
* found and what it expected, and the test goes on with the next one; the
* return value of main() says whether any failed.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "AbstractBlock.h"
#include "BoxSolver.h"
#include "Change.h"
#include "DataMapping.h"
#include "DQuadFunction.h"
#include "FakeSolver.h"
#include "FRealObjective.h"
#include "GlobalInformation.h"
#include "LinearFunction.h"
#include "OneVarConstraint.h"
#include "UpdateSolver.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#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;
using Index = Block::Index;
using Range = Block::Range;
using Subset = Block::Subset;
using MF_dbl_it = Block::MF_dbl_it;
/*--------------------------------------------------------------------------*/
/*------------------------------- CONSTANTS --------------------------------*/
/*--------------------------------------------------------------------------*/
static constexpr double c_eps = 1e-10;
static constexpr double INF = Inf< double >();
/*--------------------------------------------------------------------------*/
/*---------------------------- A TEST Change -------------------------------*/
/*--------------------------------------------------------------------------*/
/// a Change setting the value of one ColVariable of the first static group
class ValueChange : public Change {
public:
explicit ValueChange( Index i = 0 , double v = 0 )
: Change() , f_i( i ) , f_v( v ) {}
using Change::serialize;
Change * apply( Block * block , bool doUndo = false ,
ModParam issueMod = eNoBlck ,
ModParam issueAMod = eNoBlck ) override {
auto & x = ( *block->get_static_variable_v< ColVariable >( 0 ) )[ f_i ];
Change * undo = doUndo ? new ValueChange( f_i , x.get_value() ) : nullptr;
x.set_value( f_v );
return( undo );
}
void serialize( netCDF::NcGroup & group ) const override {
Change::serialize( group );
group.putAtt( "index" , netCDF::NcUint() , f_i );
group.putAtt( "value" , netCDF::NcDouble() , f_v );
}
void deserialize( const netCDF::NcGroup & group ) override {
group.getAtt( "index" ).getValues( & f_i );
group.getAtt( "value" ).getValues( & f_v );
}
[[nodiscard]] Index index( void ) const { return( f_i ); }
[[nodiscard]] double value( void ) const { return( f_v ); }
protected:
void print( std::ostream & output ) const override {
output << "ValueChange x[ " << f_i << " ] = " << f_v;
}
private:
Index f_i;
double f_v;
SMSpp_insert_in_factory_h;
};
SMSpp_insert_in_factory_cpp_0( ValueChange );
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
static int n_failures = 0; ///< number of failed checks of the library
/// records a failed check of the library, saying what was found
static void expect( bool ok , const std::string & what )
{
if( ok )
return;
std::cout << "FAILED: " << what << std::endl;
++n_failures;
}
/*--------------------------------------------------------------------------*/
static bool equal( double a , double b )
{
if( std::isinf( b ) )
return( a == b );
return( std::abs( a - b ) <= c_eps * std::max( 1.0 , std::abs( b ) ) );
}
/*--------------------------------------------------------------------------*/
/// true if calling f() throws an E
template< class E , class F >
static bool throws( F f )
{
try {
f();
}
catch( E & ) {
return( true );
}
return( false );
}
/*--------------------------------------------------------------------------*/
/// an AbstractBlock with n ColVariable in its first static group, "x"
static AbstractBlock * block_with_x( Index n , Block * father = nullptr )
{
auto block = new AbstractBlock( father );
block->add_static_variable( *( new std::vector< ColVariable >( n ) ) , "x" );
return( block );
}
/*--------------------------------------------------------------------------*/
static std::vector< ColVariable > & x_of( Block * block )
{
return( *block->get_static_variable_v< ColVariable >( 0 ) );
}
/*--------------------------------------------------------------------------*/
/// the values of the ColVariable in the first static group
static std::vector< double > values_of( Block * block )
{
std::vector< double > v;
for( auto & x : x_of( block ) )
v.push_back( x.get_value() );
return( v );
}
/*--------------------------------------------------------------------------*/
/*------------------------- GlobalInformation ------------------------------*/
/*--------------------------------------------------------------------------*/
/* Collection are added, found by name and type, and removed; a name is
* taken once whatever the type; the values of a Collection are read and
* written, directly and through a functor; the atomic form gives out a
* reference that stays valid as the map grows. */
static void test_GlobalInformation( void )
{
GlobalInformation gi;
assert( ! gi.exists( "pool" ) );
assert( ! gi.get_from_Universe< int >( "pool" ) );
gi.add_to_Universe< int >( "pool" );
assert( gi.exists( "pool" ) );
// a name is taken once, whatever the type
assert( throws< std::runtime_error >( [ & ]() {
gi.add_to_Universe< int >( "pool" ); } ) );
assert( throws< std::runtime_error >( [ & ]() {
gi.add_to_Universe< double >( "pool" ); } ) );
// found with its type, not with another one
auto pool = gi.get_from_Universe< int >( "pool" );
assert( pool );
assert( ! gi.get_from_Universe< double >( "pool" ) );
assert( gi.get_from_Universe< int >( "pool" ) == pool );
{
const GlobalInformation & cgi = gi;
assert( cgi.get_from_Universe< int >( "pool" ) == pool );
}
// reading and writing the values
int v = -1;
assert( pool->size() == 0 );
assert( ! pool->read( "a" , v ) && ( v == -1 ) );
pool->write( "a" , 3 );
pool->write( "b" , 4 );
pool->write( "a" , 5 );
assert( pool->size() == 2 );
assert( pool->contains( "a" ) && ( ! pool->contains( "c" ) ) );
assert( pool->read( "a" , v ) && ( v == 5 ) );
{
auto keys = pool->keys();
std::sort( keys.begin() , keys.end() );
assert( ( keys == std::vector< std::string >( { "a" , "b" } ) ) );
}
int sum = 0;
pool->for_each( [ & sum ]( const std::string & , int val ) { sum += val; } );
assert( sum == 9 );
// the functors run on the value found, and are not run on a missing key
bool run = false;
assert( pool->write_with( "b" , []( int & val ) { val *= 10; } ) );
assert( pool->read_with( "b" , [ & v ]( const int & val ) { v = val; } ) &&
( v == 40 ) );
assert( ! pool->write_with( "c" , [ & run ]( int & ) { run = true; } ) );
assert( ! pool->read_with( "c" , [ & run ]( const int & ) { run = true; } ) );
assert( ! run );
// the atomic form: operator[] creates the value as T{}, and the reference
// it gives stays valid while the map grows past a rehash
gi.add_to_Universe< std::atomic< double > >(
GlobalInformation::str_AtomicScalars );
auto scalars = gi.get_from_Universe< std::atomic< double > >(
GlobalInformation::str_AtomicScalars );
assert( scalars );
auto & inc = ( *scalars )[ GlobalInformation::str_Incumbent ];
assert( inc.load() == 0 );
inc.store( 12.5 );
for( int i = 0 ; i < 1000 ; ++i )
scalars->write( "k" + std::to_string( i ) , i );
assert( scalars->size() == 1001 );
double d = 0;
assert( scalars->read( GlobalInformation::str_Incumbent , d ) && ( d == 12.5 ) );
assert( & ( *scalars )[ GlobalInformation::str_Incumbent ] == & inc );
scalars->write( GlobalInformation::str_Incumbent , 7 );
assert( inc.load() == 7 );
assert( ! scalars->read( "missing" , d ) && ( d == 12.5 ) );
// removal: whoever holds the Collection keeps it, and the name is free
gi.remove_from_Universe( "pool" );
assert( ! gi.exists( "pool" ) );
assert( ! gi.get_from_Universe< int >( "pool" ) );
assert( pool->read( "a" , v ) && ( v == 5 ) );
gi.add_to_Universe< double >( "pool" );
assert( gi.get_from_Universe< double >( "pool" ) );
assert( gi.get_from_Universe< double >( "pool" )->size() == 0 );
gi.remove_from_Universe( "nonesuch" ); // removing nothing is not an error
std::cout << "GlobalInformation: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*---------------------------- DataMapping ---------------------------------*/
/*--------------------------------------------------------------------------*/
/// the method of the tests: sets the values of x[ range ]
static void set_x_range( Block * block , MF_dbl_it data , Range range ,
ModParam , ModParam )
{
auto & x = x_of( block );
for( Index i = range.first ; i < range.second ; ++i )
x[ i ].set_value( *( data++ ) );
}
/// the method of the tests: sets the values of x[ subset ]
static void set_x_subset( Block * block , MF_dbl_it data , Subset && subset ,
bool , ModParam , ModParam )
{
auto & x = x_of( block );
for( auto i : subset )
x[ i ].set_value( *( data++ ) );
}
/*--------------------------------------------------------------------------*/
/* SimpleDataMapping takes the SetFrom part of the data and gives it to the
* SetTo part of the caller, broadcasting when SetFrom is the smaller; the
* serialization gives back a mapping that does the same to the caller found
* again from the reference Block, and the one whose sets cannot be matched
* is refused when it is read. */
static void test_DataMapping( void )
{
using FR = Block::FunctionType< MF_dbl_it , Range >;
using FS = Block::FunctionType< MF_dbl_it , Subset && , bool >;
Block::register_method( "tests_Misc::set_x" , new FR( & set_x_range ) );
Block::register_method( "tests_Misc::set_x" , new FS( & set_x_subset ) );
auto fr = Block::get_method< FR >( "tests_Misc::set_x" );
auto fs = Block::get_method< FS >( "tests_Misc::set_x" );
assert( fr && fs );
assert( Block::get_method_name( fr ) == "tests_Misc::set_x" );
// the caller is a sub-Block of the reference one
auto outer = block_with_x( 1 );
auto inner = block_with_x( 4 , outer );
outer->add_nested_Block( inner );
const std::vector< double > data = { 10 , 11 , 12 , 13 , 14 , 15 };
SimpleDataMapping< Range , Range > rr( fr , inner , Range( 1 , 3 ) ,
Range( 0 , 2 ) );
rr.set_data( data.cbegin() );
assert( ( values_of( inner ) == std::vector< double >( { 11 , 12 , 0 , 0 } ) ) );
// subsets, SetTo not ordered: the i-th of SetFrom (an ordered multiset)
// goes to the i-th of SetTo
SimpleDataMapping< Subset , Subset > ss( fs , inner , Subset( { 0 , 5 } ) ,
Subset( { 3 , 1 } ) );
ss.set_data( data.cbegin() );
assert( ( values_of( inner ) == std::vector< double >( { 11 , 15 , 0 , 10 } ) ) );
// one value broadcast over three
SimpleDataMapping< Range , Range > bc( fr , inner , Range( 4 , 5 ) ,
Range( 1 , 4 ) );
bc.set_data( data.cbegin() );
assert( ( values_of( inner ) == std::vector< double >( { 11 , 14 , 14 , 14 } ) ) );
// the round trip
const char * file = "Misc_test_DataMapping.nc4";
{
netCDF::NcFile nc( file , netCDF::NcFile::replace );
auto g1 = nc.addGroup( "RR" );
rr.serialize( g1 , outer );
auto g2 = nc.addGroup( "SS" );
ss.serialize( g2 , outer );
auto g3 = nc.addGroup( "BAD" );
SimpleDataMapping< Range , Range > bad( fr , inner , Range( 0 , 2 ) ,
Range( 0 , 3 ) );
bad.serialize( g3 , outer );
}
{
netCDF::NcFile nc( file , netCDF::NcFile::read );
auto rr2 = SimpleDataMappingFactory::deserialize( nc.getGroup( "RR" ) ,
outer );
auto ss2 = SimpleDataMappingFactory::deserialize( nc.getGroup( "SS" ) ,
outer );
assert( rr2 && ss2 );
using SDM_RR = SimpleDataMapping< Range , Range >;
using SDM_SS = SimpleDataMapping< Subset , Subset >;
assert( dynamic_cast< SDM_RR * >( rr2 ) );
assert( dynamic_cast< SDM_SS * >( ss2 ) );
for( auto & x : x_of( inner ) )
x.set_value( 0 );
rr2->set_data( data.cbegin() );
ss2->set_data( data.cbegin() );
assert( ( values_of( inner ) ==
std::vector< double >( { 11 , 15 , 0 , 10 } ) ) );
assert( ( values_of( outer ) == std::vector< double >( { 0 } ) ) );
delete rr2;
delete ss2;
// two values over three: neither is a multiple of the other
assert( throws< std::logic_error >( [ & ]() {
delete SimpleDataMappingFactory::deserialize( nc.getGroup( "BAD" ) ,
outer ); } ) );
}
std::remove( file );
delete outer;
std::cout << "SimpleDataMapping: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------------ Change and GroupChange --------------------------*/
/*--------------------------------------------------------------------------*/
/* A Change applied with and without its undo; a GroupChange applies its
* sub-Change in order and gives back the undo of each in the reverse order,
* nested ones included; a Change is written to a file and read back through
* the factory, also by its position in the file. */
static void test_Change( void )
{
auto block = block_with_x( 2 );
x_of( block )[ 0 ].set_value( 1 );
x_of( block )[ 1 ].set_value( 2 );
// the factory knows both, by name
{
auto c = Change::new_Change( "ValueChange" );
assert( c && ( c->classname() == "ValueChange" ) );
delete c;
auto g = Change::new_Change( "GroupChange" );
assert( g && ( g->classname() == "GroupChange" ) );
delete g;
assert( throws< std::invalid_argument >( []() {
delete Change::new_Change( "nonesuch" ); } ) );
}
// no undo is asked, none is given
ValueChange c0( 0 , 5 );
assert( c0.apply( block ) == nullptr );
assert( ( values_of( block ) == std::vector< double >( { 5 , 2 } ) ) );
// the undo gives back what was there
auto undo = c0.apply( block , true );
assert( undo );
ValueChange( 0 , 9 ).apply( block );
delete undo->apply( block );
delete undo;
assert( ( values_of( block ) == std::vector< double >( { 5 , 2 } ) ) );
// a group: two Change on the same ColVariable, and a nested group
GroupChange group;
group.add( new ValueChange( 0 , 7 ) );
group.add( new ValueChange( 0 , 8 ) );
auto nested = new GroupChange();
nested->add( new ValueChange( 1 , 3 ) );
nested->add_front( new ValueChange( 1 , 4 ) );
group.add( nested );
assert( group.sub_Changes().size() == 3 );
auto gundo = group.apply( block , true );
assert( ( values_of( block ) == std::vector< double >( { 8 , 3 } ) ) );
auto gu = dynamic_cast< GroupChange * >( gundo );
assert( gu && ( gu->sub_Changes().size() == 3 ) );
// the first undo is that of the last sub-Change, the nested group
assert( dynamic_cast< GroupChange * >( gu->sub_Changes().front() ) );
delete gundo->apply( block );
delete gundo;
assert( ( values_of( block ) == std::vector< double >( { 5 , 2 } ) ) );
// written and read back
const char * file = "Misc_test_Change.nc4";
ValueChange( 1 , 6.5 ).serialize( std::string( file ) );
{
auto c = Change::deserialize( std::string( file ) );
auto vc = dynamic_cast< ValueChange * >( c );
assert( vc && ( vc->index() == 1 ) && ( vc->value() == 6.5 ) );
delete vc->apply( block );
assert( ( values_of( block ) == std::vector< double >( { 5 , 6.5 } ) ) );
delete c;
}
// two in the same file, the second found by its position
{
netCDF::NcFile nc( file , netCDF::NcFile::replace );
ValueChange( 0 , 1 ).serialize_f( nc );
ValueChange( 1 , 2 ).serialize_f( nc );
}
{
auto c = Change::deserialize( std::string( file ) + "[1]" );
auto vc = dynamic_cast< ValueChange * >( c );
assert( vc && ( vc->index() == 1 ) && ( vc->value() == 2 ) );
delete c;
}
// a GroupChange cannot be read back yet: the factory says so with nullptr
{
netCDF::NcFile nc( file , netCDF::NcFile::replace );
group.serialize_f( nc );
}
{
netCDF::NcFile nc( file , netCDF::NcFile::read );
auto g = nc.getGroup( "Change_0" );
std::string type;
g.getAtt( "type" ).getValues( type );
assert( type == "GroupChange" );
assert( Change::new_Change( g ) == nullptr );
}
std::remove( file );
delete block;
std::cout << "Change and GroupChange: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------------------- BoxSolver --------------------------------*/
/*--------------------------------------------------------------------------*/
/// a box model: ColVariable with a BoxConstraint each, a separable Objective
struct BoxModel {
AbstractBlock * block;
std::vector< BoxConstraint > * box;
FRealObjective * obj;
BoxSolver * solver;
/// linear if quad is empty, else the DQuadFunction sum quad x^2 + lin x
BoxModel( const std::vector< double > & lb , const std::vector< double > & ub ,
const std::vector< double > & lin ,
const std::vector< double > & quad , int sense ) {
const Index n = lb.size();
block = block_with_x( n );
auto & x = x_of( block );
box = new std::vector< BoxConstraint >( n );
block->add_static_constraint( *box , "box" );
for( Index j = 0 ; j < n ; ++j ) {
( *box )[ j ].set_variable( & x[ j ] , eNoMod );
( *box )[ j ].set_lhs( lb[ j ] , eNoMod );
( *box )[ j ].set_rhs( ub[ j ] , eNoMod );
}
Function * f;
if( quad.empty() ) {
LinearFunction::v_coeff_pair cp;
for( Index j = 0 ; j < n ; ++j )
cp.push_back( { & x[ j ] , lin[ j ] } );
f = new LinearFunction( std::move( cp ) );
}
else {
DQuadFunction::v_coeff_triple ct;
for( Index j = 0 ; j < n ; ++j )
ct.push_back( { & x[ j ] , lin[ j ] , quad[ j ] } );
f = new DQuadFunction( std::move( ct ) );
}
obj = new FRealObjective( block , f );
obj->set_sense( sense , eNoMod );
block->set_objective( obj , eNoMod );
solver = new BoxSolver();
block->register_Solver( solver );
}
/// the value of the Objective at the values in the ColVariable
double objective_value( void ) {
obj->compute();
return( obj->value() );
}
~BoxModel() {
block->unregister_Solvers( true );
delete block;
}
};
/*--------------------------------------------------------------------------*/
/* A 3-variable box LP, min and max: the value, the solution, the opposite
* value, and the reduced costs on the BoxConstraint. */
static void test_BoxSolver_LP( void )
{
const std::vector< double > lb = { 0 , -1 , 1 } , ub = { 2 , 1 , 3 };
BoxModel m( lb , ub , { 1 , -2 , 0.5 } , {} , Objective::eMin );
m.solver->set_par( BoxSolver::intPDSol , 2 );
assert( throws< std::logic_error >( [ & ]() { (void) m.solver->get_lb(); } ) );
assert( m.solver->compute() == Solver::kOK );
assert( equal( m.solver->get_var_value() , -1.5 ) );
assert( equal( m.solver->get_lb() , -1.5 ) &&
equal( m.solver->get_ub() , -1.5 ) );
assert( equal( m.solver->get_opposite_value() , 5.5 ) );
assert( m.solver->has_var_solution() && m.solver->has_dual_solution() );
m.solver->get_var_solution();
assert( ( values_of( m.block ) == std::vector< double >( { 0 , 1 , 1 } ) ) );
assert( equal( m.objective_value() , -1.5 ) );
m.solver->get_dual_solution();
for( Index j = 0 ; j < 3 ; ++j )
expect( equal( ( *m.box )[ j ].get_dual() , std::vector< double >(
{ -1 , 2 , -0.5 } )[ j ] ) , "box LP, min: dual of box " +
std::to_string( j ) + " = " +
std::to_string( ( *m.box )[ j ].get_dual() ) + ", expected " +
std::to_string( std::vector< double >( { -1 , 2 , -0.5 } )[ j ] ) );
// the sense changes: the ObjectiveMod makes it computed again
m.obj->set_sense( Objective::eMax );
assert( m.solver->compute() == Solver::kOK );
assert( equal( m.solver->get_var_value() , 5.5 ) );
assert( equal( m.solver->get_opposite_value() , -1.5 ) );
m.solver->get_var_solution();
{
const auto x = values_of( m.block );
expect( x == std::vector< double >( { 2 , -1 , 3 } ) ,
"box LP, min then max: get_var_solution() writes ( " +
std::to_string( x[ 0 ] ) + " , " + std::to_string( x[ 1 ] ) + " , " +
std::to_string( x[ 2 ] ) + " ), expected ( 2 , -1 , 3 )" );
}
// a changed bound makes it computed again
( *m.box )[ 2 ].set_rhs( 4 );
assert( m.solver->compute() == Solver::kOK );
assert( equal( m.solver->get_var_value() , 6 ) );
std::cout << "BoxSolver, box LP: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A separable box QP, with a convex, a convex clipped and a concave term,
* min and max: the values are the closed-form ones, and the solution written
* has that value. */
static void test_BoxSolver_QP( void )
{
// x0^2 - 2 x0 on [0,2], 2 x1^2 + 8 x1 on [-1,1], - x2^2 on [1,3]
BoxModel m( { 0 , -1 , 1 } , { 2 , 1 , 3 } , { -2 , 8 , 0 } , { 1 , 2 , -1 } ,
Objective::eMin );
m.solver->set_par( BoxSolver::intPDSol , 2 );
assert( m.solver->compute() == Solver::kOK );
assert( equal( m.solver->get_var_value() , -16 ) );
// the dual values: x0 is interior, x1 is at its lower bound with
// derivative 4 * ( -1 ) + 8 = 4, hence dual - 4, x2 is concave
m.solver->get_dual_solution();
assert( ( *m.box )[ 0 ].get_dual() == 0 );
assert( equal( ( *m.box )[ 1 ].get_dual() , -4 ) );
assert( ( *m.box )[ 2 ].get_dual() == 0 );
assert( equal( m.solver->get_opposite_value() , 9 ) );
m.solver->get_var_solution();
assert( ( values_of( m.block ) == std::vector< double >( { 1 , -1 , 3 } ) ) );
assert( equal( m.objective_value() , -16 ) );
m.obj->set_sense( Objective::eMax );
assert( m.solver->compute() == Solver::kOK );
assert( equal( m.solver->get_var_value() , 9 ) );
m.solver->get_var_solution();
expect( equal( m.objective_value() , 9 ) , "box QP, min then max: the "
"solution written has value " + std::to_string( m.objective_value() )
+ ", expected 9" );
// the same in a maximization from the start: x2 is at its lower bound 1
// with derivative - 2, and the multiplier w of the lower bound is 2
{
BoxModel mx( { 0 , -1 , 1 } , { 2 , 1 , 3 } , { -2 , 8 , 0 } ,
{ 1 , 2 , -1 } , Objective::eMax );
mx.solver->set_par( BoxSolver::intPDSol , 2 );
assert( mx.solver->compute() == Solver::kOK );
assert( equal( mx.solver->get_var_value() , 9 ) );
mx.solver->get_dual_solution();
expect( equal( ( *mx.box )[ 2 ].get_dual() , 2 ) , "box QP, max: dual of "
"the lower bound of - x2^2 on [ 1 , 3 ] = " +
std::to_string( ( *mx.box )[ 2 ].get_dual() ) + ", expected 2" );
}
std::cout << "BoxSolver, box QP: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A lower and an upper bound given by two different OneVarConstraint: the
* dual value goes to the one that is tight at the optimum, and the other
* one gets 0, whether the dual solution is produced in compute() or asked
* for afterwards. The sign is that of RowConstraint: the coefficient of the
* row in the Lagrangian function, i.e., minus the cost in both senses (z - w
* for a minimization, w - z for a maximization, z the multiplier of the
* upper bound and w that of the lower one). */
static void test_BoxSolver_duals( void )
{
struct Case {
int sense;
double cost;
bool lb_tight;
};
const std::vector< Case > cases = { { Objective::eMin , 1 , true } ,
{ Objective::eMin , -1 , false } ,
{ Objective::eMax , 1 , false } ,
{ Objective::eMax , -1 , true } };
for( const auto & c : cases )
for( int when = 0 ; when < 2 ; ++when ) {
auto block = block_with_x( 1 );
auto & x = x_of( block );
auto lbc = new std::vector< LBConstraint >( 1 );
auto ubc = new std::vector< UBConstraint >( 1 );
block->add_static_constraint( *lbc , "lb" );
block->add_static_constraint( *ubc , "ub" );
( *lbc )[ 0 ].set_variable( & x[ 0 ] , eNoMod );
( *lbc )[ 0 ].set_lhs( 0 , eNoMod );
( *ubc )[ 0 ].set_variable( & x[ 0 ] , eNoMod );
( *ubc )[ 0 ].set_rhs( 2 , eNoMod );
auto obj = new FRealObjective( block , new LinearFunction(
LinearFunction::v_coeff_pair( { { & x[ 0 ] , c.cost } } ) ) );
obj->set_sense( c.sense , eNoMod );
block->set_objective( obj , eNoMod );
auto solver = new BoxSolver();
block->register_Solver( solver );
// when == 0: produced in compute(); when == 1: asked for afterwards
if( when == 0 )
solver->set_par( BoxSolver::intPDSol , 3 );
assert( solver->compute() == Solver::kOK );
if( when == 1 )
solver->set_par( BoxSolver::intPDSol , 3 );
solver->get_var_solution();
assert( x[ 0 ].get_value() == ( c.lb_tight ? 0 : 2 ) );
const std::string where = std::string( c.sense == Objective::eMin ?
"min " : "max " ) +
std::to_string( c.cost ) + " x on [ 0 , 2 ], "
+ ( when ? "asked after" : "made in" ) +
" compute(): ";
try {
solver->get_dual_solution();
const double dl = ( *lbc )[ 0 ].get_dual();
const double du = ( *ubc )[ 0 ].get_dual();
const double want = - c.cost;
expect( ( dl == ( c.lb_tight ? want : 0 ) ) &&
( du == ( c.lb_tight ? 0 : want ) ) , where + "dual of LB = " +
std::to_string( dl ) + ", of UB = " + std::to_string( du ) +
", expected " + std::to_string( c.lb_tight ? want : 0 ) +
" and " + std::to_string( c.lb_tight ? 0 : want ) );
}
catch( std::exception & e ) {
expect( false , where + "get_dual_solution() throws: " + e.what() );
}
block->unregister_Solvers( true );
delete block;
}
// a maximization bounded by an upper bound alone, the lower one being
// infinite: the dual solution asked for after compute() is there
{
auto block = block_with_x( 1 );
auto & x = x_of( block );
auto ubc = new std::vector< UBConstraint >( 1 );
block->add_static_constraint( *ubc , "ub" );
( *ubc )[ 0 ].set_variable( & x[ 0 ] , eNoMod );
( *ubc )[ 0 ].set_rhs( 2 , eNoMod );
auto obj = new FRealObjective( block , new LinearFunction(
LinearFunction::v_coeff_pair( { { & x[ 0 ] , 1.0 } } ) ) );
obj->set_sense( Objective::eMax , eNoMod );
block->set_objective( obj , eNoMod );
auto solver = new BoxSolver();
block->register_Solver( solver );
assert( solver->compute() == Solver::kOK );
assert( equal( solver->get_var_value() , 2 ) );
solver->set_par( BoxSolver::intPDSol , 2 );
try {
solver->get_dual_solution();
expect( ( *ubc )[ 0 ].get_dual() == -1 , "max x, x <= 2: dual of UB = " +
std::to_string( ( *ubc )[ 0 ].get_dual() ) + ", expected -1" );
}
catch( std::exception & e ) {
expect( false , std::string( "max x, x <= 2, x free below: "
"get_dual_solution() throws: " ) + e.what() );
}
block->unregister_Solvers( true );
delete block;
}
std::cout << "BoxSolver, dual values: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A box that is empty ( lb > ub ) makes the problem infeasible in both
* senses, even for a ColVariable with zero cost and even if another one is
* unbounded; get_lb() and get_ub() then say what the Solver interface says
* they say for kInfeasible. */
static void test_BoxSolver_empty_box( void )
{
for( int sense : { Objective::eMin , Objective::eMax } ) {
const std::string where = sense == Objective::eMin ? "min" : "max";
// with a cost, and with zero cost
for( double cost : { 1.0 , 0.0 } ) {
BoxModel m( { 0 , 3 } , { 2 , 1 } , { 1 , cost } , {} , sense );
assert( m.solver->compute() == Solver::kInfeasible );
assert( ! m.solver->has_var_solution() );
const double want = sense == Objective::eMin ? INF : -INF;
expect( ( m.solver->get_lb() == want ) && ( m.solver->get_ub() == want ) ,
where + " over an empty box, cost " + std::to_string( cost ) +
": get_lb() = " + std::to_string( m.solver->get_lb() ) +
", get_ub() = " + std::to_string( m.solver->get_ub() ) +
", expected both " + std::to_string( want ) );
}
// an empty box beats an unbounded direction
BoxModel m( { 0 , 3 } , { INF , 1 } ,
{ sense == Objective::eMin ? -1.0 : 1.0 , 1 } , {} , sense );
assert( m.solver->compute() == Solver::kInfeasible );
}
std::cout << "BoxSolver, empty box: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A cost pushing towards an infinite bound makes the problem unbounded:
* get_lb() and get_ub() are both infinite on the side of the sense, the
* opposite value is finite, the direction is along the infinite bound, and
* the solution, said to be there, can be read. */
static void test_BoxSolver_unbounded( void )
{
for( int sense : { Objective::eMin , Objective::eMax } ) {
const std::string where = sense == Objective::eMin ? "min" : "max";
// min: - 2 x1 with x1 <= INF; max: - 2 x1 with x1 >= -INF
const bool mn = ( sense == Objective::eMin );
BoxModel m( { 0 , mn ? -1 : -INF , 1 } , { 2 , mn ? INF : 1 , 3 } ,
{ 1 , -2 , 0.5 } , {} , sense );
assert( m.solver->compute() == Solver::kUnbounded );
const double want = mn ? -INF : INF;
assert( ( m.solver->get_lb() == want ) && ( m.solver->get_ub() == want ) );
assert( equal( m.solver->get_opposite_value() , mn ? 5.5 : -1.5 ) );
assert( m.solver->has_var_direction() );
m.solver->get_var_direction();
assert( ( values_of( m.block ) ==
std::vector< double >( { 0 , mn ? 1.0 : -1.0 , 0 } ) ) );
assert( m.solver->has_var_solution() );
try {
m.solver->get_var_solution();
auto x = values_of( m.block );
bool feasible = true;
for( Index j = 0 ; j < 3 ; ++j )
feasible = feasible && ( x[ j ] >= ( *m.box )[ j ].get_lhs() ) &&
( x[ j ] <= ( *m.box )[ j ].get_rhs() );
expect( feasible , where + ", unbounded: the solution is not feasible" );
}
catch( std::exception & e ) {
expect( false , where + ", unbounded: has_var_solution() is true but "
"get_var_solution() throws: " + e.what() );
}
}
std::cout << "BoxSolver, unbounded: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------------------ UpdateSolver ------------------------------*/
/*--------------------------------------------------------------------------*/
/* An UpdateSolver attached to a Block passes the Modification it receives,
* unchanged, to another Block, and from there to its Solver; the options
* filter them by the Block they come from and by concerns_Block(). Mapping
* them is left to map_forward_Modification(), which the base Block does
* not implement, and then nothing arrives and the other Block is not left
* locked. */
static void test_UpdateSolver( void )
{
assert( throws< std::invalid_argument >( []() { UpdateSolver( nullptr ); } ) );
struct Case {
int options;
bool from_root_mod , from_root_nomod , from_son;
};
// 2: pass unchanged; 4: only from the Block itself; 8: only from its sons;
// 16: only concerns_Block(); 32: only ! concerns_Block()
const std::vector< Case > cases = { { 2 , true , true , true } ,
{ 2 | 4 , true , true , false } ,
{ 2 | 8 , false , false , true } ,
{ 2 | 16 , true , false , true } ,
{ 2 | 32 , false , true , false } ,
{ 0 , false , false , false } };
for( const auto & c : cases ) {
auto a = block_with_x( 2 );
auto son = block_with_x( 1 , a );
a->add_nested_Block( son );
auto b = block_with_x( 1 );
auto us = new UpdateSolver( b , nullptr , c.options );
a->register_Solver( us );
auto fake = new FakeSolver();
b->register_Solver( fake );
auto & mods = fake->get_Modification_list();
mods.clear();
x_of( a )[ 0 ].is_fixed( true , eModBlck ); // concerns_Block()
x_of( a )[ 1 ].is_fixed( true , eNoBlck ); // ! concerns_Block()
x_of( son )[ 0 ].is_fixed( true , eModBlck );
std::vector< Variable * > expected;
if( c.from_root_mod )
expected.push_back( & x_of( a )[ 0 ] );
if( c.from_root_nomod )
expected.push_back( & x_of( a )[ 1 ] );
if( c.from_son )
expected.push_back( & x_of( son )[ 0 ] );
assert( mods.size() == expected.size() );
Index k = 0;
for( auto & mod : mods ) {
auto vmod = std::dynamic_pointer_cast< VariableMod >( mod );
assert( vmod && ( vmod->variable() == expected[ k++ ] ) );
}
assert( b->is_owned_by( nullptr ) ); // not left locked
// an inhibited UpdateSolver passes nothing
mods.clear();
us->inhibit_Modification( true );
x_of( a )[ 0 ].is_fixed( false , eModBlck );
assert( mods.empty() );
a->unregister_Solvers( true );
b->unregister_Solvers( true );
delete a;
delete b;
}
std::cout << "UpdateSolver: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------------------ Solver base -------------------------------*/
/*--------------------------------------------------------------------------*/
/* The list of Solver registered to a Block: in the order they came, at the
* front when so asked, a Solver registered twice being there once; each
* Solver knows its Block, and forgets it when it is unregistered or
* replaced; the sub-Block know that someone is listening as long as a
* Solver is there. */
static void test_Solver_registration( void )
{
auto a = block_with_x( 1 );
auto son = block_with_x( 1 , a );
a->add_nested_Block( son );
auto s1 = new FakeSolver() , s2 = new FakeSolver() , s3 = new FakeSolver();
assert( ! son->anyone_there() );
a->register_Solver( s1 );
a->register_Solver( s2 );
a->register_Solver( s3 , true );
a->register_Solver( s1 );
assert( son->anyone_there() );
{
const auto & lst = a->get_registered_solvers();
assert( ( std::vector< Solver * >( lst.begin() , lst.end() ) ==
std::vector< Solver * >( { s3 , s1 , s2 } ) ) );
}
assert( ( s1->get_Block() == a ) && ( s2->get_Block() == a ) &&
( s3->get_Block() == a ) );
assert( throws< std::invalid_argument >( [ & ]() {
a->register_Solver( nullptr ); } ) );
// unregistered, not deleted; unregistering it again does nothing
a->unregister_Solver( s1 );
assert( s1->get_Block() == nullptr );
a->unregister_Solver( s1 );
assert( a->get_registered_solvers().size() == 2 );
// s1 takes the place of s3
a->replace_Solver( s1 , a->get_registered_solvers().begin() );
{
const auto & lst = a->get_registered_solvers();
assert( ( std::vector< Solver * >( lst.begin() , lst.end() ) ==
std::vector< Solver * >( { s1 , s2 } ) ) );
}
assert( ( s1->get_Block() == a ) && ( s3->get_Block() == nullptr ) );
delete s3;
a->unregister_Solver( s2 , true );
a->unregister_Solver( s1 , true );
assert( a->get_registered_solvers().empty() );
assert( ! son->anyone_there() );
delete a;
std::cout << "Solver registration: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The queue of Modification a Solver sees: in the order they are issued,
* from the Block and from its sons; a NBModification wipes what came
* before; the Modification sent on a channel arrive as one GroupModification
* when the channel is closed, with the sub-Modification in their order; an
* inhibited Solver sees nothing. */
static void test_Solver_Modification_queue( void )
{
auto a = block_with_x( 3 );
auto son = block_with_x( 1 , a );
a->add_nested_Block( son );
auto fake = new FakeSolver();
a->register_Solver( fake );
auto & mods = fake->get_Modification_list();
mods.clear();
auto & x = x_of( a );
x[ 2 ].is_fixed( true );
x_of( son )[ 0 ].is_fixed( true );
x[ 0 ].is_fixed( true );
{