diff --git a/math/scipy/inc/Math/ScipyMinimizer.h b/math/scipy/inc/Math/ScipyMinimizer.h index 4ee3ba2d2ed03..37b7c12b449ef 100644 --- a/math/scipy/inc/Math/ScipyMinimizer.h +++ b/math/scipy/inc/Math/ScipyMinimizer.h @@ -21,8 +21,6 @@ #include "Math/BasicMinimizer.h" -#include "Math/GenAlgoOptions.h" - #include "Rtypes.h" #include "TString.h" @@ -75,7 +73,7 @@ class ScipyMinimizer : public BasicMinimizer { PyObject *fHessian; PyObject *fBoundsMod; PyObject *fConstraintsList; /// contraints functions - GenAlgoOptions fExtraOpts; + GenAlgoOptions *fExtraOpts; std::function &, double *)> fHessianFunc; unsigned int fConstN; unsigned int fCalls; @@ -140,7 +138,11 @@ class ScipyMinimizer : public BasicMinimizer { Copy constructor */ ScipyMinimizer(const ScipyMinimizer &) : BasicMinimizer() {} - void SetAlgoExtraOptions(); + + /** + Get extra options from IOptions + */ + void SetExtraOptions(); public: /// method to perform the minimization diff --git a/math/scipy/src/ScipyMinimizer.cxx b/math/scipy/src/ScipyMinimizer.cxx index abcd706be0c3b..bd3eea3e5882c 100644 --- a/math/scipy/src/ScipyMinimizer.cxx +++ b/math/scipy/src/ScipyMinimizer.cxx @@ -75,10 +75,9 @@ ScipyMinimizer::ScipyMinimizer() : BasicMinimizer() fOptions.SetMinimizerAlgorithm("L-BFGS-B"); PyInitialize(); fHessianFunc = nullptr; - // set extra options - SetAlgoExtraOptions(); fConstraintsList = PyList_New(0); fConstN = 0; + fExtraOpts = nullptr; } //_______________________________________________________________________ @@ -89,16 +88,9 @@ ScipyMinimizer::ScipyMinimizer(const char *type) fOptions.SetMinimizerAlgorithm(type); PyInitialize(); fHessianFunc = nullptr; - // set extra options - SetAlgoExtraOptions(); fConstraintsList = PyList_New(0); fConstN = 0; -} - -//_______________________________________________________________________ -void ScipyMinimizer::SetAlgoExtraOptions() -{ - SetExtraOptions(fExtraOpts); + fExtraOpts = nullptr; } //_______________________________________________________________________ @@ -191,6 +183,13 @@ ScipyMinimizer::~ScipyMinimizer() Py_DECREF(fBoundsMod); } +//_______________________________________________________________________ +void ScipyMinimizer::SetExtraOptions() +{ + auto constExtraOpts = dynamic_cast(fOptions.ExtraOptions()); + fExtraOpts = const_cast(constExtraOpts); +} + //_______________________________________________________________________ bool ScipyMinimizer::Minimize() { @@ -205,12 +204,23 @@ bool ScipyMinimizer::Minimize() } auto method = fOptions.MinimizerAlgorithm(); PyObject *pyoptions = PyDict_New(); - if (method == "L-BFGS-B") { - for (std::string key : fExtraOpts.GetAllRealKeys()) { + SetExtraOptions(); + if (fExtraOpts) { + for (std::string key : fExtraOpts->GetAllRealKeys()) { double value = 0; - fExtraOpts.GetRealValue(key.c_str(), value); + fExtraOpts->GetRealValue(key.c_str(), value); PyDict_SetItemString(pyoptions, key.c_str(), PyFloat_FromDouble(value)); } + for (std::string key : fExtraOpts->GetAllIntKeys()) { + int value = 0; + fExtraOpts->GetIntValue(key.c_str(), value); + PyDict_SetItemString(pyoptions, key.c_str(), PyLong_FromLong(value)); + } + for (std::string key : fExtraOpts->GetAllNamedKeys()) { + std::string value = ""; + fExtraOpts->GetNamedValue(key.c_str(), value); + PyDict_SetItemString(pyoptions, key.c_str(), PyUnicode_FromString(value.c_str())); + } } PyDict_SetItemString(pyoptions, "maxiter", PyLong_FromLong(MaxIterations())); if (PrintLevel() > 0) {