-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepQLearningAgents.py
More file actions
75 lines (55 loc) · 2.66 KB
/
Copy pathdeepQLearningAgents.py
File metadata and controls
75 lines (55 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from otherBustersAgents import *
import numpy as np
from deepQLearning import QAgent
from qstate import QState
class DeepQLearningAgent(BustersAgent):
def registerInitialState(self, gameState):
BustersAgent.registerInitialState(self, gameState)
self.actions = {0:"North", 1:"East", 2:"South", 3:"West"}
self.actions_rev = {"North":0, "East":1, "South":2, "West":3}
self.distancer = Distancer(gameState.data.layout, False)
self.epsilon = 1
self.alpha = 0.1
self.discount = 0.1
self.qstate = QState(gameState)
self.state = self.qstate.getVectorState(gameState)
self.model_path = "agent.net"
self.qagent = QAgent(self.discount, self.epsilon, self.alpha, self.state.shape[0], 32, 4, model_name=self.model_path, hidden_dim=64)
try:
self.qagent.recover_agent(self.model_path)
except FileNotFoundError:
print("No model found")
self.ilegal = False
self.last_action = None
self.episodes = self.qagent.total_episodes
def getAction(self, gameState):
qstate = QState(gameState)
state = qstate.getVectorState()
action = self.qagent.choose_action(state)
legal_actions = gameState.getLegalActions()
legal_actions.remove("Stop")
if self.actions[action] not in legal_actions:
self.ilegal = True
return np.random.choice(legal_actions)
return self.actions[action]
def getReward(self, state, action, nextstate, gameState, nextGameState):
reward_arr = np.zeros(4) + nextGameState.getScore()/10000
if action not in gameState.getLegalPacmanActions():
reward_arr[self.actions_rev[action]] = -1
return reward_arr
"Return the obtained reward"
if state.isfinal():
return gameState.getScore()*0.01
reward = 0
directions = {"North": 1, "South": -1, "East": 2, "West": -2, 'Stop':0}
dir = gameState.data.agentStates[0].getDirection()
next_dir = nextGameState.data.agentStates[0].getDirection()
if directions[dir] == -directions[next_dir]:
reward -= 0.1
if state.countGhosts(gameState) - nextstate.countGhosts(nextGameState) != 0:
reward += 0.3
return (nextGameState.getScore() - gameState.getScore())/20 + reward
def update(self, state, action, nextState, reward):
self.qagent.store_transition(state.getVectorState(), self.actions_rev[action], reward=reward, state_=nextState.getVectorState())
self.qagent.learn()
self.episodes = self.qagent.total_episodes