Moved the includes to simann.h.
6 const double INFTY = 1e24;
12 Controller *controller;
18 virtual void mutate() = 0;
19 virtual void revert() = 0;
20 virtual void saveAsBest() = 0;
23 curr_cost = prev_cost = best_cost = INFTY;
25 void setController(Controller &_controller) {
26 controller = &_controller;
27 controller->setBase(this);
29 double getCurrCost() { return curr_cost; }
30 double getPrevCost() { return prev_cost; }
31 double getBestCost() { return best_cost; }
33 while (controller->next()) {
35 if (controller->accept()) {
36 controller->acceptEvent();
37 if (curr_cost < best_cost) {
39 controller->improveEvent();
44 controller->rejectEvent();
52 virtual void acceptEvent() {}
53 virtual void improveEvent() {}
54 virtual void rejectEvent() {}
55 virtual void setBase(SimAnnBase *_base) { base = _base; }
56 virtual bool next() = 0;
57 virtual bool accept() = 0;
62 class SimAnn : public SimAnnBase {
67 SimAnn() : SimAnnBase() {}
68 void setEntity(E &ent) {
69 curr_ent = new E(ent);
70 best_ent = new E(ent);
72 E getBestEntity() { return *best_ent; }
80 *best_ent = *curr_ent;
81 best_cost = curr_cost;
85 class EntitySkeleton {
87 // returns the new cost
88 double mutate() { return 0.0; }
89 // restores the entity to its previous state i.e. reverts the effects of
94 class SimpleController : public SimAnnBase::Controller {
96 long iter, last_impr, max_iter, max_no_impr;
97 double temp, annealing_factor;
102 annealing_factor = 0.9999;
108 void improveEvent() {
115 temp *= annealing_factor;
116 bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
120 return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp));