dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
KP.h
Go to the documentation of this file.
1 
11 #ifndef __DIGNEA_KNP_H__
12 #define __DIGNEA_KNP_H__
13 
14 #include <dignea/core/Problem.h>
15 #include <dignea/types/SolutionTypes.h>
16 #include <dignea/utilities/random/PseudoRandom.h>
17 
18 #include <fstream>
19 #include <iostream>
20 #include <utility>
21 
22 using iEff = pair<int, float>;
23 
28 class KP : public Problem<BoolFloatSolution> {
29  public:
30  KP();
31 
32  virtual ~KP();
33 
34  explicit KP(const int &numberOfVars);
35 
36  KP(const string &pathToInstance);
37 
38  KP(const int &numberOfVars, const int &capacity, const vector<int> &weights,
39  const vector<int> &profits);
40 
41  // Generacion de una instancia del KP de manera aleatoria con los datos
42  // proporcionados
43  KP(const int &numberOfVars, const int &lowerWeight, const int &upperWeight,
44  const int &lowerProfit, const int &upperProfit);
45 
46  KP(const KP *);
47 
48  float getUpperLimit(int i) const override;
49 
50  int getOptimizationDirection(const int i) const override;
51 
52  float getLowerLimit(int i) const override;
53 
54  string getName() const override { return "Knapsack Problem"; };
55 
56  void evaluate(BoolFloatSolution &solution) const override;
57 
58  bool evaluateConstraints(BoolFloatSolution &ptr) const override;
59 
60  BoolFloatSolution createSolution() const override;
61 
62  BoolFloatSolution createSolution(ParallelPRNG &engine) const override;
63 
64  int getLowWeight() const;
65 
66  void setLowWeight(int lowWeight);
67 
68  int getUpWeight() const;
69 
70  void setUpWeight(int upWeight);
71 
72  int getLowProfit() const;
73 
74  void setLowProfit(int lowProfit);
75 
76  int getUpProfit() const;
77 
78  void setUpProfit(int upProfit);
79 
80  const vector<int> &getProfits() const;
81 
82  void setProfits(const vector<int> &profits);
83 
84  const vector<int> &getWeights() const;
85 
86  void setWeights(const vector<int> &weights);
87 
88  int getCapacity() const;
89 
90  void setCapacity(int capacity);
91 
92  void readData(const string &path) override;
93 
94  json to_json() const override;
95 
96  vector<iEff> getItemsSortedByEff() const { return itemsEff; }
97 
98  protected:
99  void computeEfficiency();
100 
101  void repair(BoolFloatSolution &solution) const;
102 
103  protected:
104  int lowWeight;
105  int upWeight;
106  int lowProfit;
107  int upProfit;
108  // Profits, weights and final capacity of the instance
109  vector<int> profits;
110  vector<int> weights;
111  int capacity;
112  std::string pathToInstance;
113  // Vector con la eficiencia de los elementos de la instancia
114  vector<iEff> itemsEff;
115 
116  public:
117  static const string INSTANCE;
118  static const string PROFITS;
119  static const string WEIGHTS;
120  static const string CAPACITY;
121 };
122 
123 #endif // __DIGNEA_KNP_H__
Class representing a Knapsack Problem.
Definition: KP.h:28
void repair(BoolFloatSolution &solution) const
Definition: KP.cpp:237
void readData(const string &path) override
Definition: KP.cpp:155
string getName() const override
Returns the name of the problem. This method must be implemented in the subclasses.
Definition: KP.h:54
void evaluate(BoolFloatSolution &solution) const override
Definition: KP.cpp:262
json to_json() const override
Definition: KP.cpp:359
float getUpperLimit(int i) const override
Returns the upper bound to the problem at dimension i. This method must be implemented in the subclas...
Definition: KP.cpp:194
float getLowerLimit(int i) const override
Returns the lower bound to the problem at dimension i. This method must be implemented in the subclas...
Definition: KP.cpp:202
int getOptimizationDirection(const int i) const override
Returns the optimization direction for each objective in the problem. It returns Minimize or Maximize...
Definition: KP.cpp:317
BoolFloatSolution createSolution() const override
Definition: KP.cpp:277
void computeEfficiency()
Definition: KP.cpp:331
bool evaluateConstraints(BoolFloatSolution &ptr) const override
Definition: KP.cpp:214
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