dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
Problem.h
1 //
2 // Created by amarrero on 17/10/19.
3 //
4 
5 #ifndef DIGNEA_PROBLEM_H
6 #define DIGNEA_PROBLEM_H
7 
8 #include <dignea/core/Solution.h>
9 #include <dignea/utilities/random/ParallelPRNG.h>
10 
11 #include <iostream>
12 #include <memory>
13 #include <nlohmann/json.hpp>
14 #include <vector>
15 
16 using namespace std;
17 using json = nlohmann::json;
18 
19 enum optimisationDirection { Minimize, Maximize };
20 
28 template <class S>
29 class Problem {
30  public:
32 
33  Problem(const int &numberOfVars, const int &numberOfObjectives,
34  const int &nCons);
35 
36  Problem(const Problem *copy);
37 
38  virtual ~Problem() = default;
39 
40  public:
49  virtual bool evaluateConstraints(S &sol) const = 0;
50 
57  virtual void evaluate(S &sol) const = 0;
58 
66  virtual S createSolution() const = 0;
67 
68  virtual S createSolution(ParallelPRNG &engine) const = 0;
69 
78  virtual int getOptimizationDirection(const int i) const = 0;
79 
87  virtual float getUpperLimit(const int i) const = 0;
88 
96  virtual float getLowerLimit(const int i) const = 0;
97 
104  virtual string getName() const = 0;
105 
112  virtual void readData(const std::string &path) = 0;
113 
114  virtual json to_json() const;
115 
116  public:
122  inline int getNumberOfVars() const { return numberOfVars; };
128  inline void setNumberOfVars(int nVars) { numberOfVars = nVars; };
129 
135  inline int getNumberOfObjs() const { return numberOfObjs; };
136 
142  inline void setNumberOfObjs(int nObjs) { numberOfObjs = nObjs; };
143 
149  inline int getNumberOfCons() const { return numberOfCons; };
155  inline void setNumberOfCons(int nCons) { numberOfCons = nCons; };
156 
157  protected:
161 };
162 
168 template <class S>
169 Problem<S>::Problem() : numberOfVars(0), numberOfObjs(0), numberOfCons(0) {}
170 
177 template <class S>
179  : numberOfVars(ind->numberOfVars),
180  numberOfObjs(ind->numberOfObjs),
181  numberOfCons(ind->numberOfCons) {}
182 
191 template <class S>
192 Problem<S>::Problem(const int &nVars, const int &nObjs, const int &nCons)
193  : numberOfVars(nVars), numberOfObjs(nObjs), numberOfCons(nCons) {}
194 
200 template <class S>
201 json Problem<S>::to_json() const {
202  json data;
203  data["name"] = this->getName();
204  data["num_objs"] = this->numberOfObjs;
205  data["num_vars"] = this->numberOfVars;
206  data["num_cons"] = this->numberOfCons;
207  data["up_limit"] = this->getUpperLimit(0);
208  data["low_limit"] = this->getLowerLimit(0);
209  return data;
210 }
211 
212 #endif // DIGNEA_PROBLEM_H
nlohmann::json json
Definition: MinKnap.h:85
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29
int getNumberOfVars() const
Get the number of variables (dimension) of the problem.
Definition: Problem.h:122
virtual void readData(const std::string &path)=0
Method to read problem information from the given path. This method must be implemented in the subcla...
virtual S createSolution() const =0
Creates a new solution for the problem. Usually uses problem information and generates a solution ran...
int numberOfVars
Definition: Problem.h:155
void setNumberOfObjs(int nObjs)
Sets the number of objectives to the problem.
Definition: Problem.h:142
virtual float getLowerLimit(const int i) const =0
Returns the lower bound to the problem at dimension i. This method must be implemented in the subclas...
int getNumberOfObjs() const
Get the number of objectives of the problem.
Definition: Problem.h:135
virtual bool evaluateConstraints(S &sol) const =0
Method to evaluate whether a solution is feasible or not. This method must be implemented in the subc...
virtual json to_json() const
Creates a JSON object with the information of the Problem.
Definition: Problem.h:201
Problem(const int &numberOfVars, const int &numberOfObjectives, const int &nCons)
Construct a new Problem with all the given parameters.
Definition: Problem.h:192
int getNumberOfCons() const
Get the number the constraint of the problem.
Definition: Problem.h:149
int numberOfObjs
Definition: Problem.h:159
void setNumberOfCons(int nCons)
Sets the number of constraint of the problem.
Definition: Problem.h:155
virtual string getName() const =0
Returns the name of the problem. This method must be implemented in the subclasses.
virtual void evaluate(S &sol) const =0
Method to evaluate a solution using the proper problem formulation. This method must be implemented i...
virtual int getOptimizationDirection(const int i) const =0
Returns the optimization direction for each objective in the problem. It returns Minimize or Maximize...
void setNumberOfVars(int nVars)
Sets the number of variables (dimension) of the problem.
Definition: Problem.h:128
virtual float getUpperLimit(const int i) const =0
Returns the upper bound to the problem at dimension i. This method must be implemented in the subclas...
int numberOfCons
Definition: Problem.h:160
Problem()
Creates a new Problem with all parameters to zero.
Definition: Problem.h:169