Samchon Framework for CPP  1.0.0
TSPMaster.hpp
1 #pragma once
2 #include <samchon/example/interaction/Master.hpp>
3 
4 #include <samchon/example/interaction/ChiefDriver.hpp>
5 #include <samchon/example/interaction/SlaveDriver.hpp>
6 #include <samchon/protocol/Invoke.hpp>
7 
8 #include <samchon/example/tsp/Scheduler.hpp>
9 
10 namespace samchon
11 {
12 namespace example
13 {
14 namespace interaction
15 {
16  using namespace std;
17 
18  using namespace library;
19  using namespace protocol;
20 
30  class TSPMaster
31  : public Master
32  {
33  private:
34  typedef Master super;
35 
36  protected:
37  shared_ptr<tsp::Scheduler> scheduler;
38 
39  public:
40  /* ---------------------------------------------------------------------------------
41  CONSTRUCTORS
42  --------------------------------------------------------------------------------- */
47  : super(37100)
48  {
49  scheduler = make_shared<tsp::Scheduler>();
50  };
51  virtual ~TSPMaster() = default;
52 
53  protected:
54  /* ---------------------------------------------------------------------------------
55  INVOKE MESSATE CHAIN
56  --------------------------------------------------------------------------------- */
57  virtual void optimize(shared_ptr<XML> xml) override
58  {
59  super::optimize(nullptr);
60 
61  scheduler->construct(xml);
62 
63  sendSegmentData
64  (
65  make_shared<Invoke>
66  (
67  "optimize",
68  scheduler->toXML()
69  ),
70  this->size()
71  );
72  };
73 
74  virtual void replyOptimization(shared_ptr<XML> xml) override
75  {
76  unique_lock<mutex> uk(mtx);
77 
78  shared_ptr<tsp::Scheduler> scheduler(new tsp::Scheduler());
79  scheduler->construct(xml);
80 
81  cout << "An optimization process from a slave system has completed" << endl;
82  cout << "\tOrdinary minimum distance: " << this->scheduler->calcDistance()
83  << ", New Price from the slave: " << scheduler->calcDistance() << endl;
84 
85  if (scheduler->calcDistance() < this->scheduler->calcDistance())
86  this->scheduler = scheduler;
87 
88  if (++optimized < this->size())
89  return;
90 
91  cout << endl;
92  cout << "Parallel optimization has completed." << endl;
93  cout << scheduler->toString() << endl << endl;
94 
95  chiefDriver->sendData
96  (
97  make_shared<Invoke>
98  (
99  "replyOptimization",
100  scheduler->toXML()
101  )
102  );
103  };
104 
105  public:
106  /* ---------------------------------------------------------------------------------
107  MAIN
108  --------------------------------------------------------------------------------- */
112  static void main()
113  {
114  cout << "----------------------------------------------------------------------------" << endl;
115  cout << " TSP SOLVER MASTER" << endl;
116  cout << "----------------------------------------------------------------------------" << endl;
117 
118  TSPMaster master;
119  master.start();
120  };
121  };
122 };
123 };
124 };
static void main()
Main function.
Definition: TSPMaster.hpp:112
A master of parallel system solving something.
Definition: Master.hpp:49
A master for parallel system solving TSP problem.
Definition: TSPMaster.hpp:30
virtual void replyOptimization(shared_ptr< XML > xml) override
Handle (replied) optimized value from a slave system.
Definition: TSPMaster.hpp:74
virtual void optimize(shared_ptr< XML > xml) override
Optimize something.
Definition: TSPMaster.hpp:57