Darwin Neuroevolution Framework
universe.h
1 // Copyright 2018 The Darwin Neuroevolution Framework Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "utils.h"
18 #include "database.h"
19 #include "properties.h"
20 #include "stringify.h"
21 
22 #include <time.h>
23 #include <mutex>
24 #include <optional>
25 using namespace std;
26 
27 // TODO: test cases
28 
29 namespace darwin {
30 
34  db::RowId id = 0;
35 
37  optional<string> comment;
38 
40  time_t timestamp = 0;
41 };
42 
44 struct DbExperiment : public DbUniverseObject {
47  optional<string> name;
48 
50  string setup;
51 
53  optional<db::RowId> last_variation_id;
54 
57  time_t last_activity_timestamp = 0;
58 };
59 
65  optional<db::RowId> previous_id;
66 
68  db::RowId experiment_id = 0;
69 
72  optional<string> name;
73 
75  string config;
76 };
77 
82  db::RowId variation_id = 0;
83 
85  string config;
86 };
87 
90 struct DbGeneration : public DbUniverseObject {
92  db::RowId trace_id = 0;
93 
95  int generation = -1;
96 
98  string summary;
99 
101  optional<string> details;
102 
104  optional<string> genotypes;
105 
107  optional<string> profile;
108 };
109 
111 class Universe : public core::NonCopyable {
112  public:
114  static unique_ptr<Universe> create(const string& path);
115 
117  static unique_ptr<Universe> open(const string& path);
118 
120  string path() const { return path_; }
121 
123  unique_ptr<DbExperiment> newExperiment(const optional<string>& name,
124  const string& setup,
125  const optional<db::RowId>& base_variation_id);
126 
128  unique_ptr<DbExperiment> loadExperiment(db::RowId experiment_id) const;
129 
131  bool findExperiment(const string& name) const;
132 
134  vector<DbExperiment> experimentsList() const;
135 
137  unique_ptr<DbExperimentVariation> newVariation(db::RowId experiment_id,
138  const string& config);
139 
141  unique_ptr<DbExperimentVariation> loadVariation(db::RowId variation_id) const;
142 
144  unique_ptr<DbEvolutionTrace> newTrace(db::RowId variation_id,
145  const string& evolution_config);
146 
148  void newGeneration(const DbGeneration& db_generation);
149 
150  // yeah, doesn't really belong here, but the standard C++ library
151  // support for formatting date/time is still broken (not thread safe)
152  string strftime(time_t timestamp, const string& format) const;
153 
154  private:
155  explicit Universe(const string& path);
156 
157  static void initializeUniverse(const string& path);
158 
159  db::RowId createVariationHelper(db::RowId experiment_id,
160  const optional<db::RowId> prev_variation_id,
161  const string& config);
162 
163  private:
164  string path_;
165 
166  // the database connection is mutable to allow Universe to
167  // expose a proper interface (including const methods)
168  mutable db::Connection db_;
169 
170  // guards all inserts in order to reliably get the last inserted RowId
171  mutex db_insert_lock_;
172 };
173 
174 } // namespace darwin
optional< string > profile
Runtime profile data (json)
Definition: universe.h:107
A recording of an evolution experiment run.
Definition: universe.h:80
A very simple relational database abstraction on top of Sqlite.
Definition: database.h:178
The persistent storage for all the experiments and variations.
Definition: universe.h:111
optional< db::RowId > last_variation_id
Pointer to the most recent variation.
Definition: universe.h:53
string config
The evolution run configuration (json)
Definition: universe.h:85
STL namespace.
int64_t RowId
Represents the ID of a row in the database.
Definition: database.h:44
optional< string > comment
Object comment/annotation.
Definition: universe.h:37
A recording of a particular generation history.
Definition: universe.h:90
optional< string > details
Extra details (json)
Definition: universe.h:101
optional< db::RowId > previous_id
Previous experiment variation.
Definition: universe.h:65
optional< string > name
Experiment name.
Definition: universe.h:47
string setup
Experiment setup values (json)
Definition: universe.h:50
Base class for all the universe database objects.
Definition: universe.h:32
Key Darwin Neuroevolution Framework interfaces.
Classes derived from this are not copyable or movable.
Definition: utils.h:69
A variation of an experiment configuration values.
Definition: universe.h:62
optional< string > name
Variation name.
Definition: universe.h:72
optional< string > genotypes
Notable genotypes (json)
Definition: universe.h:104
string summary
Generation summary (json)
Definition: universe.h:98
Universe representation of an experiment.
Definition: universe.h:44
string config
The experiment configuration values for this variation (json)
Definition: universe.h:75
string path() const
The path of this universe database.
Definition: universe.h:120