GithubHelp home page GithubHelp logo

andim / scipydirect Goto Github PK

View Code? Open in Web Editor NEW
28.0 3.0 7.0 118 KB

Python wrapper to the DIRECT global optimization algorithm with scipy.optimize compatible call syntax

Home Page: http://scipydirect.readthedocs.io/

License: MIT License

Python 9.57% Fortran 90.43%
optimization

scipydirect's Introduction

Latest release Build Status Documentation Status License

DOI

ScipyDIRECT: a Python wrapper of the DIRECT global optimization algorithm

DIRECT is a method to solve global bound constraint optimization problems and was originally developed by D. R. Jones, C. D. Perttunen and B. E. Stuckmann.

ScipyDIRECT is a python wrapper around DIRECT. It enables using DIRECT from the comfort of the Python scripting language.

The Scipydirect package uses the Fortran implementation of DIRECT written by Joerg M. Gablonsky, DIRECT Version 2.0.4. More information on the DIRECT algorithm can be found in Gablonsky's thesis.

ScipyDIRECT is a fork of pydirect. It provides an alternative interface to the DIRECT algorithm compatible with that used in scipy.optimize.

Note that as of scipy version 1.9 the DIRECT algorithm is included in scipy.optimize, which supersedes this package.

Quick start

ScipyDIRECT is on PyPI so you can install it using pip install scipydirect. As a prerequisite make you sure you have a Fortran compiler such as gfortran installed.

Then run the following minimal example.

import numpy as np
from scipydirect import minimize

def obj(x):
    return (x**2).sum()

bounds = [[-3.0, 3.0], [0.5, 5.0]]
res = minimize(obj, bounds=bounds)
print(res)

Documentation

You can access the documentation online at Read the docs. If you install from source you can generate a local version by running make html from the doc directory.

Support and contributing

For bug reports and enhancement requests use the Github issue tool, or (even better!) open a pull request with relevant changes. If you have any questions don't hesitate to contact me by email ([email protected]) or Twitter (@andimscience).

You can run the testsuite from the test directory:

cd test python test_direct.py

If you receive the warning message "Fortran code not compiled", when you try to use the package the installation has not been successful. Numpy.distutils is used to handle the Fortran dependency, so you might find advice for how to solve your problems by searching for "numpy distutils fortran" online. Unfortunately Fortran installation issues seem to be common, but I cannot assist you in that regard as such an issue is specific to the details of your computer setup.

scipydirect's People

Contributors

andim avatar ibaidev avatar jungtaekkim avatar rgommers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

scipydirect's Issues

Inclusion into SciPy?

Hey Andreas,

thank you for maintaining this wrapper for DIRECT for the python optimization community.

In a recent benchmark of derivative free global optimization algorithms in Python, DIRECT was quite competitive. On the SciPy mailing list, this sparked interest in incorporating DIRECT in SciPy directly. The benchmarks used NLopt's implementation which would be quite complicated to integrate but this Fortran wrapper should be easier.

Would you be interested in spearheading or supporting this? I imagine that most of the code can simply be reused.

Thanks in advance!

Discussion on the SciPy Side: scipy/scipy/issues/14121

Sementation Fault

Code

import numpy as np
import scipydirect
from scipydirect import minimize

print(scipydirect.__file__)

def obj(x):
    return (x**2).sum()

bounds = [[-3.0, 3.0], [0.5, 5.0]]
res = minimize(obj, bounds=bounds)
print(res)

Output

/home/czgdp1807ssd/scipy_project/scipydirect/scipydirect/__init__.py
Segmentation fault

Python - 3.9.5, 3.7.6
GCC - 9.3.0

Comments

It seems like the problem is with tool used to convert direct.pyf to direcmodule.c because, with Python - 3.7.10 and GCC - 7.5.0, the above code works fine.

ping @andim

setup.py try-except block hides fortran compilation problems

The project's README.md file says:

