Example #1
0
	def test_counting_4_center_cell_4_neighbours(self):
		world = World(3, 2)
		world.set([
			[Cell(1), Cell(1), Cell(1)],
			[Cell(1), Cell(1), Cell(0)]
		])
		self.assertEqual(4, world.get_neighbourhoods()[0][1])
Example #2
0
	def test_counting_0_corner_cell_neighbours(self):
		world = World(2, 2)
		world.set([
			[Cell(0), Cell(0)],
			[Cell(0), Cell(0)]
		])
		self.assertEqual(0, world.get_neighbourhoods()[0][0])
Example #3
0
	def test_that_death_cell_lives_with_3_alive_neighbours(self):
		world = World(2, 2)
		world.set([
			[Cell(0), Cell(1)],
			[Cell(1), Cell(1)]
		])
		world.evolve()
		self.assertEqual(1, world.world[0][0].status)
Example #4
0
	def test_that_cell_keeps_death_4_alive_neighbours(self):
		world = World(3, 2)
		world.set([
			[Cell(1), Cell(0), Cell(1)],
			[Cell(1), Cell(1), Cell(0)]
		])
		world.evolve()
		self.assertEqual(0, world.world[0][1].status)
Example #5
0
    def test_cell_dies(self):
        world = World(set([(1, 1)]))
        self.assertFalse(world.cell_lives((1, 1)))

        world.cells.add((1, 2))
        self.assertFalse(world.cell_lives((1, 1)))

        world.cells.add((2, 2))
        self.assertTrue(world.cell_lives((1, 1)))
Example #6
0
def main():
    world = World(
        Grid(3, [[False, False, False], [True, True, True],
                 [False, False, False]]))
    for i in range(100):
        world.tick()
    for event in world.history:
        for row in event.display():
            print(" ".join(row))
        print("\n")
Example #7
0
 def test_tick_with_three_grid_with_horizontal_bar(self, three_grid_with_horizontal_bar):
     w = World(three_grid_with_horizontal_bar)
     w.tick()
     assert w.history[0] == three_grid_with_horizontal_bar
     expected_future = [
         [Cell(False), Cell(True), Cell(False)],
         [Cell(False), Cell(True), Cell(False)],
         [Cell(False), Cell(True), Cell(False)]
     ]
     for y in range(2):
         for x in range(2):
             assert w.now.diagram[y][x].alive == expected_future[y][x].alive
Example #8
0
class GameOfLife(Widget):
	
	def create_world(self):
		self.world = World(80, 60)
		self.world.generate()
		self.paint()

	def update(self, dt):
		self.world.evolve()
		self.paint()

	def paint(self):
		self.canvas.clear()
		with self.canvas:
			for x in range(self.world.width):
				for y in range(self.world.height):
					if self.world.world[y][x].status == 1:
						Rectangle(pos=(x*10, y*10), size=(10, 10))
Example #9
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('players', nargs='+')
    parser.add_argument('--seed', type=int, default=None)
    parser.add_argument('-o',
                        '--output',
                        type=argparse.FileType('w'),
                        default=sys.stdout)
    parser.add_argument('-m',
                        '--map',
                        choices=['empty', 'blocks', 'cave'],
                        default='empty')
    parser.add_argument('--long', action='store_true')
    args = parser.parse_args()

    bots = []
    for player in args.players:
        with open(player, 'r') as f:
            code = f.read().strip()
            parsed = []
            for i in range(0, len(code), 4):
                if i + 4 <= len(code):
                    parsed.append(int(code[i:i + 4], 16))
            bots.append(parsed)

    map_generator = map.Map if args.map == 'empty' \
        else map.RocksMap if args.map == 'blocks' \
        else map.CaveMap
    world = World(bots, map_generator, args.seed)

    if args.long:
        World.ENERGY_PER_TURN = 1000
        World.STARTING_ENERGY = 1000000

    meta = {
        'players': args.players,
        'seed': args.seed if args.seed is not None else -1,
        'map': args.map
    }

    args.output.write(world.run_with_log(1000, meta))
Example #10
0
def do_action(action):
    s = World.player
    r = -World.score
    if action == actions[0]:
        World.try_move(0, -1)
    elif action == actions[1]:
        World.try_move(0, 1)
    elif action == actions[2]:
        World.try_move(-1, 0)
    elif action == actions[3]:
        World.try_move(1, 0)
    else:
        return
    s2 = World.player
    r += World.score
    return s, action, r, s2
Example #11
0
def startWorld(hosting):
    name, ip, port = nameField.get(), ipField.get(), int(portField.get())
    startFrame.destroy()

    log = open(
        time.strftime('LOG-' + ('server' if hosting else 'client') +
                      '-%Y.%m.%d.%H.%M.%S.txt'), 'w')
    try:
        World(hosting, name, ip, port, log=log)
    except:
        print 'Unexpected Error', sys.exc_info()
        log.write('#ERROR:' + str(sys.exc_info()) + '/n')
        log.flush()
        sys.exit()
    log.flush()
Example #12
0
    def __init__(self,
                 num_states=(2 * 4) * (2 * 4) * 2,
                 num_actions=5,
                 alpha=1.0,
                 alpha_decay=0.99996,
                 gamma=0.9,
                 epsilon=1.0,
                 epsilon_decay=0.99996,
                 max_steps_per_episode=1000,
                 max_num_episodes=1000000,
                 save_model_per=1000000,
                 verbose=False):

        self.world = World()
        self.game = Play_game()

        # inputs
        self.alpha = alpha
        self.alpha_decay = alpha_decay
        self.gamma = gamma
        self.epsilon = epsilon
        self.epsilon_decay = epsilon_decay
        self.max_steps_per_episode = max_steps_per_episode
        self.max_num_episodes = max_num_episodes
        self.save_model_per = save_model_per

        # initialize
        self.num_states = num_states  # 4x2 grid, two players, with or without ball
        self.num_actions = num_actions

        self.q_table = np.full(shape=(num_states, num_actions), fill_value=1.0)
        self.q_tables = {
            'A': deepcopy(self.q_table),
            'B': deepcopy(self.q_table)
        }

        self.state = {}
        self.actions = {
            'A': 0,
            'B': 0
        }  # map N, S, E, W, and stick to [0,1,2,3,4]

        # error
        self.ERRs = []
        self.steps_to_plot = []

        self.verbose = verbose
Example #13
0
class GameHandler(EventHandler):
    def __init__(self):
        """
        init the world
        """
        self.world = World()
        self.world.draw()

    def on_key_down(self, key):
        """
        on key down handler
        """

    def on_key_pressed(self, keys):
        self.world.main_character.downward = keys[pygame.K_DOWN] or keys[
            pygame.K_s]
        self.world.main_character.upward = keys[pygame.K_UP] or keys[
            pygame.K_w]
        self.world.main_character.rightward = keys[pygame.K_RIGHT] or keys[
            pygame.K_d]
        self.world.main_character.leftward = keys[pygame.K_LEFT] or keys[
            pygame.K_a]
        return keys[pygame.K_DOWN] or keys[pygame.K_s] or keys[
            pygame.K_UP] or keys[pygame.K_w] or keys[pygame.K_RIGHT] or keys[
                pygame.K_d] or keys[pygame.K_LEFT] or keys[pygame.K_a]

    def on_mouse_click(self, *point):
        """
        on mouse click handler
        """

    def update(self, changed):
        """
        update world handler
        """
        self.world.update()
        self.world.draw()
Example #14
0
actions = World.actions
states = []
Q = {}
E = {}
for i in range(World.x):
    for j in range(World.y):
        states.append((i, j))

for state in states:
    temp = {}
    temp_e = {}
    for action in actions:
        temp[action] = 0.0  # Set to 0.1 if following greedy policy
        temp_e[action] = 0.0
        World.set_cell_score(state, action, temp[action])
    Q[state] = temp
    E[state] = temp_e

for (i, j, c, w) in World.specials:
    for action in actions:
        Q[(i, j)][action] = w
        World.set_cell_score((i, j), action, w)


def do_action(action):
    s = World.player
    r = -World.score
    if action == actions[0]:
        World.try_move(0, -1)
    elif action == actions[1]:
Example #15
0
 def test_can_set_initial_state(self):
     seed = set([(1, 2), (2, 3), (5, 7)])
     world = World(seed)
     self.assertTrue(world.cell_at((1, 2)))
     self.assertFalse(world.cell_at((1, 7)))
Example #16
0
lives_text = BitmapText( font_2, [Game.screen_width - 200, 50] )
lives_text.setSurface( screen )

