Exemple #1
0
    def __init__(self, actor_id, args):
        self.tetris = TetrisApp(emulator=True)

        self.legal_actions = [0, 1, 2, 3, 4]
        self.screen_width, self.screen_height = 288, 396
        self.lives = 1

        self.random_start = args.random_start
        self.single_life_episodes = args.single_life_episodes
        self.call_on_new_frame = args.visualize
        self.global_step = 0

        self.compteur = 0

        # Processed historcal frames that will be fed in to the network
        # (i.e., four 84x84 images)
        self.rgb = args.rgb
        self.depth = 1
        if self.rgb: self.depth = 3
        self.rgb_screen = np.zeros((self.screen_height, self.screen_width, 3),
                                   dtype=np.uint8)
        self.gray_screen = np.zeros((self.screen_height, self.screen_width, 1),
                                    dtype=np.uint8)
        self.frame_pool = FramePool(
            np.empty((2, self.screen_height, self.screen_width, self.depth),
                     dtype=np.uint8), self.__process_frame_pool)
        self.observation_pool = ObservationPool(
            np.zeros((IMG_SIZE_X, IMG_SIZE_Y, self.depth, NR_IMAGES),
                     dtype=np.uint8), self.rgb)
	def __init__(self):
		self.app = TetrisApp(self)
		self.ai = AI(self.app)
		self.app.ai = self.ai
		self.population = [self.random_chromosome() for _ in range(POPULATION_SIZE)]
		self.current_chromosome = 0
		self.current_generation = 1
		self.ai.heuristics = self.population[self.current_chromosome].heuristics
Exemple #3
0
 def __init__(self):
     self.num_of_organisms = POPSIZE
     self.survivors = ELITE
     self.new_organisms = self.num_of_organisms - self.survivors
     self.mutation_rate = MUTRATE
     self.crossover_rate = CROSSRATE
     #initialize the population
     self.population = self.InitPop(self.num_of_organisms)
     #keep track of which organism in the population we are working on
     self.current_organism = 0
     #keeps track of what generation we are on
     self.current_generation = 0
     #GA gets the application
     self.sequenceType = SEQUENCE
     self.seed = numpy.random.random()
     self.app = TetrisApp(self)
     #GA gets our agent, which needs the organism
     #so it can access weights of the organism
     self.ai = Agent(self.app)
     self.app.ai = self.ai
     self.cycleStart = 0
     self.cycleEnd = 0
     self.fitnessDictionary = {}
     self.lastBest = []
Exemple #4
0
    # Setup to grab statistics
    #   - Max, mean, min, std, variance
    # Setup containers for highest score and highest scoring individual
    # Create a Genetic Algorithm loop
    #   - This loop will achieve the following:
    #       a. Select and clone the next generation individuals
    #       b. Apply crossover and mutation on the offspring
    #       c. Replace population with offspring
    #------------------------------------------------------------
    n_gen = 50
    # n_gen = 100
    prob_xover = 0.3
    prob_mut = 0.05
    pop = toolbox.population(n=25)
    # pop = toolbox.population(n=1000)
    game = TetrisApp(training=True)
    best_ind = []
    best_score = -1

    max_out  = open("max50.txt", "w")
    mean_out = open("mean50.txt", "w")
    min_out  = open("min50.txt", "w")
    std_out  = open("std50.txt", "w")
    var_out  = open("var50.txt", "w")

    for g in range(1, n_gen + 1):
        print("Current Generation " + str(g))
        scores = []
        for ind in pop:
            score = game.run_train(ind)
            scores.append(score)
Exemple #5
0
    def run_game(self, player, seed=10000, debug=False):

        App = TetrisApp(player, seed=seed, debug=debug)
        score = App.run()
        return score
