Skip to content

Commit

Permalink
Scipy: Added support for extra options using GenAlgoOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
omazapa committed Jul 4, 2023
1 parent fc555ab commit 088808b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
10 changes: 6 additions & 4 deletions math/scipy/inc/Math/ScipyMinimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

#include "Math/BasicMinimizer.h"

#include "Math/GenAlgoOptions.h"

#include "Rtypes.h"
#include "TString.h"

Expand Down Expand Up @@ -75,7 +73,7 @@ class ScipyMinimizer : public BasicMinimizer {
PyObject *fHessian;
PyObject *fBoundsMod;
PyObject *fConstraintsList; /// contraints functions
GenAlgoOptions fExtraOpts;
GenAlgoOptions *fExtraOpts;
std::function<bool(const std::vector<double> &, double *)> fHessianFunc;
unsigned int fConstN;
unsigned int fCalls;
Expand Down Expand Up @@ -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
Expand Down
36 changes: 23 additions & 13 deletions math/scipy/src/ScipyMinimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

//_______________________________________________________________________
Expand All @@ -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;
}

//_______________________________________________________________________
Expand Down Expand Up @@ -191,6 +183,13 @@ ScipyMinimizer::~ScipyMinimizer()
Py_DECREF(fBoundsMod);
}

//_______________________________________________________________________
void ScipyMinimizer::SetExtraOptions()
{
auto constExtraOpts = dynamic_cast<const GenAlgoOptions *>(fOptions.ExtraOptions());
fExtraOpts = const_cast<GenAlgoOptions *>(constExtraOpts);
}

//_______________________________________________________________________
bool ScipyMinimizer::Minimize()
{
Expand All @@ -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) {
Expand Down

0 comments on commit 088808b

Please sign in to comment.