1 #ifndef INMOST_INMOST_OPTIMIZER_H
2 #define INMOST_INMOST_OPTIMIZER_H
6 #if defined(USE_OPTIMIZER)
12 #include <unordered_map>
20 enum class OptimizationParameterType {
21 PARAMETER_TYPE_DEFAULT,
22 PARAMETER_TYPE_EXPONENT
28 class OptimizationParameterPoint {
33 OptimizationParameterPoint(
const std::string &name,
double value, OptimizationParameterType type);
35 static void swap(OptimizationParameterPoint &left, OptimizationParameterPoint &right);
38 OptimizationParameterPoint(
const OptimizationParameterPoint &other);
40 OptimizationParameterPoint(OptimizationParameterPoint &&other) noexcept;
42 OptimizationParameterPoint &operator=(
const OptimizationParameterPoint &other);
44 const std::string &GetName() const noexcept;
46 double GetValue() const noexcept;
48 ~OptimizationParameterPoint();
50 static
double convert(
double value, OptimizationParameterType type);
52 friend class OptimizationParameters;
59 typedef std::vector<OptimizationParameterPoint> OptimizationParameterPoints;
63 typedef std::pair<
double,
double> OptimizationParameterRange;
69 class OptimizationParameter {
72 std::vector<double> values;
74 OptimizationParameterType type;
76 static void swap(OptimizationParameter &left, OptimizationParameter &right);
85 OptimizationParameter(
const std::string &name,
const OptimizationParameterRange &range,
double step,
86 double default_value, OptimizationParameterType type = OptimizationParameterType::PARAMETER_TYPE_DEFAULT);
93 OptimizationParameter(
const std::string &name,
const std::vector<double> &values,
94 double default_value, OptimizationParameterType type = OptimizationParameterType::PARAMETER_TYPE_DEFAULT);
98 OptimizationParameter(
const OptimizationParameter &other);
102 OptimizationParameter(OptimizationParameter &&other) noexcept;
106 OptimizationParameter &operator=(
const OptimizationParameter &other);
109 const std::string &GetName() const noexcept;
112 const std::vector<
double> &GetValues() const noexcept;
115 double GetMinimalValue() const noexcept;
118 double GetMaximumValue() const noexcept;
120 double GetMinStrictBound(
double v,
double modifier = 1.0) const noexcept;
122 double GetMaxStrictBound(
double v,
double modifier = 1.0) const noexcept;
125 double GetClosestTo(
double to) const noexcept;
128 std::
size_t GetClosestIndexTo(
double to) const noexcept;
131 std::
size_t GetValuesCount() const noexcept;
134 double GetDefaultValue() const noexcept;
137 OptimizationParameterType GetType() const noexcept;
139 double ExtractValueFromPoint(const OptimizationParameterPoint &point) const noexcept;
144 typedef std::pair<OptimizationParameter,
double> OptimizationParametersEntry;
149 typedef std::vector<OptimizationParametersEntry> OptimizationParameterEntries;
151 class SuggestionChangedParameter {
156 static void swap(SuggestionChangedParameter &left, SuggestionChangedParameter &right);
159 SuggestionChangedParameter(std::size_t index,
double value);
161 SuggestionChangedParameter(
const SuggestionChangedParameter &other);
163 SuggestionChangedParameter(SuggestionChangedParameter &&other);
165 SuggestionChangedParameter &operator=(
const SuggestionChangedParameter &other);
167 std::size_t GetIndex() const noexcept;
169 double GetValue() const noexcept;
171 ~SuggestionChangedParameter();
174 typedef std::vector<SuggestionChangedParameter> SuggestionChangedParameters;
181 class OptimizationParametersSuggestion {
183 SuggestionChangedParameters changed;
184 OptimizationParameterPoints before;
185 double metrics_before;
186 OptimizationParameterPoints after;
188 OptimizationParametersSuggestion(
const SuggestionChangedParameters &changed,
189 const OptimizationParameterPoints &before,
double metrics_before,
190 const OptimizationParameterPoints &after);
192 const SuggestionChangedParameters &GetChangedParameters() const noexcept;
194 const OptimizationParameterPoints &GetPointsBefore() const noexcept;
196 double GetMetricsBefore() const noexcept;
198 const OptimizationParameterPoints &GetPointsAfter() const noexcept;
203 class OptimizationParameters {
205 OptimizationParameterEntries entries;
208 static void swap(OptimizationParameters &left, OptimizationParameters &right);
214 OptimizationParameters(
const OptimizationParameterEntries &entries,
double metrics);
218 OptimizationParameters(
const OptimizationParameters &other);
222 OptimizationParameters(OptimizationParameters &&other) noexcept;
226 OptimizationParameters &operator=(
const OptimizationParameters &other);
229 const OptimizationParameterEntries &GetParameterEntries() const noexcept;
232 const OptimizationParametersEntry &GetParameterEntry(std::
size_t index) const;
235 const OptimizationParameter &GetParameter(std::
size_t index) const;
238 std::
size_t Size() const;
241 const OptimizationParameterPoints GetPoints() const noexcept;
244 double GetMetrics() const noexcept;
247 const OptimizationParameterPoints GetPointsWithChangedParameter(const OptimizationParameter ¶meter,
double value) const noexcept;
250 const OptimizationParameterPoints GetPointsWithChangedParameters(const SuggestionChangedParameters &changed) const noexcept;
252 void Update(const SuggestionChangedParameters &changed,
double metrics);
254 void Update(std::
size_t index,
double value,
double metrics);
258 class OptimizationParameterResult {
260 SuggestionChangedParameters changed;
261 OptimizationParameterPoints before;
262 double metrics_before;
263 OptimizationParameterPoints after;
264 double metrics_after;
267 static void swap(OptimizationParameterResult &left, OptimizationParameterResult &right);
271 OptimizationParameterResult(
const SuggestionChangedParameters &changed,
272 const OptimizationParameterPoints &before,
const OptimizationParameterPoints &after,
273 double metrics_before,
double metrics_after,
bool is_good);
277 OptimizationParameterResult(
const OptimizationParameterResult &other);
281 OptimizationParameterResult(OptimizationParameterResult &&other) noexcept;
285 OptimizationParameterResult &operator=(
const OptimizationParameterResult &other);
288 const OptimizationParameterPoints &GetPointsBefore() const noexcept;
291 const OptimizationParameterPoints &GetPointsAfter() const noexcept;
294 double GetMetricsBefore() const noexcept;
297 double GetMetricsAfter() const noexcept;
300 bool IsGood() const noexcept;
303 const SuggestionChangedParameters& GetChangedParameters() const noexcept;
308 class OptimizationParameterResultsBuffer {
310 std::size_t capacity;
311 std::deque<OptimizationParameterResult> buffer;
313 static void swap(OptimizationParameterResultsBuffer &left, OptimizationParameterResultsBuffer &right);
318 OptimizationParameterResultsBuffer(std::size_t capacity);
322 OptimizationParameterResultsBuffer(
const OptimizationParameterResultsBuffer &other);
326 OptimizationParameterResultsBuffer(OptimizationParameterResultsBuffer &&other) noexcept;
330 OptimizationParameterResultsBuffer &operator=(
const OptimizationParameterResultsBuffer &other);
333 std::deque<OptimizationParameterResult>::const_reverse_iterator cbegin() const noexcept;
335 const OptimizationParameterResult &at(std::
size_t index) const;
338 std::deque<OptimizationParameterResult>::const_reverse_iterator cend() const noexcept;
343 void push(const OptimizationParameterResult &result);
346 std::
size_t size() const noexcept;
349 bool IsEmpty() const noexcept;
352 bool IsLastResultSuccessful() const noexcept;
354 bool IsSuccessfulResultExist() const noexcept;
357 const OptimizationParameterResult &GetLastSuccessfulResult() const;
360 const std::vector<OptimizationParameterResult> GetLastUniqueEntries(std::
size_t max) const;
363 typedef std::map<std::
string, std::
string> OptimizerProperties;
365 enum class OptimizerVerbosityLevel {
372 enum class OptimizerRestartStrategy {
373 RESTART_STRATEGY_NO_RESTART,
374 RESTART_STRATEGY_WITH_BEST
377 typedef std::pair<bool, double> OptimizationFunctionInvokeResult;
379 class OptimizerInterface {
384 OptimizerVerbosityLevel verbosity = OptimizerVerbosityLevel::Level0;
386 OptimizerRestartStrategy restart_strategy = OptimizerRestartStrategy::RESTART_STRATEGY_NO_RESTART;
387 std::size_t max_fails = std::numeric_limits<std::size_t>::max();
388 std::size_t fails_count = 0;
390 std::deque<OptimizationParametersSuggestion> suggestions;
392 void RestartWithBestStrategy();
395 OptimizationParameterResultsBuffer results;
396 OptimizationParameters parameters;
397 const OptimizerProperties properties;
399 virtual bool UpdateSpaceWithLatestResults();
401 virtual SuggestionChangedParameters AlgorithmMakeSuggestion()
const = 0;
404 OptimizerInterface(
const std::string &name,
const OptimizationParameters ¶meters,
const OptimizerProperties &properties, std::size_t buffer_capacity) :
405 name(name), parameters(parameters), properties(properties), results(buffer_capacity), mpi_rank(0) {
407 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
411 OptimizationParametersSuggestion Suggest();
413 void SaveResult(
const SuggestionChangedParameters &changed,
414 const OptimizationParameterPoints &before,
double metrics_before,
415 const OptimizationParameterPoints &after,
double metrics_after,
bool is_good);
417 void SaveResult(
const SuggestionChangedParameters &changed,
const OptimizationParameterPoints &after,
double metrics_after,
bool is_good);
419 void SaveResult(
const OptimizationParametersSuggestion &suggestion,
double metrics,
bool is_good);
421 const OptimizationParameterResultsBuffer &GetResults() const noexcept;
423 const OptimizationParameterPoints GetPoints() const noexcept;
425 void SetVerbosityLevel(OptimizerVerbosityLevel level) noexcept;
427 OptimizerVerbosityLevel GetVerbosityLevel() const noexcept;
429 bool HasProperty(const std::
string &name) const noexcept;
431 const std::
string &GetProperty(const std::
string &name) const;
433 const OptimizationParametersSuggestion &GetLastSuggestion() const;
435 void SetRestartStrategy(OptimizerRestartStrategy strategy, std::
size_t max_fails) noexcept;
437 const std::
string &GetName() const noexcept;
439 virtual ~OptimizerInterface() {};
442 typedef std::unordered_map<std::string, OptimizerInterface *> SavedOptimizersMap;
446 static SavedOptimizersMap optimizers;
448 Optimizers() =
delete;
450 static bool IsOptimizerAvailable(
const std::string &type);
452 static std::vector<std::string> GetAvailableOptimizers();
454 static OptimizerInterface *GetOptimizer(
const std::string &name,
const std::string &type,
455 const OptimizationParameters ¶meters,
const OptimizerProperties &properties, std::size_t buffer_capacity);
457 static void SaveOptimizerOrReplace(
const std::string &name,
const std::string &type,
458 const OptimizationParameters ¶meters,
const OptimizerProperties &properties, std::size_t buffer_capacity);
460 static void SaveOptimizerOrReplace(
const std::string &name,
const std::string &type,
const OptimizerProperties &properties, std::size_t buffer_capacity);
462 static OptimizerInterface *GetSavedOptimizer(
const std::string &name);
465 class OptimizersConfiguration {
467 enum class OptimizerConfigurationReaderState {
468 READ_GLOBAL_PARAMETER
471 static std::vector<OptimizationParameter> global_parameters;
473 OptimizersConfiguration() =
delete;
475 static OptimizationParameter ParseOptimizationParameter(
const std::string &line);
477 static void FromFile(
const std::string &path);
479 static void FromStream(std::istream &stream);
481 static OptimizationParameters GetGlobalOptimizationParameters();