dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
TSP.h
Go to the documentation of this file.
1 
12 #ifndef DIGNEA_TSP_H
13 #define DIGNEA_TSP_H
14 
15 #include <dignea/core/Problem.h>
16 #include <dignea/types/SolutionTypes.h>
17 #include <dignea/utilities/random/PseudoRandom.h>
18 
19 #include <fstream>
20 #include <iostream>
21 #include <utility>
22 
27 using coords = std::pair<float, float>;
28 
35 class TSP : public Problem<IntFloatSolution> {
36  public:
38  TSP();
39 
40  virtual ~TSP() = default;
41 
44  explicit TSP(const int &numberOfVars);
45 
50  explicit TSP(const int &numberOfVars, const vector<coords> &coordinates);
51 
54  TSP(const string &pathToInstance);
55 
58  TSP(TSP const *other);
59 
65  float getUpperLimit(int i) const override;
66 
71  int getOptimizationDirection(const int i) const override;
72 
76  float getLowerLimit(int i) const override;
77 
80  string getName() const override { return "TSP"; };
81 
84  const vector<vector<float>> &getDistances() const { return distances; }
85 
88  const vector<coords> &getPoints() const { return points; }
89 
92  const std::string getPathToInstance() const { return pathToInstance; }
93 
96  void evaluate(IntFloatSolution &solution) const override;
97 
102  bool evaluateConstraints(IntFloatSolution &solution) const override;
103 
106  IntFloatSolution createSolution() const override;
107 
110  IntFloatSolution createSolution(ParallelPRNG &engine) const override;
111 
114  void readData(const string &path) override;
115 
118  json to_json() const override;
119 
120  protected:
121  void computeDistances();
122 
123  protected:
124  std::string pathToInstance;
125  vector<coords> points;
126  vector<vector<float>> distances;
127 };
128 
129 #endif
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29
int numberOfVars
Definition: Problem.h:155
Class to represent a solution to the optimization problems in the tool.
Definition: Solution.h:25
Traveling Salesman Problem implemented in dignea. Uses a IntFloat representation where the solutions ...
Definition: TSP.h:35
bool evaluateConstraints(IntFloatSolution &solution) const override
Evaluates whether a solution is feasible of not regarding to the TSP formulation.
Definition: TSP.cpp:169
int getOptimizationDirection(const int i) const override
Get the optimization direction. Only for dimension = 0 and returns Minimize.
Definition: TSP.cpp:243
const vector< coords > & getPoints() const
Get the points of the instances as coordinates.
Definition: TSP.h:88
vector< coords > points
Definition: TSP.h:125
float getUpperLimit(int i) const override
Get the upper bound of the search space. In this case the upper bound is the number of variables minu...
Definition: TSP.cpp:139
void computeDistances()
Computes the distance from each point to the other in the instance.
Definition: TSP.cpp:256
TSP()
Creates a default TSP object.
Definition: TSP.cpp:26
string getName() const override
Get the name of the problem.
Definition: TSP.h:80
json to_json() const override
Creates and return the JSON representation of the TSP problem.
Definition: TSP.cpp:284
vector< vector< float > > distances
Definition: TSP.h:126
std::string pathToInstance
Definition: TSP.h:124
IntFloatSolution createSolution() const override
Creates and return a random solution for the TSP.
Definition: TSP.cpp:231
void evaluate(IntFloatSolution &solution) const override
Evaluates a solution to the TSP.
Definition: TSP.cpp:192
const vector< vector< float > > & getDistances() const
Get the matrix of distances between each city in the instance.
Definition: TSP.h:84
const std::string getPathToInstance() const
Get the path to the instance if used.
Definition: TSP.h:92
void readData(const string &path) override
Reads the instance information from the filesystem.
Definition: TSP.cpp:108
float getLowerLimit(int i) const override
Get the lower bound of the search space (1).
Definition: TSP.cpp:153