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

Restarting from Checkpoint, multiple runs, always reverts back to first checkpoint. #574

Open
corsair20141 opened this issue Mar 6, 2024 · 0 comments

Comments

@corsair20141
Copy link

corsair20141 commented Mar 6, 2024

Hello, I am trying to create multiple checkpoints, esentially in a loop to ask the user if they would like to iterate 2 more generations or be done with the initial optimization run. However, if I try and continue the loop twice, the second time (and subsequent times) just restarts from the original checkpoint n_gen rather than the latest checkpoint n_gen.

How can I fix this? Is it a bug in Pymoo or an error on my part?

 res = minimize(problem,
             algorithm,
             termination,
             #seed=1,
             copy_algorithm=False,
             save_history=True,
             verbose=True) 

if os.path.exists("checkpoint"): # remove "checkpoint" file if already exists, before creating new one
    os.remove("checkpoint")
with open("checkpoint", "wb") as f: # create checkpoint file
    dill.dump(algorithm, f)
    
elapsed_time = time.time() - start_time
print(f"Optimization runtime: {elapsed_time/60:.2f} min")

# Offer to run n_gen_continue more generations
while True:
    n_gen_continue = 2
    
    selec1 = input(f"\n\n>>> Continue for another {n_gen_continue} generations? (y/n): ")
    if selec1 == "Y" or selec1 == "y": # then continue optimizaiton for another "n_gen_continue" generations
        print("\nContinuing...")
        with open("checkpoint", 'rb') as f:
            checkpoint = dill.load(f)
            print("> Loaded Checkpoint:", checkpoint)
            print("\n\n")
        start_time = time.time()
     
        checkpoint.termination = MaximumGenerationTermination(checkpoint.n_gen + n_gen_continue)
        res = minimize(problem,
                    checkpoint,
                    #seed=1,
                    copy_algorithm=True,
                    save_history=True,
                    verbose=True) 
        # if os.path.exists("checkpoint"): # remove "checkpoint" file if already exists, before creating new one
        #     os.remove("checkpoint")
        with open("checkpoint", "wb") as f: # create checkpoint
            dill.dump(algorithm, f)
                
        elapsed_time = time.time() - start_time
        print(f"Optimization runtime: {elapsed_time/60:.2f} min")
        
    elif selec1 == "N" or selec1 == "n": # then don't continue optimization
        break
    
    else:
        print("***Invalid selection ***")
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