numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
adc_sample_dict.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <string_view>
24
25#include <hash_tables/maps/open_address_map_st.h>
26
33
34namespace num_collect::opt::impl {
35
43template <concepts::objective_function ObjectiveFunction, index_type MaxDigits>
45
53template <concepts::multi_variate_objective_function ObjectiveFunction,
54 index_type MaxDigits>
55class adc_sample_dict<ObjectiveFunction, MaxDigits> {
56public:
58 using objective_function_type = ObjectiveFunction;
59
61 using variable_type = typename objective_function_type::variable_type;
62
64 using value_type = typename objective_function_type::value_type;
65
68
76 : obj_fun_(obj_fun) {}
77
84 obj_fun_ = obj_fun;
85 }
86
93 void init(const variable_type& lower, const variable_type& upper) {
94 NUM_COLLECT_PRECONDITION(lower.size() == upper.size(),
95 "Element-wise limits must have the same size.");
96 NUM_COLLECT_PRECONDITION((lower.array() < upper.array()).all(),
97 "Element-wise limits must satisfy lower < upper for each element.");
98
99 lower_ = lower;
100 width_ = upper - lower;
101 dim_ = lower.size();
102 value_dict_.clear();
103 }
104
110 void reserve(index_type size) {
111 value_dict_.reserve(static_cast<std::size_t>(size));
112 }
113
122 [[nodiscard]] auto operator()(const ternary_vector_type& point)
123 -> value_type {
124 return value_dict_.get_or_create_with_factory(
125 point, [this, &point] { return evaluate_on(point); });
126 }
127
133 [[nodiscard]] auto dim() const -> index_type { return dim_; }
134
138 [[nodiscard]] auto opt_variable() const -> const variable_type& {
139 return opt_variable_;
140 }
141
148 [[nodiscard]] auto opt_point() const -> const ternary_vector_type& {
149 return opt_point_;
150 }
151
155 [[nodiscard]] auto opt_value() const -> const value_type& {
156 return opt_value_;
157 }
158
162 [[nodiscard]] auto evaluations() const noexcept -> index_type {
163 return static_cast<index_type>(value_dict_.size());
164 }
165
166private:
175 [[nodiscard]] auto evaluate_on(const ternary_vector_type& point)
176 -> value_type {
177 NUM_COLLECT_DEBUG_ASSERT(point.dim() == dim_);
178 const auto var = point.as_variable(lower_, width_);
179 obj_fun_.evaluate_on(var);
180
181 if (value_dict_.empty() || obj_fun_.value() < opt_value_) {
182 opt_point_ = point;
183 opt_variable_ = var;
184 opt_value_ = obj_fun_.value();
185 }
186
187 return obj_fun_.value();
188 }
189
192
195
198
201
203 hash_tables::maps::open_address_map_st<ternary_vector_type, value_type>
205
208
211
214};
215
216} // namespace num_collect::opt::impl
Definition of adc_ternary_vector class.
Definition of assertion macros.
#define NUM_COLLECT_DEBUG_ASSERT(CONDITION)
Macro to check whether a condition is satisfied in debug build only.
Definition assert.h:75
void reserve(index_type size)
Reserve memory for the dictionary.
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.
adc_sample_dict(const objective_function_type &obj_fun=objective_function_type())
Constructor.
typename objective_function_type::value_type value_type
Type of function values.
auto operator()(const ternary_vector_type &point) -> value_type
Evaluate or get function value.
ternary_vector_type opt_point_
Point in the unit hyper-cube for the current optimal variable.
ObjectiveFunction objective_function_type
Type of the objective function.
auto dim() const -> index_type
Get the number of dimension.
adc_ternary_vector< variable_type, MaxDigits > ternary_vector_type
Type of ternary vectors.
hash_tables::maps::open_address_map_st< ternary_vector_type, value_type > value_dict_
Dictionary of sampled points.
void init(const variable_type &lower, const variable_type &upper)
Initialize this object.
auto opt_value() const -> const value_type &
Get current optimal value.
auto opt_point() const -> const ternary_vector_type &
Get the point in the unit hyper-cube for the current optimal variable.
auto evaluate_on(const ternary_vector_type &point) -> value_type
Evaluate function value.
typename objective_function_type::variable_type variable_type
Type of variables.
auto opt_variable() const -> const variable_type &
Get current optimal variable.
Class of dictionaries of sampling points in num_collect::opt::adaptive_diagonal_curves.
Class of vectors of ternary floating-point numbers in num_collect::opt::adaptive_diagonal_curves.
Concept of multi-variate objective functions in optimization.
Definition of index_type type.
Definition of multi_variate_objective_function concept.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of internal implementations.
Definition adc_group.h:30
Definition of objective_function concept.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.