bisection.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #ifndef CASADI_BISECTION_HPP
26 #define CASADI_BISECTION_HPP
27 
28 #include "casadi/core/rootfinder_impl.hpp"
29 #include <casadi/solvers/casadi_rootfinder_bisection_export.h>
30 
32 namespace casadi {
33  struct BisectionMemory : public RootfinderMemory {
34  int return_status;
35  casadi_int iter;
36  casadi_int search_iter;
37  double f_mid;
38  double bracket_width;
39  };
40 
41  class Bisection : public Rootfinder {
42  public:
43  explicit Bisection(const std::string &name, const Function &f);
44 
45  ~Bisection() override;
46 
47  const char *plugin_name() const override { return "bisection"; }
48 
49  std::string class_name() const override { return "Bisection"; }
50 
51  static Rootfinder *creator(const std::string &name, const Function &f) {
52  return new Bisection(name, f);
53  }
54 
55  static const Options options_;
56 
57  const Options &get_options() const override { return options_; }
58 
59  Dict get_stats(void *mem) const override;
60 
61  void init(const Dict &opts) override;
62 
63  void *alloc_mem() const override { return new BisectionMemory(); }
64 
65  int init_mem(void *mem) const override;
66 
67  void free_mem(void *mem) const override { delete static_cast<BisectionMemory *>(mem); }
68 
69  void set_work(void *mem, const double **&arg, double **&res,
70  casadi_int *&iw, double *&w) const override;
71 
72  int solve(void *mem) const override;
73 
74  static const std::string meta_doc;
75 
76  // void codegen_body(CodeGenerator &g) const override;
77 
78  // void codegen_declarations(CodeGenerator &g) const override;
79 
80  void serialize_body(SerializingStream &s) const override;
81 
82  static ProtoFunction *deserialize(DeserializingStream &s) { return new Bisection(s); }
83 
84  protected:
85  explicit Bisection(DeserializingStream &s);
86 
87  casadi_int max_iter_;
88  casadi_int max_search_;
89 
90  double abstol_;
91  double abstol_step_;
92 
93  double search_step_;
94 
95  double lb_;
96  double ub_;
97 
98  int finish(BisectionMemory *m, double x_sol, double f_sol, double width,
99  int status, bool success, UnifiedReturnStatus urs) const;
100  static std::string status_str(int status);
101  };
102 
103 } // namespace casadi
104 
106 #endif // CASADI_BISECTION_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
UnifiedReturnStatus