Tifa's CP Library

:heavy_check_mark: heuristic_lbsa (src/code/opt/heuristic_lbsa.hpp)

Depends on

Verified with

Code

#ifndef TIFALIBS_OPT_HEURISTIC_LBSA
#define TIFALIBS_OPT_HEURISTIC_LBSA

#include "../rand/gen.hpp"

namespace tifa_libs::opt {

template <class Cont, class Ff, class Ft = f64, class Tt = f64>
requires requires(Ff f, Cont const& t) {
  { f(t) } -> std::same_as<Ft>;
}
class heuristic_lbsa {
  Ff f;
  rand::Gen<std::uniform_int_distribution<u32>> g_idx;
  rand::Gen<std::uniform_real_distribution<Tt>> g;
  Cont x;
  Ft fx;
  pq<Tt> tlist;

 public:
  // Find minimum argument of f(x)
  explicit constexpr heuristic_lbsa(Ff f, Cont const& init, u32 L, Tt p0 = .2) : f(f), g_idx(0, (u32)init.size() - 1), g(0, 1), x(init), fx(f(init)), tlist() {
    const Tt lp0 = std::log(p0);
    while (tlist.size() < L) {
      auto [fy, y] = gen();
      if (fy < fx) std::swap(fx, fy), std::swap(x, y);
      else tlist.push((fx - fy) / lp0);
    }
  }

  std::pair<Ft, Cont> operator()(u32 K, u32 M) {
    for (u32 k = 0; k < K; ++k) {
      Tt tmax = tlist.top(), t = 0;
      u32 c = 0;
      for (u32 m = 0; m < M; ++m) {
        auto [fy, y] = gen();
        if (fy < fx) std::swap(fx, fy), std::swap(x, y);
        else if (Tt r = g.next(); std::exp((fx - fy) / tmax) > r) t += (fx - fy) / std::log(r), ++c;
      }
      if (c) tlist.pop(), tlist.push(t / c);
    }
    return {fx, x};
  }

 private:
  static constexpr void inv_(typename Cont::iterator l, typename Cont::iterator r) { std::reverse(l, std::next(r)); }
  static constexpr void ins_(typename Cont::iterator l, typename Cont::iterator r) {
    auto x = *r;
    std::move_backward(l, r, std::next(r));
    *l = x;
  }
  static constexpr void swap_(typename Cont::iterator l, typename Cont::iterator r) { std::iter_swap(l, r); }
  std::pair<Ft, Cont> gen() {
    u32 l = g_idx.next(), r = g_idx.next();
    while (l == r) r = g_idx.next();
    if (l > r) std::swap(l, r);
    Cont c0 = x, c1 = x, c2 = x;
    inv_(c0.begin() + l, c0.begin() + r);
    ins_(c1.begin() + l, c1.begin() + r);
    swap_(c2.begin() + l, c2.begin() + r);
    Ft f0 = f(c0), f1 = f(c1), f2 = f(c2);
    if (f0 > f1) std::swap(f0, f1), std::swap(c0, c1);
    if (f0 > f2) std::swap(f0, f2), std::swap(c0, c2);
    return {f0, c0};
  }
};

}  // namespace tifa_libs::opt

#endif
#line 1 "src/code/opt/heuristic_lbsa.hpp"



#line 1 "src/code/rand/gen.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/rand/gen.hpp"

namespace tifa_libs::rand {

template <class Distri>
class Gen {
  std::conditional_t<sizeof(typename Distri::result_type) <= 4, std::mt19937, std::mt19937_64> re;
  Distri dist;

 public:
  using random_engine = decltype(re);
  using distribution = Distri;
  using result_type = typename Distri::result_type;

  constexpr Gen() : re(std::random_device{}()), dist() {}
  constexpr Gen(result_type a, result_type b) : re(std::random_device{}()), dist(a, b) {}

  constexpr void set_range(result_type a, result_type b) { dist = Distri(a, b); }
  constexpr random_engine& rand_eng() { return re; }
  constexpr Distri& distrib() { return dist; }

  void reset_seed() { re.seed((result_type)std::chrono::duration_cast<std::conditional_t<sizeof(typename Distri::result_type) <= 4, std::chrono::seconds, std::chrono::nanoseconds>>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); }
  constexpr result_type operator()() { return dist(re); }
  result_type next() { return dist(re); }
};

}  // namespace tifa_libs::rand


#line 5 "src/code/opt/heuristic_lbsa.hpp"

namespace tifa_libs::opt {

template <class Cont, class Ff, class Ft = f64, class Tt = f64>
requires requires(Ff f, Cont const& t) {
  { f(t) } -> std::same_as<Ft>;
}
class heuristic_lbsa {
  Ff f;
  rand::Gen<std::uniform_int_distribution<u32>> g_idx;
  rand::Gen<std::uniform_real_distribution<Tt>> g;
  Cont x;
  Ft fx;
  pq<Tt> tlist;

 public:
  // Find minimum argument of f(x)
  explicit constexpr heuristic_lbsa(Ff f, Cont const& init, u32 L, Tt p0 = .2) : f(f), g_idx(0, (u32)init.size() - 1), g(0, 1), x(init), fx(f(init)), tlist() {
    const Tt lp0 = std::log(p0);
    while (tlist.size() < L) {
      auto [fy, y] = gen();
      if (fy < fx) std::swap(fx, fy), std::swap(x, y);
      else tlist.push((fx - fy) / lp0);
    }
  }

  std::pair<Ft, Cont> operator()(u32 K, u32 M) {
    for (u32 k = 0; k < K; ++k) {
      Tt tmax = tlist.top(), t = 0;
      u32 c = 0;
      for (u32 m = 0; m < M; ++m) {
        auto [fy, y] = gen();
        if (fy < fx) std::swap(fx, fy), std::swap(x, y);
        else if (Tt r = g.next(); std::exp((fx - fy) / tmax) > r) t += (fx - fy) / std::log(r), ++c;
      }
      if (c) tlist.pop(), tlist.push(t / c);
    }
    return {fx, x};
  }

 private:
  static constexpr void inv_(typename Cont::iterator l, typename Cont::iterator r) { std::reverse(l, std::next(r)); }
  static constexpr void ins_(typename Cont::iterator l, typename Cont::iterator r) {
    auto x = *r;
    std::move_backward(l, r, std::next(r));
    *l = x;
  }
  static constexpr void swap_(typename Cont::iterator l, typename Cont::iterator r) { std::iter_swap(l, r); }
  std::pair<Ft, Cont> gen() {
    u32 l = g_idx.next(), r = g_idx.next();
    while (l == r) r = g_idx.next();
    if (l > r) std::swap(l, r);
    Cont c0 = x, c1 = x, c2 = x;
    inv_(c0.begin() + l, c0.begin() + r);
    ins_(c1.begin() + l, c1.begin() + r);
    swap_(c2.begin() + l, c2.begin() + r);
    Ft f0 = f(c0), f1 = f(c1), f2 = f(c2);
    if (f0 > f1) std::swap(f0, f1), std::swap(c0, c1);
    if (f0 > f2) std::swap(f0, f2), std::swap(c0, c2);
    return {f0, c0};
  }
};

}  // namespace tifa_libs::opt


Back to top page