gameover_text = BitmapText( font_1, [(Game.screen_width / 2)-300, 100] )
gameover_text.setSurface( screen )

dead_text = BitmapText( font_2, [(Game.screen_width / 2)-50, 100] )
dead_text.setSurface( screen )

restart_text = BitmapText( font_2, [(Game.screen_width / 2)-200, 200] )
restart_text.setSurface( screen )

Game.level = 1

world = World( )
player = Player( )

pygame.mouse.set_visible( False )

# Define core colours
black = ( 0, 0, 0 )
white = ( 255, 255, 255 )
red   = ( 255, 0, 0 )
green = ( 0, 255, 0 )
blue  = ( 0, 0, 255 )

# Create clock
clock = pygame.time.Clock( )

# Main Program Loop flag
Example #17
0
	def test_that_worlds_get_created_with_expected_size(self):
		world = World(10, 10)
		world.generate()
		self.assertEqual(10, len(world.world))
		self.assertEqual(10, len(world.world[0]))
Example #18
0
 def __init__(self):
     """
     init the world
     """
     self.world = World()
     self.world.draw()
Example #19
0
def run():
    global discount
    global epsilon
    global alpha
    global log
    score = 0
    time.sleep(1)
    s1 = World.player
    a1, q_val1 = policy(s1)
    for episode_num in range(40):
        steps = 0
        score = 0
        while not World.has_restarted():
            # Do the action
            (s1, a1, r1, s2) = do_action(a1)
            score += r1

            # Update Q
            a2, q_val2 = policy(
                s2)  # Change to max_Q(s2) if following Greedy policy
            a_best, q_best = max_Q(s2)
            delta = r1 + discount * q_best - Q[s1][a1]
            E[s1][a1] = 1

            for state in states:
                for action in actions:
                    inc_Q(state, action, alpha, delta)
                    if a_best == a2:
                        E[state][action] *= discount * e_decay
                    else:
                        E[state][action] = 0
            # print('new q:', Q[s1][a1])
            s1 = s2
            a1 = a2
            q_val1 = q_val2

            steps += 1

            # Update the learning rate

            # MODIFY THIS SLEEP IF THE GAME IS GOING TOO FAST.
            # time.sleep(0.005)

        World.restart_game()
        reset_E()
        log.append({
            'episode': episode_num,
            'score': score,
            'steps': steps,
            'alpha': alpha,
            'epsilon': 0
        })
        # time.sleep(0.01)
        alpha = max(0.1, pow(episode_num + 1, -0.4))
        epsilon = min(0.3, pow(episode_num + 1, -1.2))

    with open('data/Q.csv', 'w', newline='') as csvfile:
        writer = csv.DictWriter(
            csvfile,
            fieldnames=['episode', 'score', 'steps', 'alpha', 'epsilon'])
        writer.writeheader()
        for episode in log:
            writer.writerow(episode)
    print('Logged')
Example #20
0
def inc_Q(s, a, alpha, inc):
    Q[s][a] += alpha * inc * E[s][a]
    World.set_cell_score(s, a, Q[s][a])
Example #21
0
 def test_can_count_neighbors(self):
     world = World(set([(1, 1), (2, 1), (1, 2), (2, 2), (5, 5)]))
     self.assertEqual(len(world.neighbors_at((1, 1))), 3)
Example #22
0
	def create_world(self):
		self.world = World(80, 60)
		self.world.generate()
		self.paint()
Example #23
0
    clients and clients[0].broadcast(clients,
                                     make_msg('world', world.dump_world()))


def reset_world(world, clients):
    log.info('Reseting world at age: {0}'.format(world.age))
    world.reset_world()
    for client in clients:
        client.send(make_msg('world', world.dump_world()))


handlers = [
    (r'/', IndexHandler),
    (r'/static/(.*)', web.StaticFileHandler, {
        'path': './static/'
    }),
] + SockJSRouter(SocketHandler, '/ws').urls

if __name__ == '__main__':
    app = web.Application(handlers, debug=True, autoreload=True)
    port = 8888
    app.listen(port)
    loop = ioloop.IOLoop.instance()
    world = World(width=width, height=height, alive_cells=SIMPLE_PLANER)
    log.info('Starting world at age {0}, listening at {1}'.format(
        world.age, port))
    period_cbk = ioloop.PeriodicCallback(partial(evolve_world, world, clients),
                                         250, loop)
    period_cbk.start()
    loop.start()