Skip to content

Commit

Permalink
update to ensure ants have starting food, upped food amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
McLeopold committed Jun 1, 2011
1 parent 21aea58 commit c13ff98
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 33 deletions.
88 changes: 71 additions & 17 deletions ants/ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ def __init__(self, options=None):
self.attackradius = int(options["attackradius2"])
self.spawnradius = int(options["spawnradius2"])
self.seed = options.get('seed')
self.food_rate = options.get('food_rate', (1,6)) # total food
self.food_rate = options.get('food_rate', (2,8)) # total food
if type(self.food_rate) in (list, tuple):
self.food_rate = randrange(*self.food_rate)
self.food_turn = options.get('food_turn', (2,10)) # per turn
self.food_turn = options.get('food_turn', (12,30)) # per turn
if type(self.food_turn) in (list, tuple):
self.food_turn = randrange(*self.food_turn)
self.food_start = options.get('food_start', (200,300)) # per turn
self.food_start = options.get('food_start', (75,175)) # per land area
if type(self.food_start) in (list, tuple):
self.food_start = randrange(*self.food_start)
self.food_visible = options.get('food_visible', (1,3)) # in starting loc
if type(self.food_visible) in (list, tuple):
self.food_visible = randrange(*self.food_visible)
self.food_extra = Fraction(0,1)

