-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_LinearFunction.cpp
More file actions
1137 lines (966 loc) · 37.7 KB
/
Copy pathtests_LinearFunction.cpp
File metadata and controls
1137 lines (966 loc) · 37.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 LinearFunction.
* They test stuff not already tested with Function.
*
* \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 <cmath>
#include <iostream>
#include <random>
#include <stdexcept>
#include <vector>
#include "AbstractBlock.h"
#include "FRealObjective.h"
#include "FRowConstraint.h"
#include "FakeSolver.h"
#include "LinearFunction.h"
#include "Observer.h"
// the checks compiled into the library headers exist only without NDEBUG
#ifdef NDEBUG
#define LIB_NDEBUG
#endif
// last, so that the headers above are read as the library was compiled
#include "TestAssert.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------------ CLASSES -----------------------------------*/
/*--------------------------------------------------------------------------*/
/// an Observer that records every Modification it is sent
class Recorder : public Observer
{
public:
Block * get_Block( void ) const override { return( nullptr ); }
bool anyone_there( void ) const override { return( true ); }
void add_Modification( sp_Mod mod , ChnlName chnl = 0 ) override {
mods.push_back( mod );
}
ChnlName open_channel( ChnlName chnl = 0 ,
GroupModification * gmpmod = nullptr ) override {
return( 0 );
}
void close_channel( ChnlName chnl , bool force = false ) override {}
void set_default_channel( ChnlName chnl = 0 ) override {}
Lst_sp_Mod mods; ///< what has been sent, in order
};
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
using Index = LinearFunction::Index;
using Range = LinearFunction::Range;
using Subset = LinearFunction::Subset;
using Coefficient = LinearFunction::Coefficient;
/// the generator of the coefficients and values, seeded once and for all
static std::mt19937 rng( 1234 );
static LinearFunction::Coefficient get_random_coeff()
{
std::uniform_real_distribution< double > unif( -100 , 100 );
return( unif( rng ) );
}
/*--------------------------------------------------------------------------*/
/// the number of soft checks that failed
static int n_failed = 0;
/// a check that reports what fails and lets the other ones run
/** Used where a failure is a defect of the library that the test documents:
* it prints what is wrong, and main() returns non-zero at the end. */
static void check( bool ok , const char * what )
{
if( ok )
return;
std::cerr << "LinearFunction_test FAILED: " << what << std::endl;
++n_failed;
}
/*--------------------------------------------------------------------------*/
/// true if calling f() throws an exception of type E
template< class E , class F >
static bool throws( F f )
{
try {
f();
}
catch( E & ) {
return( true );
}
catch( ... ) {
return( false );
}
return( false );
}
/*--------------------------------------------------------------------------*/
/// f with the Variable of vars, coefficient i + 1 and value 2 i - 3 each
static void fill( LinearFunction & f , std::vector< ColVariable > & vars )
{
LinearFunction::v_coeff_pair p( vars.size() );
for( Index i = 0 ; i < vars.size() ; ++i ) {
vars[ i ].set_value( 2.0 * i - 3 );
p[ i ] = { & vars[ i ] , Coefficient( i + 1 ) };
}
f.add_variables( std::move( p ) );
}
/*--------------------------------------------------------------------------*/
/// the value of c + sum_i a_i x_i summed in the order of the Variable
static double value_of( LinearFunction & f )
{
double v = f.get_constant_term();
for( Index i = 0 ; i < f.get_num_active_var() ; ++i )
v += static_cast< ColVariable * >( f.get_active_var( i ) )->get_value() *
f.get_coefficient( i );
return( v );
}
/*--------------------------------------------------------------------------*/
/* Adding and removing single Variable and sets of them, the value being
* checked at a point where every Variable is non-zero. */
void runAllTests()
{
// test AddsVariable
LinearFunction add_fun;
ColVariable v;
v.set_value( get_random_coeff() );
LinearFunction::Coefficient c = get_random_coeff();
add_fun.add_variable( &v , c );
assert( add_fun.get_num_active_var() == 1 );
assert( add_fun.is_active( &v ) == 0 );
assert( add_fun.get_active_var( 0 ) == &v );
assert( add_fun.get_coefficient( 0 ) == c );
assert( add_fun.compute( true ) == LinearFunction::kOK );
assert( add_fun.get_value() == v.get_value() * c );
// test AddsVariables
LinearFunction::v_coeff_pair add_vars( 10 );
LinearFunction adds_fun;
for( auto & p : add_vars ) {
p.first = new ColVariable();
p.first->set_value( get_random_coeff() );
p.second = get_random_coeff();
}
LinearFunction::v_coeff_pair add_check = add_vars;
adds_fun.add_variables( std::move( add_vars ) );
assert( adds_fun.get_num_active_var() == add_check.size() );
for( int i = 0 ; i < add_check.size() ; ++i ) {
assert( adds_fun.is_active( add_check[ i ].first ) == i );
assert( adds_fun.get_active_var( i ) == add_check[ i ].first );
assert( adds_fun.get_coefficient( i ) == add_check[ i ].second );
}
assert( adds_fun.compute( true ) == LinearFunction::kOK );
LinearFunction::FunctionValue sum = 0;
for( auto & i : add_check )
sum += i.first->get_value() * i.second;
assert( sum != 0 );
assert( adds_fun.get_value() == sum );
// test RemovesVariable
LinearFunction del_fun;
ColVariable v1 , v2;
v1.set_value( get_random_coeff() );
v2.set_value( get_random_coeff() );
LinearFunction::Coefficient c1 = get_random_coeff();
LinearFunction::Coefficient c2 = get_random_coeff();
del_fun.add_variable( &v1 , c1 );
del_fun.add_variable( &v2 , c2 );
assert( del_fun.get_num_active_var() == 2 );
assert( del_fun.is_active( &v1 ) == 0 );
assert( del_fun.is_active( &v2 ) == 1 );
assert( del_fun.get_active_var( 0 ) == &v1 );
assert( del_fun.get_coefficient( 0 ) == c1 );
assert( del_fun.get_active_var( 1 ) == &v2 );
assert( del_fun.get_coefficient( 1 ) == c2 );
del_fun.remove_variable( 0 );
assert( del_fun.is_active( &v1 ) == Inf< LinearFunction::Index >() );
assert( del_fun.is_active( &v2 ) == 0 );
assert( del_fun.get_active_var( 0 ) == &v2 );
assert( del_fun.get_coefficient( 0 ) == c2 );
assert( del_fun.get_num_active_var() == 1 );
assert( del_fun.compute( true ) == LinearFunction::kOK );
assert( del_fun.get_value() == v2.get_value() * c2 );
// test RemovesVariables
LinearFunction::v_coeff_pair del_vars( 10 );
LinearFunction dels_fun;
for( auto & p : del_vars ) {
p.first = new ColVariable();
p.first->set_value( get_random_coeff() );
p.second = get_random_coeff();
}
LinearFunction::v_coeff_pair del_check = del_vars;
dels_fun.add_variables( std::move( del_vars ) );
assert( dels_fun.get_num_active_var() == 10 );
LinearFunction::Range range{ 1 , 9 };
dels_fun.remove_variables( range );
assert( dels_fun.get_num_active_var() == 2 );
assert( dels_fun.is_active( del_check[ 0 ].first ) == 0 );
assert( dels_fun.get_active_var( 0 ) == del_check[ 0 ].first );
assert( dels_fun.get_coefficient( 0 ) == del_check[ 0 ].second );
assert( dels_fun.is_active( del_check[ 9 ].first ) == 1 );
assert( dels_fun.get_active_var( 1 ) == del_check[ 9 ].first );
assert( dels_fun.get_coefficient( 1 ) == del_check[ 9 ].second );
assert( dels_fun.compute( true ) == LinearFunction::kOK );
assert( dels_fun.get_value() ==
del_check[ 0 ].first->get_value() * del_check[ 0 ].second +
del_check[ 9 ].first->get_value() * del_check[ 9 ].second );
for( auto & p : add_check )
delete p.first;
for( auto & p : del_check )
delete p.first;
}
/*--------------------------------------------------------------------------*/
/* What the tests above never touch: a LinearFunction with nothing in it, the
* constant term, a coefficient that is zero, and the three ways of removing
* Variable at their own edges, one of which means the opposite of what it
* looks like. */
static void test_edge_cases( void )
{
// ---- a function with no Variable at all ----------------------------
LinearFunction empty;
assert( empty.get_num_active_var() == 0 );
assert( empty.get_constant_term() == 0 );
assert( empty.compute( true ) == LinearFunction::kOK );
assert( empty.get_value() == 0 );
// a Variable that was never added is not active, and answering with Inf
// is what tells it apart from the one that sits at index 0
ColVariable stranger;
assert( empty.is_active( & stranger ) ==
Inf< LinearFunction::Index >() );
// ---- the constant term ---------------------------------------------
// it is the value of a function of no Variable, and it is added to the
// value of one that has some: an affine function, not a linear one
empty.set_constant_term( 3.5 );
assert( empty.get_constant_term() == 3.5 );
assert( empty.compute( true ) == LinearFunction::kOK );
assert( empty.get_value() == 3.5 );
ColVariable v;
v.set_value( 2 );
empty.add_variable( & v , 4 );
assert( empty.compute( true ) == LinearFunction::kOK );
assert( empty.get_value() == 3.5 + 8 );
// ---- a coefficient of zero -----------------------------------------
// the Variable is active all the same: it is in the function, it just
// does not move its value, and whoever walks the pairs sees it
LinearFunction zero;
ColVariable z;
z.set_value( 7 );
zero.add_variable( & z , 0 );
assert( zero.get_num_active_var() == 1 );
assert( zero.is_active( & z ) == 0 );
assert( zero.get_coefficient( 0 ) == 0 );
assert( zero.compute( true ) == LinearFunction::kOK );
assert( zero.get_value() == 0 );
// and a coefficient can be changed to something that does move it
zero.modify_coefficient( 0 , 2 );
assert( zero.get_coefficient( 0 ) == 2 );
assert( zero.compute( true ) == LinearFunction::kOK );
assert( zero.get_value() == 14 );
// ---- removing by range ---------------------------------------------
auto ten = []( LinearFunction & f , std::vector< ColVariable > & vars ) {
LinearFunction::v_coeff_pair p( vars.size() );
for( LinearFunction::Index i = 0 ; i < vars.size() ; ++i )
p[ i ] = { & vars[ i ] , double( i + 1 ) };
f.add_variables( std::move( p ) );
};
std::vector< ColVariable > vars( 10 );
{ // an empty range removes nothing
LinearFunction f;
ten( f , vars );
f.remove_variables( LinearFunction::Range{ 4 , 4 } );
assert( f.get_num_active_var() == 10 );
}
{ // a range past the end stops at the end
LinearFunction f;
ten( f , vars );
f.remove_variables( LinearFunction::Range{ 8 , 1000 } );
assert( f.get_num_active_var() == 8 );
assert( f.get_active_var( 7 ) == & vars[ 7 ] );
}
{ // and one that covers it all empties it
LinearFunction f;
ten( f , vars );
f.remove_variables( LinearFunction::Range{ 0 , 10 } );
assert( f.get_num_active_var() == 0 );
assert( f.compute( true ) == LinearFunction::kOK );
assert( f.get_value() == 0 );
}
// ---- removing by subset --------------------------------------------
{ // the last one, which leaves it empty
LinearFunction f;
ColVariable only;
f.add_variable( & only , 1 );
f.remove_variable( 0 );
assert( f.get_num_active_var() == 0 );
assert( f.is_active( & only ) == Inf< LinearFunction::Index >() );
}
{ // an unordered subset, said to be unordered
LinearFunction f;
ten( f , vars );
f.remove_variables( LinearFunction::Subset{ 7 , 1 , 4 } , false );
assert( f.get_num_active_var() == 7 );
assert( f.is_active( & vars[ 1 ] ) == Inf< LinearFunction::Index >() );
assert( f.is_active( & vars[ 4 ] ) == Inf< LinearFunction::Index >() );
assert( f.is_active( & vars[ 7 ] ) == Inf< LinearFunction::Index >() );
assert( f.is_active( & vars[ 0 ] ) == 0 );
}
{
/* ⚠️ An EMPTY subset removes EVERY Variable, which is the opposite of
* what an empty range does and of what the word suggests: it is written
* in the comments of remove_variables( Subset ), and a caller that
* builds the subset and finds it empty has to know it. */
LinearFunction f;
ten( f , vars );
f.remove_variables( LinearFunction::Subset{} );
assert( f.get_num_active_var() == 0 );
}
// ---- modifying coefficients with someone listening ------------------
// the part of NCoef that is not used does not reach the Modification,
// whose delta() has one entry per Variable changed
{ // a Range past the end stops at the end
Recorder rec;
LinearFunction f;
ten( f , vars );
f.register_Observer( & rec );
f.modify_coefficients( { 20 , 30 , 40 } ,
LinearFunction::Range{ 9 , 12 } );
assert( f.get_coefficient( 9 ) == 20 );
assert( rec.mods.size() == 1 );
auto mod = std::dynamic_pointer_cast< C05FunctionModLinRngd >(
rec.mods.front() );
assert( mod );
assert( mod->range() == LinearFunction::Range( 9 , 10 ) );
assert( mod->delta() == std::vector< double >( { 10 } ) );
}
{ // an NCoef longer than the Subset
Recorder rec;
LinearFunction f;
ten( f , vars );
f.register_Observer( & rec );
f.modify_coefficients( { 20 , 30 , 40 } ,
LinearFunction::Subset{ 3 , 0 } );
assert( f.get_coefficient( 3 ) == 20 );
assert( f.get_coefficient( 0 ) == 30 );
assert( rec.mods.size() == 1 );
auto mod = std::dynamic_pointer_cast< C05FunctionModLinSbst >(
rec.mods.front() );
assert( mod );
assert( mod->subset() == LinearFunction::Subset( { 0 , 3 } ) );
assert( mod->delta() == std::vector< double >( { 29 , 16 } ) );
}
{ // a wrong index changes nothing
Recorder rec;
LinearFunction f;
ten( f , vars );
f.register_Observer( & rec );
bool thrown = false;
try {
f.modify_coefficients( { 20 , 30 } , LinearFunction::Subset{ 0 , 10 } );
}
catch( std::invalid_argument & ) {
thrown = true;
}
assert( thrown );
assert( f.get_coefficient( 0 ) == 1 );
assert( rec.mods.empty() );
}
}
/*--------------------------------------------------------------------------*/
/* modify_coefficients() over a Range, a Subset and an empty Subset: the
* coefficients named change and the others do not, and the value follows.
* An empty Subset changes nothing (it is not "all of them", as it is for
* remove_variables()), and an index out of range throws. */
static void test_modify_coefficients( void )
{
std::vector< ColVariable > vars( 5 );
LinearFunction f;
fill( f , vars ); // coefficients 1 2 3 4 5
// a Range
f.modify_coefficients( { 10 , 20 } , Range( 1 , 3 ) );
assert( f.get_coefficient( 0 ) == 1 );
assert( f.get_coefficient( 1 ) == 10 );
assert( f.get_coefficient( 2 ) == 20 );
assert( f.get_coefficient( 3 ) == 4 );
assert( f.compute( true ) == LinearFunction::kOK );
assert( f.get_value() == value_of( f ) );
// a Range past the end stops at the end
f.modify_coefficients( { 40 , 50 } , Range( 3 , 1000 ) );
assert( f.get_coefficient( 3 ) == 40 );
assert( f.get_coefficient( 4 ) == 50 );
// an empty Range changes nothing
f.modify_coefficients( {} , Range( 2 , 2 ) );
assert( f.get_coefficient( 2 ) == 20 );
// an unordered Subset: NCoef[ k ] goes to nms[ k ]
f.modify_coefficients( { -4 , 0.5 } , Subset( { 4 , 0 } ) , false );
assert( f.get_coefficient( 0 ) == 0.5 );
assert( f.get_coefficient( 4 ) == -4 );
assert( f.get_coefficient( 1 ) == 10 );
assert( f.compute( true ) == LinearFunction::kOK );
assert( f.get_value() == value_of( f ) );
// an empty Subset changes nothing
f.modify_coefficients( {} , Subset() );
assert( f.get_num_active_var() == 5 );
assert( f.get_coefficient( 0 ) == 0.5 );
assert( f.get_coefficient( 4 ) == -4 );
// a wrong index, a short NCoef and a wrong single index throw
assert( throws< std::invalid_argument >( [ & f ]() {
f.modify_coefficients( { 1 } , Subset( { 5 } ) ); } ) );
assert( throws< std::invalid_argument >( [ & f ]() {
f.modify_coefficients( { 1 } , Subset( { 0 , 1 } ) ); } ) );
assert( throws< std::invalid_argument >( [ & f ]() {
f.modify_coefficients( { 1 } , Range( 0 , 2 ) ); } ) );
assert( throws< std::invalid_argument >( [ & f ]() {
f.modify_coefficient( 5 , 1 ); } ) );
}
/*--------------------------------------------------------------------------*/
/* The linearization of a LinearFunction is the function itself: the
* coefficients over any Range or Subset, dense or sparse, are the
* coefficients whatever the point, the constant is the constant term, and
* the Hessian is zero. */
static void test_linearization( void )
{
std::vector< ColVariable > vars( 5 );
LinearFunction f;
fill( f , vars );
f.modify_coefficient( 2 , 0 ); // a zero one, left out of a sparse g
f.set_constant_term( -7.25 );
assert( f.compute( true ) == LinearFunction::kOK );
// dense, the whole Range and a part of it
std::vector< double > g( 5 , 1e30 );
f.get_linearization_coefficients( g.data() );
for( Index i = 0 ; i < 5 ; ++i )
assert( g[ i ] == f.get_coefficient( i ) );
std::vector< double > gr( 2 , 1e30 );
f.get_linearization_coefficients( gr.data() , Range( 3 , 5 ) );
assert( ( gr[ 0 ] == f.get_coefficient( 3 ) ) &&
( gr[ 1 ] == f.get_coefficient( 4 ) ) );
// dense, a Subset, in the order it is given
std::vector< double > gs( 3 , 1e30 );
f.get_linearization_coefficients( gs.data() , Subset( { 4 , 0 , 2 } ) );
assert( ( gs[ 0 ] == f.get_coefficient( 4 ) ) &&
( gs[ 1 ] == f.get_coefficient( 0 ) ) && ( gs[ 2 ] == 0 ) );
// sparse, a Range and a Subset
LinearFunction::SparseVector sg;
f.get_linearization_coefficients( sg , Range( 1 , 4 ) );
assert( sg.size() == 5 );
assert( sg.nonZeros() == 2 ); // index 2 is zero
assert( ( sg.coeff( 1 ) == f.get_coefficient( 1 ) ) &&
( sg.coeff( 3 ) == f.get_coefficient( 3 ) ) &&
( sg.coeff( 0 ) == 0 ) );
LinearFunction::SparseVector ss;
f.get_linearization_coefficients( ss , Subset( { 0 , 4 } ) );
assert( ss.nonZeros() == 2 );
assert( ( ss.coeff( 0 ) == f.get_coefficient( 0 ) ) &&
( ss.coeff( 4 ) == f.get_coefficient( 4 ) ) );
// a wrong index in a Subset throws
assert( throws< std::invalid_argument >( [ & f , & gs ]() {
f.get_linearization_coefficients( gs.data() , Subset( { 5 } ) ); } ) );
// the point does not matter
for( auto & v : vars )
v.set_value( v.get_value() * 3 + 1 );
assert( f.compute( true ) == LinearFunction::kOK );
std::vector< double > g2( 5 );
f.get_linearization_coefficients( g2.data() );
assert( g2 == g );
// the constant is the constant term, and constant + g x is the value
assert( f.get_linearization_constant() == -7.25 );
double lin = f.get_linearization_constant();
for( Index i = 0 ; i < 5 ; ++i )
lin += g2[ i ] * vars[ i ].get_value();
assert( lin == f.get_value() );
// the Hessian is zero, dense of the right size and sparse
f.compute_hessian_approximation();
C15Function::DenseHessian dh;
f.get_hessian_approximation( dh );
assert( ( dh.rows() == 5 ) && ( dh.cols() == 5 ) );
assert( dh.isZero( 0 ) );
C15Function::SparseHessian sh( 5 , 5 );
sh.insert( 1 , 1 ) = 3; // whatever was there goes
f.get_hessian_approximation( sh );
assert( sh.nonZeros() == 0 );
assert( f.is_convex() && f.is_concave() );
}
/*--------------------------------------------------------------------------*/
/* A Variable given twice: add_variables() does not check it (as its comments
* say), so the function has it twice and its value counts both, whereas the
* constructor rejects it when the library is compiled with its checks. */
static void test_duplicate_variable( void )
{
ColVariable x;
x.set_value( 3 );
LinearFunction f;
f.add_variables( { { & x , 2 } , { & x , 5 } } );
assert( f.get_num_active_var() == 2 );
assert( f.is_active( & x ) == 0 ); // the first one
assert( f.get_active_var( 1 ) == & x );
assert( f.compute( true ) == LinearFunction::kOK );
assert( f.get_value() == 3 * 2 + 3 * 5 );
#ifndef LIB_NDEBUG
assert( throws< std::invalid_argument >( [ & x ]() {
LinearFunction g( { { & x , 2 } , { & x , 5 } } ); } ) );
#endif
}
/*--------------------------------------------------------------------------*/
/* remove_variable() with no Variable at the index throws, and leaves the
* function as it was. */
static void test_remove_out_of_range( void )
{
std::vector< ColVariable > vars( 3 );
LinearFunction f;
fill( f , vars );
assert( throws< std::logic_error >( [ & f ]() { f.remove_variable( 3 ); } ) );
assert( throws< std::logic_error >( [ & f ]() {
f.remove_variable( Inf< Index >() ); } ) );
assert( f.get_num_active_var() == 3 );
LinearFunction empty;
assert( throws< std::logic_error >( [ & empty ]() {
empty.remove_variable( 0 ); } ) );
// a Subset with an index out of range throws as well
assert( throws< std::invalid_argument >( [ & f ]() {
f.remove_variables( Subset( { 0 , 3 } ) ); } ) );
}
/*--------------------------------------------------------------------------*/
/* map_active() gives the index of each Variable asked for, and throws if one
* is not active; with ordered == true the Variable asked for are sorted by
* address, a subset of the active ones, which may be active in any order.
* map_index() gives the current index of Variable known by an old one. */
static void test_map_active_and_index( void )
{
std::vector< ColVariable > x( 4 ); // &x[ 0 ] < &x[ 1 ] < ...
// active in the order x2 x0 x3 x1
LinearFunction f( { { & x[ 2 ] , 1 } , { & x[ 0 ] , 2 } ,
{ & x[ 3 ] , 3 } , { & x[ 1 ] , 4 } } );
// unordered
{
Subset map;
f.map_active( { & x[ 1 ] , & x[ 2 ] , & x[ 0 ] } , map , false );
assert( map.size() == 3 );
assert( ( map[ 0 ] == 3 ) && ( map[ 1 ] == 0 ) && ( map[ 2 ] == 1 ) );
}
// a map longer than vars keeps its tail
{
Subset map( 3 , 77 );
f.map_active( { & x[ 3 ] } , map , false );
assert( ( map[ 0 ] == 2 ) && ( map[ 1 ] == 77 ) && ( map[ 2 ] == 77 ) );
}
// a Variable that is not active throws
{
ColVariable stranger;
Subset map;
assert( throws< std::invalid_argument >( [ & ]() {
f.map_active( { & x[ 0 ] , & stranger } , map , false ); } ) );
}
// ordered, all of them
{
Subset map;
f.map_active( { & x[ 0 ] , & x[ 1 ] , & x[ 2 ] , & x[ 3 ] } , map , true );
check( ( map.size() == 4 ) && ( map[ 0 ] == 1 ) && ( map[ 1 ] == 3 ) &&
( map[ 2 ] == 0 ) && ( map[ 3 ] == 2 ) ,
"map_active( ordered ) on all the active Variable" );
}
/* Ordered, a part of them: { x0 , x2 } is a set of active Variable, and
* map_active() has to give { 1 , 0 }. */
{
Subset map;
bool threw = false;
try {
f.map_active( { & x[ 0 ] , & x[ 2 ] } , map , true );
}
catch( std::exception & ) {
threw = true;
}
check( ! threw , "map_active( { x0 , x2 } , ordered ) throws although "
"both are active" );
check( ( map.size() == 2 ) && ( map[ 0 ] == 1 ) && ( map[ 1 ] == 0 ) ,
"map_active( { x0 , x2 } , ordered ) gives { 1 , 0 }" );
}
{
Subset map;
bool threw = false;
try {
f.map_active( { & x[ 0 ] , & x[ 3 ] } , map , true );
}
catch( std::exception & ) {
threw = true;
}
check( ( ! threw ) && ( map.size() == 2 ) && ( map[ 0 ] == 1 ) &&
( map[ 1 ] == 2 ) ,
"map_active( { x0 , x3 } , ordered ) gives { 1 , 2 }" );
}
// map_index(): nothing changed, each is where it was
{
auto map = f.map_index( { & x[ 0 ] , & x[ 3 ] } , Subset( { 1 , 2 } ) );
assert( ( map[ 0 ] == 1 ) && ( map[ 1 ] == 2 ) );
auto mapr = f.map_index( { & x[ 0 ] , & x[ 3 ] } , Range( 1 , 3 ) );
assert( ( mapr[ 0 ] == 1 ) && ( mapr[ 1 ] == 2 ) );
}
// map_index(): x2 removed, the others moved one to the left, and a removed
// one is Inf
f.remove_variable( 0 , eNoMod ); // now x0 x3 x1
{
auto map = f.map_index( { & x[ 2 ] , & x[ 0 ] , & x[ 3 ] , & x[ 1 ] } ,
Subset( { 0 , 1 , 2 , 3 } ) );
assert( map[ 0 ] >= f.get_num_active_var() );
assert( ( map[ 1 ] == 0 ) && ( map[ 2 ] == 1 ) && ( map[ 3 ] == 2 ) );
auto mapr = f.map_index( { & x[ 0 ] , & x[ 3 ] , & x[ 1 ] } ,
Range( 1 , 4 ) );
assert( ( mapr[ 0 ] == 0 ) && ( mapr[ 1 ] == 1 ) && ( mapr[ 2 ] == 2 ) );
}
// map_index(): x2 re-added, it is at the end
f.add_variable( & x[ 2 ] , 1 , eNoMod ); // now x0 x3 x1 x2
{
auto map = f.map_index( { & x[ 2 ] } , Subset( { 0 } ) );
assert( map[ 0 ] == 3 );
}
// sizes that do not match throw
assert( throws< std::invalid_argument >( [ & ]() {
f.map_index( { & x[ 2 ] } , Subset( { 0 , 1 } ) ); } ) );
assert( throws< std::invalid_argument >( [ & ]() {
f.map_index( { & x[ 2 ] } , Range( 0 , 2 ) ); } ) );
// on an empty function, everything is Inf
LinearFunction empty;
auto map = empty.map_index( { & x[ 2 ] } , Subset( { 0 } ) );
assert( map[ 0 ] >= empty.get_num_active_var() );
}
/*--------------------------------------------------------------------------*/
/* The iterators: on an empty function begin() is end(), both for the
* virtual v_begin()/v_end() and the plain ones, const or not; on a full one
* they walk the Variable in their order, and a clone() walks on its own. */
static void test_iterators( void )
{
LinearFunction empty;
{
auto b = empty.v_begin();
auto e = empty.v_end();
assert( *b == *e );
assert( ! ( *b != *e ) );
delete b;
delete e;
}
{
const LinearFunction & ce = empty;
auto b = ce.v_begin();
auto e = ce.v_end();
assert( *b == *e );
delete b;
delete e;
}
Index n = 0;
for( auto & v : empty ) {
( void ) v;
++n;
}
assert( n == 0 );
std::vector< ColVariable > vars( 3 );
LinearFunction f;
fill( f , vars );
Index i = 0;
for( auto it = f.begin() ; it != f.end() ; ++it , ++i )
assert( &( *it ) == & vars[ i ] );
assert( i == 3 );
// a copy assigned moves on its own, and the one it held is released
auto i1 = f.begin();
auto i2 = f.end();
i2 = i1;
++i2;
assert( &( *i1 ) == & vars[ 0 ] );
assert( &( *i2 ) == & vars[ 1 ] );
i2 = std::move( i1 );
assert( &( *i2 ) == & vars[ 0 ] );
i2 = i2; // assigned to itself, it is still valid
assert( &( *i2 ) == & vars[ 0 ] );
auto b = f.v_begin();
auto c = b->clone(); // the clone moves on its own
++( *c );
assert( &( **b ) == & vars[ 0 ] );
assert( &( **c ) == & vars[ 1 ] );
assert( *b != *c );
delete b;
delete c;
}
/*--------------------------------------------------------------------------*/
/// a note for the maintainer: what differs from the documentation, and is
/// not (yet) required of the library, hence it does not fail the test
static void note( bool ok , const char * what )
{
if( ! ok )
std::cout << "LinearFunction_test NOTE: documented but not implemented: "
<< what << std::endl;
}
/*--------------------------------------------------------------------------*/
/* eDryRun says that the change is not to be done, and hence that no
* Modification is issued [see Observer::make_par()]. Here the function sits
* in an FRowConstraint of a Block with a FakeSolver, so that a Modification
* issued would be seen. That no Modification is issued is required; that
* the change is not done is not implemented yet, and it is only noted. */
static void test_dry_run( void )
{
auto block = new AbstractBlock();
auto x = new std::vector< ColVariable >( 3 );
block->add_static_variable( *x , "x" );
auto rows = new std::vector< FRowConstraint >( 1 );
block->add_static_constraint( *rows , "c" );
auto f = new LinearFunction();
f->add_variable( &( *x )[ 0 ] , 2 , eNoMod );
( *rows )[ 0 ].set_function( f , eNoMod );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
f->modify_coefficient( 0 , 9 , eDryRun );
assert( mods.empty() );
note( f->get_coefficient( 0 ) == 2 ,
"modify_coefficient( eDryRun ) does not change the coefficient" );
f->add_variable( &( *x )[ 1 ] , 1 , eDryRun );
assert( mods.empty() );
note( f->get_num_active_var() == 1 ,
"add_variable( eDryRun ) does not add the Variable" );
f->set_constant_term( 4 , eDryRun );
assert( mods.empty() );
note( f->get_constant_term() == 0 ,
"set_constant_term( eDryRun ) does not change the constant term" );
// a Variable added all the same has not been told of the FRowConstraint,
// which would fail to leave it when the Block is deleted: take it away
// just as silently
if( f->get_num_active_var() > 1 )
f->remove_variable( 1 , eNoMod );
block->unregister_Solvers( true );
delete block;
}
/*--------------------------------------------------------------------------*/
/* The Modification issued by each change, as seen by a FakeSolver of the
* Block when the function sits in an FRowConstraint: their type, the
* Variable, the indices and the deltas they carry; eNoMod issues none, and
* a change that is not one (an empty Subset of coefficients, the same
* coefficient) issues none either. The FRowConstraint registers itself in
* the Variable added and leaves those removed. */
static std::vector< sp_Mod > take( FakeSolver * solver )
{
auto & l = solver->get_Modification_list();
std::vector< sp_Mod > v( l.begin() , l.end() );
l.clear();
return( v );
}
static void test_Modification_in_FRowConstraint( void )
{
auto block = new AbstractBlock();
auto x = new std::vector< ColVariable >( 6 );
block->add_static_variable( *x , "x" );
auto rows = new std::vector< FRowConstraint >( 1 );
block->add_static_constraint( *rows , "c" );
auto & row = ( *rows )[ 0 ];
auto f = new LinearFunction();
row.set_function( f , eNoMod );
auto solver = new FakeSolver();
block->register_Solver( solver );
take( solver );
auto X = [ x ]( Index i ) { return( &( *x )[ i ] ); };
// add_variable
f->add_variable( X( 0 ) , 1 );
{
auto m = take( solver );
assert( m.size() == 1 );
auto a = std::dynamic_pointer_cast< LinearFunctionModVarsAddd >( m[ 0 ] );
assert( a && ( a->function() == f ) && ( a->first() == 0 ) );
assert( ( a->vars().size() == 1 ) && ( a->vars()[ 0 ] == X( 0 ) ) );
assert( ( a->coeff().size() == 1 ) && ( a->coeff()[ 0 ] == 1 ) );
assert( a->concerns_Block() );
assert( X( 0 )->is_active( & row ) < X( 0 )->get_num_active() );
}
// add_variables
f->add_variables( { { X( 1 ) , 2 } , { X( 2 ) , 3 } , { X( 3 ) , 4 } ,
{ X( 4 ) , 5 } } );
{
auto m = take( solver );
assert( m.size() == 1 );
auto a = std::dynamic_pointer_cast< LinearFunctionModVarsAddd >( m[ 0 ] );
assert( a && ( a->first() == 1 ) && ( a->vars().size() == 4 ) );
assert( ( a->vars()[ 0 ] == X( 1 ) ) && ( a->vars()[ 3 ] == X( 4 ) ) );
assert( ( a->coeff()[ 0 ] == 2 ) && ( a->coeff()[ 3 ] == 5 ) );
for( Index i = 1 ; i < 5 ; ++i )
assert( X( i )->is_active( & row ) < X( i )->get_num_active() );
}
// modify_coefficient: the delta, over the Range of one
f->modify_coefficient( 2 , 10 );
{
auto m = take( solver );
assert( m.size() == 1 );
auto l = std::dynamic_pointer_cast< C05FunctionModLinRngd >( m[ 0 ] );
assert( l && ( l->function() == f ) );
assert( l->range() == Range( 2 , 3 ) );
assert( ( l->vars().size() == 1 ) && ( l->vars()[ 0 ] == X( 2 ) ) );
assert( ( l->delta().size() == 1 ) && ( l->delta()[ 0 ] == 10 - 3 ) );
assert( std::isnan( l->shift() ) );
}
// the same coefficient again is no change
f->modify_coefficient( 2 , 10 );
assert( take( solver ).empty() );
// modify_coefficients over a Range
f->modify_coefficients( { 20 , 30 } , Range( 0 , 2 ) );
{
auto m = take( solver );
assert( m.size() == 1 );
auto l = std::dynamic_pointer_cast< C05FunctionModLinRngd >( m[ 0 ] );
assert( l && ( l->range() == Range( 0 , 2 ) ) );
assert( ( l->vars()[ 0 ] == X( 0 ) ) && ( l->vars()[ 1 ] == X( 1 ) ) );
assert( ( l->delta()[ 0 ] == 20 - 1 ) && ( l->delta()[ 1 ] == 30 - 2 ) );
}
// modify_coefficients over an unordered Subset: sorted in the Modification
f->modify_coefficients( { 50 , 40 } , Subset( { 4 , 3 } ) , false );
{
auto m = take( solver );
assert( m.size() == 1 );
auto l = std::dynamic_pointer_cast< C05FunctionModLinSbst >( m[ 0 ] );
assert( l && ( l->subset() == Subset( { 3 , 4 } ) ) );
assert( ( l->vars()[ 0 ] == X( 3 ) ) && ( l->vars()[ 1 ] == X( 4 ) ) );
assert( ( l->delta()[ 0 ] == 40 - 4 ) && ( l->delta()[ 1 ] == 50 - 5 ) );
}
// modify_coefficients over an empty Subset: nothing
f->modify_coefficients( {} , Subset() );
assert( take( solver ).empty() );
// set_constant_term: a C05FunctionMod with the shift
f->set_constant_term( 2.5 );
{
auto m = take( solver );
assert( m.size() == 1 );
auto c = std::dynamic_pointer_cast< C05FunctionMod >( m[ 0 ] );
assert( c && ( c->type() == C05FunctionMod::NothingChanged ) );
assert( c->which().empty() && ( c->shift() == 2.5 ) );
}
// eNoMod: the change is done, nothing is issued (the Variable are left
// alone, since an FRowConstraint relies on the Modification to register
// itself in the Variable added)
f->modify_coefficient( 0 , -1 , eNoMod );
f->set_constant_term( 0 , eNoMod );
assert( take( solver ).empty() );
assert( f->get_coefficient( 0 ) == -1 );
// remove_variable
f->remove_variable( 1 ); // x1 goes: x0 x2 x3 x4
{
auto m = take( solver );
assert( m.size() == 1 );
auto r = std::dynamic_pointer_cast< C05FunctionModVarsRngd >( m[ 0 ] );
assert( r && ( r->function() == f ) && ( ! r->added() ) );
assert( r->range() == Range( 1 , 2 ) );
assert( ( r->vars().size() == 1 ) && ( r->vars()[ 0 ] == X( 1 ) ) );