Source code for physbo.search.discrete._history

# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2020- The University of Tokyo
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import numpy as np
import copy

from .. import utility

MAX_SEARCH = int(30000)


[docs] class History: def __init__(self): self.num_runs = int(0) self.total_num_search = int(0) self.fx = np.zeros(MAX_SEARCH, dtype=float) self.chosen_actions = np.zeros(MAX_SEARCH, dtype=int) self.terminal_num_run = np.zeros(MAX_SEARCH, dtype=int) self.time_total_ = np.zeros(MAX_SEARCH, dtype=float) self.time_update_predictor_ = np.zeros(MAX_SEARCH, dtype=float) self.time_get_action_ = np.zeros(MAX_SEARCH, dtype=float) self.time_run_simulator_ = np.zeros(MAX_SEARCH, dtype=float) @property def time_total(self): return copy.copy(self.time_total_[0 : self.num_runs]) @property def time_update_predictor(self): return copy.copy(self.time_update_predictor_[0 : self.num_runs]) @property def time_get_action(self): return copy.copy(self.time_get_action_[0 : self.num_runs]) @property def time_run_simulator(self): return copy.copy(self.time_run_simulator_[0 : self.num_runs]) @property def valid_mask(self): """ Mask of valid observations (True) vs failed ones (False). An observation is failed when its objective value is not finite (NaN or +-Inf). Failed observations are kept in the history but excluded from the training data and the best-value tracking. Returns ------- numpy.ndarray of bool, shape (total_num_search,) """ return utility.finite_mask(self.fx[0 : self.total_num_search])
[docs] def export_valid(self): """ Export the valid (successfully evaluated) observations. Returns ------- actions: numpy.ndarray Indexes of the actions of the valid observations. fx: numpy.ndarray Objective values of the valid observations. """ N = self.total_num_search mask = self.valid_mask return self.chosen_actions[0:N][mask], self.fx[0:N][mask]
[docs] def write( self, t, action, time_total=None, time_update_predictor=None, time_get_action=None, time_run_simulator=None, ): """ Overwrite fx and chosen_actions by t and action. Parameters ---------- t: numpy.ndarray N dimensional array. The negative energy of each search candidate (value of the objective function to be optimized). action: numpy.ndarray N dimensional array. The indexes of actions of each search candidate. time_total: numpy.ndarray N dimenstional array. The total elapsed time in each step. If None (default), filled by 0.0. time_update_predictor: numpy.ndarray N dimenstional array. The elapsed time for updating predictor (e.g., learning hyperparemters) in each step. If None (default), filled by 0.0. time_get_action: numpy.ndarray N dimenstional array. The elapsed time for getting next action in each step. If None (default), filled by 0.0. time_run_simulator: numpy.ndarray N dimenstional array. The elapsed time for running the simulator in each step. If None (default), filled by 0.0. Returns ------- """ N = utility.length_vector(action) st = self.total_num_search en = st + N t_shape = t.shape if t_shape[0] != N: raise ValueError(f"Number of actions and t must be the same: {N} != {t_shape[0]}") if t_shape[1] != 1: raise ValueError(f"t.shape[1] must be 1: {t_shape}") self.terminal_num_run[self.num_runs] = en self.fx[st:en] = t[:, 0] self.chosen_actions[st:en] = action self.num_runs += 1 self.total_num_search += N if time_total is None: time_total = np.zeros(N, dtype=float) self.time_total_[st:en] = time_total if time_update_predictor is None: time_update_predictor = np.zeros(N, dtype=float) self.time_update_predictor_[st:en] = time_update_predictor if time_get_action is None: time_get_action = np.zeros(N, dtype=float) self.time_get_action_[st:en] = time_get_action if time_run_simulator is None: time_run_simulator = np.zeros(N, dtype=float) self.time_run_simulator_[st:en] = time_run_simulator
[docs] def export_sequence_best_fx(self): """ Export fx and actions at each sequence. (The total number of data is num_runs.) Returns ------- best_fx: numpy.ndarray best_actions: numpy.ndarray """ all_best_fx, all_best_actions = self.export_all_sequence_best_fx() best_fx = np.zeros(self.num_runs, dtype=float) best_actions = np.zeros(self.num_runs, dtype=int) for n in range(self.num_runs): index = self.terminal_num_run[n] - 1 best_fx[n] = all_best_fx[index] best_actions[n] = all_best_actions[index] return best_fx, best_actions
[docs] def export_all_sequence_best_fx(self): """ Export all fx and actions at each sequence. (The total number of data is total_num_research.) Returns ------- best_fx: numpy.ndarray best_actions: numpy.ndarray """ # Failed observations (non-finite fx) are skipped. Until the first # valid observation, best_fx is NaN and best_actions is -1. best_fx = np.full(self.total_num_search, np.nan, dtype=float) best_actions = np.full(self.total_num_search, -1, dtype=int) for n in range(self.total_num_search): if n > 0: best_fx[n] = best_fx[n - 1] best_actions[n] = best_actions[n - 1] fx = self.fx[n] if np.isfinite(fx) and (np.isnan(best_fx[n]) or best_fx[n] < fx): best_fx[n] = fx best_actions[n] = self.chosen_actions[n] return best_fx, best_actions
[docs] def save(self, filename): """ Save the information of the history. Parameters ---------- filename: str The name of the file which stores the information of the history Returns ------- """ N = self.total_num_search M = self.num_runs np.savez_compressed( filename, num_runs=M, total_num_search=N, fx=self.fx[0:N], chosen_actions=self.chosen_actions[0:N], terminal_num_run=self.terminal_num_run[0:M], )
[docs] def load(self, filename): """ Load the information of the history. Parameters ---------- filename: str The name of the file which stores the information of the history Returns ------- """ data = np.load(filename) M = int(data["num_runs"]) N = int(data["total_num_search"]) self.num_runs = M self.total_num_search = N self.fx[0:N] = data["fx"] self.chosen_actions[0:N] = data["chosen_actions"] self.terminal_num_run[0:M] = data["terminal_num_run"]
[docs] def show_search_results(self, N): n = self.total_num_search fx = self.fx[0:n] valid = np.isfinite(fx) if np.any(valid): # failed observations are skipped index = np.argmax(np.where(valid, fx, -np.inf)) best_msg = "current best f(x) = %f (best action=%d)" % ( fx[index], self.chosen_actions[index], ) else: best_msg = "current best f(x) = (no valid observation yet)" if N == 1: print( "%04d-th step: f(x) = %f (action=%d)" % (n, self.fx[n - 1], self.chosen_actions[n - 1]) ) print(" " + best_msg + " \n") else: print(best_msg + " ") print("list of simulation results") st = self.total_num_search - N en = self.total_num_search for n in range(st, en): print("f(x)=%f (action = %d)" % (self.fx[n], self.chosen_actions[n])) print("\n")