If you receive the warning message "Fortran code not compiled", when you try to use the package the installation has not been successful. Numpy.distutils is used to handle the Fortran dependency, so you might find advice for how to solve your problems by searching for "numpy distutils fortran" online. Unfortunately Fortran installation issues seem to be common, but I cannot assist you in that regard as such an issue is specific to the details of your computer setup.

The current setup.py file suppresses exceptions from extension module compilation, and modern pip (e.g. pip 22.0.4, used below) does not show the warning message printed in setup.py:

    try:
        thiskwargs = kwargs.copy()
        config = configuration()
        config.add_extension('direct', sources=['src/direct.pyf', 'src/DIRect.f', 'src/DIRserial.f', 'src/DIRsubrout.f'])
        thiskwargs.update(config.todict())
        setup(**thiskwargs)
    except:
        # if there was an error try building module without Fortran extension
        # the module will not be usable, but documentation can be built
        # (for readthedocs)
        warnings.warn('There was an error with building the Fortran extension.')
        thiskwargs = kwargs.copy()
        config = configuration()
        thiskwargs.update(config.todict())
        setup(**thiskwargs)
$ pip install -e .
[...]
Installing collected packages: scipydirect
  Attempting uninstall: scipydirect
    Found existing installation: scipydirect 1.3
    Uninstalling scipydirect-1.3:
      Successfully uninstalled scipydirect-1.3
  Running setup.py develop for scipydirect
Successfully installed scipydirect-1.3

This output was obtained in an environment without a Fortran compiler, and the resulting package was not functional:

$ python3.8 -c 'import scipydirect'
Fortran code not compiled, module not functional

If the scipydirect extension module cannot be built, it is much more conventional and useful to fail at installation time, rather than to silently succeed, only to later print a message at module load time. Installing a non-functional module for the docs is generally not useful to users.

'NoneType' object is not callable

when i try on the demo:

TypeError Traceback (most recent call last)
in ()
17
18 bounds = [(-3, 3), (-2, 2)]
---> 19 res = minimize(obj, bounds)
20
21 #

~\Miniconda3\lib\site-packages\scipydirect-1.3-py3.6.egg\scipydirect_init_.py in minimize(func, bounds, nvar, args, disp, eps, maxf, maxT, algmethod, fglobal, fglper, volper, sigmaper, **kwargs)
252 ddata,
253 cdata,
--> 254 disp
255 )
256

TypeError: 'NoneType' object is not callable

"Fortran code not compiled" Error Python 3.8

I managed to compile scipydirect with python 3.6 and it runs successfully; with python 3.8 on the other hand, I get the infamous "Fortran code not compiled" error message, which surprises me; question: have you tried to run scipydirect on python 3.8?

'NoneType' object is not callable

I install scipydirect in spyder and directly run the example given. The error message is

File "C:\Users\XXX\AppData\Local\Continuum\anaconda2\lib\site-packages\scipydirect_init_.py", line 254, in minimize
disp

TypeError: 'NoneType' object is not callable

objective_wrap function catches exceptions too broadly

Within minimize(), the local _objective_wrap function catches exceptions thrown by the user-provided objective function:

        try:
            return func(x, *args), 0
        except:
            return np.nan, 1

The bare except: clause catches all exceptions, including KeyboardInterrupt and SystemExit. This is generally not recommended, as in this case it can prevent the user from interrupting their application within minimize().

It would be more conventional to use except Exception to catch user-thrown exceptions, while allowing KeyboardInterrupt to stop the Python processn as the user intends.

WARNING : Capacity of array S in DIRDoubleInsert reached. Increase maxdiv.

I am getting this warning "
WARNING : Capacity of array S in DIRDoubleInsert reached. Increase maxdiv.
This means that there are a lot of hyperrectangles with the same function value at the center. We suggest to use our modification instead (Jones = 1)". What is the modification algo,? How can I use it?

Thanks,
Tinu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.