libROM  v1.0
Data-driven physical simulation library
GreedySampler.h
1 
11 // Description: This class greedily selects parameter points
12 // for the construction of a ROM database. The implemented
13 // greedy algorithm is derived from algorithm 2 of Choi et. al's
14 // paper "Gradient-based constrained optimization using a database
15 // of linear reduced-order models": https://arxiv.org/abs/1506.07849
16 //
17 // The greedy algorithm workflow is as follows:
18 // 1. Construct the GreedySampler by giving it a
19 // domain of parameter points.
20 // 2. Request a parameter point to create a local ROM at.
21 // 3. Request a parameter point to compute an error error indicator
22 // for using the nearest local ROM.
23 // 4. Give the computed error indicator to the GreedySampler.
24 // 5. Repeat steps 3 and 4 until the GreedySampler
25 // no longer requires more error indicators to be computed.
26 // 6. Repeat steps 2 to 5 until the GreedySampler
27 // no longer requires more local ROMs to be created.
28 // 7. The ROM database is now complete, meeting the error tolerance
29 // for all parameter points within the domain.
30 
31 #ifndef included_GreedySampler_h
32 #define included_GreedySampler_h
33 
34 #include "linalg/Vector.h"
35 #include <string>
36 #include <vector>
37 #include <set>
38 #include <random>
39 
40 /* Use C++11 built-in shared pointers if available; else fallback to Boost. */
41 #if __cplusplus >= 201103L
42 #include <memory>
43 #else
44 #include <boost/shared_ptr.hpp>
45 #endif
46 
47 namespace CAROM {
48 
49 class Vector;
50 
56 
60  std::shared_ptr<Vector> point;
61 
65  std::shared_ptr<Vector> localROM;
66 };
67 
75 {
76 public:
103  std::vector<Vector> parameter_points,
104  bool check_local_rom,
105  double relative_error_tolerance,
106  double alpha,
107  double max_clamp,
108  int subset_size,
109  int convergence_subset_size,
110  std::string output_log_path = "",
111  std::string warm_start_file_name = "",
112  bool use_centroid = true,
113  int random_seed = 1,
114  bool debug_algorithm = false);
115 
141  std::vector<double> parameter_points,
142  bool check_local_rom,
143  double relative_error_tolerance,
144  double alpha,
145  double max_clamp,
146  int subset_size,
147  int convergence_subset_size,
148  std::string output_log_path = "",
149  std::string warm_start_file_name = "",
150  bool use_centroid = true,
151  int random_seed = 1,
152  bool debug_algorithm = false);
153 
183  Vector param_space_min,
184  Vector param_space_max,
185  int num_parameter_points,
186  bool check_local_rom,
187  double relative_error_tolerance,
188  double alpha,
189  double max_clamp,
190  int subset_size,
191  int convergence_subset_size,
192  std::string output_log_path = "",
193  std::string warm_start_file_name = "",
194  bool use_centroid = true,
195  int random_seed = 1,
196  bool debug_algorithm = false);
197 
227  double param_space_min,
228  double param_space_max,
229  int num_parameter_points,
230  bool check_local_rom,
231  double relative_error_tolerance,
232  double alpha,
233  double max_clamp,
234  int subset_size,
235  int convergence_subset_size,
236  std::string output_log_path = "",
237  std::string warm_start_file_name = "",
238  bool use_centroid = true,
239  int random_seed = 1,
240  bool debug_algorithm = false);
241 
251  std::string base_file_name,
252  std::string output_log_path = "");
253 
257  ~GreedySampler();
258 
265  std::shared_ptr<Vector>
267 
277 
287 
293  void
294  setPointRelativeError(double error);
295 
303  void
304  setPointErrorIndicator(double error, int vec_size);
305 
315  int
317 
325  std::shared_ptr<Vector>
326  getNearestROM(Vector point);
327 
333  std::vector<Vector>
335 
341  std::vector<Vector>
343 
350  virtual void
351  save(std::string base_file_name);
352 
359  bool
360  isComplete();
361 
362 protected:
363 
371  void addDatabaseFromFile(
372  std::string const& warm_start_file_name);
373 
380  virtual void load(
381  std::string base_file_name);
382 
386  virtual void constructParameterPoints() = 0;
387 
392 
396  void constructObject(
397  bool check_local_rom,
398  double relative_error_tolerance,
399  double alpha,
400  double max_clamp,
401  int subset_size,
402  int convergence_subset_size,
403  std::string output_log_path,
404  bool use_centroid,
405  int random_seed,
406  bool debug_algorithm);
407 
412 
420 
429 
437  std::vector<Vector> generateRandomPoints(int num_points);
438 
442  void agnosticPrint(std::string str);
443 
452  void printErrorIndicator(Vector errorIndicatorPoint, double proc_errors);
453 
458 
465  void printSamplingType(std::string sampling_type);
466 
471 
478  void setSubsetErrorIndicator(double proc_errors);
479 
486  void setConvergenceErrorIndicator(double proc_errors);
487 
492 
496  void startConvergence();
497 
502 
513  int
514  getNearestROMIndexToParameterPoint(int index, bool ignore_self);
515 
519  std::vector<Vector> d_parameter_points;
520 
524  std::vector<Vector> d_convergence_points;
525 
530 
535 
540 
545 
549  std::vector<double> d_parameter_point_errors;
550 
554  std::vector<int> d_parameter_point_local_rom;
555 
559  std::string d_output_log_path;
560 
564  double d_max_error;
565 
570 
574  double d_alpha;
575 
579  double d_max_clamp;
580 
585 
590 
595 
600 
605 
610 
615 
621 
627 
633 
638 
644 
650 
656 
662 
668 
674 
679 
683  int d_rank;
684 
689 
693  std::default_random_engine rng;
694 };
695 
706  Vector* localROM);
707 
718  std::shared_ptr<Vector>& localROM);
719 
728 Vector getNearestPoint(std::vector<Vector> paramPoints, Vector point);
729 
738 double getNearestPoint(std::vector<double> paramPoints, double point);
739 
748 int getNearestPointIndex(std::vector<Vector> paramPoints, Vector point);
749 
758 int getNearestPointIndex(std::vector<double> paramPoints, double point);
759 }
760 
761 #endif
void printConvergenceAchieved()
Print that convergence was achieved.
virtual void constructParameterPoints()=0
Construct the list of parameter point candidates to sample.
void setPointRelativeError(double error)
Set the relative error of the specified point.
GreedySampler(std::vector< Vector > parameter_points, bool check_local_rom, double relative_error_tolerance, double alpha, double max_clamp, int subset_size, int convergence_subset_size, std::string output_log_path="", std::string warm_start_file_name="", bool use_centroid=true, int random_seed=1, bool debug_algorithm=false)
Constructor.
void generateConvergenceSubset()
Generate a new set of convergence points.
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.
bool d_subset_created
Whether the database has already created a random subset for this iteration.
int d_next_point_requiring_error_indicator
The next parameter point requiring a error indicator.
void printSamplingType(std::string sampling_type)
Print the sampling type.
void setSubsetErrorIndicator(double proc_errors)
Set the error indicator for a subset point.
int d_subset_counter
An internal subset counter.
double d_max_clamp
The max_clamp constant.
bool d_use_centroid
Whether the use the centroid heuristic for obtaining the first parameter point.
bool d_point_requiring_error_indicator_computed
Whether the database has already computed a new paramter point requiring a error indicator.
void setConvergenceErrorIndicator(double proc_errors)
Set the error indicator for a convergence point.
bool isComplete()
Check if the greedy algorithm procedure is complete.
int d_random_seed
Then random seed used to generate subsets.
int d_subset_size
The size of the subset of parameter points used per iteration.
bool d_debug_algorithm
Turn off randomness for debugging purposes.
std::vector< Vector > getParameterPointDomain()
Get the domain of the parameter points.
double d_relative_error_tol
The relative error tolerance used to terminate the greedy procedure.
std::string d_output_log_path
Output log path.
void constructObject(bool check_local_rom, double relative_error_tolerance, double alpha, double max_clamp, int subset_size, int convergence_subset_size, std::string output_log_path, bool use_centroid, int random_seed, bool debug_algorithm)
Construct the GreedySampler object.
bool d_check_local_rom
Whether the check the last sampled local ROM's error indicator after each iteration.
std::vector< int > d_parameter_point_random_indices
The parameter point indices (used to generate the random subsets).
virtual void getNextParameterPointAfterConvergenceFailure()=0
Get the next parameter point to sample after a convergence failure.
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.
double d_curr_relative_error
The current relative error of the current iteration.
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.
bool d_next_parameter_point_computed
Whether the database has already computed a new parameter point to sample.
~GreedySampler()
Destructor.
void setPointErrorIndicator(double error, int vec_size)
Set the error indicator error of the specified parameter point.
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.
void agnosticPrint(std::string str)
Print to output_log_file or cout.
int d_num_parameter_points
The maximum number of parameter points to sample.
std::shared_ptr< Vector > getNearestROM(Vector point)
Returns the nearest local ROM to the specified parameter point.
double d_error_indicator_tol
The error indicator tolerance used to terminate the greedy procedure.
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.
int d_convergence_subset_size
The size of the subset of parameter points used to check convergence.
bool d_convergence_started
Whether the database is in the convergence verifying phase.
void printErrorIndicatorToleranceNotMet()
Print that the error indicator was not met.
double d_alpha
The alpha constant.
std::vector< Vector > getSampledParameterPoints()
Get the sampled parameter points.
Vector d_max_param_point
The maximum value of the parameter space.
std::set< int > d_parameter_sampled_indices
The parameter points that were already sampled.
void startConvergence()
Switch to convergence mode.
struct GreedyErrorIndicatorPoint getNextSubsetPointRequiringErrorIndicator()
Get the next subset point requiring an error indicator.
struct GreedyErrorIndicatorPoint getNextPointRequiringRelativeError()
Returns the next parameter point for which a relative error is required.
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.
std::shared_ptr< Vector > getNextParameterPoint()
Returns the next parameter point for which sampling is required.
void printErrorIndicator(Vector errorIndicatorPoint, double proc_errors)
Print the error indicator.
struct GreedyErrorIndicatorPoint getNextPointRequiringErrorIndicator()
Returns the next parameter point for which an error indicator is required.
bool d_iteration_started
Whether the database has already computed a new parameter point for the current iteration.
struct GreedyErrorIndicatorPoint getNextConvergencePointRequiringErrorIndicator()
Get the next convergence point requiring an error indicator.
void checkParameterPointInput()
Construct the list of parameter point candidates to sample.
std::vector< Vector > generateRandomPoints(int num_points)
Generate a vector of random points.
int getNearestNonSampledPoint(Vector point)
Returns the index of the nearest non-sampled parameter point to the given point.
Vector getNearestPoint(std::vector< Vector > param_points, Vector point)
Given a vector, find the nearest point in a domain.
struct GreedyErrorIndicatorPoint createGreedyErrorIndicatorPoint(Vector *point, Vector *localROM)
Create a greedy error indicator point.
int getNearestPointIndex(std::vector< Vector > param_points, Vector point)
Given a vector, find the nearest point in a domain.
std::shared_ptr< Vector > localROM
The parameter point of the closest local ROM.
Definition: GreedySampler.h:65
std::shared_ptr< Vector > point
The parameter point to calculate the error indicator.
Definition: GreedySampler.h:60