libROM  v1.0
Data-driven physical simulation library
GreedyRandomSampler.cpp
1 
11 // Description: This class greedily selects parameter points
12 // for the construction of a ROM database.
13 
14 #include "GreedyRandomSampler.h"
15 #include "utils/HDFDatabase.h"
16 #include "mpi.h"
17 #include <cmath>
18 #include <algorithm>
19 #include <limits.h>
20 #include <fstream>
21 
22 namespace CAROM {
23 
25  double param_space_min,
26  double param_space_max,
27  int num_parameter_points,
28  bool check_local_rom,
29  double relative_error_tolerance,
30  double alpha,
31  double max_clamp,
32  int subset_size,
33  int convergence_subset_size,
34  bool use_latin_hypercube,
35  std::string output_log_path,
36  std::string warm_start_file_name,
37  bool use_centroid,
38  int random_seed,
39  bool debug_algorithm) :
41  param_space_min,
42  param_space_max,
43  num_parameter_points,
44  check_local_rom,
45  relative_error_tolerance,
46  alpha,
47  max_clamp,
48  subset_size,
49  convergence_subset_size,
50  output_log_path,
51  warm_start_file_name,
52  use_centroid,
53  random_seed,
54  debug_algorithm
55  )
56 {
57  d_use_latin_hypercube = use_latin_hypercube;
58 
60  {
61  printSamplingType("latin-hypercube");
62  }
63  else
64  {
65  printSamplingType("random");
66  }
67 
70 
71  if (warm_start_file_name != "")
72  {
73  addDatabaseFromFile(warm_start_file_name);
74  }
75 }
76 
78  Vector param_space_min,
79  Vector param_space_max,
80  int num_parameter_points,
81  bool check_local_rom,
82  double relative_error_tolerance,
83  double alpha,
84  double max_clamp,
85  int subset_size,
86  int convergence_subset_size,
87  bool use_latin_hypercube,
88  std::string output_log_path,
89  std::string warm_start_file_name,
90  bool use_centroid,
91  int random_seed,
92  bool debug_algorithm) :
94  param_space_min,
95  param_space_max,
96  num_parameter_points,
97  check_local_rom,
98  relative_error_tolerance,
99  alpha,
100  max_clamp,
101  subset_size,
102  convergence_subset_size,
103  output_log_path,
104  warm_start_file_name,
105  use_centroid,
106  random_seed,
107  debug_algorithm
108  )
109 {
110  d_use_latin_hypercube = use_latin_hypercube;
111 
113  {
114  printSamplingType("latin-hypercube");
115  }
116  else
117  {
118  printSamplingType("random");
119  }
120 
123 
124  if (warm_start_file_name != "")
125  {
126  addDatabaseFromFile(warm_start_file_name);
127  }
128 }
129 
131  std::string base_file_name,
132  std::string output_log_path) :
134  base_file_name,
135  output_log_path
136  ) {}
137 
138 void
140 {
142 
143  Vector vec(d_min_param_point.dim(), false);
144 
146  {
147  std::vector<double> frequencies;
148  std::vector<std::vector<double>> point_coordinates;
149  std::vector<int> point_indices;
150  for (int i = 0; i < d_min_param_point.dim(); i++)
151  {
152  frequencies.push_back(std::abs(d_max_param_point.item(i) -
154  }
155 
156  for (int i = 0; i < d_num_parameter_points; i++) {
157  point_coordinates.push_back(std::vector<double>());
158  for (int j = 0; j < d_min_param_point.dim(); j++)
159  {
160  double low = d_min_param_point.item(j) + i * frequencies[j];
161  double high = d_min_param_point.item(j) + ((i + 1) * frequencies[j]);
162  std::uniform_real_distribution<double> unif(low, high);
163  point_coordinates[i].push_back(unif(rng));
164  }
165  }
166 
167  for (int i = 0; i < std::pow(d_num_parameter_points, d_min_param_point.dim());
168  i++)
169  {
170  point_indices.push_back(i);
171  }
172 
173  std::shuffle(point_indices.begin(), point_indices.end(), rng);
174 
175  for (int i = 0; i < d_num_parameter_points; i++)
176  {
177  int point_index = point_indices[i];
178  for (int j = 0; j < d_min_param_point.dim(); j++)
179  {
180  vec.item(j) = point_coordinates[point_index % d_num_parameter_points][j];
181  point_index /= d_num_parameter_points;
182  }
183  d_parameter_points.push_back(vec);
184  }
185  }
186  else
187  {
189  }
190 }
191 
192 void
193 GreedyRandomSampler::load(std::string base_file_name)
194 {
195  GreedySampler::load(base_file_name);
196 
197  char tmp[100];
198  std::string full_file_name = base_file_name;
199  HDFDatabase database;
200  database.open(full_file_name, "r");
201 
203  {
204  int bool_int_temp;
205  sprintf(tmp, "use_latin_hypercube");
206  database.getInteger(tmp, bool_int_temp);
207  d_use_latin_hypercube = (bool) bool_int_temp;
208  }
209 
210  database.close();
211 }
212 
213 void
214 GreedyRandomSampler::save(std::string base_file_name)
215 {
216  GreedySampler::save(base_file_name);
217 
218  if (d_rank == 0)
219  {
220  char tmp[100];
221  std::string full_file_name = base_file_name;
222  HDFDatabase database;
223  database.open(full_file_name, "wr");
224 
226  {
227  sprintf(tmp, "use_latin_hypercube");
228  database.putInteger(tmp, d_use_latin_hypercube);
229  }
230 
231  database.close();
232  }
233 }
234 
235 void
237 {
241  d_next_point_to_sample, true));
245 }
246 
248 {
249 }
250 
251 }
void getInteger(const std::string &key, int &data)
Reads an integer associated with the supplied key from the currently open database file.
Definition: Database.h:182
void putInteger(const std::string &key, int data)
Writes an integer associated with the supplied key to currently open database file.
Definition: Database.h:95
void load(std::string base_file_name)
Load the object state from a file.
void save(std::string base_file_name)
Save the object state to a file.
GreedyRandomSampler(Vector param_space_min, Vector param_space_max, int num_parameter_points, bool check_local_rom, double relative_error_tolerance, double alpha, double max_clamp, int subset_size, int convergence_subset_size, bool use_latin_hypercube, std::string output_log_path="", std::string warm_start_file_name="", bool use_centroid=true, int random_seed=1, bool debug_algorithm=false)
Constructor.
bool d_use_latin_hypercube
Use latin hypercube sampling instead of fixed uniform.
void getNextParameterPointAfterConvergenceFailure()
Get the next parameter point to sample after a convergence failure.
void constructParameterPoints()
Construct the list of parameter point candidates to sample.
double d_max_error
The current max error of the parameter points of the current iteration.
virtual void save(std::string base_file_name)
Save the object state to a file.
void printSamplingType(std::string sampling_type)
Print the sampling type.
std::vector< int > d_parameter_point_random_indices
The parameter point indices (used to generate the random subsets).
std::vector< Vector > d_convergence_points
The convergence parameter points to explore.
std::vector< Vector > d_parameter_points
The parameter points to explore.
int d_rank
The rank of the given processor.
std::vector< double > d_parameter_point_errors
The current errors of the parameter points.
void initializeParameterPoints()
Initialize the list of parameter point candidates to sample.
int d_counter
An internal counter.
std::default_random_engine rng
Random engine used to generate subsets.
std::vector< int > d_parameter_point_local_rom
The local ROMs used to obtain the current errors of the parameter points.
int d_next_point_to_sample
The next parameter point to sample.
int d_num_parameter_points
The maximum number of parameter points to sample.
virtual void load(std::string base_file_name)
Load the object state from a file.
int getNearestROMIndexToParameterPoint(int index, bool ignore_self)
Returns the index to the nearest local ROM to the specified parameter point index in the parameter po...
bool d_procedure_completed
Whether the greedy procedure has completed.
Vector d_max_param_point
The maximum value of the parameter space.
Vector d_min_param_point
The minimum value of the parameter space.
void addDatabaseFromFile(std::string const &warm_start_file_name)
Do a warm start by adding the sampled parameters from the database in a file to the current database.
void checkParameterPointInput()
Construct the list of parameter point candidates to sample.
std::vector< Vector > generateRandomPoints(int num_points)
Generate a vector of random points.
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL) override
Opens an existing HDF5 database file with the supplied name.
Definition: HDFDatabase.cpp:76
virtual bool close()
Closes the currently open HDF5 database file.
const double & item(int i) const
Const Vector member access.
Definition: Vector.h:656
int dim() const
Returns the dimension of the Vector on this processor.
Definition: Vector.h:266