Skip to content
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

Allow Python functions in minimize with NCG method #23074

Open
mforets mannequin opened this issue May 25, 2017 · 28 comments
Open

Allow Python functions in minimize with NCG method #23074

mforets mannequin opened this issue May 25, 2017 · 28 comments

Comments

@mforets
Copy link
Mannequin

mforets mannequin commented May 25, 2017

If algorithm="ncg" is used in the top-level minimize function, one should be able to work with Python functions, as with the other algorithms.

CC: @dimpase

Component: numerical

Keywords: minimize

Author: Marcelo Forets, Vincent Delecroix

Branch/Commit: public/23074 @ 36d0f31

Reviewer: Dima Pasechnik

Issue created by migration from https://trac.sagemath.org/ticket/23074

@mforets mforets mannequin added this to the sage-8.0 milestone May 25, 2017
@mforets mforets mannequin added c: numerical labels May 25, 2017
@mforets
Copy link
Mannequin Author

mforets mannequin commented May 25, 2017

comment:1

For instance, this use case is currently broken:

sage: f = lambda x : 100*(x[1]-x[0]^2)^2+(1-x[0])^2+100*(x[2]-x[1]^2)^2+(1-x[1])^2
sage: gradient = lambda x : scipy.array([400*(x[0]**2 - x[1])*x[0] + 2*x[0] - 2, \
                                    -200*x[0]**2 + 400*(x[1]**2 - x[2])*x[1] + 202*x[1] - 2, \
                                     -200*x[1]**2 + 200*x[2]])
sage: x0 = [.1,.3,.4]
sage: minimize(f, x0, gradient, algorithm="ncg")
Traceback (most recent call last)
...
UnboundLocalError: local variable 'min' referenced before assignment

but it shouldn't, since this works:

sage: import scipy
sage: from scipy import optimize
sage: optimize.fmin_ncg(f, [float(_) for _ in x0], fprime=gradient, disp=True)
Optimization terminated successfully.
...
array([ 0.99999732,  0.99999463,  0.99998926])

Moreover, the Hessian can also be passed to ncg (but it's optional => docstring is inaccurate)

sage: hessian = lambda x : [[1200*x[0]^2 - 400*x[1] + 2, -400*x[0], 0], \
                    [-400*x[0], 1200*x[1]^2 - 400*x[2] + 202, -400*x[1]], \
                    [0, -400*x[1], 200]]
sage: optimize.fmin_ncg(f, [float(_) for _ in x0], fprime=gradient, fhess=hessian, disp=True)
Optimization terminated successfully.
...
array([ 1.,  1.,  1.])

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 25, 2017

comment:2

on a separate line, in the doc it says that

Only one of hessp or hess needs to be given.

Hence, in the current if clause of algorithm=="ncg" one does not need to compute & pass both hessian and hessian_p, but just one of them, i think.

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 25, 2017

Dependencies: 23062

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 25, 2017

Changed dependencies from 23062 to #23062

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 27, 2017

Branch: u/mforets/23074

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 27, 2017

Commit: 9416552

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 27, 2017

Author: Marcelo Forets

@mforets
Copy link
Mannequin Author

mforets mannequin commented May 27, 2017

Last 10 new commits:

ff2f590change disp -> verbose keyword
1f1fecffix keyword in doctests
be7fcf5add rename keyword deprecation warning
469bd82set default -> False, and update doctests
46e3497add missing algorithm descriptions and wiki link
f0373a0update verbose in docstring
540bdeaadd complementary note
83e9a04remove pair of backticks (all or nothing)
8e8289dMerge branch 'develop' into t/mforets/23074
9416552add ncg method to python funcs and add test

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

b658d33add example with hessian
74da21cupdate test

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2017

Changed commit from 9416552 to 74da21c

@mforets mforets mannequin added the s: needs review label May 27, 2017
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2017

Changed commit from 74da21c to ac91ce2

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

ac91ce2fix typo

@videlec
Copy link
Contributor

videlec commented Feb 23, 2020

comment:10

rebased


New commits:

c2fa6a5add ncg method to python funcs and add test
e8c3208add example with hessian
e2d29a1update test
f7a5fb4fix typo

@videlec
Copy link
Contributor

videlec commented Feb 23, 2020

Changed branch from u/mforets/23074 to public/23074

@videlec
Copy link
Contributor

videlec commented Feb 23, 2020

Changed dependencies from #23062 to none

@videlec
Copy link
Contributor

videlec commented Feb 23, 2020

Changed commit from ac91ce2 to f7a5fb4

@videlec videlec modified the milestones: sage-8.0, sage-9.1 Feb 23, 2020
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 23, 2020

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

4388049add ncg method to python funcs and add test
e6c8e38clean doctests

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 23, 2020

Changed commit from f7a5fb4 to e6c8e38

@videlec
Copy link
Contributor

videlec commented Feb 23, 2020

comment:12

Some refactorization in progress..

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 23, 2020

Changed commit from e6c8e38 to 36d0f31

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 23, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

36d0f31refactor

@videlec
Copy link
Contributor

videlec commented Feb 23, 2020

Changed author from Marcelo Forets to Marcelo Forets, Vincent Delecroix

@dimpase
Copy link
Member

dimpase commented Feb 23, 2020

comment:15

Thanks, looks good.

@dimpase
Copy link
Member

dimpase commented Feb 23, 2020

Reviewer: Dima Pasechnik

@vbraun
Copy link
Member

vbraun commented Feb 24, 2020

comment:16

On 32-bit:

**********************************************************************
File "src/sage/numerical/optimize.py", line 352, in sage.numerical.optimize.?
Failed example:
    minimize(f, [.1, .3, .4], algorithm="ncg") # abs tol 1e-6
Expected:
    (1.0, 1.0, 1.0)
Got:
    (1.0000003254615812, 1.000000652320023, 1.0000013080122123)
Tolerance exceeded in 1 of 3:
    1.0 vs 1.0000013080122123, tolerance 2e-6 > 1e-6
**********************************************************************
1 item had failures:
   1 of  20 in sage.numerical.optimize.?
    [104 tests, 1 failure, 1.15 s]
----------------------------------------------------------------------
sage -t --long src/sage/numerical/optimize.py  # 1 doctest failed
----------------------------------------------------------------------

@mkoeppe
Copy link
Member

mkoeppe commented May 1, 2020

comment:17

Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity.

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 May 1, 2020
@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Sep 5, 2020
@mkoeppe
Copy link
Member

mkoeppe commented Feb 13, 2021

comment:19

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Feb 13, 2021
@mkoeppe
Copy link
Member

mkoeppe commented Jul 19, 2021

comment:20

Setting a new milestone for this ticket based on a cursory review.

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 7, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Sep 19, 2022
@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants