50template <concepts::
objective_function ObjectiveFunction>
58template <concepts::multi_variate_
objective_function ObjectiveFunction>
75 using value_type =
typename objective_function_type::value_type;
81 reflection_and_expansion,
95 case process_type::none:
97 case process_type::reflection:
99 case process_type::reflection_and_expansion:
100 return "reflection and expansion";
101 case process_type::contraction:
102 return "contraction";
103 case process_type::multiple_contraction:
104 return "multiple contraction";
106 return "invalid process";
138 dim_ = init_var.size();
147 points_.push_back(var);
148 values_.push_back(evaluate_on(var));
152 points_.push_back(var);
153 values_.push_back(evaluate_on(var));
157 value_order_.clear();
159 for (std::size_t i = 0; i < util::safe_cast<std::size_t>(dim_ + 1);
161 value_order_.push_back(i);
171 const auto min_ind = value_order_.front();
172 const auto second_max_ind =
174 const auto max_ind = value_order_.back();
176 reflect(face_center);
177 if (values_[max_ind] < values_[min_ind]) {
179 }
else if (values_[max_ind] >= values_[second_max_ind]) {
180 contract(face_center);
181 if (values_[max_ind] >= values_[second_max_ind]) {
196 return (simplex_size() < tol_simplex_size_) ||
197 (iterations() >= max_iterations_);
206 iteration_logger.template append<index_type>(
207 "Iter.", &this_type::iterations);
208 iteration_logger.template append<index_type>(
209 "Eval.", &this_type::evaluations);
210 iteration_logger.template append<value_type>(
211 "Value", &this_type::opt_value);
212 iteration_logger.template append<variable_scalar_type>(
213 "SimplexSize", &this_type::simplex_size);
216 .template append<std::string_view>(
217 "Process", &this_type::last_process_name)
218 ->width(process_width);
225 return points_[value_order_.front()];
232 return values_[value_order_.front()];
264 return process_name(last_process());
273 return (points_[value_order_.front()] - points_[value_order_.back()])
278 static inline const auto default_width =
289 tol_simplex_size_ = value;
301 obj_fun_.evaluate_on(variable);
303 return obj_fun_.value();
310 std::sort(std::begin(value_order_), std::end(value_order_),
311 [
this](std::size_t i, std::size_t j) {
312 return values_[i] < values_[j];
324 for (std::size_t i = 0; i < util::safe_cast<std::size_t>(dim_); ++i) {
325 face_center += points_[value_order_[i]];
344 const std::size_t ind_move = value_order_.back();
345 points_[ind_move] = twice * face_center - points_[ind_move];
346 values_[ind_move] = evaluate_on(points_[ind_move]);
347 process_ = process_type::reflection;
357 const std::size_t ind_move = value_order_.back();
358 points_[ind_move] = twice * points_[ind_move] - face_center;
359 values_[ind_move] = evaluate_on(points_[ind_move]);
360 process_ = process_type::reflection_and_expansion;
370 const std::size_t ind_move = value_order_.back();
371 points_[ind_move] = half * (points_[ind_move] + face_center);
372 values_[ind_move] = evaluate_on(points_[ind_move]);
373 process_ = process_type::contraction;
381 const auto& min_point = points_[value_order_.front()];
382 for (std::size_t i = 1; i <= util::safe_cast<std::size_t>(dim_); ++i) {
383 const std::size_t ind_move = value_order_[i];
384 points_[ind_move] = half * (points_[ind_move] + min_point);
385 values_[ind_move] = evaluate_on(points_[ind_move]);
387 process_ = process_type::multiple_contraction;
397 std::vector<variable_type, Eigen::aligned_allocator<variable_type>>
401 std::vector<value_type> values_{};
404 std::vector<std::size_t> value_order_{};
416 static inline const auto default_tol_simplex_size =
Class to write logs of iterations.
Class of tags of logs without memory management.
Class of downhill simplex method press2007.
typename variable_type::Scalar variable_scalar_type
Type of scalars in variables.
void contract(const variable_type &face_center)
Contract the highest point to the opposite face.
void expand(const variable_type &face_center)
Expand the simplex.
process_type
Type of processes.
auto opt_variable() const -> const variable_type &
Get current optimal variable.
auto evaluate_on(const variable_type &variable)
Evaluate function value.
void reorder()
Reorder function values.
static auto process_name(process_type process) -> std::string_view
Convert type of process to string.
auto is_stop_criteria_satisfied() const -> bool
Determine if stopping criteria of the algorithm are satisfied.
auto last_process_name() const noexcept -> std::string_view
Get the name of the last process.
typename objective_function_type::value_type value_type
Type of function values.
ObjectiveFunction objective_function_type
Type of the objective function.
auto calc_face_center() const -> variable_type
Calculate center of the face composed from points other than highest point.
auto iterations() const noexcept -> index_type
Get the number of iterations.
auto simplex_size() const -> variable_scalar_type
Get the size of simplex.
downhill_simplex(const objective_function_type &obj_fun=objective_function_type())
Constructor.
objective_function_type obj_fun_
Objective function.
auto tol_simplex_size(const variable_scalar_type &value) -> downhill_simplex &
Set tolerance of size of simplex.
typename objective_function_type::variable_type variable_type
Type of variables.
void iterate()
Iterate the algorithm once.
void reflect(const variable_type &face_center)
Reflect the highest point.
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.
void multi_contract()
Contract all points other than the lowest point toward the lowest point.
auto opt_value() const -> const value_type &
Get current optimal value.
void configure_iteration_logger(logging::iterations::iteration_logger< this_type > &iteration_logger) const
Configure an iteration logger.
auto last_process() const noexcept -> process_type
Get last process.
void init(const variable_type &init_var, const variable_scalar_type &width=default_width)
Initialize the algorithm.
Class of downhill simplex method.
Base class of implementations of optimization algorithms.
Definition of index_type type.
Definition of iteration_logger class.
Definition of log_tag_view class.
Definition of multi_variate_objective_function concept.
std::ptrdiff_t index_type
Type of indices in this library.
Namespace of optimization algorithms.
constexpr auto downhill_simplex_tag
Tag of downhill_simplex.
auto safe_cast(const From &value) -> To
Cast safely.
Definition of objective_function concept.
Definition of optimizer_base class.
Definition of safe_cast function.