BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
573 stars 102 forks source link

Provide context manager for auto-cleanup() #112

Open LordFckHelmchen opened 3 years ago

LordFckHelmchen commented 3 years ago

The problem

After a local solver run, I understand that it might make sense to cleanup() the gekko files and the application (temp) directory. This always requires an explicit call to the according function. If forgotten, the files remain there.

Users

Normal users that just want to have a solution and don't care about the files.

The solution

Provide a context manager for the GEKKO class. For example

@contextmanager
def managed_gekko_solver(*args, **kwargs):
    """
    Context manager for the GEKKO class, to automatically clean-up temp. files & folders after solving.
    """
    solver = GEKKO(*args, **kwargs)
    try:
        yield solver
    finally:
        solver.cleanup()