Samchon Framework for CPP  1.0.0
TSPProcess.hpp
1 #pragma once
2 #include <samchon/templates/distributed/DistributedProcess.hpp>
3 #include <samchon/templates/distributed/base/DistributedSystemArrayBase.hpp>
4 
5 #include <iostream>
6 #include <samchon/library/Math.hpp>
7 #include <samchon/examples/tsp/Travel.hpp>
8 
9 namespace samchon
10 {
11 namespace examples
12 {
13 namespace interaction
14 {
15  class TSPProcess : public templates::distributed::DistributedProcess
16  {
17  private:
18  typedef templates::distributed::DistributedProcess super;
19 
20  public:
21  TSPProcess(templates::distributed::base::DistributedSystemArrayBase *systemArray)
22  : super(systemArray)
23  {
24  this->name = "tsp";
25  };
26  virtual ~TSPProcess() = default;
27 
28  void solve()
29  {
30  using namespace samchon::library;
31 
32  std::unique_ptr<tsp::Travel> travel(new tsp::Travel());
33  for (size_t i = 0; i < 9; i++)
34  travel->emplace_back(new tsp::GeometryPoint(i+1, Math::random(), Math::random()));
35 
36  sendData(std::make_shared<protocol::Invoke>("optimizeTSP", travel->toXML()));
37  };
38 
39  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
40  {
41  if (invoke->getListener() == "replyOptimization")
42  printSolution(invoke->front()->getValueAsXML());
43  };
44 
45  private:
46  void printSolution(std::shared_ptr<library::XML> xml)
47  {
48  std::unique_ptr<tsp::Travel> travel(new tsp::Travel());
49  travel->construct(xml);
50 
51  std::cout << "The best travel: " << travel->calcDistance() << " km" << std::endl;
52  };
53  };
54 };
55 };
56 };