TCPSPSuite
storage.hpp
1 //
2 // Created by lukas on 24.10.17.
3 //
4 
5 #ifndef TCPSPSUITE_STORAGE_HPP
6 #define TCPSPSUITE_STORAGE_HPP
7 
8 #include "../datastructures/maybe.hpp" // for Maybe
9 #include "../instance/solution.hpp" // for Solution
10 #include "../manager/memoryinfo.hpp"
11 #include "../util/log.hpp" // for Log
12 #include "db_factory.hpp"
13 
14 #include <memory> // for shared_ptr, unique_ptr
15 #include <mutex>
16 #include <odb/database.hxx>
17 #include <string> // for string
18 #include <vector> // for vector
19 
20 class DBConfig;
21 class DBResult;
22 class DBSolution;
23 class SolverConfig;
24 class DBMerger;
25 class DBInvocation;
26 
27 class AdditionalResultStorage {
28 public:
29  // We never ever want to copy this. Results must be stored in the created
30  // instance!
31  AdditionalResultStorage() = default;
32  AdditionalResultStorage(const AdditionalResultStorage &) = delete;
33  AdditionalResultStorage & operator=(const AdditionalResultStorage &) = delete;
34 
35  struct IntermediateResult
36  {
37  public:
38  Maybe<double> time;
39  Maybe<unsigned int> iteration;
40  Maybe<double> costs;
41  Maybe<double> bound;
42  Maybe<Solution> solution;
43  };
44 
45  struct ExtendedMeasure
46  {
47  public:
48  // const static unsigned int TYPE_STRING;
49  constexpr static unsigned int TYPE_DOUBLE = 2;
50  constexpr static unsigned int TYPE_INT = 3;
51 
52  std::string key;
53  Maybe<unsigned int> iteration;
54  Maybe<double> time;
55 
56  unsigned int type;
57  union V {
58  // std::string v_string;
59  double v_double;
60  int v_int;
61  V(double v) : v_double(v){};
62  V(int v) : v_int(v){};
63  } value;
64  };
65 
66  std::vector<IntermediateResult> intermediate_results;
67  std::vector<ExtendedMeasure> extended_measures;
68 };
69 
70 class Storage {
71 public:
72  explicit Storage(std::string filename, unsigned int retry_count = 1000);
73 
74  long unsigned int insert(const Solution & sol, const std::string & run_id,
75  const std::string & algorithm_id,
76  const std::string & config_name, int instance_seed,
77  double elapsed_time, const SolverConfig & sc,
78  const AdditionalResultStorage & additional,
79  const manager::LinuxMemoryInfo * mem_info,
80  const manager::PAPIPerformanceInfo * papi_info);
81 
82  void insert_error(const std::string & instance_id, const std::string & run_id,
83  const std::string & algorithm_id,
84  const std::string & config_name, int seed,
85  unsigned int error_id, int fault_code);
86 
87  /*
88  * Querying
89  */
90  bool check_result(const std::string & instance_id, const std::string & run_id,
91  const std::string & algorithm_id, const SolverConfig & sc,
92  bool only_optimal = false, bool ignore_config_name = false,
93  bool ignore_run_name = false);
94 
95  bool check_error(std::vector<int> error_ids, std::vector<int> fault_codes,
96  const std::string & instance_id, const std::string & run_id,
97  const std::string & algorithm_id, const SolverConfig & sc,
98  bool ignore_config_name = false,
99  bool ignore_run_name = false);
100 
101  std::vector<std::shared_ptr<DBResult>>
102  get_results_for_config(const SolverConfig & sc);
103 
104  std::shared_ptr<DBConfig>
105  find_equivalent_config(std::shared_ptr<DBConfig> src);
106 
107  static void initialize(std::string filename, int argc, const char ** argv);
108  static std::shared_ptr<DBInvocation> get_invocation();
109 
110 private:
111  static std::mutex insert_mutex;
112  static std::mutex insert_error_mutex;
113  static std::mutex check_result_mutex;
114  static std::mutex check_error_mutex;
115 
116  static std::shared_ptr<DBInvocation> invocation;
117 
118  unsigned int retry_count;
119 
120  std::vector<unsigned long> find_db_configs(const SolverConfig & sc,
121  bool ignore_name = false);
122  /*
123  bool are_configs_equal(const SolverConfig & sc,
124  std::shared_ptr<DBConfig> dbcfg) const;
125  */
126  void insert_intermediate_result(
127  std::shared_ptr<DBResult> res,
128  const AdditionalResultStorage::IntermediateResult & intermediate);
129  void insert_extended_measure(
130  std::shared_ptr<DBResult> res,
131  const AdditionalResultStorage::ExtendedMeasure & measure);
132  std::shared_ptr<DBSolution> insert_solution(std::shared_ptr<DBResult> res,
133  const Solution & sol);
134 
135  std::unique_ptr<odb::database> db;
136 
137  std::shared_ptr<DBConfig> get_solverconfig(const SolverConfig & sc);
138  std::shared_ptr<DBConfig> get_or_insert_solverconfig(const SolverConfig & sc);
139 
140  Log l;
141 
142  // The merger has raw access to the database
143  friend class DBMerger;
144 };
145 
146 #endif // TCPSPSUITE_STORAGE_HPP
Solution
a soultion for a TCPSP instance
Definition: solution.hpp:17