| 1 |
// $Id$ |
| 2 |
// Copyright (C) 2002, International Business Machines |
| 3 |
// Corporation and others. All Rights Reserved. |
| 4 |
// This code is licensed under the terms of the Eclipse Public License (EPL). |
| 5 |
|
| 6 |
// Pierre Bonami 13/10/2011 copy and modify CbcNWay to adapt to use an OsiObject for Bonmin |
| 7 |
// |
| 8 |
// Edwin 11/9/2009-- carved out of CbcBranchActual |
| 9 |
|
| 10 |
/** Define an n-way class for variables. |
| 11 |
Only valid value is one at UB others at LB |
| 12 |
Normally 0-1 |
| 13 |
*/ |
| 14 |
#ifndef BonNWayObject_H |
| 15 |
#define BonNWayObject_H |
| 16 |
#include "OsiBranchingObject.hpp" |
| 17 |
#include "CbcConsequence.hpp" |
| 18 |
#include <list> |
| 19 |
|
| 20 |
namespace Bonmin { |
| 21 |
class n_way_consequences { |
| 22 |
public: |
| 23 |
std::vector<int> indices; |
| 24 |
|
| 25 |
n_way_consequences(): indices(){ |
| 26 |
} |
| 27 |
|
| 28 |
n_way_consequences(const n_way_consequences& rhs): |
| 29 |
indices(rhs.indices){ |
| 30 |
} |
| 31 |
|
| 32 |
n_way_consequences& operator=(const n_way_consequences& rhs){ |
| 33 |
if(this != &rhs){ |
| 34 |
indices = rhs.indices; |
| 35 |
} |
| 36 |
return *this; |
| 37 |
} |
| 38 |
|
| 39 |
n_way_consequences * clone() const{ |
| 40 |
return new n_way_consequences(*this); |
| 41 |
} |
| 42 |
|
| 43 |
void applyToSolver(OsiSolverInterface * solver, int state) const{ |
| 44 |
if(state < 0) return; |
| 45 |
int num_one = 0; |
| 46 |
for(size_t i = 0 ; i < indices.size() ; i++){ |
| 47 |
if(solver->getColLower()[indices[i]] > 0.9) num_one++; |
| 48 |
solver->setColUpper(indices[i],solver->getColLower()[indices[i]]); |
| 49 |
} |
| 50 |
assert(num_one == 0); |
| 51 |
} |
| 52 |
|
| 53 |
}; |
| 54 |
|
| 55 |
class BonNWayObject : public OsiObject { |
| 56 |
|
| 57 |
public: |
| 58 |
|
| 59 |
// Default Constructor |
| 60 |
BonNWayObject (); |
| 61 |
|
| 62 |
/** Useful constructor (which are matrix indices) |
| 63 |
*/ |
| 64 |
BonNWayObject (int numberMembers, |
| 65 |
const int * which, int identifier); |
| 66 |
|
| 67 |
// Copy constructor |
| 68 |
BonNWayObject ( const BonNWayObject &); |
| 69 |
|
| 70 |
/// Clone |
| 71 |
virtual OsiObject * clone() const; |
| 72 |
|
| 73 |
/// Assignment operator |
| 74 |
BonNWayObject & operator=( const BonNWayObject& rhs); |
| 75 |
|
| 76 |
/// Destructor |
| 77 |
virtual ~BonNWayObject (); |
| 78 |
|
| 79 |
/// Set up a consequence for a single member |
| 80 |
void setConsequence(int iMember, const n_way_consequences & consequence); |
| 81 |
|
| 82 |
/// Applies a consequence for a single member |
| 83 |
void applyConsequence(OsiSolverInterface * solver, |
| 84 |
int iSequence, int state) const; |
| 85 |
|
| 86 |
/// Infeasibility - large is 0.5 (and 0.5 will give this) |
| 87 |
virtual double infeasibility(const OsiBranchingInformation * info, |
| 88 |
int &preferredWay) const; |
| 89 |
|
| 90 |
/// This looks at solution and sets bounds to contain solution |
| 91 |
virtual double feasibleRegion(OsiSolverInterface * solver, const OsiBranchingInformation * info) const; |
| 92 |
|
| 93 |
/// Creates a branching object |
| 94 |
virtual OsiBranchingObject * createBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) const; |
| 95 |
|
| 96 |
/// Number of members |
| 97 |
inline size_t numberMembers() const { |
| 98 |
return members_.size(); |
| 99 |
} |
| 100 |
|
| 101 |
/// Members (indices in range 0 ... numberColumns-1) |
| 102 |
inline const int * members() const { |
| 103 |
return members_.data(); |
| 104 |
} |
| 105 |
|
| 106 |
void set_bounds(std::vector<double> & bounds) const{ |
| 107 |
bounds_ = bounds; |
| 108 |
} |
| 109 |
|
| 110 |
void make_quick(){quicky_ = true;} |
| 111 |
|
| 112 |
void set_only_frac_branches(int depth){ |
| 113 |
only_frac_branch_ = depth; |
| 114 |
} |
| 115 |
private: |
| 116 |
/// data |
| 117 |
|
| 118 |
/// Members (indices in range 0 ... numberColumns-1) |
| 119 |
std::vector<int> members_; |
| 120 |
/// Consequences (normally NULL) |
| 121 |
n_way_consequences ** consequence_; |
| 122 |
|
| 123 |
/// Bounds on the members |
| 124 |
mutable std::vector<double> bounds_; |
| 125 |
|
| 126 |
/// Quicky only branch up on variables with non zero value |
| 127 |
bool quicky_; |
| 128 |
/// Only branch on fractional variables (last branch puts all of them to 0) |
| 129 |
int only_frac_branch_; |
| 130 |
}; |
| 131 |
/** N way branching Object class. |
| 132 |
Variable is number of set. |
| 133 |
*/ |
| 134 |
class BonNWayBranchingObject : public OsiBranchingObject { |
| 135 |
|
| 136 |
public: |
| 137 |
|
| 138 |
// Default Constructor |
| 139 |
BonNWayBranchingObject (); |
| 140 |
|
| 141 |
/** Useful constructor - order had matrix indices |
| 142 |
way_ -1 corresponds to setting first, +1 to second, +3 etc. |
| 143 |
this is so -1 and +1 have similarity to normal |
| 144 |
*/ |
| 145 |
BonNWayBranchingObject (OsiSolverInterface * solver, const BonNWayObject * nway, |
| 146 |
const std::vector<int>& order, const std::list<int>& skipped); |
| 147 |
|
| 148 |
// Copy constructor |
| 149 |
BonNWayBranchingObject ( const BonNWayBranchingObject &); |
| 150 |
|
| 151 |
// Assignment operator |
| 152 |
BonNWayBranchingObject & operator=( const BonNWayBranchingObject& rhs); |
| 153 |
|
| 154 |
/// Clone |
| 155 |
virtual OsiBranchingObject * clone() const; |
| 156 |
|
| 157 |
// Destructor |
| 158 |
virtual ~BonNWayBranchingObject (); |
| 159 |
|
| 160 |
using OsiBranchingObject::branch ; |
| 161 |
/// Does next branch and updates state |
| 162 |
virtual double branch(OsiSolverInterface * solver); |
| 163 |
|
| 164 |
/// Branch on specified state |
| 165 |
virtual double branch(OsiSolverInterface * solver, int state); |
| 166 |
|
| 167 |
/** The number of branch arms created for this branching object |
| 168 |
*/ |
| 169 |
virtual int numberBranches() const { |
| 170 |
return static_cast<int>(order_.size()) + (!skipped_.empty()); |
| 171 |
} |
| 172 |
/// Is this a two way object (-1 down, +1 up) |
| 173 |
virtual bool twoWay() const { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
inline int var_branched_on(){ |
| 178 |
if(branchIndex_ > 0) |
| 179 |
return object_->members()[order_[branchIndex_ - 1]]; |
| 180 |
return -1; |
| 181 |
} |
| 182 |
|
| 183 |
inline int seq_branched_on(){ |
| 184 |
if(branchIndex_ > 0) |
| 185 |
return order_[branchIndex_ - 1]; |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
inline int state_branched_on(){ |
| 190 |
return branchIndex_ - 1; |
| 191 |
} |
| 192 |
private: |
| 193 |
/// Points back to object |
| 194 |
const BonNWayObject * object_; |
| 195 |
/// order of branching |
| 196 |
std::vector<int> order_; |
| 197 |
/// Is only branching on a subset of variables (has to do a last branch with all variables in order set to 0) |
| 198 |
std::list<int> skipped_; |
| 199 |
}; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
#endif |