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

Handling crashes while optimizing #31

Open
oarcelus opened this issue May 22, 2024 · 3 comments
Open

Handling crashes while optimizing #31

oarcelus opened this issue May 22, 2024 · 3 comments

Comments

@oarcelus
Copy link

Which is the best strategy to handle a model crashing upon evaluation while optimizing input parameters?

@lindonroberts
Copy link
Collaborator

I assume you mean that the objective function you provide as an input to Py-BOBYQA sometimes crashes for particular parameter combinations? Py-BOBYQA doesn't have the ability to explicitly handle nan/error values, so the best option when a crash occurs would be to return a large function value (e.g. at least 1 order of magnitude larger than the objective value at nearby points). That way, Py-BOBYQA will never return these parameters as a solution, and it will try to search for a solution away from parameter combinations close to where the crash occurred.

@oarcelus
Copy link
Author

In that case my objective function should have access to the solution history, how should I do that? I could access the logging string and parse the evaluations but that solution is subpar imho. Is there a better way?

@lindonroberts
Copy link
Collaborator

You can either just pick a moderately large value arbitrarily, from just observing your problem in a few runs. Otherwise, you can wrap your function in a class which saves the values you've seen, something like:

class FunctionWrapper:
    def __init__(self, objfun):
        self.objfun = objfun
        self.history = []

    def __call__(self, x):
        try:
            f = self.objfun(x)
            self.history.append(f)
        except:
            f = 10 * self.history[-1]
        return f

myfun = FunctionWrapper(original_objfun)
soln = pybobyqa.solve(myfun, x0, ...)

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

2 participants