Exemple #6
0
def dqn():
    env = TetrisApp(8, 16, 750, False, 40, 30 * 100)
    episodes = 5000
    max_steps = None
    epsilon_stop_episode = 1500
    mem_size = 20000
    discount = 0.95
    batch_size = 512
    epochs = 1
    render_every = 50
    log_every = 50
    replay_start_size = 2000
    train_every = 1
    n_neurons = [32, 32]
    render_delay = None
    activations = ['relu', 'relu', 'linear']

    agent = DQNAgent(env.get_state_size(),
                     n_neurons=n_neurons,
                     activations=activations,
                     epsilon_stop_episode=epsilon_stop_episode,
                     mem_size=mem_size,
                     discount=discount,
                     replay_start_size=replay_start_size)

    # log_dir = f'logs/tetris-nn={str(n_neurons)}-mem={mem_size}-bs={batch_size}-e={epochs}-{datetime.now().strftime("%Y%m%d-%H%M%S")}'
    # log = CustomTensorBoard(log_dir=log_dir)

    scores = []
    env.pcrun()
    for episode in tqdm(range(episodes)):
        env.reset()
        current_state = env._get_board_props(env.board)
        done = False
        steps = 0

        if render_every and episode % render_every == 0:
            render = True
        else:
            render = False

        # Game
        while not done and (not max_steps or steps < max_steps):
            next_states = env.get_next_states()
            best_state = agent.best_state(next_states.values())

            best_action = None
            for action, state in next_states.items():
                if state == best_state:
                    best_action = action
                    break

            reward, done = env.pcplace(best_action[0], best_action[1])

            agent.add_to_memory(current_state, next_states[best_action],
                                reward, done)
            current_state = next_states[best_action]
            steps += 1

        scores.append(env.get_game_score())

        # Train
        if episode % train_every == 0:
            agent.train(batch_size=batch_size, epochs=epochs)

        # Logs
        # if log_every and episode and episode % log_every == 0:
        #     avg_score = mean(scores[-log_every:])
        #     min_score = min(scores[-log_every:])
        #     max_score = max(scores[-log_every:])

        #     log.log(episode, avg_score=avg_score, min_score=min_score,
        #             max_score=max_score)
    plt.xlabel("Episodes")
    plt.ylabel('Average score over 30 episodes')
    plt.grid()
    plt.plot(np.linspace(30, episodes, episodes - 29),
             moving_average(scores, 30))
    plt.savefig("nlinker.png")
Exemple #7
0
    within the `models` directory. An example is given as `ai_rando`.",
    )
    parser.add_argument(
        "-d",
        "--debug",
        action="store_true",
        help="Enable debug mode: The Tetris Engine will wait until input \
                is received from your Model before updating the frame")
    parser.add_argument(
        '-s',
        '--seed',
        dest='seed',
        default=69,
        help=
        'The seed for the random number generator. Affects block generation')
    args = parser.parse_args()
    args.seed = int(args.seed)

    if args.model:

        model = import_player(args.model)
        # Let the games begin
        App = TetrisApp(model, debug=args.debug, seed=args.seed)

    if args.arena:
        players = args.arena
        player_models = [import_player(player) for player in players]
        Arena(player_models, debug=args.debug).run_round_robin(seed=args.seed)

    print("Nothing else to do.")
Exemple #8
0
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0],
[1,1,1,1,1,1,1,0,0,0],
[1,1,1,1,1,1,1,1,1,1]],

]

app = TetrisApp()
ai = TetrisAI(app)

# test indavidual function used for score moves
class TestEval(unittest.TestCase):

    def test_get_max_height(self):
      heights = []
      for board in test_boards:
        heights.append(ai.get_max_height(board))
      self.assertEqual(heights, [0,5,5,2]) 

    def test_get_roughness(self):
      roughs = []
      for board in test_boards:
        roughs.append(ai.get_roughness(board))
Exemple #9
0
def fitness(individual, seeds, pieceLimit):
    results = []
    for seed in seeds:
        results.append(TetrisApp(False, seed).run(indiv, pieceLimit))
    return int(sum(results)/len(results))