dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
TwoOpt.h
1 
2 
3 #ifndef DIGNEA_TSP_TwoOpt_HEURISTIC_H
4 #define DIGNEA_TSP_TwoOpt_HEURISTIC_H
5 
6 #include <dignea/core/Heuristic.h>
7 #include <dignea/problems/TSP.h>
8 
9 #include <nlohmann/json.hpp>
10 #include <vector>
11 
12 namespace tsp_heuristics {
13 
14 using json = nlohmann::json;
15 using namespace std;
21 class TwoOpt : public Heuristic<IntFloatSolution> {
22  public:
23  TwoOpt(const bool randomInit = true);
24 
25  virtual ~TwoOpt() = default;
26 
27  void run() override;
28 
29  string getName() const {
30  return randomInitialisation ? "TwoOpt TSP" : "NN-TwoOpt TSP";
31  };
32 
33  string getID() const {
34  return randomInitialisation ? "TwoOpt TSP" : "NN-TwoOpt TSP";
35  }
36 
37  void setProblem(shared_ptr<TSP> prob);
38 
39  void setProblem(TSP* prob);
40 
41  Front<IntFloatSolution> getResults() const override;
42 
43  private:
44  vector<int> performSwap(const int& i, const int& k,
45  const vector<int>& tour);
46 
47  float calculateLength(const vector<int>& tour, const TSP* problem);
48 
49  private:
50  bool randomInitialisation;
51 };
52 
53 } // namespace tsp_heuristics
54 #endif // DIGNEA_TSP_TwoOpt_HEURISTIC_H
nlohmann::json json
Definition: MinKnap.h:85
Front class which stores the final results of an EA execution.
Definition: Front.h:26
Base heuristic class. This is the skeleton for future heuristic implementations. Extends AbstractEA f...
Definition: Heuristic.h:19
Traveling Salesman Problem implemented in dignea. Uses a IntFloat representation where the solutions ...
Definition: TSP.h:35
TwoOpt Heuristic for the Knapsack Problem. Tries to insert each item in the knapsack while there is r...
Definition: TwoOpt.h:21
string getName() const
Returns the name of the algorithm, this is used in the to_json method. Must be implemented in the sub...
Definition: TwoOpt.h:29
string getID() const
Returns the identificator of the algorithm, this is used in the to_json method. Must be implemented i...
Definition: TwoOpt.h:33