Repository navigation
Expand file tree
/
Copy pathtests_ColVariable.cpp
More file actions
351 lines (301 loc) · 11.1 KB
/
Copy pathtests_ColVariable.cpp
File metadata and controls
351 lines (301 loc) · 11.1 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
/** @file
* Unit tests for Variable and ColVariable.
*
* \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 "ColVariable.h"
#include "SMSTypedefs.h"
#include <cmath>
#include <deque>
#include <list>
#include <vector>
#include <stdexcept>
// last, so that the headers above are read as the library was compiled
#include "TestAssert.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
void runAllTests()
{
ColVariable var;
// test FixUnfix
assert( ! var.is_fixed() );
var.is_fixed( true );
assert( var.is_fixed() );
var.is_fixed( false );
assert( ! var.is_fixed() );
// test IsContinuous
assert( var.get_value() == 0 );
assert( var.get_type() == ColVariable::kContinuous );
assert( ! var.is_integer() );
assert( ! var.is_positive() );
assert( ! var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == -Inf< ColVariable::VarValue >() );
assert( var.get_ub() == Inf< ColVariable::VarValue >() );
// test SetsValue
ColVariable::VarValue val = 42.0;
var.set_value( val );
assert( val == var.get_value() );
// test IsInteger
var.set_type( ColVariable::kInteger );
assert( var.get_type() == ColVariable::kInteger );
assert( var.is_integer() );
assert( ! var.is_positive() );
assert( ! var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == -Inf< ColVariable::VarValue >() );
assert( var.get_ub() == Inf< ColVariable::VarValue >() );
// test IsPositive
var.set_type( ColVariable::kNonNegative );
assert( var.get_type() == ColVariable::kNonNegative );
assert( ! var.is_integer() );
assert( var.is_positive() );
assert( ! var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == 0 );
assert( var.get_ub() == Inf< ColVariable::VarValue >() );
// test IsNegative
var.set_type( ColVariable::kNonPositive );
assert( var.get_type() == ColVariable::kNonPositive );
assert( ! var.is_integer() );
assert( ! var.is_positive() );
assert( var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == -Inf< ColVariable::VarValue >() );
assert( var.get_ub() == 0 );
// test IsUnitary
var.set_type( ColVariable::kUnitary );
assert( var.get_type() == ColVariable::kUnitary );
assert( ! var.is_integer() );
assert( ! var.is_positive() );
assert( ! var.is_negative() );
assert( var.is_unitary() );
assert( var.get_lb() == -1 );
assert( var.get_ub() == 1 );
// test IsFeasible
var.set_value( 42.42 );
var.set_type( ColVariable::kInteger );
assert( ! var.is_feasible() );
var.set_value( 42.42 );
var.set_type( ColVariable::kNonNegative );
assert( var.is_feasible() );
var.set_value( -42.42 );
var.set_type( ColVariable::kUnitary );
assert( ! var.is_feasible() );
var.set_value( 0.42 );
var.set_type( ColVariable::kUnitary );
assert( var.is_feasible() );
var.set_value( 42 );
var.set_type( ColVariable::kNatural );
assert( var.is_feasible() );
}
/*--------------------------------------------------------------------------*/
/* Every one of the sixteen types, and the values at the edge of each. The
* tests above try six of them with a value well inside or well outside the
* set, which says nothing about what happens at the bounds themselves, and
* the ten that are left are the "weird" combinations the class warns about,
* which are the ones a caller gets wrong. Rather than writing down the two
* bounds of each type, which would only repeat the code, what is checked
* here is what has to hold of any of them. */
static void test_every_type( void )
{
using VV = ColVariable::VarValue;
const VV inf = Inf< VV >();
/* Nine values that straddle every bound any of the types has: each of -1,
* 0 and 1 is a bound of one of them, so each is tried on the bound, half a
* unit inside it and half a unit outside, and one unit past either end. */
static const VV probe[] = { -2 , -1.5 , -1 , -0.5 , 0 , 0.5 , 1 , 1.5 , 2 };
for( int t = 0 ; t < ColVariable::ColVarLastType ; ++t ) {
ColVariable var;
var.set_type( ColVariable::col_var_type( t ) );
const auto lb = var.get_lb();
const auto ub = var.get_ub();
// a set that is empty would be a type nobody can satisfy
assert( lb <= ub );
// an integer type has integer bounds, or the bound itself would be a
// value the type forbids
if( var.is_integer() ) {
if( lb > - inf )
assert( lb == std::floor( lb ) );
if( ub < inf )
assert( ub == std::floor( ub ) );
}
// is_positive() and is_negative() say where the set sits, and both hold
// of the types that are the single point 0
assert( var.is_positive() == ( lb >= 0 ) );
assert( var.is_negative() == ( ub <= 0 ) );
/* And the set is exactly what the two bounds and the integrality say:
* this is the one thing every caller assumes of a type, it is what the
* tests above check of six of the sixteen with one value each, and it is
* what has to hold at the bounds themselves. */
for( auto x : probe ) {
var.set_value( x );
const bool in = ( x >= lb ) && ( x <= ub ) &&
( ( ! var.is_integer() ) || ( x == std::floor( x ) ) );
assert( var.is_feasible() == in );
}
}
}
/*--------------------------------------------------------------------------*/
/* The types whose set is a single point, and the two that are a point but
* carry a range: these are the ones where an off-by-one in the bounds does
* not show, both bounds being the same number. */
static void test_single_point_types( void )
{
for( auto t : { ColVariable::kZeroReal , ColVariable::kZeroInteger ,
ColVariable::kZeroRealU , ColVariable::kZeroIntU } ) {
ColVariable var;
var.set_type( t );
assert( var.get_lb() == 0 );
assert( var.get_ub() == 0 );
var.set_value( 0 );
assert( var.is_feasible() );
var.set_value( 1e-12 );
assert( ! var.is_feasible() );
}
// the binary one, which is the type most models are made of
ColVariable bin;
bin.set_type( ColVariable::kBinary );
assert( bin.get_lb() == 0 );
assert( bin.get_ub() == 1 );
assert( bin.is_integer() );
for( auto v : { 0.0 , 1.0 } ) {
bin.set_value( v );
assert( bin.is_feasible() );
}
for( auto v : { -1.0 , 0.5 , 2.0 } ) {
bin.set_value( v );
assert( ! bin.is_feasible() );
}
}
/*--------------------------------------------------------------------------*/
// a fixed ColVariable keeps its value: the same up to rounding is accepted
// and changes nothing, another one throws and changes nothing either, and
// the value is changed by unfixing, setting and fixing again
void test_fixed_value()
{
ColVariable var;
var.set_value( 3 );
var.is_fixed( true );
var.set_value( 3 * ( 1 + 1e-12 ) );
assert( var.get_value() == 3 );
bool thrown = false;
try { var.set_value( 4 ); }
catch( const std::domain_error & ) { thrown = true; }
assert( thrown );
assert( var.get_value() == 3 );
// the tolerance is relative with a floor of 1: near zero it is absolute
ColVariable zero;
zero.is_fixed( true );
zero.set_value( 1e-10 );
assert( zero.get_value() == 0 );
thrown = false;
try { zero.set_value( 1e-5 ); }
catch( const std::domain_error & ) { thrown = true; }
assert( thrown );
// set_to_default_value() is a set_value( 0 ) like any other
thrown = false;
try { var.set_to_default_value(); }
catch( const std::domain_error & ) { thrown = true; }
assert( thrown );
var.is_fixed( false );
var.set_value( 4 );
var.is_fixed( true );
assert( var.is_fixed() && ( var.get_value() == 4 ) );
}
/*--------------------------------------------------------------------------*/
// ColVariable::is_feasible() of the collections that none of the specific
// overloads takes, whose items are any container: a std::deque, a std::deque
// of std::vector, a std::list of std::list and a boost::multi_array of
// std::deque. The empty collection and the one of empty collections are
// feasible, the one with an infeasible ColVariable is not, wherever it is,
// whether the ColVariable are fixed or not, and the tolerance is the one
// given
static void test_is_feasible_collections( void )
{
// a binary ColVariable with value v, fixed if fix
auto bin = []( ColVariable & var , double v , bool fix ) {
var.set_type( ColVariable::kBinary );
var.set_value( v );
var.is_fixed( fix );
};
{ // a std::deque
std::deque< ColVariable > d;
assert( ColVariable::is_feasible( d ) );
d.resize( 3 );
bin( d[ 0 ] , 1 , true );
bin( d[ 1 ] , 0 , false );
bin( d[ 2 ] , 1 + 1e-6 , false );
assert( ! ColVariable::is_feasible( d ) );
assert( ColVariable::is_feasible( d , 1e-5 ) );
d[ 2 ].set_value( 1 );
assert( ColVariable::is_feasible( d ) );
d[ 0 ].is_fixed( false );
d[ 0 ].set_value( 0.5 );
d[ 0 ].is_fixed( true );
assert( ! ColVariable::is_feasible( d ) );
}
{ // a std::deque of std::vector
std::deque< std::vector< ColVariable > > d;
assert( ColVariable::is_feasible( d ) );
d.resize( 3 );
assert( ColVariable::is_feasible( d ) );
d[ 2 ].resize( 2 ); // d[ 0 ] and d[ 1 ] stay empty
bin( d[ 2 ][ 0 ] , 1 , true );
bin( d[ 2 ][ 1 ] , 2 , false );
assert( ! ColVariable::is_feasible( d ) );
d[ 2 ][ 1 ].set_value( 0 );
assert( ColVariable::is_feasible( d ) );
}
{ // a std::list of std::list
std::list< std::list< ColVariable > > l;
assert( ColVariable::is_feasible( l ) );
l.resize( 2 );
assert( ColVariable::is_feasible( l ) );
l.front().resize( 1 );
bin( l.front().front() , -1 , false );
assert( ! ColVariable::is_feasible( l ) );
l.front().front().set_value( 1 );
assert( ColVariable::is_feasible( l ) );
}
{ // a boost::multi_array of std::deque
boost::multi_array< std::deque< ColVariable > , 2 > m;
assert( ColVariable::is_feasible( m ) );
m.resize( boost::extents[ 2 ][ 3 ] );
assert( ColVariable::is_feasible( m ) );
m[ 1 ][ 2 ].resize( 2 );
bin( m[ 1 ][ 2 ][ 0 ] , 0 , true );
bin( m[ 1 ][ 2 ][ 1 ] , 0.25 , false );
assert( ! ColVariable::is_feasible( m ) );
assert( ColVariable::is_feasible( m , 0.3 ) );
}
}
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
runAllTests();
test_fixed_value();
test_every_type();
test_single_point_types();
test_is_feasible_collections();
return( 0 );
}
/*--------------------------------------------------------------------------*/
/*--------------------- End File tests_ColVariable.cpp ---------------------*/
/*--------------------------------------------------------------------------*/