Commit 41458bdf authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg


parent 9ec3a513
......@@ -30,14 +30,12 @@ class Game:
return logic.game_state(self.matrix) == logic.STATE_LOSE
def highest_tile(self):
location = (-1, -1)
score = 0
for row in self.matrix:
for col in self.matrix:
if self.matrix[row][col] > score:
score = self.matrix[row][col]
location = (row, col)
return score, location
return score
def _process_move(self, action):
if logic.game_state(self.matrix) == logic.STATE_PROGRESS:
from deap import base, creator, gp, tools
import operator
import random
from functools import partial
pset = gp.PrimitiveSet("MAIN", 1)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
import numpy
from deap import base, creator, gp, tools, algorithms
creator.create("Individual", gp.PrimitiveTree)
from game import Game
def progn(*args):
for arg in args:
def prog2(out1, out2):
return partial(progn, out1, out2)
def prog3(out1, out2, out3):
return partial(progn, out1, out2, out3)
def if_then_else(condition, out1, out2):
out1() if condition() else out2()
class GamePlayer:
game = Game()
def _reset(self): = Game()
def fitness(self):
return sum( + 9 *
def if_lost(self, out1, out2):
return partial(if_then_else,, out1, out2)
def up(self):
def down(self):
def left(self):
def right(self):
def play(self, routine):
while not
player = GamePlayer()
pset = gp.PrimitiveSet("main", 0)
pset.addTerminal(player.up, name='up')
pset.addTerminal(player.down, name='down')
pset.addTerminal(player.left, name='left')
pset.addTerminal(player.right, name='right')
pset.addPrimitive(player.if_lost, 2)
pset.addPrimitive(prog2, 2)
pset.addPrimitive(prog3, 3)
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
expr = toolbox.individual()
nodes, edges, labels = gp.graph(expr)
# Attribute generator
toolbox.register("expr_init", gp.genFull, pset=pset, min_=1, max_=2)
# Structure initializers
toolbox.register("individual", tools.initIterate, creator.Individual,
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalArtificialAnt(individual):
# Transform the tree expression to functionnal Python code
routine = gp.compile(individual, pset)
# Run the generated routine
toolbox.register("evaluate", evalArtificialAnt)
toolbox.register("select", tools.selTournament, tournsize=7)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
def main():
pop = toolbox.population(n=300)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind:
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, 40, stats, halloffame=hof)
return pop, hof, stats
if __name__ == "__main__":
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment