Samchon Framework for CPP  1.0.0
Scheduler.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 #include <samchon/example/tsp/Travel.hpp>
4 
5 #include <samchon/library/GeneticAlgorithm.hpp>
6 
7 #include <iostream>
8 #include <Windows.h>
9 
10 namespace samchon
11 {
12 namespace example
13 {
14 namespace tsp
15 {
16  using namespace std;
17 
18  using namespace library;
19  using namespace protocol;
20 
31  struct GAParameters
32  {
33  double mutationRate;
34  size_t tournament;
35 
36  size_t population;
37  size_t generation;
38  };
39 
46  class Scheduler
47  : public Entity
48  {
49  private:
50  typedef Entity super;
51 
52  protected:
56  shared_ptr<Travel> travel;
57 
61  struct GAParameters gaParameters;
62 
63  public:
64  /* -----------------------------------------------------------
65  CONSTRUCTORS
66  ----------------------------------------------------------- */
71  : super()
72  {
73  this->travel = make_shared<Travel>();
74  this->gaParameters = {.03, 50, 100, 300};
75  };
76 
80  Scheduler(shared_ptr<Travel> travel, const struct GAParameters &gaParameteres)
81  : super()
82  {
83  this->travel = travel;
84  this->gaParameters = gaParameteres;
85  };
86 
87  virtual ~Scheduler() = default;
88 
89  virtual void construct(shared_ptr<XML> xml) override
90  {
91  super::construct(xml);
92  gaParameters.mutationRate = xml->getProperty<double>("mutationRate");
93  gaParameters.tournament = xml->getProperty<size_t>("tournament");
94  gaParameters.population = xml->getProperty<size_t>("population");
95  gaParameters.generation = xml->getProperty<size_t>("generation");
96 
97  travel->construct(xml->get(travel->TAG())->at(0));
98  };
99 
100  /* -----------------------------------------------------------
101  CALCULATORS
102  ----------------------------------------------------------- */
108  auto optimize() -> shared_ptr<Travel>
109  {
110  GeneticAlgorithm<Travel> geneticAlgorithm
111  (
112  true,
113  gaParameters.mutationRate,
114  gaParameters.tournament
115  );
116 
117  travel =
118  geneticAlgorithm.evolveGeneArray
119  (
120  travel,
121  gaParameters.population,
122  gaParameters.generation
123  );
124 
125  return travel;
126  };
127 
128  auto calcDistance() const -> double
129  {
130  return travel->calcDistance();
131  };
132 
133  /* -----------------------------------------------------------
134  EXPORTER
135  ----------------------------------------------------------- */
136  virtual auto TAG() const -> string override
137  {
138  return "scheduler";
139  };
140 
141  virtual auto toXML() const -> shared_ptr<XML> override
142  {
143  shared_ptr<XML> &xml = super::toXML();
144  xml->setProperty("mutationRate", gaParameters.mutationRate);
145  xml->setProperty("tournament", gaParameters.tournament);
146  xml->setProperty("population", gaParameters.population);
147  xml->setProperty("generation", gaParameters.generation);
148 
149  xml->push_back(travel->toXML());
150  return xml;
151  };
152 
153  auto toString() const -> string
154  {
155  return travel->toString();
156  };
157 
158  /* -----------------------------------------------------------
159  MAIN
160  ----------------------------------------------------------- */
161  static void main()
162  {
163  shared_ptr<Travel> travel = make_shared<Travel>();
164  for(int i = 0; i < 30; i++)
165  travel->emplace_back(new GeometryPoint(i + 1));
166 
167  // OPTIMIZING
168  struct GAParameters gaParameters = {.03, 30, 400, 400};
169 
170  Scheduler scheduler(travel, gaParameters);
171  travel = scheduler.optimize();
172 
173  // PRINTING
174  string &str = travel->toString();
175 
176  toClipboard(str);
177  cout << str << endl;
178  };
179 
180  private:
181  static void toClipboard(const string &str)
182  {
183  #if defined(_WINDOWS) || defined(_WIN32) || defined(_WIN64)
184  OpenClipboard(0);
185  EmptyClipboard();
186  HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, str.size());
187 
188  if (!hg)
189  {
190  CloseClipboard();
191  return;
192  }
193  memcpy(GlobalLock(hg), str.c_str(), str.size());
194 
195  GlobalUnlock(hg);
196  SetClipboardData(CF_TEXT, hg);
197  CloseClipboard();
198  GlobalFree(hg);
199  #endif
200  };
201  };
202 };
203 };
204 };
An entity, a standard data class.
Definition: Entity.hpp:48
virtual auto toXML() const -> shared_ptr< XML > override
Get an XML object represents the Entity.
Definition: Scheduler.hpp:141
Definition: RWMutex.hpp:4
Parameters for Genetic-Algorithm.
Definition: Scheduler.hpp:31
A genetic algorithm class.
shared_ptr< Travel > travel
A travel to optimize or optimized.
Definition: Scheduler.hpp:56
Scheduler()
Default Constructor.
Definition: Scheduler.hpp:70
Scheduler(shared_ptr< Travel > travel, const struct GAParameters &gaParameteres)
Construct from points and parameter of genetic algorithm.
Definition: Scheduler.hpp:80
A geometry coordinates (point)
XML is a class representing xml object.
Definition: XML.hpp:72
auto optimize() -> shared_ptr< Travel >
Derive optimized schedule.
Definition: Scheduler.hpp:108
virtual auto TAG() const -> string override
A tag name when represented by XML.
Definition: Scheduler.hpp:136
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
auto evolveGeneArray(std::shared_ptr< GeneArray > geneArray, size_t population, size_t generation) const -> std::shared_ptr< GeneArray >
Evolve a GeneArray.