Logging runs
Logging is useful for long or unattended batches. MIMOSA records the solver status, solve time, save
messages, warnings and errors through Python's logging system. The following example appends these
messages to mainlog.log:
import logging
import logging.handlers
from mimosa import MIMOSA, load_params
handler = logging.handlers.WatchedFileHandler("mainlog.log")
handler.setFormatter(
logging.Formatter("[%(levelname)s, %(asctime)s] %(name)s - %(message)s")
)
root = logging.getLogger()
root.setLevel("INFO")
root.addHandler(handler)
params = load_params()
# Make changes to the params if needed
params["emissions"]["carbonbudget"] = False
model1 = MIMOSA(params)
model1.solve(verbose=False) # (1)!
model1.save("run1_logged")
verbose=Falsehides IPOPT's iteration output from the terminal. MIMOSA still writes its termination status and solve time to the configured Python log.
The model output is saved separately as output/run1_logged.csv with its parameter file. The log
does not replace the model output and does not currently include the final objective value.
Points to keep in mind
mainlog.logis created relative to the directory from which the script is run.WatchedFileHandlerappends to an existing log, so repeated runs retain earlier messages.- Running the setup code repeatedly in the same interactive Python session can attach the same handler more than once and duplicate messages. Configure logging once per process.
verbose=Falsesuppresses detailed IPOPT iterations; it does not redirect them into the Python log. To retain a separate IPOPT log, passipopt_output_file="ipopt.log"tosolve().