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

Delay after optimize #36

Open
benbrastmckie opened this issue Jul 30, 2024 · 0 comments
Open

Delay after optimize #36

benbrastmckie opened this issue Jul 30, 2024 · 0 comments

Comments

@benbrastmckie
Copy link
Owner

I have added the following code in order to show the progress being made when running the model-checker:

def progress_bar(max_time, stop_event):
    """Show progress bar for how much of max_time has elapsed."""
    step_time = max_time / 100
    with tqdm(
        desc="Running model-checker: ",
        total=100,
        unit="step",
        bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}",
    ) as pbar:
        while not stop_event.is_set():
            time.sleep(step_time) # NOTE: gives bad result if multiplied by 4
            pbar.update(1)
            if pbar.n >= 100:
                stop_event.set()  # Signal the progress bar to stop

def create_model_setup(module):
    """Creates a model setup based on module attributes."""
    return make_model_for(
        module.N,
        module.premises,
        module.conclusions,
        module.max_time,
        module.contingent_bool,
        module.disjoint_bool,
    )

def handle_timeout(module, model_setup):
    """Handles timeout scenarios by asking the user for a new time limit."""
    print(f"The model timed out at {model_setup.model_runtime} seconds.")
    new_max_time = ask_time(model_setup.model_runtime, model_setup.max_time)
    if new_max_time is None:
        print("Terminating the process.")
        os._exit(1)
    module.update_max_time(new_max_time)

def optimize_model_setup(module, optimize_model):
    """Runs make_model_for on the values provided by the module and user, optimizing if required."""
    max_time = module.max_time
    stop_event = Event()
    progress_thread = Thread(target=progress_bar, args=(max_time, stop_event))
    progress_thread.start()

    model_setup = None
    try:
        model_setup = create_model_setup(module)
        run_time = model_setup.model_runtime
        if run_time > max_time:
            handle_timeout(module, model_setup)
            module, model_setup = optimize_model_setup(module, model_setup)
        if optimize_model:
            module, model_setup = optimize_N(module, model_setup, module, model_setup)

    finally:
        stop_event.set()  # Signal the progress bar to stop
        progress_thread.join(timeout=max_time)  # Wait for the thread to finish

    return module, model_setup

It seems to work well in all circumstances, however, when optimize_bool = True there is a delay after the bar completes. I haven't been able to figure out how to include whatever processing it is doing during that time in the completion of the bar. I also noticed that there are some strange results when the step_time in time.sleep(step_time) is multiplied by 4. I'm not sure what to make of either of these results.

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