Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: 黄金分割探索 (src/Optimization/golden_search.hpp)

引数の型が long double の単峰関数が対象.
引数の型が整数な単峰関数を対象にする場合はフィボナッチ探索 を使う.

関数

名前 概要 計算量
golden_search<sgn>(f,l,r,iter=100) $\lbrack l, r\rbrack$ 上で単峰となる関数 $f(x)$ の最適値とその最適解を返す.
返り値は { 最適解 $x^\ast$, 最適値 $f(x^\ast)$ }
templateの引数で最大最小を指定.
第四引数で反復回数を指定. デフォルトは100
$n=r-l$ とおき, $f(x)$ の評価が$O(A)$ かかるとしたとき
$O(A\log n)$

Depends on

Verified with

Code

#pragma once
#include <cmath>
#include <cassert>
#include "src/Internal/function_traits.hpp"
#include "src/Optimization/MinMaxEnum.hpp"
// [l,r]
template <MinMaxEnum obj, class F> std::pair<long double, result_type_t<F>> golden_search(const F &f, long double l, long double r, int iter= 100) {
 static constexpr long double c= 0.38196601125;
 assert(l <= r);
 long double x= l + (r - l) * c, y= r - (r - l) * c;
 result_type_t<F> fx= f(x), fy= f(y);
 for (bool g; iter--;) {
  if constexpr (obj == MINIMIZE) g= fx < fy;
  else g= fx > fy;
  if (g) r= y, y= x, fy= fx, fx= f(x= l + (r - l) * c);
  else l= x, x= y, fx= fy, fy= f(y= r - (r - l) * c);
 }
 return {x, fx};
}
#line 2 "src/Optimization/golden_search.hpp"
#include <cmath>
#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/golden_search.hpp"
// [l,r]
template <MinMaxEnum obj, class F> std::pair<long double, result_type_t<F>> golden_search(const F &f, long double l, long double r, int iter= 100) {
 static constexpr long double c= 0.38196601125;
 assert(l <= r);
 long double x= l + (r - l) * c, y= r - (r - l) * c;
 result_type_t<F> fx= f(x), fy= f(y);
 for (bool g; iter--;) {
  if constexpr (obj == MINIMIZE) g= fx < fy;
  else g= fx > fy;
  if (g) r= y, y= x, fy= fx, fx= f(x= l + (r - l) * c);
  else l= x, x= y, fx= fy, fy= f(y= r - (r - l) * c);
 }
 return {x, fx};
}
Back to top page