numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
any_objective_function.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 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 <functional>
23#include <type_traits>
24#include <utility>
25
27
28namespace num_collect::opt {
29
35template <typename Signature>
37
44template <typename Value, typename Variable>
45class any_objective_function<Value(Variable)> {
46public:
48 using variable_type = std::decay_t<Variable>;
49
51 using value_type = std::decay_t<Value>;
52
59
65 template <base::concepts::invocable_as<Value(Variable)> Function>
66 any_objective_function( // NOLINT(*-explicit-constructor,*-explicit-conversions)
67 Function&& function)
68 : function_(std::forward<Function>(function)) {}
69
76 template <base::concepts::invocable_as<Value(Variable)> Function>
77 auto operator=( // NOLINT(*-explicit-constructor,*-explicit-conversions)
78 Function&& function) -> any_objective_function& {
79 function_ = std::forward<Function>(function);
80 return *this;
81 }
82
88 void evaluate_on(const variable_type& variable) {
89 value_ = function_(variable);
90 }
91
97 [[nodiscard]] auto value() const noexcept -> const value_type& {
98 return value_;
99 }
100
101private:
103 std::function<Value(Variable)> function_{};
104
106 value_type value_{};
107};
108
109} // namespace num_collect::opt
auto value() const noexcept -> const value_type &
Get function value.
std::decay_t< Variable > variable_type
Type of variables.
void evaluate_on(const variable_type &variable)
Evaluate function value on a variable.
auto operator=(Function &&function) -> any_objective_function &
Assign a function object.
std::decay_t< Value > value_type
Type of function values.
Class to store any type of objects of objective functions.
Definition of invocable_as concept.
Namespace of optimization algorithms.
STL namespace.