graph_model.cpp
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 
26 #include "graph_model_internal.hpp"
27 
28 namespace casadi {
29 
30  // ---------- public GraphModel handle ----------
31 
33  }
34 
35  GraphModel::GraphModel(const std::string& format, const std::vector<uint8_t>& model_data,
36  const Dict& opts) {
38  (*this)->construct(opts);
39  }
40 
42  return static_cast<GraphModelInternal*>(SharedObject::operator->());
43  }
45  return static_cast<const GraphModelInternal*>(SharedObject::operator->());
46  }
48  return static_cast<GraphModelInternal*>(SharedObject::get());
49  }
50 
51  std::string GraphModel::format() const { return (*this)->plugin_name(); }
52 
54  (*this)->fill_metadata(gb);
55  }
57  const std::string& name) const {
58  // Shallow const: the backend engine mutates internally, but importing does not
59  // change the model handle's logical state.
60  return get()->import_symbolic(gb, name);
61  }
62  std::vector<uint8_t> GraphModel::export_symbolic(const Function& f, const Dict& opts) {
63  return (*this)->export_symbolic(f, opts);
64  }
65  const std::vector<uint8_t>& GraphModel::model_data() const { return (*this)->model_data(); }
66 
67  bool GraphModel::has_plugin(const std::string& name) {
68  return GraphModelInternal::has_plugin(name);
69  }
70  void GraphModel::load_plugin(const std::string& name) {
72  }
73  std::string GraphModel::doc(const std::string& name) {
74  return GraphModelInternal::getPlugin(name).doc;
75  }
76 
77  bool has_graphmodel(const std::string& name) { return GraphModel::has_plugin(name); }
78  void load_graphmodel(const std::string& name) { GraphModel::load_plugin(name); }
79  std::string doc_graphmodel(const std::string& name) { return GraphModel::doc(name); }
80 
81  // ---------- GraphModelInternal ----------
82 
83  GraphModelInternal::GraphModelInternal(const std::vector<uint8_t>& model_data)
84  : model_data_(model_data), verbose_(false) {
85  }
86 
88  }
89 
91  = {{},
92  {{"verbose",
93  {OT_BOOL, "Verbose evaluation -- for debugging"}}}
94  };
95 
96  void GraphModelInternal::construct(const Dict& opts) {
97  for (auto&& op : opts) {
98  if (op.first == "verbose") verbose_ = op.second;
99  }
100  init(opts);
101  }
102 
103  void GraphModelInternal::init(const Dict& opts) {
104  }
105 
106  void GraphModelInternal::disp(std::ostream& stream, bool more) const {
107  stream << "GraphModel(" << plugin_name() << ")";
108  }
109 
110  std::map<std::string, GraphModelInternal::Plugin> GraphModelInternal::solvers_;
111 
112 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
113  std::mutex GraphModelInternal::mutex_solvers_;
114 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
115 
116  const std::string GraphModelInternal::infix_ = "graphmodel";
117 
118 } // namespace casadi
Function object.
Definition: function.hpp:60
SharedObjectInternal * get() const
Get a const pointer to the node.
SharedObjectInternal * operator->() const
Access a member function or object.
Internal class for GraphBuilder.
Base interface for format-specific graph-model backends.
void disp(std::ostream &stream, bool more) const override
Print a description of the object.
GraphModelInternal(const std::vector< uint8_t > &model_data)
Definition: graph_model.cpp:83
static const Options options_
Options.
static const std::string infix_
Infix used to form plugin registration symbols (casadi_register_graphmodel_<name>)
const char * plugin_name() const override=0
Query plugin name.
bool verbose_
Verbose – for debugging.
static std::map< std::string, Plugin > solvers_
Collection of available format plugins.
void construct(const Dict &opts)
Prepare the backend for use.
Definition: graph_model.cpp:96
virtual void init(const Dict &opts)
Initialize.
virtual Function import_symbolic(const GraphBuilderInternal &gb, const std::string &name)=0
Rebuild the graph as a CasADi Function (symbolic import; mutates the backend's engine)
static bool has_plugin(const std::string &name)
Check if a format plugin is available.
Definition: graph_model.cpp:67
std::vector< uint8_t > export_symbolic(const Function &f, const Dict &opts=Dict())
Symbolic export: serialize a CasADi Function to model bytes.
Definition: graph_model.cpp:62
static void load_plugin(const std::string &name)
Explicitly load a format plugin.
Definition: graph_model.cpp:70
void fill_metadata(GraphBuilderInternal &gb) const
Populate a builder's Node metadata from the parsed model.
Definition: graph_model.cpp:53
const std::vector< uint8_t > & model_data() const
Raw model bytes.
Definition: graph_model.cpp:65
GraphModel()
Default constructor (null handle)
Definition: graph_model.cpp:32
GraphModelInternal * get() const
Definition: graph_model.cpp:47
std::string format() const
Format name (plugin name)
Definition: graph_model.cpp:51
static std::string doc(const std::string &name)
Get format-specific documentation.
Definition: graph_model.cpp:73
Function import_symbolic(const GraphBuilderInternal &gb, const std::string &name) const
Symbolic import: rebuild the graph as a CasADi Function.
Definition: graph_model.cpp:56
GraphModelInternal * operator->()
Definition: graph_model.cpp:41
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.
The casadi namespace.
Definition: archiver.cpp:28
bool has_graphmodel(const std::string &name)
Check if a graph-model format plugin is available.
Definition: graph_model.cpp:77
void load_graphmodel(const std::string &name)
Explicitly load a graph-model format plugin.
Definition: graph_model.cpp:78
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string doc_graphmodel(const std::string &name)
Get format-specific documentation.
Definition: graph_model.cpp:79
Options metadata for a class.
Definition: options.hpp:40