1 #if !defined(_NEIGHBORHOOD_EXPLORER_HH_)
2 #define _NEIGHBORHOOD_EXPLORER_HH_
26 template <
class Move,
typename CFtype>
39 template <
class Move,
typename CFtype>
40 std::ostream& operator<<(std::ostream& os, const EvaluatedMove<Move, CFtype>& em)
50 template <
class Move,
typename CFtype>
51 EvaluatedMove<Move, CFtype> EvaluatedMove<Move, CFtype>::empty = EvaluatedMove<Move, CFtype>();
57 template <
class Input,
class State,
class Move,
typename CFtype =
int>
58 class NeighborhoodExplorer
66 typedef typename std::function<bool(const Move& mv, CostStructure<CFtype> move_cost)>
MoveAcceptor;
108 virtual bool NextMove(
const State &st, Move& mv)
const = 0;
115 virtual void MakeMove(State &st,
const Move& mv)
const = 0;
171 virtual
EvaluatedMove<Move, CFtype>
SelectBest(const State& st,
size_t& explored, const
MoveAcceptor& AcceptMove, const std::vector<
double>& weights = std::vector<
double>(0)) const throw (EmptyNeighborhood);
177 virtual
EvaluatedMove<Move, CFtype>
RandomFirst(const State& st,
size_t samples,
size_t& explored, const
MoveAcceptor& AcceptMove, const std::vector<
double>& weights = std::vector<
double>(0)) const throw (EmptyNeighborhood);
183 virtual
EvaluatedMove<Move, CFtype>
RandomBest(const State& st,
size_t samples,
size_t& explored, const
MoveAcceptor& AcceptMove, const std::vector<
double>& weights = std::vector<
double>(0)) const throw (EmptyNeighborhood);
205 template <class Input, class State, class Move, typename CFtype>
207 : in(i), sm(e_sm), name(e_name), unimplemented_hard_components(false), unimplemented_soft_components(false)
215 template <
class Input,
class State,
class Move,
typename CFtype>
218 CFtype delta_hard_cost = 0, delta_soft_cost = 0;
219 double delta_weighted_cost = 0.0;
227 CFtype current_delta_cost = delta_cost_function[dcc->
Index()] = dcc->
DeltaCost(st, mv);
228 delta_hard_cost += current_delta_cost;
229 if (!weights.empty())
230 delta_weighted_cost +=
HARD_WEIGHT * weights[dcc->
Index()] * current_delta_cost;
238 CFtype current_delta_cost = delta_cost_function[dcc->
Index()] = dcc->
DeltaCost(st, mv);
239 delta_soft_cost += current_delta_cost;
240 if (!weights.empty())
241 delta_weighted_cost += weights[dcc->
Index()] * current_delta_cost;
261 delta_hard_cost += current_delta_cost;
262 if (!weights.empty())
263 delta_weighted_cost +=
HARD_WEIGHT * weights[cc.
Index()] * current_delta_cost;
275 delta_soft_cost += current_delta_cost;
276 if (!weights.empty())
277 delta_weighted_cost += weights[cc.
Index()] * current_delta_cost;
282 if (!weights.empty())
288 template <
class Input,
class State,
class Move,
typename CFtype>
297 template <
class Input,
class State,
class Move,
typename CFtype>
318 template <
class Input,
class State,
class Move,
typename CFtype>
343 template <
class Input,
class State,
class Move,
typename CFtype>
346 unsigned int number_of_bests = 0;
358 if (number_of_bests == 0)
378 if (number_of_bests == 0)
388 template <
class Input,
class State,
class Move,
typename CFtype>
393 while (explored < samples)
410 template <
class Input,
class State,
class Move,
typename CFtype>
413 unsigned int number_of_bests = 0;
417 while (explored < samples)
425 if (number_of_bests == 0)
444 if (number_of_bests == 0)
452 #endif // _NEIGHBORHOOD_EXPLORER_HH_
virtual bool FeasibleMove(const State &st, const Move &mv) const
CostStructure< CFtype > cost
virtual void AddDeltaCostComponent(DeltaCostComponent< Input, State, Move, CFtype > &dcc)
std::function< bool(const Move &mv, CostStructure< CFtype > move_cost)> MoveAcceptor
virtual CostStructure< CFtype > DeltaCostFunctionComponents(const State &st, const Move &mv, const std::vector< double > &weights=std::vector< double >(0)) const
virtual ~NeighborhoodExplorer()
EvaluatedMove(const Move &move, CostStructure< CFtype > cost)
virtual CFtype DeltaCost(const State &st, const Move &mv) const
std::vector< DeltaCostComponent< Input, State, Move, CFtype > * > delta_soft_cost_components
void CopyDeltaCostComponents(const NeighborhoodExplorer< Input, State, Move, CFtype > &ne)
virtual EvaluatedMove< Move, CFtype > RandomFirst(const State &st, size_t samples, size_t &explored, const MoveAcceptor &AcceptMove, const std::vector< double > &weights=std::vector< double >(0)) const
virtual size_t DeltaCostComponents() const
CostComponent< Input, State, CFtype > & GetCostComponent() const
NeighborhoodExplorer(const Input &in, StateManager< Input, State, CFtype > &sm, std::string name)
virtual CFtype ComputeCost(const State &st) const =0
bool unimplemented_soft_components
std::vector< DeltaCostComponent< Input, State, Move, CFtype > * > delta_hard_cost_components
std::vector< std::shared_ptr< DeltaCostComponentAdapter< Input, State, Move, CFtype > > > dcc_adapters
virtual bool NextMove(const State &st, Move &mv) const =0
virtual bool IsDeltaImplemented() const
virtual void MakeMove(State &st, const Move &mv) const =0
virtual EvaluatedMove< Move, CFtype > SelectFirst(const State &st, size_t &explored, const MoveAcceptor &AcceptMove, const std::vector< double > &weights=std::vector< double >(0)) const
The class CostComponent manages one single component of the cost, either hard or soft.
virtual void RandomMove(const State &st, Move &mv) const =0
virtual size_t Modality() const
EvaluatedMove(const Move &move)
StateManager< Input, State, CFtype > & sm
virtual EvaluatedMove< Move, CFtype > RandomBest(const State &st, size_t samples, size_t &explored, const MoveAcceptor &AcceptMove, const std::vector< double > &weights=std::vector< double >(0)) const
virtual void AddCostComponent(CostComponent< Input, State, CFtype > &cc)
This component is responsible for all operations on the state which are independent of the neighborho...
bool unimplemented_hard_components
virtual EvaluatedMove< Move, CFtype > SelectBest(const State &st, size_t &explored, const MoveAcceptor &AcceptMove, const std::vector< double > &weights=std::vector< double >(0)) const
virtual void FirstMove(const State &st, Move &mv) const =0
static EvaluatedMove empty