Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8fe3e78
Math: working in the initial prototype for Scipy optimizers
omazapa Dec 16, 2022
1247628
Math Scipy: extracted informatio for the results
omazapa Dec 16, 2022
19cd43a
implemented support for jacobian
omazapa Dec 17, 2022
58ea062
Scipy: added support for extra options
omazapa Dec 21, 2022
b8a1dfa
Scipy: pointer to values in the jacobian can not be free, It belongs …
omazapa Dec 21, 2022
e609802
Scipy: printing python error when minimization fails
omazapa Dec 21, 2022
07ae9b3
Scipy: working in the hessian implementation
omazapa Dec 22, 2022
d1a201c
Scipy: added support for bounds
omazapa Apr 13, 2023
7cd71e6
Scipy: removed compilation warnings
omazapa Apr 13, 2023
8dd259c
added some documentations, removed some comments, added some deletes …
omazapa Apr 14, 2023
8f6ee82
added scipy.C macro in fit tutorial
omazapa Apr 14, 2023
214b996
Scipy: implemented method NCalls
omazapa Apr 15, 2023
13add25
Scipy: implemented maxiter and verbose option, returned the proper va…
omazapa Apr 18, 2023
2c83094
Scipy: working in Constraint functions, it was possible to load the f…
omazapa Apr 20, 2023
2405528
Scipy: passed constraints list to the minimizer function
omazapa Apr 20, 2023
043f9cf
Scipy: removed Py_DECREF for lower and upper bounds, calling it to py…
omazapa Apr 30, 2023
9ef0d94
Scipy: assigned fStatus
omazapa May 2, 2023
93d4c2b
Scipy: fixed bounds, if there is not bounds It will pass Py_None
omazapa May 7, 2023
96412eb
Scipy: added tests
omazapa Jun 5, 2023
32e3d58
Scipy: remove method to pass extra options, this is not defined yet
omazapa Jun 12, 2023
8f9b91a
Scipy: fixed style with clang-format
omazapa Jun 12, 2023
8f5040f
Scipy: Added support for extra options using GenAlgoOptions
omazapa Jul 3, 2023
706ae0e
Scipy: added documentation for extra options
omazapa Jul 4, 2023
2cee20e
[math] Make ScipyMinimizer compile again
guitargeek Jul 29, 2025
2e498fe
[ci] Build with `scipy=ON`
guitargeek Jul 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmake/modules/SearchInstalledSoftware.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,6 @@ set(Python3_FIND_FRAMEWORK LAST)
list(APPEND python_components Interpreter)
if(pyroot OR tmva-pymva)
list(APPEND python_components Development)
endif()
if(tmva-pymva)
list(APPEND python_components NumPy)
endif()
find_package(Python3 3.8 COMPONENTS ${python_components})
Expand Down
5 changes: 5 additions & 0 deletions etc/plugins/ROOT@@Math@@Minimizer/P100_Scipy.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void P100_Scipy()
{
gPluginMgr->AddHandler("ROOT::Math::Minimizer", "Scipy", "ROOT::Math::Experimental::ScipyMinimizer", "Scipy",
"ScipyMinimizer(const char*)");
}
2 changes: 2 additions & 0 deletions math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ if(r)
add_subdirectory(rtools)
endif()

add_subdirectory(scipy)

add_subdirectory(vecops)
22 changes: 22 additions & 0 deletions math/scipy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
############################################################################
# CMakeLists.txt file for building ROOT math/scipy package
############################################################################
# author: [email protected] 2022

include_directories(SYSTEM ${PYTHON_INCLUDE_DIRS_Development_Main} ${NUMPY_INCLUDE_DIRS})
ROOT_STANDARD_LIBRARY_PACKAGE(Scipy
HEADERS
Math/ScipyMinimizer.h
LINKDEF
Math/LinkDef.h
LIBRARIES
Python3::NumPy
Python3::Python
SOURCES
src/ScipyMinimizer.cxx
DEPENDENCIES Core MathCore RIO
)

