-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_Modification.cpp
More file actions
2271 lines (2052 loc) · 86.3 KB
/
Copy pathtests_Modification.cpp
File metadata and controls
2271 lines (2052 loc) · 86.3 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_Modification.cpp ----------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Unit tests for the Modification issued by the modifying methods of the
* core components: ColVariable, FRowConstraint, the OneVarConstraint family,
* FRealObjective, LinearFunction within an FRowConstraint and within an
* FRealObjective, and the dynamic Variable and Constraint of a Block; and
* that under eDryRun those of DQuadFunction, QuadFunction,
* PolyhedralFunction, LagBFunction, BendersBFunction and C05SumFunction
* change and issue nothing, those that can be exercised without a Solver
* being also checked to do the change under eNoMod.
*
* For each method the test checks what Observer::make_par() says of the
* ModParam: under eDryRun the change is not done and nothing is issued;
* under eNoMod the change is done and nothing is issued; under
* eNoBlck the Modification is issued, with concerns_Block() == false, only
* if a Solver is listening; under eModBlck it is issued, with
* concerns_Block() == true, whether or not a Solver is listening; on a
* channel of the Block it arrives packed in the GroupModification that
* closing the channel dispatches. It then checks the type of the
* Modification and what it says (the object it refers to, the type code,
* the range or subset, the elements added or removed), and the edge cases:
* a call that changes nothing, empty and full Range, Range at the
* boundaries, empty Subset (which for the removing methods means "all"),
* unordered Subset, adding nothing and removing everything.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <stdexcept>
#include <type_traits>
#include <vector>
#include "AbstractBlock.h"
#include "BendersBFunction.h"
#include "C05SumFunction.h"
#include "ColVariable.h"
#include "DQuadFunction.h"
#include "FakeSolver.h"
#include "FRealObjective.h"
#include "FRowConstraint.h"
#include "LagBFunction.h"
#include "LinearFunction.h"
#include "OneVarConstraint.h"
#include "PolyhedralFunction.h"
#include "QuadFunction.h"
// 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 var_type = Variable::var_type;
using RHSValue = RowConstraint::RHSValue;
using v_coeff_pair = LinearFunction::v_coeff_pair;
using Vec_FV = Function::Vec_FunctionValue;
using Addrs = std::vector< const void * >;
static constexpr RHSValue INF = RowConstraint::RHSINF;
/*--------------------------------------------------------------------------*/
/*------------------------------ CLASSES -----------------------------------*/
/*--------------------------------------------------------------------------*/
/// an AbstractBlock that records every Modification it is sent
/** The Block is sent a Modification before looking at whether anyone is
* listening, which is what tells eModBlck from eNoBlck when no Solver is
* registered. */
class RecBlock : public AbstractBlock
{
public:
void add_Modification( sp_Mod mod , ChnlName chnl = 0 ) override {
v_seen.push_back( mod );
AbstractBlock::add_Modification( mod , chnl );
}
Lst_sp_Mod v_seen; ///< what the Block has been sent, in order
};
/*--------------------------------------------------------------------------*/
/// the Block under test, with the FakeSolver that records what it receives
struct Rig
{
Rig( void ) : block( new RecBlock ) , solver( new FakeSolver ) {
block->register_Solver( solver );
}
~Rig() {
clear();
block->unregister_Solvers( true );
delete block; // it clear()-s its Objective, hence before this
delete objective;
}
Lst_sp_Mod & got( void ) { return( solver->get_Modification_list() ); }
Lst_sp_Mod & seen( void ) { return( block->v_seen ); }
void clear( void ) {
got().clear();
seen().clear();
}
void listen( bool yn ) {
if( yn )
block->register_Solver( solver ); // does nothing if it is there
else
block->unregister_Solver( solver );
}
RecBlock * block;
FakeSolver * solver;
Objective * objective = nullptr; ///< owned here, the Block does not
};
/*--------------------------------------------------------------------------*/
/// one modifying call and what it has to do
/** reset() puts the object in the state before the call without issuing
* anything, call() does the call under the given ModParam, before() and
* after() tell the two states apart, and check() looks at the type and the
* content of the one Modification the call issues. */
struct Case
{
std::function< void( void ) > reset;
std::function< void( ModParam ) > call;
std::function< bool( void ) > before;
std::function< bool( void ) > after;
std::function< void( const sp_Mod & ) > check;
};
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
/// true if calling f() throws std::invalid_argument
template< class F >
static bool throws( F f )
{
try {
f();
}
catch( std::invalid_argument & ) {
return( true );
}
return( false );
}
/*--------------------------------------------------------------------------*/
/// true if s is among the stuff in which v is active
/** The list is scanned, so that the checks do not rely on
* ColVariable::is_active(), which test_active_list() checks apart. */
static bool active_in( const Variable & v , const ThinVarDepInterface * s )
{
for( Index i = 0 ; i < v.get_num_active() ; ++i )
if( v.get_active( i ) == s )
return( true );
return( false );
}
/*--------------------------------------------------------------------------*/
/// true if exactly the Variable of p among those of x see s as active
static bool registered( const std::vector< ColVariable > & x ,
const ThinVarDepInterface * s ,
const v_coeff_pair & p )
{
for( const auto & v : x ) {
bool in = false;
for( const auto & pr : p )
if( pr.first == & v )
in = true;
if( active_in( v , s ) != in )
return( false );
}
return( true );
}
/*--------------------------------------------------------------------------*/
/// true if the LinearFunction has exactly the pairs in p, in that order
static bool is_lf( const Function * f , const v_coeff_pair & p )
{
auto lf = dynamic_cast< const LinearFunction * >( f );
if( ( ! lf ) || ( lf->get_num_active_var() != p.size() ) )
return( false );
for( Index i = 0 ; i < p.size() ; ++i )
if( ( lf->get_active_var( i ) != p[ i ].first ) ||
( lf->get_coefficient( i ) != p[ i ].second ) )
return( false );
return( true );
}
/*--------------------------------------------------------------------------*/
/// gives s (an FRowConstraint or FRealObjective) a new LinearFunction on p
/** A LinearFunction changed under eNoMod leaves s out of step with its
* Variable, since s learns of added and removed Variable from the
* FunctionModVars: the old Function is emptied first, so that nothing is
* unregistered through it, and the Variable are then told by hand. */
template< class S >
static void rebuild( S & s , std::vector< ColVariable > & x , v_coeff_pair p )
{
s.clear();
for( auto & v : x )
if( active_in( v , & s ) )
v.remove_active( & s );
s.set_function( new LinearFunction( std::move( p ) ) , eNoMod );
}
/*--------------------------------------------------------------------------*/
/// the addresses of the elements of a container, in order
template< class L >
static Addrs addresses( const L & l )
{
Addrs rv;
for( const auto & el : l )
rv.push_back( & el );
return( rv );
}
/*--------------------------------------------------------------------------*/
/// the elements of v in the given positions
static Addrs pick( const Addrs & v , std::initializer_list< Index > pos )
{
Addrs rv;
for( auto i : pos )
rv.push_back( v[ i ] );
return( rv );
}
/*--------------------------------------------------------------------------*/
/*------------------------ CHECKING ONE Case -------------------------------*/
/*--------------------------------------------------------------------------*/
/// resets the Case with or without a Solver listening, nothing recorded
static void prepare( Rig & r , const Case & c , bool listening )
{
r.clear();
r.listen( listening );
c.reset();
r.clear();
assert( c.before() );
}
/*--------------------------------------------------------------------------*/
/// eNoMod: the change is done, nothing is issued
static void check_nomod( Rig & r , const Case & c )
{
prepare( r , c , true );
c.call( eNoMod );
assert( c.after() );
assert( r.got().empty() && r.seen().empty() );
}
/*--------------------------------------------------------------------------*/
/// with a Solver listening, one Modification with the given concerns_Block
static void check_issued( Rig & r , const Case & c , ModParam iM , bool cB )
{
prepare( r , c , true );
c.call( iM );
assert( c.after() );
assert( ( r.got().size() == 1 ) && ( r.seen().size() == 1 ) );
const auto & mod = r.got().front();
assert( mod == r.seen().front() );
assert( mod->concerns_Block() == cB );
assert( mod->get_Block() == r.block );
c.check( mod );
}
/*--------------------------------------------------------------------------*/
/// eNoBlck and eModBlck with a Solver listening
static void check_listened( Rig & r , const Case & c )
{
check_nomod( r , c );
check_issued( r , c , eNoBlck , false );
check_issued( r , c , eModBlck , true );
}
/*--------------------------------------------------------------------------*/
/// eNoBlck with no Solver listening: the change is done, nothing is issued
static void check_unheard_noblck( Rig & r , const Case & c )
{
prepare( r , c , false );
c.call( eNoBlck );
assert( c.after() );
assert( r.seen().empty() );
r.listen( true );
}
/*--------------------------------------------------------------------------*/
/// eModBlck with no Solver listening: the Block is sent the Modification
static void check_unheard_modblck( Rig & r , const Case & c )
{
prepare( r , c , false );
c.call( eModBlck );
assert( c.after() );
assert( r.seen().size() == 1 );
const auto & mod = r.seen().front();
assert( mod->concerns_Block() );
assert( mod->get_Block() == r.block );
c.check( mod );
r.clear();
r.listen( true );
}
/*--------------------------------------------------------------------------*/
/// on a channel: nothing until it is closed, then one GroupModification
static void check_channel( Rig & r , const Case & c )
{
prepare( r , c , true );
auto chnl = r.block->open_channel();
c.call( Observer::make_par( eModBlck , chnl ) );
assert( c.after() );
assert( r.got().empty() );
assert( r.seen().size() == 1 );
r.block->close_channel( chnl );
assert( r.got().size() == 1 );
auto gmod = std::dynamic_pointer_cast< GroupModification >(
r.got().front() );
assert( gmod && ( gmod->sub_Modifications().size() == 1 ) );
const auto & mod = gmod->sub_Modifications().front();
assert( mod == r.seen().front() );
assert( mod->concerns_Block() );
c.check( mod );
}
/*--------------------------------------------------------------------------*/
/// eDryRun: the change is not done, nothing is issued
static void check_dry_run( Rig & r , const Case & c )
{
prepare( r , c , true );
c.call( eDryRun );
assert( c.before() );
assert( r.got().empty() && r.seen().empty() );
}
/*--------------------------------------------------------------------------*/
/// all the above but eDryRun
static void check_all( Rig & r , const Case & c )
{
check_listened( r , c );
check_unheard_noblck( r , c );
check_unheard_modblck( r , c );
check_channel( r , c );
}
/*--------------------------------------------------------------------------*/
/// a call that changes nothing issues nothing, whatever the ModParam
static void check_nothing( Rig & r , const Case & c )
{
for( bool listening : { true , false } )
for( ModParam iM : { eDryRun , eNoMod , eNoBlck , eModBlck } ) {
prepare( r , c , listening );
c.call( iM );
assert( c.before() );
assert( r.got().empty() && r.seen().empty() );
}
r.listen( true );
}
/*--------------------------------------------------------------------------*/
/// a call that throws, changes nothing and issues nothing
static void check_throws( Rig & r , const Case & c )
{
for( ModParam iM : { eDryRun , eNoMod , eNoBlck , eModBlck } ) {
prepare( r , c , true );
assert( throws( [ & ]() { c.call( iM ); } ) );
assert( c.before() );
assert( r.got().empty() && r.seen().empty() );
}
}
/*--------------------------------------------------------------------------*/
/*--------------------------------- TESTS ----------------------------------*/
/*--------------------------------------------------------------------------*/
/* is_fixed(), set_type(), is_integer(), is_positive(), is_negative() and
* is_unitary() of a ColVariable: a VariableMod with the old and the new
* state, the fixed bit being kept by the type setters and the type by
* is_fixed(). */
static void test_ColVariable( void )
{
Rig r;
auto x = new ColVariable;
r.block->add_static_variable( *x , "x" );
// the state as ColVariable encodes it: the type above the fixed bit
auto st = []( bool fixed , int type ) {
return( var_type( type * 2 + ( fixed ? 1 : 0 ) ) );
};
auto make = [ & ]( bool f0 , int t0 , bool f1 , int t1 ,
std::function< void( ModParam ) > call ) {
return( Case{ [ = ]() {
x->is_fixed( f0 , eNoMod );
x->set_type( t0 , eNoMod );
} ,
call ,
[ = ]() { return( x->get_state() == st( f0 , t0 ) ); } ,
[ = ]() { return( x->get_state() == st( f1 , t1 ) ); } ,
[ = ]( const sp_Mod & mod ) {
auto vmod = std::dynamic_pointer_cast< VariableMod >( mod );
assert( vmod && ( vmod->variable() == x ) );
assert( vmod->old_state() == st( f0 , t0 ) );
assert( vmod->new_state() == st( f1 , t1 ) );
} } );
};
const int C = ColVariable::kContinuous;
const int I = ColVariable::kInteger;
const int B = ColVariable::kBinary;
std::vector< Case > changes = {
make( false , C , true , C , [ = ]( ModParam iM ) {
x->is_fixed( true , iM ); } ) ,
make( true , B , false , B , [ = ]( ModParam iM ) {
x->is_fixed( false , iM ); } ) ,
// the type changes, the fixed bit stays
make( true , C , true , B , [ = ]( ModParam iM ) {
x->set_type( B , iM ); } ) ,
make( false , I , false , C , [ = ]( ModParam iM ) {
x->set_type( C , iM ); } ) ,
make( true , ColVariable::kNonNegative , true , ColVariable::kNatural ,
[ = ]( ModParam iM ) { x->is_integer( true , iM ); } ) ,
make( false , B , false , B & ~I ,
[ = ]( ModParam iM ) { x->is_integer( false , iM ); } ) ,
make( false , C , false , ColVariable::kNonNegative ,
[ = ]( ModParam iM ) { x->is_positive( true , iM ); } ) ,
make( false , B , false , B & ~ColVariable::kNonNegative ,
[ = ]( ModParam iM ) { x->is_positive( false , iM ); } ) ,
make( false , I , false , ColVariable::kNegative ,
[ = ]( ModParam iM ) { x->is_negative( true , iM ); } ) ,
make( true , C , true , ColVariable::kUnitary ,
[ = ]( ModParam iM ) { x->is_unitary( true , iM ); } ) ,
make( false , B , false , B & ~ColVariable::kUnitary ,
[ = ]( ModParam iM ) { x->is_unitary( false , iM ); } ) };
// a Case that changes nothing: before() == after()
auto same = [ & ]( bool f0 , int t0 ,
std::function< void( ModParam ) > call ) {
return( make( f0 , t0 , f0 , t0 , call ) );
};
std::vector< Case > noops = {
same( true , C , [ = ]( ModParam iM ) { x->is_fixed( true , iM ); } ) ,
same( false , C , [ = ]( ModParam iM ) { x->is_fixed( false , iM ); } ) ,
same( false , B , [ = ]( ModParam iM ) { x->set_type( B , iM ); } ) ,
same( false , B , [ = ]( ModParam iM ) { x->is_integer( true , iM ); } ) ,
same( false , C , [ = ]( ModParam iM ) { x->is_positive( false , iM ); } ) ,
same( false , C , [ = ]( ModParam iM ) { x->is_negative( false , iM ); } ) ,
same( false , B , [ = ]( ModParam iM ) { x->is_unitary( true , iM ); } ) };
for( const auto & c : changes )
check_all( r , c );
for( const auto & c : noops )
check_nothing( r , c );
for( const auto & c : changes )
check_dry_run( r , c );
// a ColVariable of no Block changes all the same, and has none to tell
ColVariable y;
for( ModParam iM : { eNoMod , eNoBlck , eModBlck } ) {
y.is_fixed( false , eNoMod );
y.set_type( C , eNoMod );
y.is_fixed( true , iM );
y.set_type( B , iM );
assert( y.is_fixed() && ( y.get_type() == B ) );
}
}
/*--------------------------------------------------------------------------*/
/* set_lhs(), set_rhs(), set_both() and relax() of an FRowConstraint, and
* set_function(): a RowConstraintMod with the type of the change, a
* ConstraintMod telling relaxing from enforcing, an FRowConstraintMod. */
static void test_RowConstraint( void )
{
Rig r;
auto x = new std::vector< ColVariable >( 2 );
r.block->add_static_variable( *x , "x" );
auto X = [ x ]( Index i ) { return( & ( *x )[ i ] ); };
auto c = new FRowConstraint( nullptr , 0 , 0 ,
new LinearFunction( { { X( 0 ) , 1 } ,
{ X( 1 ) , 2 } } ) );
r.block->add_static_constraint( *c , "c" );
auto bounds = [ c ]( RHSValue l , RHSValue u ) {
return( ( c->get_lhs() == l ) && ( c->get_rhs() == u ) );
};
auto make = [ & ]( RHSValue l0 , RHSValue u0 , RHSValue l1 , RHSValue u1 ,
int type , std::function< void( ModParam ) > call ) {
return( Case{ [ = ]() {
c->set_lhs( l0 , eNoMod );
c->set_rhs( u0 , eNoMod );
} ,
call ,
[ = ]() { return( bounds( l0 , u0 ) ); } ,
[ = ]() { return( bounds( l1 , u1 ) ); } ,
[ = ]( const sp_Mod & mod ) {
auto cmod = std::dynamic_pointer_cast< RowConstraintMod >(
mod );
assert( cmod && ( cmod->constraint() == c ) );
assert( cmod->type() == type );
assert( ! std::dynamic_pointer_cast< OneVarConstraintMod >(
mod ) );
assert( ! std::dynamic_pointer_cast< FRowConstraintMod >(
mod ) );
} } );
};
const auto LHS = RowConstraintMod::eChgLHS;
const auto RHS = RowConstraintMod::eChgRHS;
const auto BTS = RowConstraintMod::eChgBTS;
std::vector< Case > changes = {
make( 0 , 5 , -1 , 5 , LHS , [ = ]( ModParam iM ) {
c->set_lhs( -1 , iM ); } ) ,
make( 0 , 5 , 0 , 7 , RHS , [ = ]( ModParam iM ) {
c->set_rhs( 7 , iM ); } ) ,
make( 0 , 5 , 0 , INF , RHS , [ = ]( ModParam iM ) {
c->set_rhs( INF , iM ); } ) ,
make( 0 , 5 , -INF , 5 , LHS , [ = ]( ModParam iM ) {
c->set_lhs( -INF , iM ); } ) ,
make( -1 , 5 , 3 , 3 , BTS , [ = ]( ModParam iM ) {
c->set_both( 3 , iM ); } ) ,
// one of the two is already there, both are said to change
make( 0 , 3 , 3 , 3 , BTS , [ = ]( ModParam iM ) {
c->set_both( 3 , iM ); } ) };
std::vector< Case > noops = {
make( 0 , 5 , 0 , 5 , LHS , [ = ]( ModParam iM ) {
c->set_lhs( 0 , iM ); } ) ,
make( 0 , 5 , 0 , 5 , RHS , [ = ]( ModParam iM ) {
c->set_rhs( 5 , iM ); } ) ,
make( 3 , 3 , 3 , 3 , BTS , [ = ]( ModParam iM ) {
c->set_both( 3 , iM ); } ) };
auto relax = [ & ]( bool from , bool to , int type ) {
return( Case{ [ = ]() { c->relax( from , eNoMod ); } ,
[ = ]( ModParam iM ) { c->relax( to , iM ); } ,
[ = ]() { return( c->is_relaxed() == from ); } ,
[ = ]() { return( c->is_relaxed() == to ); } ,
[ = ]( const sp_Mod & mod ) {
auto cmod = std::dynamic_pointer_cast< ConstraintMod >( mod );
assert( cmod && ( cmod->constraint() == c ) );
assert( cmod->type() == type );
assert( ! std::dynamic_pointer_cast< RowConstraintMod >(
mod ) );
} } );
};
changes.push_back( relax( false , true , ConstraintMod::eRelaxConst ) );
changes.push_back( relax( true , false , ConstraintMod::eEnforceConst ) );
noops.push_back( relax( true , true , ConstraintMod::eRelaxConst ) );
noops.push_back( relax( false , false , ConstraintMod::eEnforceConst ) );
// the whole Function changes, and with it the Variable c is active in
changes.push_back( Case{
[ = ]() {
rebuild( *c , *x , { { X( 0 ) , 1 } } );
} ,
[ = ]( ModParam iM ) {
auto f = new LinearFunction( { { X( 1 ) , 3 } } );
c->set_function( f , iM );
if( c->get_function() != f ) // not taken, as under eDryRun
delete f;
} ,
[ = ]() {
return( is_lf( c->get_function() , { { X( 0 ) , 1 } } ) &&
registered( *x , c , { { X( 0 ) , 1 } } ) );
} ,
[ = ]() {
return( is_lf( c->get_function() , { { X( 1 ) , 3 } } ) &&
registered( *x , c , { { X( 1 ) , 3 } } ) );
} ,
[ = ]( const sp_Mod & mod ) {
auto cmod = std::dynamic_pointer_cast< FRowConstraintMod >( mod );
assert( cmod && ( cmod->constraint() == c ) );
assert( cmod->type() == FRowConstraintMod::eFunctionChanged );
} } );
for( const auto & cs : changes )
check_all( r , cs );
for( const auto & cs : noops )
check_nothing( r , cs );
for( const auto & cs : changes )
check_dry_run( r , cs );
}
/*--------------------------------------------------------------------------*/
/* set_lhs(), set_rhs(), set_both() of BoxConstraint, LBConstraint,
* UBConstraint and LB0Constraint, and set_variable() and remove_variable*()
* of a OneVarConstraint: a OneVarConstraintMod with the RowConstraintMod
* type of the change, or eVariableChanged; a bound that the class does not
* have throws, changing and issuing nothing. */
static void test_OneVarConstraint( void )
{
Rig r;
auto x = new std::vector< ColVariable >( 2 );
r.block->add_static_variable( *x , "x" );
auto X = [ x ]( Index i ) { return( & ( *x )[ i ] ); };
auto box = new BoxConstraint( nullptr , X( 0 ) , 0 , 10 );
r.block->add_static_constraint( *box , "box" );
auto lb = new LBConstraint( nullptr , X( 0 ) , 0 );
r.block->add_static_constraint( *lb , "lb" );
auto ub = new UBConstraint( nullptr , X( 0 ) , 10 );
r.block->add_static_constraint( *ub , "ub" );
auto lb0 = new LB0Constraint( nullptr , X( 1 ) );
r.block->add_static_constraint( *lb0 , "lb0" );
// the OneVarConstraintMod of cnst of the given type
auto ovc_mod = []( OneVarConstraint * cnst , int type ) {
return( [ = ]( const sp_Mod & mod ) {
auto cmod = std::dynamic_pointer_cast< OneVarConstraintMod >( mod );
assert( cmod && ( cmod->constraint() == cnst ) );
assert( cmod->type() == type );
} );
};
auto make = [ & ]( OneVarConstraint * cnst ,
RHSValue l0 , RHSValue u0 , RHSValue l1 , RHSValue u1 ,
int type , std::function< void( ModParam ) > call ) {
return( Case{ [ = ]() {
// the bound a class does not have is left alone
if( cnst->get_lhs() != l0 )
cnst->set_lhs( l0 , eNoMod );
if( cnst->get_rhs() != u0 )
cnst->set_rhs( u0 , eNoMod );
} ,
call ,
[ = ]() {
return( ( cnst->get_lhs() == l0 ) &&
( cnst->get_rhs() == u0 ) ); } ,
[ = ]() {
return( ( cnst->get_lhs() == l1 ) &&
( cnst->get_rhs() == u1 ) ); } ,
ovc_mod( cnst , type ) } );
};
const auto LHS = RowConstraintMod::eChgLHS;
const auto RHS = RowConstraintMod::eChgRHS;
const auto BTS = RowConstraintMod::eChgBTS;
std::vector< Case > changes = {
make( box , 0 , 10 , -2 , 10 , LHS , [ = ]( ModParam iM ) {
box->set_lhs( -2 , iM ); } ) ,
make( box , 0 , 10 , 0 , 12 , RHS , [ = ]( ModParam iM ) {
box->set_rhs( 12 , iM ); } ) ,
make( box , 0 , 10 , 0 , INF , RHS , [ = ]( ModParam iM ) {
box->set_rhs( INF , iM ); } ) ,
make( box , 0 , 10 , 4 , 4 , BTS , [ = ]( ModParam iM ) {
box->set_both( 4 , iM ); } ) ,
make( box , 0 , 4 , 4 , 4 , BTS , [ = ]( ModParam iM ) {
box->set_both( 4 , iM ); } ) ,
make( lb , 0 , INF , 2 , INF , LHS , [ = ]( ModParam iM ) {
lb->set_lhs( 2 , iM ); } ) ,
make( lb , 0 , INF , -INF , INF , LHS , [ = ]( ModParam iM ) {
lb->set_lhs( -INF , iM ); } ) ,
make( ub , -INF , 10 , -INF , 8 , RHS , [ = ]( ModParam iM ) {
ub->set_rhs( 8 , iM ); } ) ,
make( lb0 , 0 , INF , 0 , 4 , RHS , [ = ]( ModParam iM ) {
lb0->set_rhs( 4 , iM ); } ) };
std::vector< Case > noops = {
make( box , 0 , 10 , 0 , 10 , LHS , [ = ]( ModParam iM ) {
box->set_lhs( 0 , iM ); } ) ,
make( box , 4 , 4 , 4 , 4 , BTS , [ = ]( ModParam iM ) {
box->set_both( 4 , iM ); } ) ,
// the bound a class does not have can be "set" to what it is
make( lb , 0 , INF , 0 , INF , RHS , [ = ]( ModParam iM ) {
lb->set_rhs( INF , iM ); } ) ,
make( ub , -INF , 10 , -INF , 10 , LHS , [ = ]( ModParam iM ) {
ub->set_lhs( -INF , iM ); } ) ,
make( lb0 , 0 , 4 , 0 , 4 , LHS , [ = ]( ModParam iM ) {
lb0->set_lhs( 0 , iM ); } ) };
// and not to anything else
std::vector< Case > throwing = {
make( lb , 0 , INF , 0 , INF , RHS , [ = ]( ModParam iM ) {
lb->set_rhs( 5 , iM ); } ) ,
make( lb , 0 , INF , 0 , INF , BTS , [ = ]( ModParam iM ) {
lb->set_both( 5 , iM ); } ) ,
make( ub , -INF , 10 , -INF , 10 , LHS , [ = ]( ModParam iM ) {
ub->set_lhs( 1 , iM ); } ) ,
make( ub , -INF , 10 , -INF , 10 , BTS , [ = ]( ModParam iM ) {
ub->set_both( 1 , iM ); } ) ,
make( lb0 , 0 , 4 , 0 , 4 , LHS , [ = ]( ModParam iM ) {
lb0->set_lhs( 1 , iM ); } ) };
// the ColVariable of box: from X( 0 ) to what the call leaves
auto var = [ & ]( ColVariable * to ,
std::function< void( ModParam ) > call ) {
return( Case{ [ = ]() { box->set_variable( X( 0 ) , eNoMod ); } ,
call ,
[ = ]() {
return( ( box->get_active_var( 0 ) == X( 0 ) ) &&
active_in( *X( 0 ) , box ) &&
( ! active_in( *X( 1 ) , box ) ) ); } ,
[ = ]() {
return( ( box->get_active_var( 0 ) == to ) &&
( box->get_num_active_var() == ( to ? 1 : 0 ) ) &&
( active_in( *X( 0 ) , box ) == ( to == X( 0 ) ) ) &&
( active_in( *X( 1 ) , box ) == ( to == X( 1 ) ) ) ); } ,
ovc_mod( box , OneVarConstraintMod::eVariableChanged ) } );
};
changes.push_back( var( X( 1 ) , [ = ]( ModParam iM ) {
box->set_variable( X( 1 ) , iM ); } ) );
changes.push_back( var( nullptr , [ = ]( ModParam iM ) {
box->set_variable( nullptr , iM ); } ) );
changes.push_back( var( nullptr , [ = ]( ModParam iM ) {
box->remove_variable( 0 , iM ); } ) );
changes.push_back( var( nullptr , [ = ]( ModParam iM ) {
box->remove_variables( Range( 0 , 1 ) , iM ); } ) );
// the empty Subset means all of them
changes.push_back( var( nullptr , [ = ]( ModParam iM ) {
box->remove_variables( Subset() , false , iM ); } ) );
changes.push_back( var( nullptr , [ = ]( ModParam iM ) {
box->remove_variables( Subset( { 0 } ) , true ,
iM ); } ) );
noops.push_back( var( X( 0 ) , [ = ]( ModParam iM ) {
box->set_variable( X( 0 ) , iM ); } ) );
for( const auto & c : changes )
check_all( r , c );
for( const auto & c : noops )
check_nothing( r , c );
for( const auto & c : throwing )
check_throws( r , c );
for( const auto & c : changes )
check_dry_run( r , c );
}
/*--------------------------------------------------------------------------*/
/* set_sense() of an FRealObjective and set_function(), and set_objective()
* of the Block: an ObjectiveMod eSetMin / eSetMax, an FRealObjectiveMod, a
* BlockMod. */
static void test_Objective( void )
{
Rig r;
auto x = new std::vector< ColVariable >( 2 );
r.block->add_static_variable( *x , "x" );
auto X = [ x ]( Index i ) { return( & ( *x )[ i ] ); };
auto obj = new FRealObjective( nullptr ,
new LinearFunction( { { X( 0 ) , 1 } } ) );
r.objective = obj;
r.block->set_objective( obj , eNoMod );
auto sense = [ & ]( int from , int to , int type ) {
return( Case{ [ = ]() { obj->set_sense( from , eNoMod ); } ,
[ = ]( ModParam iM ) { obj->set_sense( to , iM ); } ,
[ = ]() { return( obj->get_sense() == from ); } ,
[ = ]() { return( obj->get_sense() == to ); } ,
[ = ]( const sp_Mod & mod ) {
auto omod = std::dynamic_pointer_cast< ObjectiveMod >( mod );
assert( omod && ( omod->of() == obj ) );
assert( omod->type() == type );
assert( ! std::dynamic_pointer_cast< FRealObjectiveMod >(
mod ) );
} } );
};
std::vector< Case > changes = {
sense( Objective::eMin , Objective::eMax , ObjectiveMod::eSetMax ) ,
sense( Objective::eMax , Objective::eMin , ObjectiveMod::eSetMin ) ,
Case{ [ = ]() { rebuild( *obj , *x , { { X( 0 ) , 1 } } ); } ,
[ = ]( ModParam iM ) {
auto f = new LinearFunction( { { X( 1 ) , 2 } } );
obj->set_function( f , iM );
if( obj->get_function() != f ) // not taken, as under eDryRun
delete f;
} ,
[ = ]() {
return( is_lf( obj->get_function() , { { X( 0 ) , 1 } } ) &&
registered( *x , obj , { { X( 0 ) , 1 } } ) ); } ,
[ = ]() {
return( is_lf( obj->get_function() , { { X( 1 ) , 2 } } ) &&
registered( *x , obj , { { X( 1 ) , 2 } } ) ); } ,
[ = ]( const sp_Mod & mod ) {
auto omod = std::dynamic_pointer_cast< FRealObjectiveMod >( mod );
assert( omod && ( omod->of() == obj ) );
assert( omod->type() == FRealObjectiveMod::eFunctionChanged );
} } };
std::vector< Case > noops = {
sense( Objective::eMin , Objective::eMin , ObjectiveMod::eSetMin ) ,
sense( Objective::eMax , Objective::eMax , ObjectiveMod::eSetMax ) };
for( const auto & c : changes )
check_all( r , c );
for( const auto & c : noops )
check_nothing( r , c );
for( const auto & c : changes )
check_dry_run( r , c );
// the Objective of the Block changes whole
FRealObjective other;
Case swap{ [ & ]() { r.block->set_objective( obj , eNoMod ); } ,
[ & ]( ModParam iM ) { r.block->set_objective( & other , iM ); } ,
[ & ]() { return( r.block->get_objective() == obj ); } ,
[ & ]() {
return( ( r.block->get_objective() == & other ) &&
( other.get_Block() == r.block ) ); } ,
[ & ]( const sp_Mod & mod ) {
auto bmod = std::dynamic_pointer_cast< BlockMod >( mod );
assert( bmod && ( bmod->get_Block() == r.block ) );
} };
check_listened( r , swap );
check_unheard_noblck( r , swap );
check_unheard_modblck( r , swap );
check_channel( r , swap );
check_dry_run( r , swap );
r.block->set_objective( obj , eNoMod );
r.clear();
}
/*--------------------------------------------------------------------------*/
/* add_variable(), add_variables(), modify_coefficient(),
* modify_coefficients() by Range and by Subset, set_constant_term() of a
* LinearFunction within an FRowConstraint of the Block, and the
* remove_variable() and remove_variables() by Range and by Subset of the
* FRowConstraint, which are how the Variable of its Function are removed:
* the LinearFunctionModVarsAddd, C05FunctionModVarsRngd / Sbst,
* C05FunctionModLinRngd / Sbst and C05FunctionMod they issue, which reach
* the Block through the FRowConstraint. The same for a few of them within
* an FRealObjective.
*
* Adding to the LinearFunction under eNoMod leaves the FRowConstraint
* unaware of the new Variable, which is what eNoMod means (neither Block nor
* Solver will need it); the registration is thus checked only when the
* Modification is issued. Removing through the FRowConstraint keeps it in
* step under every ModParam. */
static void test_LinearFunction( void )
{
Rig r;
auto x = new std::vector< ColVariable >( 4 );
r.block->add_static_variable( *x , "x" );
auto X = [ x ]( Index i ) { return( & ( *x )[ i ] ); };
auto c = new FRowConstraint;
r.block->add_static_constraint( *c , "c" );
auto lf = [ c ]() {
return( static_cast< LinearFunction * >( c->get_function() ) );
};
const v_coeff_pair P1 = { { X( 0 ) , 1 } };
const v_coeff_pair P2 = { { X( 0 ) , 1 } , { X( 1 ) , 2 } };
const v_coeff_pair P3 = { { X( 0 ) , 1 } , { X( 1 ) , 2 } , { X( 2 ) , 3 } };
const v_coeff_pair P4 = { { X( 0 ) , 1 } , { X( 1 ) , 2 } , { X( 2 ) , 3 } ,
{ X( 3 ) , 4 } };
// from the pairs p0 to the pairs p1; the registration of c with its
// Variable is part of the state after if the call is by c itself
auto make = [ & ]( const v_coeff_pair & p0 , const v_coeff_pair & p1 ,
bool by_c , std::function< void( ModParam ) > call ,
std::function< void( const sp_Mod & ) > check ) {
return( Case{ [ = ]() { rebuild( *c , *x , p0 ); } ,
call ,
[ = ]() { return( is_lf( c->get_function() , p0 ) &&
registered( *x , c , p0 ) ); } ,
[ = ]() {
return( is_lf( c->get_function() , p1 ) &&
( ( ! by_c ) || registered( *x , c , p1 ) ) ); } ,
[ = ]( const sp_Mod & mod ) {
// the Modification went through c, which knows by now
assert( registered( *x , c , p1 ) );
check( mod );
} } );
};
// the LinearFunctionModVarsAddd of the given Variable and coefficients
auto added = [ = ]( Vec_p_Var vars , LinearFunction::v_coeff coeff ,
Index first ) {
return( [ = ]( const sp_Mod & mod ) {
auto fmod = std::dynamic_pointer_cast< LinearFunctionModVarsAddd >( mod );
assert( fmod && ( fmod->function() == lf() ) );
assert( fmod->added() );
assert( ( fmod->vars() == vars ) && ( fmod->coeff() == coeff ) );
assert( fmod->first() == first );
assert( fmod->shift() == 0 );
} );
};
// the C05FunctionModVarsRngd of the given Variable and Range
auto rmvd_rngd = [ = ]( Vec_p_Var vars , Range range ) {
return( [ = ]( const sp_Mod & mod ) {
auto fmod = std::dynamic_pointer_cast< C05FunctionModVarsRngd >( mod );
assert( fmod && ( fmod->function() == lf() ) );
assert( ! fmod->added() );
assert( ( fmod->vars() == vars ) && ( fmod->range() == range ) );
assert( fmod->shift() == 0 );
} );
};
// the C05FunctionModVarsSbst of the given Variable and Subset
auto rmvd_sbst = [ = ]( Vec_p_Var vars , Subset subset ) {
return( [ = ]( const sp_Mod & mod ) {
auto fmod = std::dynamic_pointer_cast< C05FunctionModVarsSbst >( mod );
assert( fmod && ( fmod->function() == lf() ) );
assert( ! fmod->added() );
assert( ( fmod->vars() == vars ) && ( fmod->subset() == subset ) );
assert( fmod->shift() == 0 );
} );
};
// the C05FunctionModLinRngd of the given Variable, deltas and Range
auto lin_rngd = [ = ]( Vec_p_Var vars , Vec_FV delta , Range range ) {
return( [ = ]( const sp_Mod & mod ) {
auto fmod = std::dynamic_pointer_cast< C05FunctionModLinRngd >( mod );
assert( fmod && ( fmod->function() == lf() ) );
assert( ( fmod->vars() == vars ) && ( fmod->delta() == delta ) );
assert( fmod->range() == range );
assert( std::isnan( fmod->shift() ) );
} );
};
// the C05FunctionModLinSbst of the given Variable, deltas and Subset,
// matched as triples whatever the order the Modification keeps them in
auto lin_sbst = [ = ]( Vec_p_Var vars , Vec_FV delta , Subset subset ) {
return( [ = ]( const sp_Mod & mod ) {
auto fmod = std::dynamic_pointer_cast< C05FunctionModLinSbst >( mod );
assert( fmod && ( fmod->function() == lf() ) );
assert( std::isnan( fmod->shift() ) );
assert( fmod->subset().size() == subset.size() );
assert( ( fmod->vars().size() == vars.size() ) &&
( fmod->delta().size() == delta.size() ) );
for( Index k = 0 ; k < subset.size() ; ++k ) {
Index h = 0;
while( ( h < subset.size() ) && ( fmod->subset()[ h ] != subset[ k ] ) )
++h;
assert( h < subset.size() );
assert( ( fmod->vars()[ h ] == vars[ k ] ) &&
( fmod->delta()[ h ] == delta[ k ] ) );
}
} );
};
std::vector< Case > changes = {
// adding
make( P2 , P3 , false , [ = ]( ModParam iM ) {
lf()->add_variable( X( 2 ) , 3 , iM ); } ,
added( { X( 2 ) } , { 3 } , 2 ) ) ,
make( P2 , P4 , false , [ = ]( ModParam iM ) {
lf()->add_variables( { { X( 2 ) , 3 } ,
{ X( 3 ) , 4 } } , iM ); } ,
added( { X( 2 ) , X( 3 ) } , { 3 , 4 } , 2 ) ) ,
// to an empty LinearFunction
make( {} , P2 , false , [ = ]( ModParam iM ) {
lf()->add_variables( v_coeff_pair( P2 ) , iM ); } ,