This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "src/Optimization/fibonacci_search.hpp"
引数の型が整数な単峰関数を対象.
引数の型が long double
の単峰関数を対象にする場合は黄金分割探索 を使う.
名前 | 概要 | 計算量 |
---|---|---|
fibonacci_search<sgn>(f,l,r) |
$\lbrack l, r\rbrack \cap\mathbb{Z}$ 上で単峰となる関数 $f(x)$ の最適値とその最適解を返す. 返り値は { 最適解 $x^\ast$, 最適値 $f(x^\ast)$ } templateの引数で最大最小を指定. |
$n=r-l$ とおき, $f(x)$ の評価が$O(A)$ かかるとしたとき $O(A\log n)$ |
https://qiita.com/tanaka-a/items/f380257328da421c6584
#pragma once
#include <algorithm>
#include <cassert>
#include "src/Internal/function_traits.hpp"
#include "src/Optimization/MinMaxEnum.hpp"
// [l,r]
template <MinMaxEnum obj, class F> std::pair<int64_t, result_type_t<F>> fibonacci_search(const F &f, int64_t l, int64_t r) {
assert(l <= r);
int64_t s= 1, t= 2, a= l - 1, x, b, y;
for (int64_t e= r - l + 2; t < e;) std::swap(s+= t, t);
b= a + t, x= b - s;
result_type_t<F> fx= f(x), fy;
for (bool g; a + b != 2 * x;) {
if (y= a + b - x; r < y) b= a, a= y;
else {
if constexpr (obj == MINIMIZE) g= fx < (fy= f(y));
else g= fx > (fy= f(y));
if (g) b= a, a= y;
else a= x, x= y, fx= fy;
}
}
return {x, fx};
}
#line 2 "src/Optimization/fibonacci_search.hpp"
#include <algorithm>
#include <cassert>
#line 2 "src/Internal/function_traits.hpp"
#include <type_traits>
// clang-format off
namespace function_template_internal{
template<class C>struct is_function_object{
template<class U,int dummy=(&U::operator(),0)> static std::true_type check(U *);
static std::false_type check(...);
static C *m;
static constexpr bool value= decltype(check(m))::value;
};
template<class F,bool,bool>struct function_type_impl{using type= void;};
template<class F>struct function_type_impl<F,true,false>{using type= F *;};
template<class F>struct function_type_impl<F,false,true>{using type= decltype(&F::operator());};
template<class F> using function_type_t= typename function_type_impl<F,std::is_function_v<F>,is_function_object<F>::value>::type;
template<class... Args>struct result_type_impl{using type= void;};
template<class R,class... Args>struct result_type_impl<R(*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)const>{using type= R;};
template<class F> using result_type_t= typename result_type_impl<function_type_t<F>>::type;
template<class... Args>struct argument_type_impl{using type= void;};
template<class R,class... Args>struct argument_type_impl<R(*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)const>{using type= std::tuple<Args...>;};
template<class F> using argument_type_t= typename argument_type_impl<function_type_t<F>>::type;
}
using function_template_internal::result_type_t,function_template_internal::argument_type_t;
// clang-format on
#line 2 "src/Optimization/MinMaxEnum.hpp"
enum MinMaxEnum { MAXIMIZE= -1, MINIMIZE= 1 };
#line 6 "src/Optimization/fibonacci_search.hpp"
// [l,r]
template <MinMaxEnum obj, class F> std::pair<int64_t, result_type_t<F>> fibonacci_search(const F &f, int64_t l, int64_t r) {
assert(l <= r);
int64_t s= 1, t= 2, a= l - 1, x, b, y;
for (int64_t e= r - l + 2; t < e;) std::swap(s+= t, t);
b= a + t, x= b - s;
result_type_t<F> fx= f(x), fy;
for (bool g; a + b != 2 * x;) {
if (y= a + b - x; r < y) b= a, a= y;
else {
if constexpr (obj == MINIMIZE) g= fx < (fy= f(y));
else g= fx > (fy= f(y));
if (g) b= a, a= y;
else a= x, x= y, fx= fy;
}
}
return {x, fx};
}