def setup(self): # Create your sprites and sprite lists here self.agents = [] self.coins = [] self.state = State.MAIN_MENU for i in range(len(self.maze)): for j in range(len(self.maze[0])): if self.maze[i][j] == 0 and randint(0, 100) > 95: self.coins.append( Coin(j, i, self.size_block // 5, self.size_block)) y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) while self.maze[y][x] == 1: y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) self.player = Player(x, y, self.size_block // 4, self.size_block) arcade.set_background_color(arcade.color.BLACK_OLIVE) for i in range(10): y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) while self.maze[y][x] == 1: y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) self.agents.append( Gosth(x, y, self.size_block // 4, self.size_block))
def testExtration(self): featureExt = FeatureExtractor() agent = Player("aql agent") enemy = Player("greedy agent") gameState = GameState(agent, enemy) enemy.accumulatedCards["Water"] += 1 enemy.accumulatedCards["Fire"] += 1 features = featureExt.getFeatures(gameState, "action", agent.name) self.assertEqual(features["enemy-distance-to-closest-win"], 1) self.assertEqual(features["agent-distance-to-closest-win"], 4) agent.cards.append((1, "Water")) enemy.accumulatedCards["Fire"] -= 1 enemy.accumulatedCards["Water"] += 1 features = featureExt.getFeatures(gameState, "action", agent.name) self.assertEqual(features["agent-distance-to-closest-win"], 3) self.assertEqual(features["enemy-distance-to-closest-win"], 1)
def __init__(self): # creating the grid self.grid = Grid(5, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.grid_length, properties.cell_size) # creating the player self.player = Player(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, properties.player_initial_row, properties.player_initial_column) # adding 4 enemies self.enemy_list = [] self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 2, 2)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 4, 2)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 2, 4)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 4, 4)) # adding treasure self.treasure = Treasure(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 1, 5) self.end = False self.number_of_trials = 3
import csv import os import time from ast import literal_eval import progressbar from kernel import Game from agents import Player, QPlayer os.system("cls") if input("Would you like to train AI vs AI? (yes/no): ") == "no": p1 = QPlayer() p2 = Player() p1.epsilon = 0 with open("q_base.csv", "r") as csv_file: reader = csv.reader(csv_file) mydict = dict(reader) mydict = { literal_eval(key): float(value) for key, value in mydict.items() } p1.q_table = mydict while True: game = Game(p1, p2) game.play_game() os.system("cls") game.display_field() if game.no_space_left(): print("\nIt`s draw") else: print("\nThe winner is {}".format(game.winner)) if input("\nOne more game?: ") == "yes":
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # background color red green blue alpha glClearColor(1, 1, 1, 0) # creating the grid self.grid = Grid(5, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.grid_length, properties.cell_size) # creating the player self.player = Player(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, properties.player_initial_state) #creating the score self.score = Score(properties.score_initial_value, properties.off_setX, "Score") #creating the score self.episode = Score(properties.score_initial_value, properties.off_setX + properties.window_width / 2, "Episode") # adding 4 enemies self.enemy_list = [] self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 2, 2)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 4, 2)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 2, 4)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 4, 4)) # adding treasure self.treasure = Treasure(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 1, 5, 'images/treasure.png') # adding small treasure self.small_treasure = Treasure(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 1, 1, 'images/coins.png') self.end = False self.taked_small_treasure = False self.q_table = np.zeros([25, 4]) # Hyperparameters self.alpha = 0.7 self.gamma = 0.6 self.epsilon = 1 self.min_epsilon = 0.01 self.max_epsilon = 1.0 self.decay_rate = 0.01
class MyWindow(pyglet.window.Window): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # background color red green blue alpha glClearColor(1, 1, 1, 0) # creating the grid self.grid = Grid(5, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.grid_length, properties.cell_size) # creating the player self.player = Player(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, properties.player_initial_state) #creating the score self.score = Score(properties.score_initial_value, properties.off_setX, "Score") #creating the score self.episode = Score(properties.score_initial_value, properties.off_setX + properties.window_width / 2, "Episode") # adding 4 enemies self.enemy_list = [] self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 2, 2)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 4, 2)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 2, 4)) self.enemy_list.append( Enemy(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 4, 4)) # adding treasure self.treasure = Treasure(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 1, 5, 'images/treasure.png') # adding small treasure self.small_treasure = Treasure(properties.cell_size, properties.off_setX, properties.off_setYtop, properties.off_setYbottom, properties.rowsbycols, 1, 1, 'images/coins.png') self.end = False self.taked_small_treasure = False self.q_table = np.zeros([25, 4]) # Hyperparameters self.alpha = 0.7 self.gamma = 0.6 self.epsilon = 1 self.min_epsilon = 0.01 self.max_epsilon = 1.0 self.decay_rate = 0.01 def take_action(self, action): observation = properties.get_observation(self.player.state, action) next_state = observation[0] reward = observation[1] done = observation[2] self.player.update(next_state) self.score.update(reward) self.end = done if self.player.state == 20: ((properties.reward_table[15])[2])[1] = -1 ((properties.reward_table[21])[3])[1] = -1 self.taked_small_treasure = True self.on_draw() return observation def draw(self): self.grid.draw() self.player.draw() self.score.draw() self.episode.draw() for enemy in self.enemy_list: enemy.draw() self.treasure.draw() if not self.taked_small_treasure: self.small_treasure.draw() def game_start(self): pyglet.app.run() def game_restart(self): print("score:" + str(self.score.score)) self.episode.update(1) if self.episode.score > 10000: self.game_over() self.epsilon = self.min_epsilon + ( self.max_epsilon - self.min_epsilon) * np.exp( -self.decay_rate * self.episode.score) initial_state_space = [ 0, 1, 2, 3, 4, 5, 7, 9, 10, 11, 12, 13, 14, 15, 17, 19, 21, 22, 23 ] self.player.update(choice(initial_state_space)) self.score.reset() self.end == False ((properties.reward_table[15])[2])[1] = 30 ((properties.reward_table[21])[3])[1] = 30 self.taked_small_treasure = False def game_over(self): pyglet.app.exit() def on_draw(self): self.clear() self.draw() def start_Qlearning(self, dt): state = self.player.state if random.uniform(0, 1) > self.epsilon: action = np.argmax(self.q_table[state]) # Exploit learned values else: action = random.randint(0, 3) # Explore action space observation = self.take_action(action) next_state = observation[0] reward = observation[1] old_value = self.q_table[state, action] next_max = np.max(self.q_table[next_state]) new_value = (1 - self.alpha) * old_value + self.alpha * ( reward + self.gamma * next_max) self.q_table[state, action] = new_value if self.end == True: self.game_restart() def exploiting(self, dt): state = self.player.state action = np.argmax(self.q_table[state]) # Exploit learned values observation = self.take_action(action) next_state = observation[0] reward = observation[1] if self.end == True: self.game_restart()
class MyGame(arcade.Window): def __init__(self, width, height, title): super().__init__(width, height, title) self.size_block = 20 self.maze = make_maze(width // (self.size_block * 3), height // (self.size_block * 2)) self.agents = [] self.coins = [] self.player = None def setup(self): # Create your sprites and sprite lists here self.agents = [] self.coins = [] self.state = State.MAIN_MENU for i in range(len(self.maze)): for j in range(len(self.maze[0])): if self.maze[i][j] == 0 and randint(0, 100) > 95: self.coins.append( Coin(j, i, self.size_block // 5, self.size_block)) y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) while self.maze[y][x] == 1: y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) self.player = Player(x, y, self.size_block // 4, self.size_block) arcade.set_background_color(arcade.color.BLACK_OLIVE) for i in range(10): y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) while self.maze[y][x] == 1: y = randint(0, len(self.maze) - 1) x = randint(0, len(self.maze[0]) - 1) self.agents.append( Gosth(x, y, self.size_block // 4, self.size_block)) #self.agents.push(Gosth(x + (self.size_block // 2), y + (self.size_block // 2), self.size_block // 4) def drow_maze(self): for i in range(len(self.maze)): for j in range(len(self.maze[0])): if self.maze[i][j] == 1: arcade.draw_point(j * self.size_block, i * self.size_block, arcade.color.BLUE, self.size_block) def on_draw(self): """ Render the screen. """ arcade.start_render() if self.state == State.MAIN_MENU: texture = arcade.load_texture("maze.jpeg") arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, SCREEN_WIDTH, SCREEN_HEIGHT, texture, 0) arcade.draw_text("PROJETO DE IA2", (SCREEN_WIDTH / 2) - 250, SCREEN_HEIGHT - 60, arcade.color.RED, 50) arcade.draw_text("ALUNOS: Carlos Walter, Lucas Sales", (SCREEN_WIDTH / 2) - 250, SCREEN_HEIGHT - 120, arcade.color.RED, 25) arcade.draw_text("Press SPACE to play", (SCREEN_WIDTH / 2) - (SCREEN_WIDTH / 4), 305, arcade.color.RED, 50) if self.state == State.PLAYING: self.drow_maze() for i in self.coins: i.drawn() for i in self.agents: i.drawn() self.player.drawn() if self.state == State.GAME_OVER: texture = arcade.load_texture("gameover.jpg") arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, SCREEN_WIDTH, SCREEN_HEIGHT, texture, 0) arcade.draw_text("Press SPACE to play Again", (SCREEN_WIDTH / 2) - (SCREEN_WIDTH / 4) - 40, 315, arcade.color.WHITE, 50) # Call draw() on all your sprite lists below def update(self, delta_time): """ All the logic to move, and the game logic goes here. Normally, you'll call update() on the sprite lists that need it. """ self.player.time += delta_time for i in self.agents: if i.x == self.player.x and i.y == self.player.y: self.state = State.GAME_OVER break if ((i.x - self.player.x)**2 + (i.y - self.player.y)**2)**( 1 / 2) < 15 and i.state != AgentState.STALk: i.state = AgentState.STALk i.target = (self.player.x, self.player.y) i.trajecoty = i.getPath(self.maze) else: i.state = AgentState.SEARCH i.time += delta_time if i.time >= 0.75: if i.move(self.maze): i.time = 0 for i in range(len(self.coins)): if self.coins[i].x == self.player.x and self.coins[ i].y == self.player.y: self.coins.pop(i) self.player.score += 10 break def on_key_press(self, key, key_modifiers): """ Called whenever a key on the keyboard is pressed. For a full list of keys, see: http://arcade.academy/arcade.key.html """ if key == arcade.key.SPACE and self.state == State.MAIN_MENU: # If the game is starting, just change the state and return self.state = State.PLAYING return if key == arcade.key.SPACE and self.state == State.GAME_OVER: # If the game is starting, just change the state and return self.setup() self.state = State.PLAYING return if self.state == State.PLAYING: if key == arcade.key.D and self.player.time > 0.4: if self.player.move(1, self.maze): self.player.time = 0 if key == arcade.key.A and self.player.time > 0.4: if self.player.move(2, self.maze): self.player.time = 0 if key == arcade.key.W and self.player.time > 0.4: if self.player.move(3, self.maze): self.player.time = 0 if key == arcade.key.S and self.player.time > 0.4: if self.player.move(4, self.maze): self.player.time = 0 def on_key_release(self, key, key_modifiers): """ Called whenever the user lets off a previously pressed key. """ def on_mouse_motion(self, x, y, delta_x, delta_y): """ Called whenever the mouse moves. """ pass def on_mouse_press(self, x, y, button, key_modifiers): """ Called when the user presses a mouse button. """ pass def on_mouse_release(self, x, y, button, key_modifiers): """ Called when a user releases a mouse button. """ pass