Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
110 lines (94 loc) · 3.79 KB
/
Copy pathtest.cpp
File metadata and controls
110 lines (94 loc) · 3.79 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
/*--------------------------------------------------------------------------*/
/*-------------------------- File test.cpp ---------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Main for testing TwoStageStochasticBlock.
*
* The common part is that of every stochastic Block [see stochastic_test.h];
* this one only adds -b, which attaches the Solver to the Benders form of
* the instance [see TwoStageStochasticBlock::get_Benders_form()] rather than
* to the Block read: the two describe the same problem, but a Solver that
* keeps the here-and-now Variable in a master of its own needs them in a
* single copy in the root, which the extensive form on file does not have.
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Antonio Frangioni, Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <iostream>
#include <map>
#include "stochastic_test.h"
#include "AbstractBlock.h"
#include "TwoStageStochasticBlock.h"
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
static bool BenForm = false; // the Solver attached to the Benders form
// the TwoStageStochasticBlock of each Benders form, which the form is given
// back to before it is deleted
static std::map< Block * , TwoStageStochasticBlock * > owner;
StochasticTester t;
t.classname = "TwoStageStochasticBlock";
t.is_one = []( Block * b ) {
return( dynamic_cast< TwoStageStochasticBlock * >( b ) != nullptr );
};
t.short_opts = "b";
t.long_opts = { { "benders" , no_argument , nullptr , 'b' } };
t.help = " -b, --benders solve the Benders form of the "
"instance [off]\n";
t.handle = []( int opt ) {
if( opt != 'b' )
return( false );
BenForm = true;
return( true );
};
/* The Benders form is assembled around the Block the file gives, and it is
* that Block, not this one, that the Solver are attached to: the two
* describe the same problem, so a :MILPSolver reading the assembled one
* whole is still solving the extensive form and is the reference of the
* cross-check. */
t.transform = []( Block * b ) -> Block * {
if( ! BenForm )
return( b );
b->generate_abstract_variables();
b->generate_abstract_constraints();
b->generate_objective();
auto tssb = static_cast< TwoStageStochasticBlock * >( b );
Block * root = tssb->get_Benders_form();
if( ! root )
std::cout << std::endl
<< "Error: the Block declares no here-and-now Variable, hence "
"there is no Benders form of it" << std::endl;
else
owner[ root ] = tssb;
return( root );
};
// the Benders form holds the leaves of the TwoStageStochasticBlock, which
// have to go back to it before either is deleted
t.release = []( Block * b ) {
auto it = owner.find( b );
if( it == owner.end() ) {
delete b;
return;
}
b->unregister_Solvers( true );
it->second->give_back_Benders_form( static_cast< AbstractBlock * >( b ) );
delete it->second;
owner.erase( it );
};
return( stochastic_test( argc , argv , t ) );
}
/*--------------------------------------------------------------------------*/
/*--------------------------- End File test.cpp ----------------------------*/
/*--------------------------------------------------------------------------*/