Operational Research Library  1.0
KnapsackProblem.h
Go to the documentation of this file.
1 #ifndef KNAPSACKPROBLEM_H
2 #define KNAPSACKPROBLEM_H
3 
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <algorithm>
8 #include <vector>
9 #include "RandomHelper.h"
10 #include "IData.h"
11 
12 namespace operational_research
13 {
14 
22 class KnapsackProblem : public IData
23 {
24 private:
25  // Number of objects.
26  unsigned int objects;
27 
28  // Weights of objects.
29  std::vector<double> weights;
30 
31  // Profits of objects.
32  std::vector<double> profits;
33 
34  // Knapsack capacity.
35  double capacity;
36 
42  bool checkSchedule(const std::vector<unsigned int>& schedule);
43 
51  void writeResultToFile(const std::vector<unsigned int>& schedule, double costFunctionValue, const std::string& filename);
52 
53 public:
62  KnapsackProblem(unsigned int objects, const std::vector<double>& weights, const std::vector<double>& profits, double capacity);
63 
75  KnapsackProblem(unsigned int objects, double capacity, double minWeight, double maxWeight, double minProfit, double maxProfit, bool integerValues = false);
76 
83  KnapsackProblem(const std::string& filename);
84 
93  void setData(unsigned int objects, const std::vector<double>& weights, const std::vector<double>& profits, double capacity);
94 
106  void generateData(unsigned int objects, double capacity, double minWeight, double maxWeight, double minProfit, double maxProfit, bool integerValues = false);
107 
114  void readDataFromFile(const std::string& filename);
115 
121  void writeDataToFile(const std::string& filename);
122 
126  void showData();
127 
134  double evaluateKP(const std::vector<unsigned int>& schedule);
135 
144  double evaluateKP(const std::vector<unsigned int>& schedule, const std::string& filename);
145 
150  double getCapacity() const {
151  return capacity;
152  }
153 
158  unsigned int getObjects() const {
159  return objects;
160  }
161 
166  const std::vector<double>& getProfits() const {
167  return profits;
168  }
169 
174  const std::vector<double>& getWeights() const {
175  return weights;
176  }
177 };
178 
179 } // namespace operational_research
180 
181 #endif // KNAPSACKPROBLEM_H
void readDataFromFile(const std::string &filename)
Reads knapsack problem data from a file with a filename.
Definition: KnapsackProblem.cpp:144
void generateData(unsigned int objects, double capacity, double minWeight, double maxWeight, double minProfit, double maxProfit, bool integerValues=false)
Generates knapsack problem data according to setup.
Definition: KnapsackProblem.cpp:78
Interface for reading, writing and displaying data.
const std::vector< double > & getWeights() const
Getter for weights of objects.
Definition: KnapsackProblem.h:174
KnapsackProblem(unsigned int objects, const std::vector< double > &weights, const std::vector< double > &profits, double capacity)
Constructor.
Definition: KnapsackProblem.cpp:11
Definition: BinPackingProblem.cpp:3
double evaluateKP(const std::vector< unsigned int > &schedule)
Evaluates knapsack problem.
Definition: KnapsackProblem.cpp:44
const std::vector< double > & getProfits() const
Getter for profits of objects.
Definition: KnapsackProblem.h:166
Definition: KnapsackProblem.h:22
void showData()
Shows knapsack problem data.
Definition: KnapsackProblem.cpp:268
void writeDataToFile(const std::string &filename)
Writes knapsack problem data to a file with a filename.
Definition: KnapsackProblem.cpp:280
Simple pseudo random number generator class.
void setData(unsigned int objects, const std::vector< double > &weights, const std::vector< double > &profits, double capacity)
Setter for knapsack problem data.
Definition: KnapsackProblem.cpp:227
unsigned int getObjects() const
Getter for number of objects.
Definition: KnapsackProblem.h:158
double getCapacity() const
Getter for knapsack capacity.
Definition: KnapsackProblem.h:150
Definition: IData.h:14