@@ -85,8 +85,71 @@ def save_experiment(
8585
8686 Returns:
8787 PKL_NAME (str):
88- Name of the pickle file. Build as "spot_" + PREFIX + "experiment .pickle".
88+ Name of the pickle file. Build as "spot_" + PREFIX + "_experiment .pickle".
8989
90+ Examples:
91+ >>> import os
92+ from spotPython.utils.file import save_experiment, load_experiment
93+ import numpy as np
94+ from math import inf
95+ from spotPython.spot import spot
96+ from spotPython.utils.init import (
97+ fun_control_init,
98+ design_control_init,
99+ surrogate_control_init,
100+ optimizer_control_init)
101+ from spotPython.fun.objectivefunctions import analytical
102+ fun = analytical().fun_branin
103+ fun_control = fun_control_init(
104+ PREFIX="branin",
105+ SUMMARY_WRITER=False,
106+ lower = np.array([0, 0]),
107+ upper = np.array([10, 10]),
108+ fun_evals=8,
109+ fun_repeats=1,
110+ max_time=inf,
111+ noise=False,
112+ tolerance_x=0,
113+ ocba_delta=0,
114+ var_type=["num", "num"],
115+ infill_criterion="ei",
116+ n_points=1,
117+ seed=123,
118+ log_level=20,
119+ show_models=False,
120+ show_progress=True)
121+ design_control = design_control_init(
122+ init_size=5,
123+ repeats=1)
124+ surrogate_control = surrogate_control_init(
125+ model_fun_evals=10000,
126+ min_theta=-3,
127+ max_theta=3,
128+ n_theta=2,
129+ theta_init_zero=True,
130+ n_p=1,
131+ optim_p=False,
132+ var_type=["num", "num"],
133+ seed=124)
134+ optimizer_control = optimizer_control_init(
135+ max_iter=1000,
136+ seed=125)
137+ spot_tuner = spot.Spot(fun=fun,
138+ fun_control=fun_control,
139+ design_control=design_control,
140+ surrogate_control=surrogate_control,
141+ optimizer_control=optimizer_control)
142+ # Call the save_experiment function
143+ pkl_name = save_experiment(
144+ spot_tuner=spot_tuner,
145+ fun_control=fun_control,
146+ design_control=None,
147+ surrogate_control=None,
148+ optimizer_control=None
149+ )
150+ # Call the load_experiment function
151+ (spot_tuner_1, fun_control_1, design_control_1,
152+ surrogate_control_1, optimizer_control_1) = load_experiment(pkl_name)
90153 """
91154 if design_control is None :
92155 design_control = design_control_init ()
@@ -106,8 +169,11 @@ def save_experiment(
106169 "surrogate_control" : surrogate_control ,
107170 "optimizer_control" : optimizer_control ,
108171 }
172+ # check if the key "spot_writer" is in the fun_control dictionary
173+ if "spot_writer" in fun_control and fun_control ["spot_writer" ] is not None :
174+ fun_control ["spot_writer" ].close ()
109175 PREFIX = fun_control ["PREFIX" ]
110- PKL_NAME = "spot_" + PREFIX + "experiment .pickle"
176+ PKL_NAME = "spot_" + PREFIX + "_experiment .pickle"
111177 with open (PKL_NAME , "wb" ) as handle :
112178 pickle .dump (experiment , handle , protocol = pickle .HIGHEST_PROTOCOL )
113179 print (f"Experiment saved as { PKL_NAME } " )
@@ -136,6 +202,4 @@ def load_experiment(PKL_NAME):
136202 design_control = experiment ["design_control" ]
137203 surrogate_control = experiment ["surrogate_control" ]
138204 optimizer_control = experiment ["optimizer_control" ]
139- # TODO: Add the key "spot_writer" to the fun_control dictionary,
140- # because it was not saved in the pickle file.
141205 return spot_tuner , fun_control , design_control , surrogate_control , optimizer_control
0 commit comments