EasyLocalpp  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
costcomponent.hh
Go to the documentation of this file.
1 #if !defined(_COST_COMPONENT_HH_)
2 #define _COST_COMPONENT_HH_
3 
4 #include <iostream>
5 #include <vector>
7 
8 namespace EasyLocal {
9 
10  namespace Core {
11 
20  template <class Input, class State, typename CFtype = int>
21  class CostComponent : public Printable
22  {
23  static size_t last_index;
24  public:
25 
27  virtual void Print(std::ostream& os = std::cout) const;
28 
33  virtual CFtype ComputeCost(const State& st) const = 0;
34 
40  CFtype Cost(const State& st) const { return weight * ComputeCost(st); }
41 
46  virtual void PrintViolations(const State& st, std::ostream& os = std::cout) const = 0;
47 
51  CFtype Weight() const { return weight; }
52 
56  void SetWeight(const CFtype& w) { weight = w; }
57 
59  void SetHard() { is_hard = true; }
60 
62  void SetSoft() { is_hard = false; }
63 
67  bool IsHard() const { return is_hard; }
68 
72  bool IsSoft() const { return !is_hard; }
73 
75  const std::string name;
76 
80  size_t Index() const
81  {
82  return index;
83  }
84 
88  static const size_t CostComponents()
89  {
90  return last_index;
91  }
92 
93 
98  {
99  return *cost_components[i];
100  }
101 
103  virtual ~CostComponent() {}
104 
105  protected:
106 
113  CostComponent(const Input& in, const CFtype& weight, bool is_hard, std::string name);
114 
116  const Input& in;
117 
119  CFtype weight;
120 
122  bool is_hard;
123 
124 
125  protected:
127  const size_t index;
128 
130  static std::vector<CostComponent<Input, State, CFtype>*> cost_components;
131  };
132 
133 
134 
137  template <class Input, class State, typename CFtype>
138  CostComponent<Input, State, CFtype>::CostComponent(const Input& in, const CFtype& weight, bool is_hard, std::string name)
139  : name(name), in(in), weight(weight), is_hard(is_hard), index(last_index++)
140  {
141  cost_components.push_back(this);
142  }
143 
144  template <class Input, class State, typename CFtype>
145  void CostComponent<Input, State, CFtype>::Print(std::ostream& os) const
146  {
147  os << "Cost Component " << name << ": weight " << weight << (is_hard ? "*" : "") << std::endl;
148  }
149 
150  template <class Input, class State, typename CFtype>
152 
153  template <class Input, class State, typename CFtype>
154  std::vector<CostComponent<Input, State, CFtype>*> CostComponent<Input, State, CFtype>::cost_components;
155  }
156 }
157 
158 #endif // _COST_COMPONENT_HH_
CFtype Cost(const State &st) const
virtual CFtype ComputeCost(const State &st) const =0
static const size_t CostComponents()
static std::vector< CostComponent< Input, State, CFtype > * > cost_components
The class CostComponent manages one single component of the cost, either hard or soft.
virtual void Print(std::ostream &os=std::cout) const
CostComponent(const Input &in, const CFtype &weight, bool is_hard, std::string name)
void SetWeight(const CFtype &w)
static const CostComponent< Input, State, CFtype > & Component(size_t i)
virtual void PrintViolations(const State &st, std::ostream &os=std::cout) const =0