def add_p(self, p, v): self.size += 1 nhood, center = self._nhood(p) for n_p in nhood: d = toolbox.dist(p, self.res * np.array(n_p)) w = toolbox.gaussian_kernel(d, self.sigma * self.sigma) #print w self.nodes[n_p] = self.nodes.get(n_p, 0.0) * (1 - w) + w * v
def add_p(self, p, v): self.size += 1 nhood, center = self._nhood(p) for n_p in nhood: d = toolbox.dist(p, self.res*np.array(n_p)) w = toolbox.gaussian_kernel(d, self.sigma*self.sigma) #print w self.nodes[n_p] = self.nodes.get(n_p, 0.0)*(1-w) + w*v
def compute_individual_fitness(self): """ Compute the fitness of the given individual. """ X = toolbox.cut_c(self._get_id(),config.N) X_dec = toolbox.bin2dec_tab(X) dist = [] for pos in range (0,len(X_dec),2): dist.append(toolbox.dist(X_dec[pos], X_dec[pos+1], EXPECTED_NUMBER[pos], EXPECTED_NUMBER[pos+1])) return numpy.mean(dist)
def _mode_goal(self): # choose a past goal if len(self.goals_interest) == 0: assert len(self.effects_interest) > 0 return self._mode_effect() else: areas, inte = zip(*[(a, i) for a, i in self.goals_interest.nodes.iteritems()]) idx = toolbox.fun.roulette_wheel(inte) ref_goal = self.goals_interest.res * np.array(areas[idx]) # generate a random goal i = 0 while (i < 100): i += 1 gen_goal = ref_goal + spread*self.R*np.array([random.uniform(-1, 1) for _ in self.Sfeats]) nn_effect = self.effects.get(self.effects.nn(gen_goal, 1)[1][0]) if True or toolbox.dist(gen_goal, nn_effect) < spread*self.R: return pandas.Series(gen_goal, index = self.Sfeats) raise ValueError("Too much tries")
def _meshgrid(self, res=None): """Return a meshgrid of resolution res.""" res = res or self.res heatmap = np.zeros((res + 2 * margin, res + 2 * margin)) weightmap = np.zeros((res + 2 * margin, res + 2 * margin)) # print list(self.self.dataset.iter_x()) if self.extent is not None: xmin, xmax, ymin, ymax = self.extent bounds0 = xmin, xmax bounds1 = ymin, ymax else: bounds0 = min((y[0] for y in self.dataset.iter_x())), max( (y[0] for y in self.dataset.iter_x())) bounds1 = min((y[1] for y in self.dataset.iter_x())), max( (y[1] for y in self.dataset.iter_x())) for y, c in self.dataset.iter_xy(): center = (margin - 1 + (y[0] - bounds0[0]) / (max(1.0, bounds0[1] - bounds0[0])) * res, margin - 1 + (y[1] - bounds1[0]) / (max(1.0, bounds1[1] - bounds1[0])) * res) neighborhood = [] for i in range(-4, 5): for j in range(-4, 5): neighborhood.append( (int(center[0]) + i, int(center[1]) + j)) for ng in neighborhood: d = toolbox.dist(ng, center) w = math.exp(-d * d / 4) heatmap[-ng[1]][ng[0]] += w * c weightmap[-ng[1]][ng[0]] += w for i in range(res + 2 * margin): for j in range(res + 2 * margin): w = weightmap[i][j] if w > 0: heatmap[i][j] /= 1 + w return heatmap, (bounds0, bounds1)
def _mode_goal(self): # choose a past goal if len(self.goals_interest) == 0: assert len(self.effects_interest) > 0 return self._mode_effect() else: areas, inte = zip( *[(a, i) for a, i in self.goals_interest.nodes.iteritems()]) idx = toolbox.fun.roulette_wheel(inte) ref_goal = self.goals_interest.res * np.array(areas[idx]) # generate a random goal i = 0 while (i < 100): i += 1 gen_goal = ref_goal + spread * self.R * np.array( [random.uniform(-1, 1) for _ in self.Sfeats]) nn_effect = self.effects.get( self.effects.nn(gen_goal, 1)[1][0]) if True or toolbox.dist(gen_goal, nn_effect) < spread * self.R: return pandas.Series(gen_goal, index=self.Sfeats) raise ValueError("Too much tries")
def _meshgrid(self, res = None): """Return a meshgrid of resolution res.""" res = res or self.res heatmap = np.zeros((res+2*margin, res+2*margin)) weightmap = np.zeros((res+2*margin, res+2*margin)) # print list(self.self.dataset.iter_x()) if self.extent is not None: xmin, xmax, ymin, ymax = self.extent bounds0 = xmin, xmax bounds1 = ymin, ymax else: bounds0 = min((y[0] for y in self.dataset.iter_x())), max((y[0] for y in self.dataset.iter_x())) bounds1 = min((y[1] for y in self.dataset.iter_x())), max((y[1] for y in self.dataset.iter_x())) for y, c in self.dataset.iter_xy(): center = (margin - 1 + (y[0]-bounds0[0])/(max(1.0, bounds0[1]-bounds0[0]))*res, margin - 1 + (y[1]-bounds1[0])/(max(1.0, bounds1[1]-bounds1[0]))*res) neighborhood = [] for i in range(-4, 5): for j in range(-4, 5): neighborhood.append((int(center[0])+i, int(center[1])+j)) for ng in neighborhood: d = toolbox.dist(ng, center) w = math.exp(-d*d/4) heatmap[-ng[1]][ng[0]] += w*c weightmap[-ng[1]][ng[0]] += w for i in range(res+2*margin): for j in range(res+2*margin): w = weightmap[i][j] if w > 0: heatmap[i][j] /= 1+w return heatmap, (bounds0, bounds1)
def competence_ident(a, b, min_d): """Return the competence as d = b-a""" d = toolbox.dist(a, b) if d <= min_d: d = 0.0 return -d
def competence_log(a, b, min_d, beta): """Return the competence as -log(d+1)""" d = toolbox.dist(a, b) if d <= min_d: d = 0.0 return -math.log((beta + d) / beta)