-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_PolyhedralFunctionBlock.cpp
More file actions
247 lines (200 loc) · 8.37 KB
/
Copy pathtests_PolyhedralFunctionBlock.cpp
File metadata and controls
247 lines (200 loc) · 8.37 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
/** @file
* Unit tests for PolyhedralFunctionBlock: the size variable of its dual
* representation.
*
* The size variable of a PolyhedralFunctionBlock is the multiplier, owned by
* the father, that set_size_variable() writes into the normalization
* constraint of the dual representation. The tests check that it gets there
* whether it is given before the abstract representation exists or after,
* that a Solver reading the model by columns finds it in that row, and that
* a change of the global scale keeps its coefficient in step while leaving
* the constraint and its LinearFunction the objects they were.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "AbstractBlock.h"
#include "FRowConstraint.h"
#include "LinearFunction.h"
#include "PolyhedralFunctionBlock.h"
#include <algorithm>
#include <cmath>
#include <iostream>
// last, so that the headers above are read as the library was compiled
#include "TestAssert.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
/// the dual representation with the global scaling on: bits 0, 1 and 3
static constexpr int dual_global = 1 + 2 + 8;
/// the father, holding the variables of the function and the size variable
struct Father {
AbstractBlock block;
std::vector< ColVariable > x;
std::vector< ColVariable > lambda;
PolyhedralFunctionBlock * pfb;
Father( void ) : x( 2 ) , lambda( 1 ) {
block.add_static_variable( x , "x" );
block.add_static_variable( lambda , "lambda" );
pfb = new PolyhedralFunctionBlock( & block );
block.add_nested_Block( pfb );
pfb->get_PolyhedralFunction().set_variables( { & x[ 0 ] , & x[ 1 ] } );
pfb->get_PolyhedralFunction().set_PolyhedralFunction(
{ { 1 , 2 } , { 3 , 1 } } , { 0 , 1 } );
}
};
/*--------------------------------------------------------------------------*/
/// the normalization constraint of the dual representation
static FRowConstraint * normalization( PolyhedralFunctionBlock * pfb )
{
FRowConstraint * row = nullptr;
pfb->for_each_constraint_group( [ & row ]( const BaseGroup & group ) {
if( group.get_name() == "PolyF_norm" )
row = group.get_Constraint( 0 ) ?
dynamic_cast< FRowConstraint * >( group.get_Constraint( 0 ) ) :
nullptr; } );
return( row );
}
/*--------------------------------------------------------------------------*/
/// the coefficient of var in the normalization constraint, NaN if absent
static double coefficient( FRowConstraint * row , ColVariable * var )
{
auto lf = static_cast< LinearFunction * >( row->get_function() );
const auto k = lf->is_active( var );
return( k < lf->get_num_active_var() ? lf->get_coefficient( k ) : NAN );
}
/*--------------------------------------------------------------------------*/
/// true if the Variable counts the row among its active stuff
static bool registered( ColVariable * var , FRowConstraint * row )
{
const auto & act = var->active_stuff();
ThinVarDepInterface * r = row;
return( std::binary_search( act.begin() , act.end() , r ) );
}
/*--------------------------------------------------------------------------*/
/// checks the row once the size variable is in it
static void check_in( PolyhedralFunctionBlock * pfb , ColVariable * lambda )
{
auto row = normalization( pfb );
assert( row );
assert( std::abs( coefficient( row , lambda ) +
1 / pfb->get_v_scale() ) < 1e-12 );
assert( row->get_lhs() == 0 );
assert( row->get_rhs() == 0 );
assert( registered( lambda , row ) );
}
/*--------------------------------------------------------------------------*/
static void test_before( void )
{
// given before the abstract representation exists, the Variable is
// written in when the normalization constraint is built
Father f;
assert( f.pfb->set_size_variable( & f.lambda[ 0 ] ) );
SimpleConfiguration< int > cfg( dual_global );
f.pfb->generate_abstract_variables( & cfg );
f.pfb->generate_abstract_constraints();
check_in( f.pfb , & f.lambda[ 0 ] );
std::cout << "given before: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
static void test_after( void )
{
// given afterwards, and without any Modification, it is added in place
Father f;
SimpleConfiguration< int > cfg( dual_global );
f.pfb->generate_abstract_variables( & cfg );
f.pfb->generate_abstract_constraints();
auto row = normalization( f.pfb );
auto function = row->get_function();
assert( f.pfb->set_size_variable( & f.lambda[ 0 ] , eNoMod ) );
check_in( f.pfb , & f.lambda[ 0 ] );
assert( normalization( f.pfb ) == row );
assert( row->get_function() == function );
// a second time changes nothing
const auto nav =
static_cast< LinearFunction * >( function )->get_num_active_var();
assert( f.pfb->set_size_variable( & f.lambda[ 0 ] ) );
assert( static_cast< LinearFunction * >( function )->get_num_active_var()
== nav );
// what is not a ColVariable is refused
assert( ! f.pfb->set_size_variable( nullptr ) );
std::cout << "given after: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
static void test_rescale( void )
{
// the global scale of the epigraph is the median of the measures of the
// rows, so one row a thousand times larger than the others leaves it where
// it is, which is what the median is there for, while enough of them move
// it; the coefficient of the size variable follows the scale in place
Father f;
SimpleConfiguration< int > cfg( dual_global );
f.pfb->generate_abstract_variables( & cfg );
f.pfb->generate_abstract_constraints();
f.pfb->generate_objective();
assert( f.pfb->set_size_variable( & f.lambda[ 0 ] ) );
auto row = normalization( f.pfb );
auto function = row->get_function();
const auto scale = f.pfb->get_v_scale();
f.pfb->get_PolyhedralFunction().add_row( { 1e4 , 1e4 } , 1e4 );
assert( f.pfb->get_v_scale() == scale );
assert( normalization( f.pfb ) == row );
assert( row->get_function() == function );
check_in( f.pfb , & f.lambda[ 0 ] );
// two more of them, and the median is among the large rows
f.pfb->get_PolyhedralFunction().add_row( { 1e4 , 2e4 } , 1e4 );
f.pfb->get_PolyhedralFunction().add_row( { 2e4 , 1e4 } , 1e4 );
assert( f.pfb->get_v_scale() != scale );
assert( normalization( f.pfb ) == row );
assert( row->get_function() == function );
check_in( f.pfb , & f.lambda[ 0 ] );
std::cout << "rescale: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
static void test_primal( void )
{
// the primal representation takes no size variable
Father f;
SimpleConfiguration< int > cfg( 1 );
f.pfb->generate_abstract_variables( & cfg );
f.pfb->generate_abstract_constraints();
assert( ! f.pfb->set_size_variable( & f.lambda[ 0 ] ) );
// and given before, it makes the generation of the primal one throw
Father g;
assert( g.pfb->set_size_variable( & g.lambda[ 0 ] ) );
g.pfb->generate_abstract_variables( & cfg );
bool thrown = false;
try {
g.pfb->generate_abstract_constraints();
}
catch( std::logic_error & ) {
thrown = true;
}
assert( thrown );
std::cout << "primal: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------- MAIN -----------------------------------*/
/*--------------------------------------------------------------------------*/
int main( void )
{
test_before();
test_after();
test_rescale();
test_primal();
std::cout << "All tests passed!!" << std::endl;
return( 0 );
}
/*--------------------------------------------------------------------------*/
/*----------------- End File tests_PolyhedralFunctionBlock.cpp -------------*/
/*--------------------------------------------------------------------------*/