self.do_attack = {
Expand Down Expand Up @@ -949,12 +952,47 @@ def do_food_sections(self, amount=1):
self.add_food((row, col))
break

def do_food_visible(self, amount=1):
""" Place food in vison of starting spots """
# if this is the first time calling this function then
# create the food sets
if not hasattr(self, 'food_sets_visible'):
self.food_sets_visible = deque(self.get_symmetric_food_sets(True))
# add a sentinal so we know when to shuffle
self.food_sets_visible.append(None)

# counter for food locations
if not hasattr(self, 'pending_food'):
self.pending_food = defaultdict(int)
# increment food counter for food spawning locations
for f in range(amount):
s = self.food_sets_visible.pop()
# if we finished one rotation, shuffle for the next
if s == None:
shuffle(self.food_sets_visible)
self.food_sets_visible.appendleft(None)
s = self.food_sets_visible.pop()
self.food_sets_visible.appendleft(s)

for loc in s:
self.pending_food[loc] += 1

# place food in scheduled locations if they are free
for loc in self.pending_food.keys():
if self.map[loc[0]][loc[1]] == LAND:
self.add_food(loc)
self.pending_food[loc] -= 1

# remove from queue if the count reaches 0
if not self.pending_food[loc]:
del self.pending_food[loc]

def do_food_symmetric(self, amount=1):
""" Place food in the same relation player start positions.
Food that can't be placed is put into a queue and is places
Food that can't be placed is put into a queue and is placed
as soon as the location becomes available.
Positions are randomly orders and cycled to evenly
Positions are randomly ordered and cycled to evenly
distribute food.
"""
# if this is the first time calling this function then
Expand All @@ -965,7 +1003,8 @@ def do_food_symmetric(self, amount=1):
self.food_sets.append(None)

# counter for food locations
self.pending_food = defaultdict(int)
if not hasattr(self, 'pending_food'):
self.pending_food = defaultdict(int)

# increment food counter for food spawning locations
for f in range(amount):
Expand All @@ -990,7 +1029,7 @@ def do_food_symmetric(self, amount=1):
if not self.pending_food[loc]:
del self.pending_food[loc]

def get_symmetric_food_sets(self):
def get_symmetric_food_sets(self, starting=False):
""" Split map into sets of squares
Each set contains self.num_players points where each point
Expand All @@ -1011,6 +1050,14 @@ def get_symmetric_food_sets(self):
if square:
continue

if starting:
# skip locations outside of initial ants' view radius
for ant in self.initial_ant_list:
if self.distance(ant.loc, (row, col)) <= self.viewradius:
break
else:
continue

# possible food locations
locations = [
self.destination((row, col), (n*row_t, n*col_t))
Expand All @@ -1022,13 +1069,16 @@ def get_symmetric_food_sets(self):
# we should not have visited these locations yet
# this also catches duplicates in the current list
if visited[loc[0]][loc[1]]:
raise Exception("Invalid map",
"This map does not support symmetric food placement")
if starting:
break # prevent starting food from breaking on asymmetric maps
else:
raise Exception("Invalid map",
"This map does not support symmetric food placement")
visited[loc[0]][loc[1]] = True

# we only care about sets where none of the locations hit water
if all(self.map[loc[0]][loc[1]] != WATER for loc in locations):
food_sets.append(locations)
else:
# we only care about sets where none of the locations hit water
if all(self.map[loc[0]][loc[1]] != WATER for loc in locations):
food_sets.append(locations)

return food_sets

Expand All @@ -1055,8 +1105,12 @@ def kill_player(self, player):

def start_game(self):
""" Called by engine at the start of the game """
self.game_started = True
self.do_food((self.land_area // self.food_start) // self.num_players)
if self.do_food != self.do_food_none:
self.game_started = True
starting_food = ((self.land_area // self.food_start) // self.num_players
- self.food_visible)
self.do_food_visible(self.food_visible)
self.do_food(starting_food)

def finish_game(self):
""" Called by engine at the end of the game """
Expand All @@ -1068,7 +1122,7 @@ def finish_game(self):
# currently 1 food is spawned per turn per player
food_bonus = (
(self.turns - self.turn) * # food that will spawn
(self.food_rate / self.food_turn)
(self.food_rate * self.num_players / self.food_turn)
+ self.food_extra
+ len(self.current_food) # food that hasn't been collected
+ len(self.current_ants) # player AND enemy ants
Expand All @@ -1095,7 +1149,7 @@ def finish_turn(self):
self.do_orders()
self.do_attack()
self.do_spawn()
self.food_extra += Fraction(self.food_rate, self.food_turn)
self.food_extra += Fraction(self.food_rate * self.num_players, self.food_turn)
food_now = self.food_extra // self.num_players
self.food_extra %= self.num_players
self.do_food(food_now)
Expand Down
32 changes: 16 additions & 16 deletions integration_testing/reset_server.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
rm -Rf uploads/*
rm -Rf compiled/*
rm -Rf games/*
cp -R aichallenge/ants/maps/* maps/
mysql -u contest -p$1 -D aichallenge < sql\0_schema.sql
mysql -u contest -p$1 -D aichallenge < sql\1_data.sql
mysql -u contest -p$1 -D aichallenge < sql\2_generate_matchup.sql
mysql -u contest -p$1 -D aichallenge < sql\3_worker.sql
manager\add_maps_to_database.py
manager\create_test_bot.py ErrorBot -c 1
manager\create_test_bot.py TimeoutBot -c 1
manager\create_test_bot.py InvalidBot -c 1
manager\create_test_bot.py HunterBot -c 3
manager\create_test_bot.py LeftyBot -c 3
manager\create_test_bot.py GreedyBot -c 3
manager\create_test_bot.py RandomBot -c 3
rm -Rf ~/uploads/*
rm -Rf ~/compiled/*
rm -Rf ~/games/*
cp -R ~/aichallenge/ants/maps/* ~/maps/
mysql -u contest -p$1 -D aichallenge < ~/aichallenge/sql/0_schema.sql
mysql -u contest -p$1 -D aichallenge < ~/aichallenge/sql/1_data.sql
mysql -u contest -p$1 -D aichallenge < ~/aichallenge/sql/2_generate_matchup.sql
mysql -u contest -p$1 -D aichallenge < ~/aichallenge/sql/3_worker.sql
python ~/aichallenge/manager/add_maps_to_database.py
python ~/aichallenge/integration_testing/create_test_bot.py ErrorBot -c 1
python ~/aichallenge/integration_testing/create_test_bot.py TimeoutBot -c 1
python ~/aichallenge/integration_testing/create_test_bot.py InvalidBot -c 1
python ~/aichallenge/integration_testing/create_test_bot.py HunterBot -c 3
python ~/aichallenge/integration_testing/create_test_bot.py LeftyBot -c 3
python ~/aichallenge/integration_testing/create_test_bot.py GreedyBot -c 3
python ~/aichallenge/integration_testing/create_test_bot.py RandomBot -c 3
1 change: 1 addition & 0 deletions setup/server_info.php.template
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $server_info = array(
"food_rate" => array(1,6),
"food_turn" => array(2,10),
"food_start" => array(200,300),
"food_visible" => array(1,3),
"food" => "symmetric",
"attack" => "damage"
)
Expand Down
1 change: 1 addition & 0 deletions setup/worker_server_info.py.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ server_info = {{
"food_rate": (1,6),
"food_turn": (2,10),
"food_start": (200,300),
"food_visible": (1, 3),
"food": "symmetric",
"attack": "damage"
}}
Expand Down

0 comments on commit c13ff98

Please sign in to comment.