Skip to content

Scipy Optimiser from Omar #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
11c552f
Math: working in the initial prototype for Scipy optimizers
omazapa Dec 16, 2022
3ee1c23
Math Scipy: extracted informatio for the results
omazapa Dec 16, 2022
b8d5e20
implemented support for jacobian
omazapa Dec 17, 2022
9c2250c
Scipy: added support for extra options
omazapa Dec 21, 2022
739dbad
Scipy: pointer to values in the jacobian can not be free, It belongs …
omazapa Dec 21, 2022
b51c5c3
Scipy: printing python error when minimization fails
omazapa Dec 21, 2022
7bcd9d7
Scipy: working in the hessian implementation
omazapa Dec 22, 2022
d2cd799
Scipy: added support for bounds
omazapa Apr 13, 2023
5637c05
Scipy: removed compilation warnings
omazapa Apr 13, 2023
5b6d7a8
added some documentations, removed some comments, added some deletes …
omazapa Apr 14, 2023
1dc8d65
added scipy.C macro in fit tutorial
omazapa Apr 14, 2023
592c6f1
Scipy: implemented method NCalls
omazapa Apr 15, 2023
23109f0
Scipy: implemented maxiter and verbose option, returned the proper va…
omazapa Apr 18, 2023
feff9a3
Scipy: working in Constraint functions, it was possible to load the f…
omazapa Apr 20, 2023
d60808d
Scipy: passed constraints list to the minimizer function
omazapa Apr 20, 2023
fb25659
Scipy: removed Py_DECREF for lower and upper bounds, calling it to py…
omazapa Apr 30, 2023
a6a970b
Scipy: assigned fStatus
omazapa May 2, 2023
ca5c627
Scipy: fixed bounds, if there is not bounds It will pass Py_None
omazapa May 7, 2023
4278209
Scipy: added tests
omazapa Jun 5, 2023
816fdac
Scipy: remove method to pass extra options, this is not defined yet
omazapa Jun 12, 2023
fc555ab
Scipy: fixed style with clang-format
omazapa Jun 12, 2023
088808b
Scipy: Added support for extra options using GenAlgoOptions
omazapa Jul 3, 2023
4765e7a
Scipy: added documentation for extra options
omazapa Jul 4, 2023
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
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*)");
}
4 changes: 4 additions & 0 deletions math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ if(r)
add_subdirectory(rtools)
endif()

if(scipy AND NUMPY_FOUND)
add_subdirectory(scipy)
endif()

add_subdirectory(vecops)
20 changes: 20 additions & 0 deletions math/scipy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
############################################################################
# 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 ${PYTHON_LIBRARIES_Development_Main}
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(const std::vector<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(const std::vector<double> &, double *)>) override;

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

} // end namespace Experimental

} // end namespace Math

} // end namespace ROOT

#endif /* ROOT_Math_ScipyMinimizer */
Loading