Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if saved_params != params:
self.logger.error(
f"Saved params {saved_params} differ from specified ones {params}; using saved"
)
# TODO could check if bounds match as well
params = saved_params
saved_solver_epsilon = info['solver_epsilon']
if saved_solver_epsilon != solver_epsilon:
self.logger.error(
f"Saved solver_epsilon {saved_solver_epsilon} differ from"
" specified one {solver_epsilon}, using specified")
# solver_epsilon = saved_solver_epsilon
_, self.pp = compare_pps(info['pp'], self.pp)
spec = dlib.function_spec(
bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
optimizer = dlib.global_function_search(
[spec],
initial_function_evals=[evals],
relative_noise_magnitude=info['relative_noise_magnitude'])
self.logger.error(f"Restored {len(evals)} trials, prev best: "
f"{prev_best[0]}@{list(zip(saved_params, prev_best[1:]))}")
except FileNotFoundError:
spec = dlib.function_spec(
bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
optimizer = dlib.global_function_search(spec)
optimizer.set_relative_noise_magnitude(relative_noise_magnitude)
optimizer.set_solver_epsilon(solver_epsilon)
result_queue = Queue()
simproc = partial(dlib_proc, self.stratclass, self.pp, params, result_queue,
self.pp['avg_runs'])
evals = [None] * n_sims
f"Saved solver_epsilon {saved_solver_epsilon} differ from"
" specified one {solver_epsilon}, using specified")
# solver_epsilon = saved_solver_epsilon
_, self.pp = compare_pps(info['pp'], self.pp)
spec = dlib.function_spec(
bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
optimizer = dlib.global_function_search(
[spec],
initial_function_evals=[evals],
relative_noise_magnitude=info['relative_noise_magnitude'])
self.logger.error(f"Restored {len(evals)} trials, prev best: "
f"{prev_best[0]}@{list(zip(saved_params, prev_best[1:]))}")
except FileNotFoundError:
spec = dlib.function_spec(
bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
optimizer = dlib.global_function_search(spec)
optimizer.set_relative_noise_magnitude(relative_noise_magnitude)
optimizer.set_solver_epsilon(solver_epsilon)
result_queue = Queue()
simproc = partial(dlib_proc, self.stratclass, self.pp, params, result_queue,
self.pp['avg_runs'])
evals = [None] * n_sims
def save():
finished_evals = optimizer.get_function_evaluations()[1][0]
dlib_save(spec, finished_evals, params, solver_epsilon,
relative_noise_magnitude, self.pp, fname)
self.logger.error("Saved progress")
def quit_opt():
# Store results of finished evals to file; print best eval
f"Params {params} and spec {raw_spec} are of different length"
)
eps = _cmp_and_choose('solver_epsilon', info['solver_epsilon'], eps)
noise_mag = _cmp_and_choose(
'relative_noise_magnitude', info['relative_noise_magnitude'],
noise_mag
)
_, pp = _compare_pps(info['pp'], pp)
except FileNotFoundError:
# Create a new file
pass
eps = 0.0005 if eps is None else eps
noise_mag = 0.001 if noise_mag is None else noise_mag
spec = dlib.function_spec(bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
if old_evals:
optimizer = dlib.global_function_search(
[spec],
initial_function_evals=[old_evals],
relative_noise_magnitude=noise_mag
)
else:
optimizer = dlib.global_function_search(
[spec]
)
optimizer.set_relative_noise_magnitude(noise_mag)
optimizer.set_solver_epsilon(eps)
self.pp, self.params, self.optimizer, self.spec = pp, params, optimizer, spec
self.eps, self.noise_mag, self.is_int = eps, noise_mag, is_int
self.fname, self.save = fname, save
# Restore saved params and settings if they differ from current/specified
if params != saved_params:
logger.error(
f"Saved params {saved_params} differ from currently specified "
f"{params}. Using saved.")
params = saved_params
raw_spec = cmp_and_choose('bounds', old_raw_spec,
(is_int, lo_bounds, hi_bounds))
spec = dlib.function_spec(
bound1=raw_spec[1], bound2=raw_spec[2], is_integer=raw_spec[0])
eps = cmp_and_choose('solver_epsilon', info['solver_epsilon'], eps)
noise_mag = cmp_and_choose('relative_noise_magnitude',
info['relative_noise_magnitude'], noise_mag)
_, pp = compare_pps(info['pp'], pp)
optimizer = dlib.global_function_search(
[spec],
initial_function_evals=[old_evals],
relative_noise_magnitude=noise_mag)
except FileNotFoundError:
spec = dlib.function_spec(
bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
optimizer = dlib.global_function_search(spec)
optimizer.set_relative_noise_magnitude(noise_mag)
optimizer.set_solver_epsilon(eps)
# Becomes populated with results as simulations finished
result_queue = Queue()
simproc = partial(dlib_proc, self.stratclass, pp, params, result_queue)
# Becomes populated with evaluation objects to be set later
evals = [None] * n_sims
# Becomes populates with losses. When n_avg losses for a particular
# set of params are ready, their mean is set for the correponding eval.
)
_, pp = _compare_pps(info['pp'], pp)
except FileNotFoundError:
# Create a new file
pass
eps = 0.0005 if eps is None else eps
noise_mag = 0.001 if noise_mag is None else noise_mag
spec = dlib.function_spec(bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
if old_evals:
optimizer = dlib.global_function_search(
[spec],
initial_function_evals=[old_evals],
relative_noise_magnitude=noise_mag
)
else:
optimizer = dlib.global_function_search(
[spec]
)
optimizer.set_relative_noise_magnitude(noise_mag)
optimizer.set_solver_epsilon(eps)
self.pp, self.params, self.optimizer, self.spec = pp, params, optimizer, spec
self.eps, self.noise_mag, self.is_int = eps, noise_mag, is_int
self.fname, self.save = fname, save
raw_spec = cmp_and_choose('bounds', old_raw_spec,
(is_int, lo_bounds, hi_bounds))
spec = dlib.function_spec(
bound1=raw_spec[1], bound2=raw_spec[2], is_integer=raw_spec[0])
eps = cmp_and_choose('solver_epsilon', info['solver_epsilon'], eps)
noise_mag = cmp_and_choose('relative_noise_magnitude',
info['relative_noise_magnitude'], noise_mag)
_, pp = compare_pps(info['pp'], pp)
optimizer = dlib.global_function_search(
[spec],
initial_function_evals=[old_evals],
relative_noise_magnitude=noise_mag)
except FileNotFoundError:
spec = dlib.function_spec(
bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
optimizer = dlib.global_function_search(spec)
optimizer.set_relative_noise_magnitude(noise_mag)
optimizer.set_solver_epsilon(eps)
# Becomes populated with results as simulations finished
result_queue = Queue()
simproc = partial(dlib_proc, self.stratclass, pp, params, result_queue)
# Becomes populated with evaluation objects to be set later
evals = [None] * n_sims
# Becomes populates with losses. When n_avg losses for a particular
# set of params are ready, their mean is set for the correponding eval.
results = [[] for _ in range(n_sims)]
def save_evals():
"""Store results of finished evals to file; print best eval"""
finished_evals = optimizer.get_function_evaluations()[1][0]
dlib_save(spec, finished_evals, params, eps, noise_mag, pp, fname)
best_eval = optimizer.get_best_function_eval()