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

Error in multistart with ls optimizers #410

Closed
Mv77 opened this issue Nov 18, 2022 · 2 comments · Fixed by #411
Closed

Error in multistart with ls optimizers #410

Mv77 opened this issue Nov 18, 2022 · 2 comments · Fixed by #411
Assignees
Labels
bug Something isn't working

Comments

@Mv77
Copy link

Mv77 commented Nov 18, 2022

Hi! this is Mateo from last year's DSE Summer School. I'm starting to use estimagic and it is just, well, magical! Thank you!

I've run into this small bug.

Bug description

Least squares algorithms appear not to work with multistarts. I think the issue is that some updating step of multistarts is looking for the best criterion value in a list where the least-squares algo has stored the vector of residuals instead.

To Reproduce

import estimagic as em
import numpy as np

def loss(params):

    resid = np.array([params['x'], params['y']])
    out = {
        "root_contributions": resid,
        "contributions": np.square(resid),
        "value": np.sum(np.square(resid)),
    }
    return out

est = em.minimize(
    criterion=loss,
    params={'x': -1.0, 'y': 1.0},
    lower_bounds={'x': -10, 'y': -10},
    upper_bounds={'x': 10, 'y': 10},
    algorithm='scipy_ls_trf',
    multistart=True,
    multistart_options={
        "n_samples": 3,
        "share_optimizations": 1.0
    }
)

print(est)

Expected behavior

I'd expect the above code to perform three starts.

Screenshots/Error messages

Instead, it raises the following error after the first start

Traceback (most recent call last):
  File "/home/mvg/em_fail.py", line 16, in <module>
    est = em.minimize(
  File "/usr/local/Anaconda3/lib/python3.9/site-packages/estimagic/optimization/optimize.py", line 403, in minimize
    return _optimize(
  File "/usr/local/Anaconda3/lib/python3.9/site-packages/estimagic/optimization/optimize.py", line 766, in _optimize
    raw_res = run_multistart_optimization(
  File "/usr/local/Anaconda3/lib/python3.9/site-packages/estimagic/optimization/tiktak.py", line 168, in run_multistart_optimization
    state, is_converged = update_convergence_state(
  File "/usr/local/Anaconda3/lib/python3.9/site-packages/estimagic/optimization/tiktak.py", line 471, in update_convergence_state
    if valid_new_y[best_index] <= best_y:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

System

  • OS: Ubuntu 18.04
@Mv77 Mv77 added the bug Something isn't working label Nov 18, 2022
@janosg janosg self-assigned this Nov 19, 2022
@janosg
Copy link
Member

janosg commented Nov 19, 2022

Hi Mateo (@Mv77), thanks a lot for this very nice bug report. It helped me to localize the error quickly.

I will fix this in #411 and add your example as a unit test. We will merge today and make a new release. The new version will be on conda-forge tomorrow or on Monday.

@Mv77
Copy link
Author

Mv77 commented Nov 19, 2022

Awesome; thank you @janosg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants