32 int CASADI_ROOTFINDER_NEWTON_EXPORT
35 plugin->name =
"newton";
37 plugin->version = CASADI_VERSION;
60 "Stopping criterion tolerance on max(|F|)"}},
63 "Stopping criterion tolerance on step size"}},
66 "Maximum number of Newton iterations to perform before returning."}},
69 "Print information about each iteration"}},
72 "Enable line-search (default: true)"}}
89 for (
auto&& op : opts) {
90 if (op.first==
"max_iter") {
92 }
else if (op.first==
"abstol") {
94 }
else if (op.first==
"abstolStep") {
96 }
else if (op.first==
"print_iteration") {
98 }
else if (op.first==
"line_search") {
104 "Newton: the supplied f must have at least one input.");
106 "Newton::init: linear_solver must be supplied");
120 casadi_int*& iw,
double*& w)
const {
125 m->x_trial = w; w +=
n_;
126 m->f_trial = w; w +=
n_;
144 if (
verbose_) casadi_message(
"Max iterations reached.");
145 m->return_status =
"max_iteration_reached";
155 std::copy_n(m->iarg,
n_in_, m->arg);
158 std::copy_n(m->ires,
n_out_, m->res+1);
159 m->res[1+
iout_] = m->f;
164 if (
abstol_ != std::numeric_limits<double>::infinity()) {
165 for (casadi_int i=0; i<
n_; ++i) {
166 abstol = std::max(abstol, fabs(m->f[i]));
180 if (std::numeric_limits<double>::infinity() !=
abstolStep_) {
181 for (casadi_int i=0; i<
n_; ++i) {
182 abstolStep = std::max(abstolStep, fabs(m->f[i]));
192 std::copy_n(m->iarg,
n_in_, m->arg);
193 m->arg[
iin_] = m->x_trial;
194 std::copy_n(m->ires,
n_out_, m->res);
195 m->res[
iout_] = m->f_trial;
198 std::copy_n(m->x,
n_, m->x_trial);
203 if (abstol_trial<=(1-alpha/2)*abstol) {
204 std::copy_n(m->x_trial,
n_, m->x);
208 if (
verbose_) casadi_message(
"Linesearch did not find a descent step "
209 "for step size " +
str(alpha*abstolStep));
223 if ((m->iter-1) % 10 ==0) {
237 if (success) m->return_status =
"success";
238 if (
verbose_) casadi_message(
"Newton algorithm took " +
str(m->iter) +
" steps");
240 m->success = success;
246 stream << std::setw(5) <<
"iter";
247 stream << std::setw(10) <<
"res";
248 stream << std::setw(10) <<
"step";
251 stream.unsetf(std::ios::floatfield);
255 double abstol,
double abstolStep,
double alpha)
const {
257 std::ios_base::fmtflags f = stream.flags();
258 stream << std::setw(5) << iter;
259 stream << std::setw(10) << std::scientific << std::setprecision(2) << abstol;
260 stream << std::setw(10) << std::scientific << std::setprecision(2) << abstolStep;
261 if (
line_search_) stream << std::setw(10) << std::scientific << std::setprecision(2) << alpha;
263 stream << std::fixed;
280 stats[
"iter_count"] = m->iter;
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
size_t n_in_
Number of inputs and outputs.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
casadi_int n_in() const
Get the number of function inputs.
bool is_null() const
Is a null pointer?
void nfact(const DM &A) const
Numeric factorization of the linear system.
DM solve(const DM &A, const DM &B, bool tr=false) const
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
static const std::string meta_doc
A documentation string.
void printIteration(std::ostream &stream) const
Print iteration header.
static const Options options_
Options.
int solve(void *mem) const override
Solve the system of equations and calculate derivatives.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
bool print_iteration_
If true, each iteration will be printed.
void init(const Dict &opts) override
Initialize.
Dict get_stats(void *mem) const override
Get all statistics.
int init_mem(void *mem) const override
Initalize memory block.
double abstolStep_
Absolute tolerance that should be met on step.
Newton(const std::string &name, const Function &f)
Constructor.
static Rootfinder * creator(const std::string &name, const Function &f)
Create a new Rootfinder.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
casadi_int max_iter_
Maximum number of Newton iterations.
double abstol_
Absolute tolerance that should be met on residual.
~Newton() override
Destructor.
void set_function(const Function &fcn, const std::string &fname, bool jit=false)
Function oracle_
Oracle: Used to generate other functions.
int calc_function(OracleMemory *m, const std::string &fcn, const double *const *arg=nullptr, int thread_id=0) const
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
bool verbose_
Verbose printout.
void clear_mem()
Clear all memory (called from destructor)
int init_mem(void *mem) const override
Initalize memory block.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int n_
Number of equations.
Dict get_stats(void *mem) const override
Get all statistics.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
casadi_int iin_
Indices of the input and output that correspond to the actual root-finding.
static const Options options_
Options.
Linsol linsol_
Linear solver.
void init(const Dict &opts) override
Initialize.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
int CASADI_ROOTFINDER_NEWTON_EXPORT casadi_register_rootfinder_newton(Rootfinder::Plugin *plugin)
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void casadi_axpy(casadi_int n, T1 alpha, const T1 *x, T1 *y)
AXPY: y <- a*x + y.
void CASADI_ROOTFINDER_NEWTON_EXPORT casadi_load_rootfinder_newton()
T1 casadi_norm_inf(casadi_int n, const T1 *x)
const char * return_status
Options metadata for a class.