Samchon Framework for CPP  1.0.0
Scheduler.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 #include <samchon/examples/tsp/Travel.hpp>
4 
5 #include <iostream>
6 #include <samchon/library/GeneticAlgorithm.hpp>
7 
8 namespace samchon
9 {
10 namespace examples
11 {
12 namespace tsp
13 {
14  using namespace std;
15 
16  using namespace library;
17  using namespace protocol;
18 
29  struct GAParameters
30  {
31  double mutationRate;
32  size_t tournament;
33 
34  size_t population;
35  size_t generation;
36  };
37 
44  class Scheduler
45  : public Entity<>
46  {
47  private:
48  typedef Entity<std::string> super;
49 
50  protected:
54  shared_ptr<Travel> travel;
55 
59  struct GAParameters gaParameters;
60 
61  public:
62  /* -----------------------------------------------------------
63  CONSTRUCTORS
64  ----------------------------------------------------------- */
69  : super()
70  {
71  this->travel = make_shared<Travel>();
72  this->gaParameters = {.03, 50, 100, 300};
73  };
74 
78  Scheduler(shared_ptr<Travel> travel, const struct GAParameters &gaParameteres)
79  : super()
80  {
81  this->travel = travel;
82  this->gaParameters = gaParameteres;
83  };
84 
85  virtual ~Scheduler() = default;
86 
87  virtual void construct(shared_ptr<XML> xml) override
88  {
89  gaParameters.mutationRate = xml->getProperty<double>("mutationRate");
90  gaParameters.tournament = xml->getProperty<size_t>("tournament");
91  gaParameters.population = xml->getProperty<size_t>("population");
92  gaParameters.generation = xml->getProperty<size_t>("generation");
93 
94  travel->construct(xml->get(travel->TAG())->at(0));
95  };
96 
97  /* -----------------------------------------------------------
98  CALCULATORS
99  ----------------------------------------------------------- */
105  auto optimize() -> shared_ptr<Travel>
106  {
107  GeneticAlgorithm<Travel> geneticAlgorithm
108  (
109  true,
110  gaParameters.mutationRate,
111  gaParameters.tournament
112  );
113 
114  travel =
115  geneticAlgorithm.evolveGeneArray
116  (
117  travel,
118  gaParameters.population,
119  gaParameters.generation
120  );
121 
122  return travel;
123  };
124 
125  auto calcDistance() const -> double
126  {
127  return travel->calcDistance();
128  };
129 
130  /* -----------------------------------------------------------
131  EXPORTER
132  ----------------------------------------------------------- */
133  virtual auto TAG() const -> string override
134  {
135  return "scheduler";
136  };
137 
138  virtual auto toXML() const -> shared_ptr<XML> override
139  {
140  shared_ptr<XML> &xml = super::toXML();
141  xml->setProperty("mutationRate", gaParameters.mutationRate);
142  xml->setProperty("tournament", gaParameters.tournament);
143  xml->setProperty("population", gaParameters.population);
144  xml->setProperty("generation", gaParameters.generation);
145 
146  xml->push_back(travel->toXML());
147  return xml;
148  };
149 
150  auto toString() const -> string
151  {
152  return travel->toString();
153  };
154 
155  /* -----------------------------------------------------------
156  MAIN
157  ----------------------------------------------------------- */
158  static void main()
159  {
160  shared_ptr<Travel> travel = make_shared<Travel>();
161  for(int i = 0; i < 30; i++)
162  travel->emplace_back(new GeometryPoint(i + 1));
163 
164  // OPTIMIZING
165  struct GAParameters gaParameters = {.03, 30, 400, 400};
166 
167  Scheduler scheduler(travel, gaParameters);
168  travel = scheduler.optimize();
169 
170  // PRINTING
171  string &str = travel->toString();
172 
173  cout << str << endl;
174  };
175  };
176 };
177 };
178 };
An entity, a standard data class.
Definition: Entity.hpp:115
auto optimize() -> shared_ptr< Travel >
Derive optimized schedule.
Definition: Scheduler.hpp:105
A genetic algorithm class.
Scheduler(shared_ptr< Travel > travel, const struct GAParameters &gaParameteres)
Construct from points and parameter of genetic algorithm.
Definition: Scheduler.hpp:78
auto evolveGeneArray(std::shared_ptr< GeneArray > individual, size_t population, size_t generation) const -> std::shared_ptr< GeneArray >
Evolve a GeneArray.
Scheduler()
Default Constructor.
Definition: Scheduler.hpp:68
shared_ptr< Travel > travel
A travel to optimize or optimized.
Definition: Scheduler.hpp:54
A geometry coordinates (point)
Parameters for Genetic-Algorithm.
Definition: Scheduler.hpp:29