Skip to content

Time-based stop #184

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

Closed
aeantipov opened this issue Apr 11, 2019 · 3 comments · Fixed by #229
Closed

Time-based stop #184

aeantipov opened this issue Apr 11, 2019 · 3 comments · Fixed by #229

Comments

@aeantipov
Copy link
Contributor

Hey guys,
Adaptive is great.

A feature request. I want to have the runner quit after a certain time. Say, something like

runner = adaptive.BlockingRunner(learner, goal=time_elapsed > 6 hours)

This is useful when you submit the runner itself as a job to a cluster with a finite time of execution. With runner.save and runner.load one can then stage, look at the data and decide whether it needs to submitted again. How can I do that?

@basnijholt
Copy link
Member

basnijholt commented Apr 11, 2019

Hi Andrey,

with the Runner you can use any goal, so to do the thing you want you can do:

def time_goal(total_seconds=3600):
    import time
    def goal(learner):
        try:
            return time.time() - learner._start_time > total_seconds
        except AttributeError:
            # Set the start time
            learner._start_time = time.time()
            return False
    return goal

runner = adaptive.Runner(learner, goal=time_goal(6 * 3600))  # stop after 6 hours 
runner.live_info()

Another cool thing that would probably interest you is runner.start_periodic_saving.

@aeantipov
Copy link
Contributor Author

aeantipov commented Apr 11, 2019

Thanks, I will use that!

Logically I think this could be improved - the learner stores the data and shouldn't care about elapsed time, particularly when you load and save it. The runner on the other end lives during the time of the execution. In fact, when looking at the source of runner I see that it actually has an attibute of start_time.

So, perhaps, I can rephrase my question as a feature request for
adaptive.Runner(learner, max_time=6 * 3600)

@jbweston
Copy link
Contributor

jbweston commented Nov 21, 2019

Logically I think this could be improved - the learner stores the data and shouldn't care about elapsed time, particularly when you load and save it. The runner on the other end lives during the time of the execution. In fact, when looking at the source of runner I see that it actually has an attibute of start_time.

Indeed, the learner should not be storing a start time at all.

I would do the following:

learner = ...

def stop_after(n_seconds):
    start_time = time.time()
    return lambda _: time.time() > start_time + n_seconds

runner = Runner(learner, goal=stop_after(600))

We can provide stop_after as a utility; I will open an MR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants