Skip to content

Commit 088808b

Browse files
committed
Scipy: Added support for extra options using GenAlgoOptions
1 parent fc555ab commit 088808b

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

math/scipy/inc/Math/ScipyMinimizer.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
#include "Math/BasicMinimizer.h"
2323

24-
#include "Math/GenAlgoOptions.h"
25-
2624
#include "Rtypes.h"
2725
#include "TString.h"
2826

@@ -75,7 +73,7 @@ class ScipyMinimizer : public BasicMinimizer {
7573
PyObject *fHessian;
7674
PyObject *fBoundsMod;
7775
PyObject *fConstraintsList; /// contraints functions
78-
GenAlgoOptions fExtraOpts;
76+
GenAlgoOptions *fExtraOpts;
7977
std::function<bool(const std::vector<double> &, double *)> fHessianFunc;
8078
unsigned int fConstN;
8179
unsigned int fCalls;
@@ -140,7 +138,11 @@ class ScipyMinimizer : public BasicMinimizer {
140138
Copy constructor
141139
*/
142140
ScipyMinimizer(const ScipyMinimizer &) : BasicMinimizer() {}
143-
void SetAlgoExtraOptions();
141+
142+
/**
143+
Get extra options from IOptions
144+
*/
145+
void SetExtraOptions();
144146

145147
public:
146148
/// method to perform the minimization

math/scipy/src/ScipyMinimizer.cxx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ ScipyMinimizer::ScipyMinimizer() : BasicMinimizer()
7575
fOptions.SetMinimizerAlgorithm("L-BFGS-B");
7676
PyInitialize();
7777
fHessianFunc = nullptr;
78-
// set extra options
79-
SetAlgoExtraOptions();
8078
fConstraintsList = PyList_New(0);
8179
fConstN = 0;
80+
fExtraOpts = nullptr;
8281
}
8382

8483
//_______________________________________________________________________
@@ -89,16 +88,9 @@ ScipyMinimizer::ScipyMinimizer(const char *type)
8988
fOptions.SetMinimizerAlgorithm(type);
9089
PyInitialize();
9190
fHessianFunc = nullptr;
92-
// set extra options
93-
SetAlgoExtraOptions();
9491
fConstraintsList = PyList_New(0);
9592
fConstN = 0;
96-
}
97-
98-
//_______________________________________________________________________
99-
void ScipyMinimizer::SetAlgoExtraOptions()
100-
{
101-
SetExtraOptions(fExtraOpts);
93+
fExtraOpts = nullptr;
10294
}
10395

10496
//_______________________________________________________________________
@@ -191,6 +183,13 @@ ScipyMinimizer::~ScipyMinimizer()
191183
Py_DECREF(fBoundsMod);
192184
}
193185

186+
//_______________________________________________________________________
187+
void ScipyMinimizer::SetExtraOptions()
188+
{
189+
auto constExtraOpts = dynamic_cast<const GenAlgoOptions *>(fOptions.ExtraOptions());
190+
fExtraOpts = const_cast<GenAlgoOptions *>(constExtraOpts);
191+
}
192+
194193
//_______________________________________________________________________
195194
bool ScipyMinimizer::Minimize()
196195
{
@@ -205,12 +204,23 @@ bool ScipyMinimizer::Minimize()
205204
}
206205
auto method = fOptions.MinimizerAlgorithm();
207206
PyObject *pyoptions = PyDict_New();
208-
if (method == "L-BFGS-B") {
209-
for (std::string key : fExtraOpts.GetAllRealKeys()) {
207+
SetExtraOptions();
208+
if (fExtraOpts) {
209+
for (std::string key : fExtraOpts->GetAllRealKeys()) {
210210
double value = 0;
211-
fExtraOpts.GetRealValue(key.c_str(), value);
211+
fExtraOpts->GetRealValue(key.c_str(), value);
212212
PyDict_SetItemString(pyoptions, key.c_str(), PyFloat_FromDouble(value));
213213
}
214+
for (std::string key : fExtraOpts->GetAllIntKeys()) {
215+
int value = 0;
216+
fExtraOpts->GetIntValue(key.c_str(), value);
217+
PyDict_SetItemString(pyoptions, key.c_str(), PyLong_FromLong(value));
218+
}
219+
for (std::string key : fExtraOpts->GetAllNamedKeys()) {
220+
std::string value = "";
221+
fExtraOpts->GetNamedValue(key.c_str(), value);
222+
PyDict_SetItemString(pyoptions, key.c_str(), PyUnicode_FromString(value.c_str()));
223+
}
214224
}
215225
PyDict_SetItemString(pyoptions, "maxiter", PyLong_FromLong(MaxIterations()));
216226
if (PrintLevel() > 0) {

0 commit comments

Comments
 (0)