target_compile_definitions(Scipy PUBLIC USE_ROOT_ERROR ${PYTHON_DEFINITIONS})
target_include_directories(Scipy PUBLIC ${PYTHON_INCLUDE_DIRS})
ROOT_ADD_TEST_SUBDIRECTORY(test)
11 changes: 11 additions & 0 deletions math/scipy/inc/Math/LinkDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @(#)root/math/ipopt:$Id$
// Authors: [email protected] 12/2022

#ifdef __CINT__

#pragma link C++ nestedclasses;
#pragma link C++ nestedtypedef;

#pragma link C++ class ROOT::Math::Experimental::ScipyMinimizer;

#endif //__CINT__
173 changes: 173 additions & 0 deletions math/scipy/inc/Math/ScipyMinimizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// @(#)root/math/scipy:$Id$
// Author: [email protected] 2023

/*************************************************************************
* Copyright (C) 1995-2022, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/

#ifndef ROOT_Math_ScipyMinimizer
#define ROOT_Math_ScipyMinimizer

#include "Math/Minimizer.h"
#include "Math/MinimizerOptions.h"

#include "Math/IFunctionfwd.h"

#include "Math/IParamFunctionfwd.h"

#include "Math/BasicMinimizer.h"

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

#include <functional>
#include <vector>
#include <map>

#ifndef PyObject_HEAD
struct _object;
typedef _object PyObject;
#define Py_single_input 256
#endif

namespace ROOT {

namespace Math {

class GenAlgoOptions;

namespace Experimental {
/**
enumeration specifying the types of Scipy solvers
@ingroup MultiMin
*/

//_____________________________________________________________________________________
/**
\class ScipyMinimizer
ScipyMinimizer class.
Scipy minimizer implementation using Python C API that supports several methods such as
Nelder-Mead, L-BFGS-B, Powell, CG, BFGS, TNC, COBYLA, SLSQP, trust-constr,
Newton-CG, dogleg, trust-ncg, trust-exact and trust-krylov.

It supports the Jacobian (Gradients), Hessian and bounds for the variables.

Support for constraint functions will be implemented in the next releases.
You can find a macro example in the folder $ROOTSYS/tutorial/fit/scipy.C

To provide extra options to the minimizer, you can use the class GenAlgoOptions
and the method SetExtraOptions().
Example:
```
ROOT::Math::GenAlgoOptions l_bfgs_b_opt;
l_bfgs_b_opt.SetValue("gtol", 1e-3);
l_bfgs_b_opt.SetValue("ftol", 1e7);
minimizer->SetExtraOptions(l_bfgs_b_opt);
```

See <A HREF="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html">Scipy doc</A>
from more info on the Scipy minimization algorithms.

@ingroup MultiMin
*/

class ScipyMinimizer : public BasicMinimizer {
private:
PyObject *fMinimize;
PyObject *fTarget;
PyObject *fJacobian;
PyObject *fHessian;
PyObject *fBoundsMod;
PyObject *fConstraintsList; /// contraints functions
GenAlgoOptions *fExtraOpts;
std::function<bool(std::span<const double>, double *)> fHessianFunc;
unsigned int fConstN;
unsigned int fCalls;

protected:
PyObject *fGlobalNS;
PyObject *fLocalNS;

public:
/**
Default constructor
*/
ScipyMinimizer();
/**
Constructor with a string giving name of algorithm
*/
ScipyMinimizer(const char *type);

/**
Destructor
*/
virtual ~ScipyMinimizer();

/**
Python initialization
*/
void PyInitialize();

/**
Checks if Python was initialized
*/
int PyIsInitialized();

/*
Python finalization
*/
void PyFinalize();
/*
Python code execution
*/
void PyRunString(TString code, TString errorMessage = "Failed to run python code", int start = Py_single_input);

/*
Number of function calls
*/
virtual unsigned int NCalls() const override;

/*
Method to add Constraint function,
multiples constraints functions can be added.
type have to be a string "eq" or "ineq" where
eq (means Equal, then fun() = 0)
ineq (means that, it is to be non-negative. fun() >=0)
https://kitchingroup.cheme.cmu.edu/f19-06623/13-constrained-optimization.html
*/
virtual void AddConstraintFunction(std::function<double(const std::vector<double> &)>, std::string type);

private:
// usually copying is non trivial, so we make this unaccessible

/**
Copy constructor
*/
ScipyMinimizer(const ScipyMinimizer &) : BasicMinimizer() {}

/**
Get extra options from IOptions
*/
void SetExtraOptions();

public:
/// method to perform the minimization
virtual bool Minimize() override;

virtual void SetHessianFunction(std::function<bool(std::span<const double>, double *)>) override;

protected:
ClassDef(ScipyMinimizer, 0) //
};

} // end namespace Experimental

} // end namespace Math

} // end namespace ROOT

#endif /* ROOT_Math_ScipyMinimizer */
Loading