Commit f7a31627 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg

things

parent 2ffa0431
import multiprocessing
import operator
import random
from functools import partial
import numpy as np
from deap import base, creator, gp, tools, algorithms
# import logic
from constants import Move
from constants import Move, GRID_LEN
from game import Game
from player import GPPlayer
def progn(*args):
for arg in args:
arg()
def prog2(out1, out2):
return partial(progn, out1, out2)
def prog3(out1, out2, out3):
return partial(progn, out1, out2, out3)
from player import GPPlayer, GridItem
def if_then_else(condition, out1, out2):
......@@ -30,7 +15,7 @@ def if_then_else(condition, out1, out2):
# class GamePlayer:
# game = Game()
# game = Game()Ò
# def _reset(self):
# self.game = Game()
......@@ -65,31 +50,55 @@ def if_then_else(condition, out1, out2):
# * Get position of highest tile
# * (integer) Constants
def grid_equals(g1, g2):
return g1.value == g2.value
def grid_lt(g1, g2):
return g1.value < g2.value
def grid_gt(g1, g2):
return g1.value > g2.value
pset = gp.PrimitiveSetTyped("main", [int] * 16, str)
def is_neighbour(g1, g2):
return g1.location == (g2.location - 1) or g1.location == (
g2.location + 1) or g1.location == (
g2.location - GRID_LEN) or g1.location == (
g2.location + GRID_LEN)
def grid_to_grid(input):
return input
pset = gp.PrimitiveSetTyped("main", [GridItem] * (GRID_LEN * GRID_LEN), str)
pset.addTerminal(Move.Left, str, name='left')
pset.addTerminal(Move.Up, str, name='up')
pset.addTerminal(Move.Right, str, name='right')
pset.addTerminal(Move.Down, str, name='down')
pset.addTerminal(True, bool, name='bool_true')
pset.addTerminal(1, bool, name='bool_true')
pset.addTerminal(12, int, name='value0')
pset.addPrimitive(grid_to_grid, [GridItem], GridItem, name='grid_item')
pset.addPrimitive(if_then_else, [bool, str, str], str, name='if_then_else')
pset.addPrimitive(operator.eq, [int, int], bool, name='eq')
pset.addPrimitive(operator.lt, [int, int], bool, name='lt')
pset.addPrimitive(operator.gt, [int, int], bool, name='gt')
pset.addPrimitive(grid_equals, [GridItem, GridItem], bool, name='eq')
pset.addPrimitive(grid_lt, [GridItem, GridItem], bool, name='lt')
pset.addPrimitive(grid_gt, [GridItem, GridItem], bool, name='gt')
pset.addPrimitive(is_neighbour, [GridItem, GridItem], bool, 'is_neighbour')
GAMES_PER_INDIVIDUAL = 11
def evaluateIndividual(individual):
# print(individual)
# print('\n')
fn = gp.compile(individual, pset)
player = GPPlayer(fn)
game = Game()
scores = np.array([game.play_game(player) for i in range(GAMES_PER_INDIVIDUAL)]).transpose()
scores = np.array([game.play_game(player) for i in
range(GAMES_PER_INDIVIDUAL)]).transpose()
ret_val = (np.median(scores[0]), # total sum of tiles
np.max(scores[1]), # max tile
......@@ -104,14 +113,13 @@ creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("expr_init", gp.genFull, pset=pset, min_=2, max_=10)
toolbox.register("expr_init", gp.genFull, pset=pset, min_=8, max_=11)
# Structure initializers
toolbox.register("individual", tools.initIterate, creator.Individual,
toolbox.expr_init)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evaluateIndividual)
toolbox.register("select", tools.selTournament, tournsize=7)
toolbox.register("mate", gp.cxOnePoint)
......@@ -153,10 +161,24 @@ def main():
stats.register("std", stats_std)
stats.register("avg", stats_avg)
algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=400, stats=stats, halloffame=hof)
algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40,
stats=stats, halloffame=hof)
# import pygraphviz as pgv
#
# g = pgv.AGraph()
# g.add_nodes_from(nodes)
# g.add_edges_from(edges)
# g.layout(prog="dot")
#
# for i in nodes:
# n = g.get_node(i)
# n.attr["label"] = labels[i]
#
# g.draw("tree.pdf")
return pop, hof, stats
if __name__ == "__main__":
main()
\ No newline at end of file
main()
from constants import Move
from constants import Move, GRID_LEN
class GridItem:
location = None
value = None
def __init__(self, location, value) -> None:
super().__init__()
self.location = location
self.value = value
class Player(object):
def __init__(self):
pass
def play(self, game):
raise NotImplementedError('Implement this in a subclass of Player!')
......@@ -27,5 +37,6 @@ class GPPlayer(object):
def play(self, game):
game = [cell for row in game for cell in row]
args = {"ARG" + str(i): game[i] for i in range(16)}
args = {"ARG" + str(i): GridItem(i, game[i])
for i in range(GRID_LEN * GRID_LEN)}
return self.individual_fn(**args)
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