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

Abort iterations upon a condition on the cost function #318

Closed
thibc81 opened this issue Jan 20, 2023 · 3 comments
Closed

Abort iterations upon a condition on the cost function #318

thibc81 opened this issue Jan 20, 2023 · 3 comments

Comments

@thibc81
Copy link

thibc81 commented Jan 20, 2023

Hello

I am trying to figure out the terminate function so as to abort the optimization if the cost function returns a certain value.
Is this possible? I couldn't find an example in the doc or on the git examples folder.

Thanks!

@stefan-k
Copy link
Member

stefan-k commented Jan 20, 2023

Are you using one of the available solvers or are you implementing your own?

The default implementation of terminate_internal of the Solver trait automatically checks if a target cost is reached.

You can set the target cost via the target_cost method of IterState (or PopulationState, depending on which solver you are using), somewhat like this:

let res = Executor::new(cost, solver)
        .configure(|state| state.param(init_param).max_iters(10).target_cost(0.0))
        .run()?;

EDIT: If you are implementing your own solver you will get this feature "for free" unless you overwrite terminate_internal.

@thibc81
Copy link
Author

thibc81 commented Jan 21, 2023

Thank you for your answer.
I am using PSO, in fact target_cost is effectively what I needed, thanks again.

@thibc81 thibc81 closed this as completed Jan 21, 2023
stefan-k added a commit to stefan-k/argmin that referenced this issue Jan 22, 2023
@stefan-k
Copy link
Member

I added a little note about target_cost(...) to the example in the book.

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