16#ifndef dealii_solver_bfgs_h
17#define dealii_solver_bfgs_h
57template <
typename VectorType>
64 using Number =
typename VectorType::value_type;
111 const std::function<
Number(
const VectorType &x, VectorType &g)> &compute,
122 boost::signals2::connection
125 Number(
Number &f, VectorType &x, VectorType &g,
const VectorType &p)>
154 boost::signals2::connection
156 const std::function<
void(VectorType & g,
170 boost::signals2::signal<
171 Number(
Number &f, VectorType &x, VectorType &g,
const VectorType &p)>
177 boost::signals2::signal<void(VectorType & g,
187template <
typename VectorType>
189 const unsigned int max_history_size_,
190 const bool debug_output_)
191 : max_history_size(max_history_size_)
192 , debug_output(debug_output_)
197template <
typename VectorType>
199 const AdditionalData &data)
201 , additional_data(data)
206template <
class VectorType>
207boost::signals2::connection
210 Number(Number &f, VectorType &x, VectorType &g,
const VectorType &p)> &slot)
212 Assert(line_search_signal.empty(),
213 ExcMessage(
"One should not attach more than one line search signal."));
214 return line_search_signal.connect(slot);
219template <
class VectorType>
220boost::signals2::connection
222 const std::function<
void(VectorType & g,
226 Assert(preconditioner_signal.empty(),
228 "One should not attach more than one preconditioner signal."));
229 return preconditioner_signal.connect(slot);
234template <
typename VectorType>
237 const std::function<
typename VectorType::value_type(
const VectorType &x,
238 VectorType &f)> &compute,
248 bool first_step =
true;
252 if (line_search_signal.empty())
255 const auto default_line_min =
256 [&](Number &f, VectorType &x, VectorType &g,
const VectorType &p) {
258 const Number g0 = g * p;
261 "Function does not decrease along the current direction"));
269 Number df = f_prev - f;
270 Assert(first_step || df >= 0.,
271 ExcMessage(
"Function value is not decreasing"));
272 df =
std::max(df, 100. * std::numeric_limits<Number>::epsilon());
275 (first_step ? 1. :
std::min(1., -1.01 * 2. * df / g0));
280 const auto line_func =
281 [&](
const Number &x_line) -> std::pair<Number, Number> {
285 const Number g_line = g * p;
286 return std::make_pair(f, g_line);
290 const auto res = LineMinimization::line_search<Number>(
294 LineMinimization::poly_fit<Number>,
304 this->connect_line_search_slot(default_line_min);
313 VectorType g(x), p(x), y_k(x), s_k(x);
315 std::vector<Number> c1;
316 c1.reserve(additional_data.max_history_size);
331 conv = this->iteration_status(k, g.l2_norm(), x);
337 if (additional_data.debug_output)
338 deallog <<
"Iteration " << k <<
" history " << m << std::endl
339 <<
"f=" << f << std::endl;
345 for (
unsigned int i = 0; i < m; ++i)
347 c1[i] = rho[i] * (s[i] * p);
351 if (!preconditioner_signal.empty())
352 preconditioner_signal(p, s, y);
355 for (
int i = m - 1; i >= 0; --i)
358 const Number c2 = rho[i] * (y[i] * p);
359 p.add(c1[i] - c2, s[i]);
366 const Number alpha = line_search_signal(f, x, g, p)
371 if (additional_data.debug_output)
372 deallog <<
"Line search a=" << alpha <<
" f=" << f << std::endl;
376 const Number g_l2 = g.l2_norm();
377 conv = this->iteration_status(k, g_l2, x);
382 const Number curvature = s_k * y_k;
383 if (additional_data.debug_output)
384 deallog <<
"Curvature " << curvature << std::endl;
386 if (curvature > 0. && additional_data.max_history_size > 0)
390 rho.add(1. / curvature);
void add(const T &element)
typename VectorType::value_type Number
boost::signals2::connection connect_line_search_slot(const std::function< Number(Number &f, VectorType &x, VectorType &g, const VectorType &p)> &slot)
boost::signals2::connection connect_preconditioner_slot(const std::function< void(VectorType &g, const FiniteSizeHistory< VectorType > &s, const FiniteSizeHistory< VectorType > &y)> &slot)
void solve(const std::function< Number(const VectorType &x, VectorType &g)> &compute, VectorType &x)
boost::signals2::signal< void(VectorType &g, const FiniteSizeHistory< VectorType > &s, const FiniteSizeHistory< VectorType > &y)> preconditioner_signal
const AdditionalData additional_data
boost::signals2::signal< Number(Number &f, VectorType &x, VectorType &g, const VectorType &p)> line_search_signal
SolverBFGS(SolverControl &residual_control, const AdditionalData &data=AdditionalData())
@ iterate
Continue iteration.
@ success
Stop iteration, goal reached.
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
AdditionalData(const unsigned int max_history_size=5, const bool debug_output=false)
unsigned int max_history_size