def create_reduced_dataset(individual, pset, X): exp = gp.PrimitiveTree(individual) string = str(exp) ind = [i for i in range(len(string)) if string.startswith('F', i)] if len(ind) == 0: ind = [0] features = [] hist = [] temp = [] for i in ind: subtree = fitness.get_subtree(i, string) if str(subtree) not in hist: hist.append(str(subtree)) newtree = exp.from_string(subtree, pset) temp.append(str(newtree)) features.append(gp.compile(newtree, pset)) if len(features) == 0: features.append(gp.compile(individual, pset)) X_new = [] i = 0 for x in X: X_new.append([]) for feature, t in zip(features, temp): X_new[i].append(feature(*x)) i += 1 return X_new
def run_game(individual): global snake routine = gp.compile(individual, pset) snake._reset() food = place_food() timer = 0 steps = 0 while not snake.snake_has_collided() and not timer == XSIZE * YSIZE: routine() snake.update_position() steps += 1 if snake.body[0] in food: snake.score += 1 food = place_food() timer = 0 else: snake.body.pop() timer += 1 # time steps since last eaten if snake.snake_has_collided() and snake.score == 0: return -20, steps if timer == XSIZE * YSIZE: return -10, -10 return math.pow(snake.score, 2), steps
def evalCube(individual): #print(individual) moves = gp.compile(individual, pset) testcube.run(moves) fit = testcube.fitness() #print("fit:"+str(fit)+'\n') return fit,
def test_example(self, ex, optimal=False, cats=1): print("\nExample:") print(ex) target_test = regex.compile(self.regex_target).findall(ex['string']) print("Target:") print(target_test) if optimal: expr = self.gen_best_individual(ex) else: expr = self.generate_individual_from_example(ex, self.PATTERN1, collapse=False, cats=cats) if len(expr) > 0: tree = gp.PrimitiveTree(expr) ts = gp.compile(tree, self.pset).s indr = regex.compile(ts) print(indr) test = indr.findall(ex['string']) print(test) print(f'Expression Length: {len(ts)}') else: print("No match found.")
def displayStrategyRun(individual): global snake global pset routine = gp.compile(individual, pset) curses.initscr() win = curses.newwin(YSIZE, XSIZE, 0, 0) win.keypad(1) curses.noecho() curses.curs_set(0) win.border(0) win.nodelay(1) win.timeout(120) snake._reset() food = placeFood(snake) for f in food: win.addch(f[0], f[1], '@') timer = 0 collided = False while not collided and not timer == ((2 * XSIZE) * YSIZE): # Set up the display win.border(0) win.addstr(0, 2, 'Score : ' + str(snake.score) + ' ') win.getch() ## EXECUTE THE SNAKE'S BEHAVIOUR HERE ## routine() snake.updatePosition() if snake.body[0] in food: snake.score += 1 for f in food: win.addch(f[0], f[1], ' ') food = placeFood(snake) for f in food: win.addch(f[0], f[1], '@') timer = 0 else: last = snake.body.pop() win.addch(last[0], last[1], ' ') timer += 1 # timesteps since last eaten win.addch(snake.body[0][0], snake.body[0][1], 'o') collided = snake.snakeHasCollided() hitBounds = (timer == ((2 * XSIZE) * YSIZE)) curses.endwin() print collided print hitBounds print snake.score return snake.score,
def selSelfPlay(individuals, k): """Play k matches where cons individual play against each other, select winners of matches""" chosen = [] for i in range(k): aspirants = tools.selection.selRandom(individuals, len(agents)) for agent, aspirant in zip(agents,aspirants): logic = gp.compile(aspirant, pset) agent.set_agent_logic(logic) for coro in coros: loop.run_forever() for agent, aspirant in zip(agents, aspirants): try: aspirant.won = aspirant.won + agent.won aspirant.played = aspirant.played + 1 except AttributeError: aspirant.won = int(agent.won) aspirant.played = 1 winner = max(zip(agents,aspirants), key=lambda a: a[0].won) if not winner[0].won: winners = sorted(zip(agents,aspirants), key=lambda a: a[0].tiles_claimed, reverse=True) if winners[0][0].tiles_claimed > winners[1][0].tiles_claimed: winner = winners[0] else: winners = [winner for winner in winners if winner[0].tiles_claimed >= winners[0][0].tiles_claimed] winner = min(winners, key=lambda a: a[1].height) chosen.append(winner[1]) return chosen
def evaluate( individual: Union["gp.creator.IndividualMin", "gp.creator.IndividualMax"] ) -> pd.Series: """ Genetic program's evaluation function. Simplifies and scores factor/factor-interaction presence. Compiles gp tree representation into flattened str format. Writes rule to new NetLogo model. Simulates new NetLogo model and records fitness. Cleans up auto-generated NetLogo model. :param individual: Union['gp.creator.IndividualMin', 'gp.creator.IndividualMax'] gp individual :return: pd.Series containing presence scores, fitness, and compiled rule of executed gp individual """ ind_record = score_factor_presence(individual, MODEL_FACTORS) newRule = str( gp.compile(individual, MODEL_FACTORS.get_DEAP_primitive_set()) ) newModelPath = NETLOGO_WRITER.inject_new_rule(newRule) fitness = simulate( newModelPath, MODEL_INIT_DATA["setup_commands"], MODEL_INIT_DATA["measurement_commands"], MODEL_INIT_DATA["ticks_to_run"], MODEL_INIT_DATA["go_command"], MODEL_INIT_DATA["agg_func"], ) remove_model(newModelPath) ind_record["Fitness"] = fitness ind_record["Rule"] = newRule[:-1] ind_record = pd.Series(list(ind_record.values()), index=ind_record.keys()) return ind_record
def fit(self, indiv, num_eps, num_steps, render=False): fitness = 0.0 net_cost = 0.0 # Create a program template that splits the Pendulum environment # in 4 quadrants and injects the programs in the blanks. template = "IFLTE(sintheta, 0.0,\ IFLTE(costheta, 0.0, neg(1.0), {}),\ IFLTE(costheta, 0.0, 1.0, {}))".format(*indiv) executable = gp.compile(template, self.pset) # Run individual on the environment to compute its fitness env = gym.make(self.env_name) for _ in range(num_eps): obs = env.reset() for _ in range(num_steps): if render: env.render() time.sleep(0.02) action = executable(obs[0], obs[1], obs[2]) obs, cost, _, _ = env.step([action]) net_cost += cost env.close() fitness = net_cost/num_eps return np.round(fitness, decimals=5),
def mse_helper(self, ind, X, y): """ Given an X and a y returns the mse of a given ind """ self.func = gp.compile(ind, self.pset) mse = self._mse(self.func, X, y) return (mse, )
def fit(self, indiv, num_eps, num_steps, render=False): fitness = 0.0 net_reward = 0.0 env = gym.make(self.env_name) executable = gp.compile(indiv, self.pset) for _ in range(num_eps): obs = env.reset() done = False while not done: if render: env.render() # time.sleep(0.02) action_obj = executable(obs[0], obs[1], obs[2], obs[3], obs[4], obs[5], obs[6], obs[7], obs[8], obs[9], obs[10], obs[11], obs[12], obs[13], obs[14], obs[15], obs[16], obs[17], obs[18], obs[19], obs[20], obs[21], obs[22], obs[23]) action = action_obj.get_actions() obs, reward, done, _ = env.step(action) net_reward += reward env.close() fitness = net_reward / num_eps return np.round(fitness, decimals=5),
def runGameFib(individual): global snake global pset routine = gp.compile(individual, pset) snake._reset() food = placeFood(snake) timer = 0 while not snake.snakeHasCollided() and not timer == XSIZE * YSIZE: ## EXECUTE THE SNAKE'S BEHAVIOUR HERE ## routine() snake.updatePosition() if snake.body[0] in food: snake.score = 1 if snake.score == 0 else snake.score + ( snake.score + 1) food = placeFood(snake) timer = 0 else: snake.body.pop() timer += 1 # timesteps since last eaten #punish snake if it goes into a wall, this should always be avoided if snake.is_wall(snake.body[0]): return 0, return snake.score,
def run_game(individual): global snake routine = gp.compile(individual, pset) snake._reset() food = place_food() timer = 0 steps = 0 while not snake.snake_has_collided() and not timer == GRID_SIZE: routine() snake.update_position() steps += 1 if snake.body[0] in food: snake.score += 1 food = place_food() timer = 0 else: snake.body.pop() timer += 1 # time steps since last eaten if timer == GRID_SIZE: return -10 if snake.score == 0: return -10 maximum_possible_coverage = snake.score * GRID_SIZE return steps / maximum_possible_coverage
def get_best(self, fits, pset, k): new = [] new_ins = [] new_str = [] new_score = [] count = 0 print("begin get best") for i in range(1, len(fits)): individual = fits[-i][0] print(i) print("count {}".format(count)) print(str(individual)) if str(individual) not in new_str: func = gp.compile(individual, pset) new_tmp = func(**self.feature_dict) if self.valid_new(new_tmp, new): new.append(new_tmp) new_str.append(str(individual)) new_score.append(fits[-i][1]) new_ins.append(individual) count += 1 if count>=k: break print("new_str") print(new) print(new_str) print(new_score) return new, new_ins
def add_func_helper(self, ind, X, y): """ Given an X and a y returns the mse of a given ind """ self.func = gp.compile(ind, self.pset) a = self.add_func(self, X, y) return (a, )
def runGame(individual): global snake global pset routine = gp.compile(individual, pset) totalScore = 0 snake._reset() food = placeFood(snake) timer = 0 steps = 0 while not snake.snakeHasCollided() and not timer == XSIZE * YSIZE: ## EXECUTE THE SNAKE'S BEHAVIOUR HERE ## routine() snake.updatePosition() if snake.body[0] in food: snake.score += 1 food = placeFood(snake) timer = 0 else: snake.body.pop() timer += 1 # timesteps since last eaten steps += 1 return snake.score, steps
def runGame(individual): global snake individual.food_eaten = 0 totalScore = 0 snake._reset() food = placeFood(snake) timer = 0 while not snake.snakeHasCollided() and not timer == XSIZE * YSIZE: ## EXECUTE THE SNAKE'S BEHAVIOUR HERE ## updateAction = gp.compile(individual, pset) updateAction() snake.updatePosition() if snake.body[0] in food: individual.food_eaten += 1 #snake.score += 1 food = placeFood(snake) timer = 0 else: snake.body.pop() timer += 1 # timesteps since last eaten totalScore += snake.score # print("-- timer:", timer) return totalScore,
def runGame(individual): global snake global pset routine = gp.compile(individual, pset) snake._reset() food = placeFood(snake) timer = 0 steps = 0 while not snake.snakeHasCollided() and not timer == XSIZE * YSIZE: ## EXECUTE THE SNAKE'S BEHAVIOUR HERE ## routine() snake.updatePosition() if snake.body[0] in food: snake.score += 1 if not (checkFood(snake)): # Check whether there's any coords free for food print("YOU WIN!!! MAX SCORE: 133 ACHIEVED!!!") return snake.score, steps # If not return; CONGRATS ON MAX SCORE!!! else: # If coords free, then place food food = placeFood(snake) timer = 0 else: snake.body.pop() timer += 1 # timesteps since last eaten steps += 1 return snake.score, steps
def evaluate(self, pop): fitnesses = [] logger.debug(len(pop)) for ind in pop: func = [compile(t, self.pset) for t in ind] fitnesses.append(self.experiment(func)) return dict(fitness=fitnesses)
def fit(self, indiv, num_eps, num_steps, render=False): fitness = 0.0 ep_cost = 0.0 ep_cost_lst = [] env = gym.make(self.env_name) executable = gp.compile(indiv, self.pset) for _ in range(num_eps): obs = env.reset() ep_cost = 0.0 for _ in range(num_steps): if render: env.render() time.sleep(0.02) action = executable(obs[0], obs[1], obs[2]) obs, cost, _, _ = env.step([action]) ep_cost += cost ep_cost_lst.append(ep_cost) env.close() fitness = np.mean(ep_cost_lst) return np.round(fitness, decimals=5),
def eval_helper(self, ind, X, y): """ Given an X and a y returns the mse + addfunc of a given ind """ self.func = gp.compile(ind, self.pset) mse = self._mse(self.func, X, y) a = self.add_func(self, X, y) return (mse + a,)
def _f_eval(self, individual): f = gp.compile(individual, self.primitive_set) Y_res = [] for i in xrange(len(self.X_train)): Y_res.append( eval("f(" + ",".join(map(str, self.X_train[i].tolist())) + ")")) return (abs(mean_squared_error(self.Y_train, Y_res)), )
def eval_symb_reg(individual, points, pset): # Compile the tree into a function func = gp.compile(expr=individual, pset=pset) # Calculate main squared error between this function and the target function sq_errors = (func(points) - (np.negative(points) + np.sin(points**2) + np.tan(points**3) + np.cos(points)))**2 # Set standard dev and length of individual as objectives return np.sqrt(np.sum(sq_errors) / len(points)), len(individual)
def predict(self, X_test): assert self.trained == True f = gp.compile(self.hof[0], self.primitive_set) Y_res = [] for i in xrange(len(X_test)): Y_res.append( eval("f(" + ",".join(map(str, X_test[i].tolist())) + ")")) return Y_res
def evalCharacteristic(individual): ''' This function applies a mapping to the domain of propagations returning the domain of characteristics ''' mapping = gp.compile(individual, pset) domainCharac = domain.evalCharacteristics(mapping) return domainCharac
def evalArtificialSnake(individual): # Transform the tree expression to functionnal Python code routine = gp.compile(individual, pset) # Run the generated routine score = snake.run(routine) #print("height: "+str(individual.height)) #print(score) return score - 0 * (individual.height),
def build_binary_cls_loss(loss_func, loss_weight=1.0): return BinaryClsLoss(compile(loss_func, get_pset(mode='BINARY_CLS', arg_num=4)), use_sigmoid=True, beta=2.0, alpha=0.25, reduction='mean', loss_weight=loss_weight)
def eval_func(individual): global robot, pset # Transform the tree expression to functionnal Python code routine = gp.compile(individual, pset) # Run the generated routine robot.run(routine) return robot.consumed,
def fiteval(individual, treeorret): global k penalty = 0 nodes, edges, evals = gp.graph(individual) print(evals) print(len(evals)) keys = get_key('gt', evals) keys1 = get_key('ls', evals) keys2 = get_key('ls1', evals) keys3 = get_key('gt1', evals) print(keys) print(keys1) f = gp.compile(individual, pset) def f1(row): x = np.array(row) try: return f(*x) except Exception as e: return 0 def pencalc(keys): penalty = 0 for i in keys: subject = [evals[i + 1], evals[i + 2]] print(subject) cond = (operator.or_(*[isinstance(j, float) for j in subject]) and (any(i in pset.arguments for i in subject))) print(cond) if (cond == True): if ((subject[0] in pset.arguments) and isinstance(subject[1], float)): penalty = penalty + 10 penalty = penalty + 10 return penalty presgt = 'gt' not in evals.values() presls = 'ls' not in evals.values() pres = presgt and presls presgt = 'gt1' not in evals.values() presls = 'ls1' not in evals.values() pres1 = presgt and presls print(pres) peninc = -100 if pres else 10 peninc1 = -100 if pres1 else 10 penaltyfin = pencalc(keys) + pencalc(keys1) + pencalc(keys2) + pencalc( keys3) + peninc + peninc1 - len(evals) if (treeorret): k['vals'] = k.drop(['targret', 'Symbol', 'ranchoice'], axis=1).apply(f1, axis=1) retfit = (k.vals * k.targret).sum() - (k.ranchoice * k.targret).sum() k = k.drop('vals', axis=1) return (penaltyfin, retfit) else: return (penaltyfin, )
def _f_eval(self, individual): f = gp.compile(individual, self.primitive_set) err = 0 for i in xrange(len(self.X_train)): if int( eval("f(" + ",".join(map(str, self.X_train[i].tolist())) + ")")) != self.Y_train[i]: err += 1 return (err, )
def restore_ind(self, ind, df): print(str(ind)) func = gp.compile(ind, self.pset) print(str(func)) feature_dict = {} for col in df.columns: feature_dict[col] = df[col].values new_add = func(**feature_dict) return new_add
def display_strategy_run(routine): global snake global pset routine = gp.compile(routine, pset=pset) curses.initscr() win = curses.newwin(YSIZE, XSIZE, 0, 0) win.scrollok(0) win.keypad(1) curses.noecho() curses.curs_set(0) win.border(0) win.nodelay(1) win.timeout(120) snake._reset() food = place_food() for f in food: win.addch(f[0], f[1], '@') timer = 0 while not snake.snake_has_collided() and not timer == ( (2 * XSIZE) * YSIZE): routine() # Set up the display win.border(0) win.addstr(0, 2, 'Score: ' + str(snake.score) + ' ') win.getch() snake.update_position() if snake.body[0] in food: # Set coord to out of the map boundaries snake.score += 1 for f in food: win.addch(f[0], f[1], ' ') food = place_food() for f in food: win.addch(f[0], f[1], '@') timer = 0 else: last = snake.body.pop() win.addch(last[0], last[1], ' ') timer += 1 # time steps since last eaten win.addch(snake.body[0][0], snake.body[0][1], 'o') hitBounds = (timer == ((2 * XSIZE) * YSIZE)) input("Press to continue") curses.endwin() return snake.score
def main(): #testcube.scramble(15) testcube.move_R() testcube.move_U2() testcube.move_Ra() testcube.move_D() testcube.move_b2() testcube.move_Ra() testcube.move_D() testcube.move_R2() testcube.move_b() testcube.move_u() testcube.move_Bb2() testcube.move_U2() testcube.move_Ll2() testcube.move_d2() testcube.move_u2() #print(fitness1(testcube.getFaces())) #testcube.printCube() testcube._store() testcube._restore() pop = toolbox.population(n=80) hof=tools.HallOfFame(2) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("std", np.std) stats.register("min", np.min) stats.register("max", np.max) gen = int(sys.argv[1]) cxpb = float(sys.argv[2]) mutpb = float(sys.argv[3]) print("CXPB = "+str(cxpb)+" MUTPB = "+str(mutpb)+" GEN = "+str(gen)) algorithms.eaSimple(pop, toolbox, cxpb, mutpb, gen, stats, halloffame=hof, verbose=False) print(hof[0]) print(hof[0].fitness) print(hof[1]) print(hof[1].fitness) bestMoves = gp.compile(hof[0],pset) testcube.run(bestMoves) #testcube.printCube() print return pop, stats, hof
def get_best_ant(): with open("map.txt") as trail_file: ant.parse_matrix(trail_file) pop = toolbox.population(n=POPULATION) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("max", numpy.max) algorithms.eaSimple(pop, toolbox, 0.5, 0.2, MAX_GENERATIONS, stats, halloffame=hof) routine = gp.compile(hof[0], pset) return ant, hof[0], routine
def evalMapping(primitiveTree): #evaluate Characteristics try: mapping = gp.compile(primitiveTree, pset) characteristic = domain.evalCharacteristics(mapping) except: mapping = lambda ARG0, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6: np.NaN characteristic = domain.evalCharacteristics(mapping) charac = characteristic['characteristic'] #evaluate domain SD of characteristic normSD = charac.std() #evaluate robustness robustness = charac.std(axis = 1) robustness = np.mean(robustness) #evaluate shift shifts = charac.std(axis = 2) shift = shifts.mean() #evaluate averages domainAv = charac.mean() return characteristic, normSD, robustness, shift, domainAv
def evalArtificialAnt(individual): routine = gp.compile(individual, pset) ant.run(routine) return ant.eaten,
'solidity_Contour(adfContour_CellPositionsMesh(ARG4, ARG6))' goodMappings = ['solidity_Contour(largestContour_Contours(Contours_DensityfloatMesh'] def plotCharac(charac): import matplotlib.pyplot as plt values = charac['characteristic'] for sim in values: plt.figure() for run in sim: plt.plot(run) plt.show() if __name__ == '__main__': pset = loadPset(psetFile) with open(domainFile, 'rb') as f: domain = cPickle.load(f) state = domain[0][0].states[0] primtree = gp.PrimitiveTree([]) ind = primtree.from_string(solFuncStr, pset) characteristic = domain.evalCharacteristics(gp.compile(ind, pset)) seedMaps = [primtree.from_string(str, pset) for str in mapStrs] for str in mapStrs: primtree.from_string(str, pset) with open(seedFile, 'wb') as f: cPickle.dump(seedMaps, f, -1)
def evalArtificialAnt(individual): # Transform the tree expression to functionnal Python code routine = gp.compile(individual, pset) # Run the generated routine ant.run(routine) return ant.eaten,
def evaluate(ind): com = gp.compile(expr=ind, pset=pset) d = com(1) return d.d["x"] - d.d["y"]