Skip to content
Snippets Groups Projects
Commit 4ab765b3 authored by Erik Strand's avatar Erik Strand
Browse files

Replace LineObjective with a lambda function

parent cf6c5c38
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include "logs/nothing.h" #include "logs/nothing.h"
#include "optimizers/line_search/brent.h" #include "optimizers/line_search/brent.h"
#include "optimizers/line_search/line_objective.h"
#include <iostream> #include <iostream>
...@@ -90,7 +89,10 @@ VectorNs<N> const& ConjugateGradientDescent<N>::optimize( ...@@ -90,7 +89,10 @@ VectorNs<N> const& ConjugateGradientDescent<N>::optimize(
gradient_.resize(point_.size()); gradient_.resize(point_.size());
last_gradient_.resize(point_.size()); last_gradient_.resize(point_.size());
LineObjective<Objective, N> line_objective{objective, point_, direction_, new_point_}; auto const line_objective = [&](Scalar t, Scalar& value) {
new_point_ = point_ + t * direction_;
objective(new_point_, value);
};
BracketFinder bracket; BracketFinder bracket;
Brent line_minimizer; Brent line_minimizer;
......
#ifndef OPTIMIZATION_LINE_SEARCH_LINE_OBJECTIVE_H
#define OPTIMIZATION_LINE_SEARCH_LINE_OBJECTIVE_H
#include "utils/vector.h"
namespace optimization {
//--------------------------------------------------------------------------------------------------
template <typename Objective, int32_t N>
struct LineObjective {
public:
LineObjective(Objective& o, VectorNs<N>& x0, VectorNs<N>& dir, VectorNs<N>& x) :
objective_(o), x0_(x0), dir_(dir), x_(x)
{}
void operator()(Scalar t, Scalar& value) {
x_ = x0_ + t * dir_;
objective_(x_, value);
}
void operator()(Scalar t, Scalar& value, VectorNs<N>& gradient) {
x_ = x0_ + t * dir_;
objective_(x_, value, gradient);
}
private:
Objective& objective_;
VectorNs<N>& x0_;
VectorNs<N>& dir_;
VectorNs<N>& x_;
};
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment