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

Not sure how to use Non Object Optimizers #1568

Closed
mxv001 opened this issue Oct 20, 2023 · 2 comments
Closed

Not sure how to use Non Object Optimizers #1568

mxv001 opened this issue Oct 20, 2023 · 2 comments

Comments

@mxv001
Copy link

mxv001 commented Oct 20, 2023

Hello experts,
Can you help with the guidelines on how to use non-object based minimization?

Thank you very much!

Steps to reproduce

Run the code below and observe sensible outputs. Then uncomment the COBYLA method and the code breaks.

Observed Results

I get an error:

TypeError: NonObjectOptimizer.__init__() got an unexpected keyword argument 'parametrization'

Expected Results

I expect this to break, because the minimizer is "non object" and cannot be instantiated as an object. But I do not understand how to get a for loop and ask-tell architecture from nevergrad. How can I adapt my code to accomplish this?

Note that I have read and implemented the functional scipy version, but my question is how to use the nevergrad wrapping so that I can have ask-tell format in a for loop.

Also is it possible to pass in a Nevergrad parameterization to the wrapped method?

Relevant Code

import nevergrad as ng

def square(x, y=12):
  return sum((x - 0.5) ** 2) + abs(y)


# IT WOULD BE GREAT TO USE NEVERGRAD NATIVE PARAMETERIZATION
parametrization = ng.p.Instrumentation(
  ng.p.Array(shape=(2,)).set_bounds(lower=-12, upper=12),
  y=ng.p.Scalar()
)

def print_candidate_and_value(optimizer, candidate, value):
  print(candidate,value)

# THIS WORKS JUST FINE
optimizer  = ng.optimizers.NGOpt(parametrization=parametrization, budget=50, num_workers=1)

# UNCOMMENT THIS AND IT BREAKS
#optimizer = ng.families.NonObjectOptimizer(method= 'COBYLA',parametrization=parametrization, budget=50, num_workers=1)

optimizer.register_callback("tell", print_candidate_and_value)

# GETTING THE LOOP BELOW TO WORK IS THE GOAL 
for i in range(optimizer.budget):
  x = optimizer.ask()
  loss = square(*x.args, **x.kwargs)
  optimizer.tell(x, loss)

@mxv001
Copy link
Author

mxv001 commented Oct 21, 2023

Well, I was able to figure it out! Here is the working code if anyone else has the same goal:

def square(x, y=12):
    return sum((x - 0.5) ** 2) + abs(y)


# IT WOULD BE GREAT TO USE NEVERGRAD NATIVE PARAMETERIZATION
parametrization = ng.p.Instrumentation(
  x=ng.p.Array(shape=(2,)).set_bounds(lower=-12, upper=12),
  y=ng.p.Scalar()
)

def print_candidate_and_value(optimizer, candidate, value):
    print(candidate,value)

optimizer=nevergrad.families.NonObjectOptimizer(method = 'Nelder-Mead')(parametrization=parametrization,budget=100)
optimizer.register_callback("tell", print_candidate_and_value)

for i in range(optimizer.budget):
    x = optimizer.ask()
    loss = square(*x.args, **x.kwargs)
    optimizer.tell(x, loss)

@mxv001
Copy link
Author

mxv001 commented Oct 21, 2023

Closed

@mxv001 mxv001 closed this as completed Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant