Skip to content

Commit

Permalink
Tests with noise.
Browse files Browse the repository at this point in the history
Forgot to commit this earlier. Including tests with noise.
  • Loading branch information
matheusportela committed Nov 19, 2015
1 parent 9370f23 commit 2c1444a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 34 deletions.
4 changes: 3 additions & 1 deletion messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ def __init__(self, agent_id=None):


class StartMessage(BaseMessage):
def __init__(self, agent_id=None):
def __init__(self, agent_id=None, map_width=None, map_height=None):
super(StartMessage, self).__init__(msg_type=START)
self.agent_id = agent_id
self.map_width = map_width
self.map_height = map_height


class RegisterMessage(BaseMessage):
Expand Down
4 changes: 3 additions & 1 deletion pacman_mas.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def run(self):
self.send_message(self.create_ack_message())
print 'Initialized %s\tID: %d\tClass: %s' % (self.agent_teams[agent_id], agent_id, self.agent_classes[agent_id].__name__)
elif received_message.msg_type == messages.START:
width = received_message.map_width
height = received_message.map_height
agent_id = received_message.agent_id
ally_ids = self.get_agent_allies(agent_id)
enemy_ids = self.get_agent_enemies(agent_id)
Expand All @@ -121,7 +123,7 @@ def run(self):
if agent_id in self.game_states:
del self.game_states[agent_id]

self.game_states[agent_id] = state.GameState(20, 11, [],
self.game_states[agent_id] = state.GameState(width, height, [],
agent_id=agent_id, ally_ids=ally_ids, enemy_ids=enemy_ids,
eater=eater, iteration=self.game_number[agent_id])
self.send_message(self.create_ack_message())
Expand Down
28 changes: 10 additions & 18 deletions run_experiments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ OUTPUT_PATH=experiments
LEARNING_GAMES=100
TESTING_GAMES=15

rm $OUTPUT_PATH/pacman_ai/*
rm $OUTPUT_PATH/ghost_ai/*
rm $OUTPUT_PATH/both_ai/*
rm -rf $OUTPUT_PATH
mkdir $OUTPUT_PATH
mkdir $OUTPUT_PATH/ghost_ai
mkdir $OUTPUT_PATH/ghost_ai_noise

# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost random -e 1 -o $OUTPUT_PATH/pacman_ai/results1.txt -p $OUTPUT_PATH/pacman_ai/policies1.txt
# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman random --ghost ai -e 1 -o $OUTPUT_PATH/ghost_ai/results1.txt -p $OUTPUT_PATH/ghost_ai/policies1.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman-agent random --ghost-agent ai -e 2 -o $OUTPUT_PATH/ghost_ai/results2.txt -p $OUTPUT_PATH/ghost_ai/policies2.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman-agent random --ghost-agent ai -e 3 -o $OUTPUT_PATH/ghost_ai/results3.txt -p $OUTPUT_PATH/ghost_ai/policies3.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman-agent random --ghost-agent ai -e 4 -o $OUTPUT_PATH/ghost_ai/results4.txt -p $OUTPUT_PATH/ghost_ai/policies4.txt

# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost random -e 2 -o $OUTPUT_PATH/pacman_ai/results2.txt -p $OUTPUT_PATH/pacman_ai/policies2.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman random --ghost ai -e 2 -o $OUTPUT_PATH/ghost_ai/results2.txt -p $OUTPUT_PATH/ghost_ai/policies2.txt

# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost random -e 3 -o $OUTPUT_PATH/pacman_ai/results3.txt -p $OUTPUT_PATH/pacman_ai/policies3.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman random --ghost ai -e 3 -o $OUTPUT_PATH/ghost_ai/results3.txt -p $OUTPUT_PATH/ghost_ai/policies3.txt

# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost random -e 4 -o $OUTPUT_PATH/pacman_ai/results4.txt -p $OUTPUT_PATH/pacman_ai/policies4.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman random --ghost ai -e 4 -o $OUTPUT_PATH/ghost_ai/results4.txt -p $OUTPUT_PATH/ghost_ai/policies4.txt

# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost ai -e 1 -o $OUTPUT_PATH/both_ai/results1.txt -p $OUTPUT_PATH/both_ai/policies1.txt
# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost ai -e 2 -o $OUTPUT_PATH/both_ai/results2.txt -p $OUTPUT_PATH/both_ai/policies2.txt
# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost ai -e 3 -o $OUTPUT_PATH/both_ai/results3.txt -p $OUTPUT_PATH/both_ai/policies3.txt
# python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman ai --ghost ai -e 4 -o $OUTPUT_PATH/both_ai/results4.txt -p $OUTPUT_PATH/both_ai/policies4.txt
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman-agent random --ghost-agent ai -e 2 -o $OUTPUT_PATH/ghost_ai_noise/results2.txt -p $OUTPUT_PATH/ghost_ai_noise/policies2.txt --noise 3
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman-agent random --ghost-agent ai -e 3 -o $OUTPUT_PATH/ghost_ai_noise/results3.txt -p $OUTPUT_PATH/ghost_ai_noise/policies3.txt --noise 3
python simulator.py -l $LEARNING_GAMES -t $TESTING_GAMES --pacman-agent random --ghost-agent ai -e 4 -o $OUTPUT_PATH/ghost_ai_noise/results4.txt -p $OUTPUT_PATH/ghost_ai_noise/policies4.txt --noise 3
40 changes: 33 additions & 7 deletions simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import agents
import os

NOISE = 0

class CommunicatingAgent(game.Agent):
def __init__(self, agent_id):
Expand All @@ -34,11 +35,20 @@ def enable_learn_mode(self):
def calculate_reward(self, current_score):
raise NotImplementedError, 'Communicating agent must calculate score'

def _introduce_position_error(self, pos, min_, max_):
ex = random.choice(range(min_, max_ + 1))
ey = random.choice(range(min_, max_ + 1))

return (pos[0] + ex, pos[1] + ey)

def create_state_message(self, state):
agent_positions = {}
agent_positions[0] = state.getPacmanPosition()[::-1]
for id_, g in enumerate(state.getGhostPositions()):
agent_positions[id_ + 1] = g[::-1]
for id_, pos in enumerate(state.getGhostPositions()):
if NOISE == 0:
agent_positions[id_ + 1] = pos[::-1]
else:
agent_positions[id_ + 1] = self._introduce_position_error(pos[::-1], -NOISE, NOISE)

food_positions = []

Expand Down Expand Up @@ -81,10 +91,13 @@ def init_agent(self):
self.send_message(messages.InitMessage(agent_id=self.agent_id))
self.receive_message()

def start_game(self):
def start_game(self, map_width, map_height):
self.previous_score = 0
self.previous_action = 'Stop'
self.send_message(messages.StartMessage(agent_id=self.agent_id))
self.send_message(messages.StartMessage(
agent_id=self.agent_id,
map_width=map_width,
map_height=map_height))
self.receive_message()

def register_agent(self, agent_team, agent_class):
Expand Down Expand Up @@ -205,28 +218,41 @@ def main():
parser.add_argument('--pacman-agent', dest='pacman_agent', type=str,
default='random', help='select pacman agent: random or ai')
parser.add_argument('--ghost-agent', dest='ghost_agent', type=str,
default='random', help='select ghost agent: random or ai')
default='ai', help='select ghost agent: random or ai')
parser.add_argument('-o', '--output', dest='output_filename', type=str,
default='results.txt', help='results output file')
parser.add_argument('--noise', dest='noise', type=int, default=0,
help='introduce noise in position measurements')
parser.set_defaults(graphics=False)

args = parser.parse_args()

if args.experiment_number == 1:
layout_file = 'classic1Ghost'
num_ghosts = 1
map_width = 28
map_height = 28
elif args.experiment_number == 2:
layout_file = 'classic2Ghosts'
num_ghosts = 2
map_width = 28
map_height = 28
elif args.experiment_number == 3:
layout_file = 'classic3Ghosts'
num_ghosts = 3
map_width = 28
map_height = 28
elif args.experiment_number == 4:
layout_file = 'classic4Ghosts'
num_ghosts = 4
map_width = 28
map_height = 28
else:
raise ValueError, 'Experiment number must be between 0 and 4'

global NOISE
NOISE = args.noise

learn_games = args.learn
test_games = args.test
policy_filename = args.policy_filename
Expand Down Expand Up @@ -287,9 +313,9 @@ def main():
print '\nGame #%d' % (i+1)

# Start new game
pacman.start_game()
pacman.start_game(map_width, map_height)
for ghost in ghosts:
ghost.start_game()
ghost.start_game(map_width, map_height)

# Load policies to agents
if policy_filename and os.path.isfile(policy_filename):
Expand Down
17 changes: 10 additions & 7 deletions state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Map(object):
Every cell contains a value in the interval [0, 1] indicating a probability.
The entire map sums up to 1.
"""
paths = None

def __init__(self, width, height, walls=[]):
self.width = width
self.height = height
Expand All @@ -17,7 +19,6 @@ def __init__(self, width, height, walls=[]):
'West': (0, -1),
'Stop': (0, 0),
}
self.paths = None
self._walls = walls
self.cells = self.generate_cells()
self.normalize()
Expand All @@ -29,7 +30,9 @@ def walls(self):
@walls.setter
def walls(self, walls):
self._walls = walls
self._calculate_all_paths()

if Map.paths == None:
self._calculate_all_paths()

def __getitem__(self, i):
return self.cells[i]
Expand All @@ -50,9 +53,9 @@ def __str__(self):
for y in range(self.height-1, -1, -1):
for x in range(self.width):
if self._is_wall((y, x)):
string.append('......')
string.append('.....')
else:
string.append('%.4f' % self[y][x])
string.append('%.3f' % self[y][x])
string.append(' ')
string.append('\n')

Expand Down Expand Up @@ -184,17 +187,17 @@ def _calculate_all_paths(self, max_distance=None):
if self._is_valid_position(pos):
paths[pos] = self._calculate_paths(pos, max_distance=max_distance)

self.paths = paths
Map.paths = paths

def calculate_distance(self, pos1, pos2):
if self.paths == None:
if Map.paths == None:
self._calculate_all_paths()

if self._is_valid_position(pos1) and self._is_valid_position(pos2):
if pos1 == pos2:
return 0
else:
return len(self.paths[pos1][pos2])
return len(Map.paths[pos1][pos2])
else:
return float('inf')

Expand Down

0 comments on commit 2c1444a

Please sign in to comment.