Debian Codegraph @

Bonmin/src/CbcBonmin/Heuristics/BonLocalSolverBasedHeuristic.cpp

raw
1 // (C) Copyright CNRS
2 // This code is published under the Eclipse Public License.
3 //
4 // Authors :
5 // Pierre Bonami, LIF Université de la Méditérannée-CNRS
6 //
7 // Date : 06/18/2008
8
9 #include "BonLocalSolverBasedHeuristic.hpp"
10 #include "BonCbc.hpp"
11
12 namespace Bonmin {
13 LocalSolverBasedHeuristic::LocalSolverBasedHeuristic():
14 CbcHeuristic(),
15 setup_(NULL),
16 time_limit_(60),
17 max_number_nodes_(1000),
18 max_number_solutions_(10){
19 }
20 LocalSolverBasedHeuristic::LocalSolverBasedHeuristic(BonminSetup * setup):
21 CbcHeuristic(),
22 setup_(setup),
23 time_limit_(60),
24 max_number_nodes_(1000),
25 max_number_solutions_(10){
26 Initialize(setup->options());
27 }
28
29 LocalSolverBasedHeuristic::LocalSolverBasedHeuristic(const LocalSolverBasedHeuristic & other):
30 CbcHeuristic(other),
31 setup_(other.setup_),
32 time_limit_(other.time_limit_),
33 max_number_nodes_(other.max_number_nodes_),
34 max_number_solutions_(other.max_number_solutions_) {
35 }
36
37 LocalSolverBasedHeuristic::~LocalSolverBasedHeuristic(){
38 }
39
40 LocalSolverBasedHeuristic &
41 LocalSolverBasedHeuristic::operator=(const LocalSolverBasedHeuristic& rhs){
42 if(this != &rhs){
43 CbcHeuristic::operator=(rhs);
44 setup_ = rhs.setup_;
45 }
46 return *this;
47 }
48
49 void
50 LocalSolverBasedHeuristic::changeIfNotSet(Ipopt::SmartPtr<Ipopt::OptionsList> options,
51 std::string prefix,
52 const std::string &option,
53 const std::string &value){
54 int dummy;
55 if(!options->GetEnumValue(option,dummy,prefix))
56 options->SetStringValue(prefix + option, value, true, true);
57
58 }
59 void
60 LocalSolverBasedHeuristic::changeIfNotSet(Ipopt::SmartPtr<Ipopt::OptionsList> options,
61 std::string prefix,
62 const std::string &option,
63 const double &value){
64 double dummy;
65 if(!options->GetNumericValue(option,dummy,prefix))
66 options->SetNumericValue(prefix + option, value, true, true);
67
68 }
69 void
70 LocalSolverBasedHeuristic::changeIfNotSet(Ipopt::SmartPtr<Ipopt::OptionsList> options,
71 std::string prefix,
72 const std::string &option,
73 const int &value){
74 int dummy;
75 if(!options->GetIntegerValue(option,dummy,prefix))
76 options->SetIntegerValue(prefix + option, value, true, true);
77
78 }
79
80 void
81 LocalSolverBasedHeuristic::setupDefaults(Ipopt::SmartPtr<Ipopt::OptionsList> options){
82 std::string prefix = "local_solver.";
83 changeIfNotSet(options, prefix, "algorithm", "B-QG");
84 changeIfNotSet(options, prefix, "variable_selection", "most-fractional");
85 changeIfNotSet(options, prefix, "time_limit", 60.);
86 changeIfNotSet(options, prefix, "node_limit", 1000);
87 changeIfNotSet(options, prefix, "solution_limit", 5);
88 }
89
90 int
91 LocalSolverBasedHeuristic::doLocalSearch(OsiTMINLPInterface * solver,
92 double *solution,
93 double & solValue,
94 double cutoff,std::string prefix) const{
95 BonminSetup * mysetup = setup_->clone(*solver, prefix);
96 Bab bb;
97 mysetup->setDoubleParameter(BabSetupBase::Cutoff, cutoff);
98 mysetup->setIntParameter(BabSetupBase::NumberStrong, 0);
99 bb(mysetup);
100 int r_val = 0;
101 if(bb.bestSolution()){
102 CoinCopyN(bb.bestSolution(), solver->getNumCols(), solution);
103 solValue = bb.bestObj();
104 r_val = 1;
105 }
106 delete mysetup;
107 return r_val;
108 }
109
110 /** Register the options common to all local search based heuristics.*/
111 void
112 LocalSolverBasedHeuristic::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){
113 }
114
115 /** Initiaize using passed options.*/
116 void
117 LocalSolverBasedHeuristic::Initialize(Ipopt::SmartPtr<Ipopt::OptionsList> options){
118 /** Some fancy defaults.*/
119 setupDefaults(options);
120 }
121 } /* Ends Bonmin namespace.*/
122