EasyLocalpp  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
deltacostcomponent.hh
Go to the documentation of this file.
1 #if !defined(_DELTA_COST_COMPONENT_HH_)
2 #define _DELTA_COST_COMPONENT_HH_
3 
4 #include <stdexcept>
5 
8 
9 namespace EasyLocal {
10 
11  namespace Core {
12 
16  template <class Input, class State, class Move, typename CFtype = int>
18  {
19  public:
20 
25 
27  virtual void Print(std::ostream& os = std::cout) const;
28 
30  bool IsHard() const { return cc.IsHard(); }
31 
33  bool IsSoft() const { return cc.IsSoft(); }
34 
39  virtual CFtype DeltaCost(const State& st, const Move& mv) const;
40 
42  virtual bool IsDeltaImplemented() const { return true; }
43 
45  const std::string name;
46 
50  size_t Index() const
51  {
52  return cc.Index();
53  }
54 
55  protected:
56 
64 
70  virtual CFtype ComputeDeltaCost(const State& st, const Move& mv) const = 0;
71 
73  const Input& in;
74 
77 
78  };
79 
82  template <class Input, class State, class Move, typename CFtype>
84  : name(name), in(i), cc(e_cc)
85  {}
86 
87  template <class Input, class State, class Move, typename CFtype>
89  {
90  os << " DeltaCost Component: " + this->name << std::endl;
91  }
92 
93  template <class Input, class State, class Move, typename CFtype>
95  const Move& mv) const
96  {
97  return this->cc.Weight() * ComputeDeltaCost(st, mv);
98  }
99 
100  template <class Input, class State, class Move, typename CFtype>
102 
106  template <class Input, class State, class Move, typename CFtype = int>
107  class DeltaCostComponentAdapter : public DeltaCostComponent<Input, State, Move, CFtype>
108  {
109  public:
110 
113 
115  virtual bool IsDeltaImplemented() const { return false; }
116 
117  protected:
118 
120  virtual CFtype ComputeDeltaCost(const State& st, const Move& mv) const
121  {
122  State new_st = st;
123  ne.MakeMove(new_st, mv);
124  return this->cc.ComputeCost(new_st) - this->cc.ComputeCost(st);
125  }
127  };
128 
129  template <class Input, class State, class Move, typename CFtype>
131  : DeltaCostComponent<Input, State, Move, CFtype>(in, cc, "DeltaAdapter" + cc.name), ne(ne)
132  {}
133  }
134 }
135 
136 #endif // _DELTA_COST_COMPONENT_HH_
DeltaCostComponentAdapter(const Input &in, CostComponent< Input, State, CFtype > &cc, NeighborhoodExplorer< Input, State, Move, CFtype > &ne)
NeighborhoodExplorer< Input, State, Move, CFtype > & ne
virtual CFtype DeltaCost(const State &st, const Move &mv) const
virtual void Print(std::ostream &os=std::cout) const
CostComponent< Input, State, CFtype > & GetCostComponent() const
virtual CFtype ComputeCost(const State &st) const =0
DeltaCostComponent(const Input &in, CostComponent< Input, State, CFtype > &cc, std::string name)
Constructs a DeltaCostComponent providing an input object, the related CostComponent and a name...
virtual CFtype ComputeDeltaCost(const State &st, const Move &mv) const =0
virtual void MakeMove(State &st, const Move &mv) const =0
The class CostComponent manages one single component of the cost, either hard or soft.
CostComponent< Input, State, CFtype > & cc
virtual CFtype ComputeDeltaCost(const State &st, const Move &mv) const