From 2b4f7ecfa282e5bc3dcc893b25e47151f4dbdc8a Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sun, 26 Dec 2010 08:10:51 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@150 ed2142bd-67ad-457f-ba7c-d818d4011675 --- interface/pyfunc_obj.hpp | 71 +++++++++++++++++++++++++++++++++++ interface/pymodel.hpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 interface/pyfunc_obj.hpp create mode 100644 interface/pymodel.hpp (limited to 'interface') diff --git a/interface/pyfunc_obj.hpp b/interface/pyfunc_obj.hpp new file mode 100644 index 0000000..db2775a --- /dev/null +++ b/interface/pyfunc_obj.hpp @@ -0,0 +1,71 @@ +#ifndef PYFUNC_OBJ_HPP +#define PYFUNC_OBJ_HPP +#include +#include +#include + + +namespace opt_utilities +{ + + template + class pyfunc_obj + :public func_obj + { + private: + boost::python::object pyfunc; + public: + pyfunc_obj() + { + if(!Py_IsInitialized()) + { + Py_Initialize(); + } + } + + pyfunc_obj(const pyfunc_obj& rhs) + :pyfunc(rhs.pyfunc) + {} + + pyfunc_obj& operator=(const pyfunc_obj& rhs) + { + pyfunc=rhs.pyfunc; + } + + ~pyfunc_obj() + {} + + public: + void attach(const std::string module_name, + const std::string func_name) + { + boost::python::object mod(boost::python::import(module_name.c_str())); + pyfunc=mod.attr(func_name.c_str()); + + } + private: + func_obj* do_clone()const + { + return new pyfunc_obj(*this); + } + + void do_destroy() + { + delete this; + } + + Ty do_eval(const Tx& x) + { + boost::python::list args; + for(size_t i=0;i(pyfunc(args)); + } + }; +} + + +#endif +//EOF diff --git a/interface/pymodel.hpp b/interface/pymodel.hpp new file mode 100644 index 0000000..5ef3885 --- /dev/null +++ b/interface/pymodel.hpp @@ -0,0 +1,98 @@ +#ifndef PYMODEL +#define PYMODEL +#include +#include +#include + + +namespace opt_utilities +{ + + template + class pymodel + :public model + { + private: + boost::python::object pyfunc; + public: + pymodel() + { + if(!Py_IsInitialized()) + { + Py_Initialize(); + } + } + + pymodel(const pymodel& rhs) + :pyfunc(rhs.pyfunc) + { + for(int i=0;ipush_param_info(rhs.get_param_info(i)); + } + } + + pymodel& operator=(const pymodel& rhs) + { + if(this==&rhs) + { + return *this; + } + pyfunc=rhs.pyfunc; + for(int i=0;ipush_param_info(rhs.get_param_info(i)); + } + return *this; + } + + ~pymodel() + {} + + public: + void attach(const std::string module_name, + const std::string arglist_name, + const std::string func_name) + { + this->clear_param_info(); + boost::python::object mod(boost::python::import(module_name.c_str())); + pyfunc=mod.attr(func_name.c_str()); + boost::python::dict args(mod.attr(arglist_name.c_str())); + boost::python::list pnames(args.keys()); + int nparams=boost::python::len(pnames); + for(size_t i=0;i!=nparams;++i) + { + boost::python::object pname_obj=pnames[i]; + std::string pname=boost::python::extract(pname_obj); + typename element_type_trait::element_type pvalue= + boost::python::extract::element_type>(args.get(pname_obj)); + + push_param_info(param_info(pname,pvalue)); + } + } + private: + model* do_clone()const + { + return new pymodel(*this); + } + + void do_destroy() + { + delete this; + } + + Ty do_eval(const Tx& x,const Tp& p) + { + boost::python::list args; + for(size_t i=0;i(pyfunc(x,args)); + } + }; +} + + +#endif +//EOF -- cgit v1.2.2