def __init__(self, conf, expl_dims, competence_measure, win_size, competence_mode, k, progress_mode): RandomInterest.__init__(self, conf, expl_dims) self.competence_measure = competence_measure self.win_size = win_size self.competence_mode = competence_mode self.dist_max = np.linalg.norm(self.bounds[0, :] - self.bounds[1, :]) self.k = k self.progress_mode = progress_mode self.data_xc = Dataset(len(expl_dims), 1) self.data_sr = Dataset(len(expl_dims), 0) self.current_progress = 0. self.current_interest = 0.
def __init__(self, conf, expl_dims, competence_measure, win_size, competence_mode, k, progress_mode): InterestModel.__init__(self, expl_dims) self.bounds = conf.bounds[:, expl_dims] self.ndims = self.bounds.shape[1] self.competence_measure = competence_measure self.win_size = win_size self.competence_mode = competence_mode # self.dist_max = np.linalg.norm(self.bounds[0, :] - self.bounds[1, :]) self.dist_max = 2 self.k = k self.progress_mode = progress_mode self.data_xc = Dataset(len(expl_dims), 1) self.data_sr = Dataset(len(expl_dims), 0) self.current_progress = 1e-5 self.current_interest = 1e-5
def __init__(self, dim_x, dim_y, **kwargs): """Create the forward model @param dim_x the input dimension @param dim_y the output dimension """ self.dim_x = dim_x self.dim_y = dim_y self.dataset = Dataset(dim_x, dim_y) self.conf = kwargs
def __init__(self, conf, expl_dims, x_card, cells_win_size, eps_random, measure, win_size, competence_measure, competence_mode, k, progress_mode): DiscretizedProgress.__init__(self, conf, expl_dims, x_card, cells_win_size, eps_random, measure) self.bounds = conf.bounds[:, expl_dims] self.ndims = self.bounds.shape[1] self.competence_measure = competence_measure self.win_size = win_size self.competence_mode = competence_mode # self.dist_max_comp = np.linalg.norm(self.bounds[0, :] - self.bounds[1, :]) self.dist_max_comp = 2 self.k = k self.progress_mode = progress_mode self.data_xc = Dataset(len(expl_dims), 1) self.data_sr = Dataset(len(expl_dims), 0) self.current_progress = 1e-5 self.current_interest = 1e-5
def __init__(self, conf, acquisition, exploration_weight, initial_points, environment, optimisation_iterations, exact_feval): ''' :param string acquisition: choose the model of acquisition function between "MPI","EI" and "LCB" :param scalar exploration_weight: module the exploration in the acquistion (base 2 for LCB and 0.01 for others) :param scalar initial_points: the number of initial points to give for the bayesian optimisation :pram Environment environment: environment on which is used the optimisation :param scalar optimisation_iterations: number of iterations of the optimisation :param boolean exact_feval: must be False if the environment is noisy ''' for attr in ['m_dims', 's_dims']: setattr(self, attr, getattr(conf, attr)) self.dim_x = len(self.m_dims) self.dim_y = len(self.s_dims) self.acquisition = acquisition self.exploration_weight = exploration_weight self.initial_points = initial_points self.dataset = Dataset(len(self.m_dims), len(self.s_dims)) self.conf = conf self.mode = 'explore' self.environment = environment self.optimisation_iterations = optimisation_iterations self.exact_feval = exact_feval
class MiscRandomInterest(RandomInterest): """ Add some features to the RandomInterest random babbling class. Allows to query the recent interest in the whole space, the recent competence on the babbled points in the whole space, the competence around a given point based on a mean of the knns. """ def __init__(self, conf, expl_dims, win_size, competence_mode, k, progress_mode): RandomInterest.__init__(self, conf, expl_dims) self.conf = conf self.win_size = win_size self.competence_mode = competence_mode self.dist_max = np.linalg.norm(self.bounds[0, :] - self.bounds[1, :]) self.k = k self.progress_mode = progress_mode self.data_xc = Dataset(len(expl_dims), 1) self.data_sr = Dataset(len(expl_dims), 0) self.current_progress = 0. self.current_interest = 0. def save(self): return [self.data_xc.data, self.data_sr.data] def forward(self, data, iteration, progress, interest): self.data_xc.add_xy_batch(data[0][0][:iteration], data[0][1][:iteration]) self.data_sr.data[0] = self.data_sr.data[0] + data[1][0][:iteration] self.data_sr.size += len(data[1][0][:iteration]) self.current_progress = progress self.current_interest = interest def competence_measure(self, sg, s, dist_max): return competence_dist(sg, s, dist_max=dist_max) / dist_max # Normalize #return competence_dist(sg, s, dist_max=dist_max) def add_xc(self, x, c): self.data_xc.add_xy(x, [c]) def add_sr(self, x): self.data_sr.add_xy(x) def update_interest(self, i): i = i / len(self.conf.s_dims) self.current_progress += (1. / self.win_size) * (i - self.current_progress) self.current_interest = abs(self.current_progress) def update(self, xy, ms, snnp=None, sp=None): c = self.competence_measure(xy[self.expl_dims], ms[self.expl_dims], dist_max=self.dist_max) if self.progress_mode == 'local': interest = self.interest_xc(xy[self.expl_dims], c) self.update_interest(interest) elif self.progress_mode == 'global': pass self.add_xc(xy[self.expl_dims], c) self.add_sr(ms[self.expl_dims]) return interest def n_points(self): return len(self.data_xc) def competence_global(self, mode='sw'): if self.n_points() > 0: if mode == 'all': return np.mean(self.data_c) elif mode == 'sw': idxs = range(self.n_points())[-self.win_size:] return np.mean([self.data_xc.get_y(idx) for idx in idxs]) else: raise NotImplementedError else: return 0. def mean_competence_pt(self, x): if self.n_points() > self.k: _, idxs = self.data_xc.nn_x(x, k=self.k) return np.mean([self.data_xc.get_y(idx) for idx in idxs]) else: return self.competence() def interest_xc(self, x, c): if self.n_points() > 0: idx_sg_NN = self.data_xc.nn_x(x, k=1)[1][0] sr_NN = self.data_sr.get_x(idx_sg_NN) c_old = self.competence_measure(x, sr_NN, self.dist_max) return c - c_old else: return 0. def interest_pt(self, x): if self.n_points() > self.k: _, idxs = self.data_xc.nn_x(x, k=self.k) idxs = sorted(idxs) v = [self.data_xc.get_y(idx) for idx in idxs] n = len(v) comp_beg = np.mean(v[:int(float(n) / 2.)]) comp_end = np.mean(v[int(float(n) / 2.):]) return np.abs(comp_end - comp_beg) else: return self.interest_global() def interest_global(self): if self.n_points() < 2: return 0. else: idxs = range(self.n_points())[-self.win_size:] v = [self.data_xc.get_y(idx) for idx in idxs] n = len(v) comp_beg = np.mean(v[:int(float(n) / 2.)]) comp_end = np.mean(v[int(float(n) / 2.):]) return np.abs(comp_end - comp_beg) def competence(self): return self.competence_global() def progress(self): return self.current_progress def interest(self): if self.progress_mode == 'local': return self.current_interest elif self.progress_mode == 'global': return self.interest_global() else: raise NotImplementedError
class MiscRandomInterest(RandomInterest): """ Add some features to the RandomInterest random babbling class. Allows to query the recent interest in the whole space, the recent competence on the babbled points in the whole space, the competence around a given point based on a mean of the knns. """ def __init__(self, conf, expl_dims, win_size, competence_mode, k, progress_mode): RandomInterest.__init__(self, conf, expl_dims) self.win_size = win_size self.competence_mode = competence_mode self.dist_max = np.linalg.norm(self.bounds[0, :] - self.bounds[1, :]) self.k = k self.progress_mode = progress_mode self.data_xc = Dataset(len(expl_dims), 0) self.data_sr = Dataset(len(expl_dims), 0) self.data_sp = Dataset(len(expl_dims), 0) self.current_competence_progress = 0. self.current_prediction_progress = 0. self.current_progress = 0. self.current_interest = 0. self.alpha = 0.5 self.beta = 1. - self.alpha def save(self): return [self.data_xc.data, self.data_sr.data] def forward(self, data, iteration, progress, interest): self.data_xc.add_xy_batch(data[0][0][:iteration], data[0][1][:iteration]) self.data_sr.data[0] = self.data_sr.data[0] + data[1][0][:iteration] self.data_sr.size += len(data[1][0][:iteration]) self.current_progress = progress self.current_interest = interest def competence_measure(self, sg, s, dist_max): return competence_dist(sg, s, dist_max=dist_max) def add_xc(self, x): self.data_xc.add_xy(x) def add_sr(self, x): self.data_sr.add_xy(x) def add_sp(self, x): self.data_sp.add_xy(x) def update_interest(self, cp, pp): self.current_competence_progress += (1. / self.win_size) * ( cp - self.current_competence_progress) self.current_prediction_progress += (1. / self.win_size) * ( pp - self.current_prediction_progress) self.current_progress = self.alpha * self.current_competence_progress + self.beta * self.current_prediction_progress self.current_interest = self.alpha * abs( self.current_competence_progress) + self.beta * abs( self.current_prediction_progress) def update(self, xy, ms, sp=None): if sp is not None: c = self.competence_measure(xy[self.expl_dims], ms[self.expl_dims], dist_max=self.dist_max) p = self.competence_measure(sp, ms[self.expl_dims], dist_max=self.dist_max) cp = self.new_competence_progress(xy[self.expl_dims], c) pp = self.new_prediction_progress(ms[self.expl_dims], p) self.update_interest(cp, pp) self.add_xc(xy[self.expl_dims]) self.add_sr(ms[self.expl_dims]) self.add_sp(sp) def n_points(self): return len(self.data_xc) def new_competence_progress(self, x, c): if self.n_points() > 0: idx_sg_NN = self.data_xc.nn_x(x, k=1)[1][0] sr_NN = self.data_sr.get_x(idx_sg_NN) c_old = self.competence_measure(x, sr_NN, self.dist_max) return c - c_old else: return 0. def new_prediction_progress(self, sr, p): if self.n_points() > 0: idx_sr_NN = self.data_sr.nn_x(sr, k=1)[1][0] sp_NN = self.data_sp.get_x(idx_sr_NN) p_old = self.competence_measure(sr, sp_NN, self.dist_max) return p - p_old else: return 0. def interest_pt(self, x): if self.n_points() > self.k: _, idxs = self.data_xc.nn_x(x, k=self.k) idxs = sorted(idxs) v = [self.data_xc.get_y(idx) for idx in idxs] n = len(v) comp_beg = np.mean(v[:int(float(n) / 2.)]) comp_end = np.mean(v[int(float(n) / 2.):]) return np.abs(comp_end - comp_beg) else: return 0. def progress(self): return self.current_progress def interest(self): return self.current_interest