1 #if !defined(_STATE_MANAGER_HH_)
2 #define _STATE_MANAGER_HH_
16 template <
typename CFtype>
54 explicit operator double()
const
63 template <
typename CFtype>
71 template <
typename CFtype>
79 template <
class CFtype>
82 if (cs1.is_weighted && cs2.is_weighted)
83 return LessThan(cs1.weighted, cs2.weighted);
84 return LessThan(cs1.total, cs2.total);
87 template <
class CFtype>
88 bool operator<(CFtype c1, const CostStructure<CFtype>& cs2)
91 return LessThan((
double)c1, cs2.weighted);
95 template <
class CFtype>
96 bool operator<(const CostStructure<CFtype>& cs1, CFtype c2)
99 return LessThan(cs1.weighted, (
double)c2);
103 template <
class CFtype,
typename OtherType>
104 bool operator<(OtherType c1, const CostStructure<CFtype>& cs2)
107 return LessThan((
double)c1, cs2.weighted);
108 return LessThan((CFtype)c1, cs2.total);
111 template <
class CFtype,
typename OtherType>
112 bool operator<(const CostStructure<CFtype>& cs1, OtherType c2)
115 return LessThan(cs1.weighted, (
double)c2);
116 return LessThan(cs1.total, (CFtype)c2);
119 template <
class CFtype>
122 if (cs1.is_weighted && cs2.is_weighted)
127 template <
class CFtype>
128 bool operator<=(CFtype c1, const CostStructure<CFtype>& cs2)
135 template <
class CFtype>
136 bool operator<=(const CostStructure<CFtype>& cs1, CFtype c2)
143 template <
class CFtype,
typename OtherType>
144 bool operator<=(OtherType c1, const CostStructure<CFtype>& cs2)
151 template <
class CFtype,
typename OtherType>
152 bool operator<=(const CostStructure<CFtype>& cs1, OtherType c2)
159 template <
class CFtype>
167 template <
class CFtype>
175 template <
class CFtype>
183 template <
class CFtype,
typename OtherType>
191 template <
class CFtype,
typename OtherType>
199 template <
class CFtype>
207 template <
class CFtype>
215 template <
class CFtype>
223 template <
class CFtype,
typename OtherType>
231 template <
class CFtype,
typename OtherType>
239 template <
class CFtype>
247 template <
class CFtype>
255 template <
class CFtype>
263 template <
class CFtype,
typename OtherType>
271 template <
class CFtype,
typename OtherType>
279 template <
class CFtype>
287 template <
class CFtype>
295 template <
class CFtype>
303 template <
class CFtype,
typename OtherType>
311 template <
class CFtype,
typename OtherType>
319 template <
typename CFtype>
320 std::ostream& operator<<(std::ostream& os, const CostStructure<CFtype>& cc)
322 os << cc.total <<
" (viol: " << cc.violations <<
", obj: " << cc.objective <<
", comps: {";
323 for (
size_t i = 0; i < cc.all_components.size(); i++)
327 os << cc.all_components[i];
340 #if !defined(HARD_WEIGHT_SET)
342 #define HARD_WEIGHT_SET
357 template <
class Input,
class State,
typename CFtype =
int>
366 void Print(std::ostream& os = std::cout)
const;
409 virtual void GreedyState(State &st,
double alpha,
unsigned int k);
465 virtual unsigned int StateDistance(
const State& st1,
const State& st2)
const;
502 template <
class Input,
class State,
typename CFtype>
507 template <
class Input,
class State,
typename CFtype>
510 os <<
"State Manager: " + name << std::endl;
511 os <<
"Violations:" << std::endl;
512 for (
unsigned int i = 0; i < cost_component.size(); i++)
513 if (cost_component[i]->IsHard())
514 cost_component[i]->Print(os);
515 os <<
"Objective:" << std::endl;
516 for (
unsigned int i = 0; i < cost_component.size(); i++)
517 if (cost_component[i]->IsSoft())
518 cost_component[i]->Print(os);
521 template <
class Input,
class State,
typename CFtype>
523 unsigned int samples)
528 State best_state(in);
534 cost = CostFunctionComponents(st);
535 if (cost < best_cost)
546 template <
class Input,
class State,
typename CFtype>
553 template <
class Input,
class State,
typename CFtype>
556 throw std::runtime_error(
"For using this feature GreedyState must be implemented in the concrete class!");
559 template <
class Input,
class State,
typename CFtype>
562 CFtype hard_cost = 0, soft_cost = 0;
563 double weighted_cost = 0.0;
565 for (
size_t i = 0; i < cost_component.size(); i++)
567 CFtype current_cost = cost_function[cost_component[i]->Index()] = cost_component[i]->Cost(st);
568 if (cost_component[i]->IsHard())
570 hard_cost += current_cost;
571 if (!weights.empty())
572 weighted_cost +=
HARD_WEIGHT * weights[cost_component[i]->Index()] * current_cost;
576 soft_cost += current_cost;
577 if (!weights.empty())
578 weighted_cost += weights[cost_component[i]->Index()] * current_cost;
582 if (!weights.empty())
588 template <
class Input,
class State,
typename CFtype>
594 template <
class Input,
class State,
typename CFtype>
597 return LowerBoundReached(CostFunctionComponents(st).total);
600 template <
class Input,
class State,
typename CFtype>
603 cost_component.push_back(&cc);
606 template <
class Input,
class State,
typename CFtype>
608 const State& st2)
const
610 throw std::runtime_error(
"For using this feature StateDistance must be implemented in the concrete class!");
616 #endif // _STATE_MANAGER_HH_
bool LessThan(CFtype value1, CFtype value2)
bool operator>(const CostStructure< CFtype > &cs1, const CostStructure< CFtype > &cs2)
std::vector< CFtype > all_components
bool LessThanOrEqualTo(CFtype value1, CFtype value2)
bool GreaterThanOrEqualTo(CFtype value1, CFtype value2)
virtual unsigned int StateDistance(const State &st1, const State &st2) const
void Print(std::ostream &os=std::cout) const
void AddCostComponent(CostComponent< Input, State, CFtype > &cc)
virtual void GreedyState(State &st, double alpha, unsigned int k)
bool EqualTo(CFtype value1, CFtype value2)
virtual bool CheckConsistency(const State &st) const =0
virtual void RandomState(State &st)=0
virtual CostStructure< CFtype > CostFunctionComponents(const State &st, const std::vector< double > &weights=std::vector< double >(0)) const
StateManager(const Input &in, std::string name)
bool GreaterThan(CFtype value1, CFtype value2)
CostStructure & operator+=(const CostStructure &other)
virtual bool OptimalStateReached(const State &st) const
The class CostComponent manages one single component of the cost, either hard or soft.
bool operator!=(const CostStructure< CFtype > &cs1, const CostStructure< CFtype > &cs2)
virtual CostStructure< CFtype > SampleState(State &st, unsigned int samples)
virtual bool LowerBoundReached(const CFtype &fvalue) const
std::vector< CostComponent< Input, State, CFtype > * > cost_component
CostStructure< CFtype > operator+(const CostStructure< CFtype > &cs1, const CostStructure< CFtype > &cs2)
bool IsZero(CFtype value)
void ClearCostStructure()
bool operator==(const CostStructure< CFtype > &cs1, const CostStructure< CFtype > &cs2)
CostStructure & operator-=(const CostStructure &other)
bool operator>=(const CostStructure< CFtype > &cs1, const CostStructure< CFtype > &cs2)
CostStructure< CFtype > operator-(const CostStructure< CFtype > &cs1, const CostStructure< CFtype > &cs2)
This component is responsible for all operations on the state which are independent of the neighborho...
CostStructure(CFtype total, CFtype violations, CFtype objective, const std::vector< CFtype > &all_components)
CostStructure(CFtype total, double weighted, CFtype violations, CFtype objective, const std::vector< CFtype > &all_components)