48template <concepts::
objective_function ObjectiveFunction>
56template <concepts::single_variate_
objective_function ObjectiveFunction>
70 using value_type =
typename objective_function_type::value_type;
112 const auto num_samples_size_t =
114 samples_.resize(num_samples_size_t);
115 values_.resize(num_samples_size_t);
117 value_type min_val = std::numeric_limits<value_type>::max();
119 for (std::size_t i = 0; i < num_samples_size_t; ++i) {
122 const variable_type variable = lower_ + (upper_ - lower_) * rate;
124 obj_fun_.evaluate_on(variable);
127 samples_[i] = variable;
130 if (value < min_val) {
138 if (ind_opt_sample_ > 0) {
139 lower_ = samples_[ind_opt_sample_ - 1];
141 if (ind_opt_sample_ < num_samples_size_t - 1) {
142 upper_ = samples_[ind_opt_sample_ + 1];
152 return iterations_ >= max_iterations_;
160 iteration_logger.template append<index_type>(
161 "Iter.", &this_type::iterations);
162 iteration_logger.template append<index_type>(
163 "Eval.", &this_type::evaluations);
164 iteration_logger.template append<value_type>(
165 "Value", &this_type::opt_value);
172 return samples_[ind_opt_sample_];
179 return values_[ind_opt_sample_];
218 "Number of samples must be at least two.");
219 num_samples_ = value;
231 "Maximum number of iterations must be a positive integer.");
232 max_iterations_ = value;
247 std::vector<variable_type> samples_{};
250 std::vector<value_type> values_{};
253 std::size_t ind_opt_sample_{0};
Class to write logs of iterations.
Class of tags of logs without memory management.
Base class of implementations of optimization algorithms.
Class to perform optimization using samples of objective functions.
void iterate()
Iterate the algorithm once.
void configure_iteration_logger(logging::iterations::iteration_logger< sampling_optimizer< ObjectiveFunction > > &iteration_logger) const
Configure an iteration logger.
objective_function_type obj_fun_
Objective function.
auto upper() const -> const variable_type &
Get the current upper limit.
sampling_optimizer(const objective_function_type &obj_fun=objective_function_type())
Constructor.
auto iterations() const noexcept -> index_type
Get the number of iterations.
auto opt_value() const -> const value_type &
Get current optimal value.
auto evaluations() const noexcept -> index_type
Get the number of function evaluations.
void change_objective_function(const objective_function_type &obj_fun)
Change the objective function.
auto lower() const -> const variable_type &
Get the current lower limit.
typename objective_function_type::variable_type variable_type
Type of variables.
auto max_iterations(index_type value) -> sampling_optimizer &
Set the maximum number of iterations.
auto num_samples(index_type value) -> sampling_optimizer &
Set the number of samples.
auto is_stop_criteria_satisfied() const -> bool
Determine if stopping criteria of the algorithm are satisfied.
typename objective_function_type::value_type value_type
Type of function values.
ObjectiveFunction objective_function_type
Type of the objective function.
auto opt_variable() const -> const variable_type &
Get current optimal variable.
void init(const variable_type &lower, const variable_type &upper)
Initialize the algorithm.
Class to perform optimization using samples of objective functions.
Definition of exceptions.
Definition of index_type type.
Definition of iteration_logger class.
Definition of log_tag_view class.
Definition of macros for logging.
std::ptrdiff_t index_type
Type of indices in this library.
Namespace of optimization algorithms.
constexpr auto sampling_optimizer_tag
Tag of golden_section_search.
auto safe_cast(const From &value) -> To
Cast safely.
Definition of objective_function concept.
Definition of optimizer_base class.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
Definition of safe_cast function.
Definition of single_variate_objective_function concept.