Debian Codegraph @

Bonmin/src/CbcBonmin/Heuristics/BonHeuristicLocalBranching.cpp

raw
1 // (C) Copyright CNRS and International Business Machines Corporation
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, LIF Université de la Méditérannée-CNRS
7 // Joao Goncalves, International Business Machines Corporation
8 //
9 // Date : 06/18/2008
10
11 #include "BonHeuristicLocalBranching.hpp"
12 #include "CbcModel.hpp"
13 #include "OsiBranchingObject.hpp"
14
15 namespace Bonmin {
16
17 /** Default constructor*/
18 HeuristicLocalBranching::HeuristicLocalBranching():
19 LocalSolverBasedHeuristic(),
20 howOften_(100),
21 numberSolutions_(0)
22 {}
23 /** Constructor with setup.*/
24 HeuristicLocalBranching::HeuristicLocalBranching(BonminSetup * setup):
25 LocalSolverBasedHeuristic(setup),
26 howOften_(100),
27 numberSolutions_(0)
28 {}
29
30 /** Copy constructor.*/
31 HeuristicLocalBranching::HeuristicLocalBranching
32 (const HeuristicLocalBranching &other):
33 LocalSolverBasedHeuristic(other),
34 howOften_(other.howOften_),
35 numberSolutions_(other.numberSolutions_)
36 {}
37
38 HeuristicLocalBranching::~HeuristicLocalBranching(){
39 }
40
41 void
42 HeuristicLocalBranching::setModel(CbcModel * model)
43 {
44 model_=model;
45 // assert(model_->solver());
46 validate();
47 }
48
49 void
50 HeuristicLocalBranching::validate()
51 {
52 assert(setup_ != NULL);
53 OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *>
54 (setup_->nonlinearSolver());
55 TMINLP2TNLP* minlp = nlp->problem();
56 int numberColumns;
57 int numberRows;
58 int nnz_jac_g;
59 int nnz_h_lag;
60 Ipopt::TNLP::IndexStyleEnum index_style;
61 minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g,
62 nnz_h_lag, index_style);
63 const Bonmin::TMINLP::VariableType* variableType = minlp->var_types();
64 const double* x_l = minlp->x_l();
65 const double* x_u = minlp->x_u();
66
67 for(int i=0; i<numberColumns; i++) {
68 if ((variableType[i] != Bonmin::TMINLP::CONTINUOUS) &&
69 (x_l[i] != 0.0 || x_u[i] != 1.0)) {
70 setWhen(0);
71 return;
72 }
73 }
74 }
75
76 /** Runs heuristic*/
77 int
78 HeuristicLocalBranching::solution(double & objectiveValue,
79 double * newSolution)
80 {
81 // if(!when() || model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0;
82 if (numberSolutions_>=model_->getSolutionCount())
83 return 0;
84 else
85 numberSolutions_=model_->getSolutionCount();
86
87 const double * bestSolution = model_->bestSolution();
88 if (!bestSolution)
89 return 0; // No solution found yet
90
91 OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *>
92 (setup_->nonlinearSolver()->clone());
93
94
95 int numberIntegers = model_->numberIntegers();
96 const int * integerVariable = model_->integerVariable();
97
98 double* vals = new double[numberIntegers];
99 int* inds = new int[numberIntegers];
100
101 for (int i=0; i<numberIntegers; i++) {
102 int iColumn = integerVariable[i];
103 vals[i] = bestSolution[iColumn];
104 inds[i] = iColumn;
105 }
106
107 double rhs_local_branching_constraint = numberIntegers / 2; //stefan: this should be the same as floor(numInt/2) since numInt and 2 are int's
108 nlp->switchToFeasibilityProblem(numberIntegers, vals, inds, rhs_local_branching_constraint);
109
110 int r_val = 0;
111 r_val = doLocalSearch(nlp, newSolution, objectiveValue, model_->getCutoff());
112
113 delete [] vals;
114 delete [] inds;
115
116 if(r_val > 0) numberSolutions_ = model_->getSolutionCount() + 1;
117
118 return r_val;
119 }
120
121 void
122 HeuristicLocalBranching::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){
123 roptions->SetRegisteringCategory("Primal Heuristics (undocumented)", RegisteredOptions::UndocumentedCategory);
124 roptions->AddStringOption2(
125 "heuristic_local_branching",
126 "if yes runs the LocalBranching heuristic",
127 "no",
128 "no", "",
129 "yes", "",
130 "");
131 roptions->setOptionExtraInfo("heuristic_local_branching", 63);
132 }
133
134 /** Initiaize using passed options.*/
135 void
136 HeuristicLocalBranching::Initialize(Ipopt::SmartPtr<Ipopt::OptionsList> options){
137 }
138 }/* ends bonmin namespace*/