coreylowman / allyourturtles Goto Github PK
View Code? Open in Web Editor NEWHalite 3 Bot
Halite 3 Bot
want to be close to enemies if they crash, especially in 4p
including dropoffs.
if a position might be occupied by a enemy ship, but is protected by one of our ships, allow ship to move to it
def od_a_star(gmap, width, height, starts, goals, halite, max_cost=math.inf, conflicts_by_t=None, illegals_by_t=None):
if not conflicts_by_t:
conflicts_by_t = defaultdict(set)
if not illegals_by_t:
illegals_by_t = defaultdict(set)
max_halite = max(gmap.values())
n = len(starts)
standard = (None,) * n
def actual_position(s, i):
return s[i + n] if s[i + n] is not None else s[i]
def heuristic(s):
return sum(2 * dist(actual_position(s, i), goals[i]) for i in range(n))
def neighbors(s, i):
for position in position_neighbors(s[i], width, height):
if position in s[n:]:
continue
neighbor = list(s)
neighbor[i + n] = position
if i == n - 1:
yield tuple(neighbor[n:]) + standard, 1
else:
yield tuple(neighbor), 0
def _reconstruct_path(prev_by_node, current, n):
total_path = [current[:n]]
while current in prev_by_node:
current = prev_by_node[current]
if current[n:] == standard:
total_path.append(current[:n])
return list(reversed(total_path))
closed_set = set()
open_set = set()
g_score = defaultdict(lambda: math.inf)
h_score = defaultdict(lambda: math.inf)
f_score = defaultdict(lambda: math.inf)
c_score = defaultdict(int)
came_from = {}
halite_at = {}
extractions_at = {}
time_at = {}
start = tuple(starts) + standard
open_set.add(start)
g_score[start] = 0
h_score[start] = heuristic(start)
f_score[start] = g_score[start] + h_score[start]
c_score[start] = 0
halite_at[start] = list(halite)
extractions_at[start] = []
time_at[start] = 0
while len(open_set) > 0:
# TODO break ties by heuristic and then conflicts
current = min(open_set, key=f_score.get)
open_set.remove(current)
if current[n:] == standard:
closed_set.add(current)
if current[:n] == goals:
return _reconstruct_path(came_from, current, n)
actuals = tuple(actual_position(current, i) for i in range(n))
t = time_at[current]
halite_left = list(halite_at[current])
halite_on_ground = list(gmap[actuals[i]] for i in range(n))
for pos, _, amt in extractions_at[current]:
for i in range(n):
if pos == actuals[i]:
halite_on_ground[i] -= amt
agent_i = current.index(None) - n
raw_move_cost = halite_on_ground[agent_i] / 4
raw_extracted = halite_on_ground[agent_i] / 10
move_cost = halite_on_ground[agent_i] / max_halite
for neighbor, dt in neighbors(current, agent_i):
if neighbor in closed_set:
continue
nt = t + dt
pos = neighbor[agent_i + n]
if pos in illegals_by_t[nt]:
continue
cost = 0 if current[agent_i] == neighbor[agent_i] else move_cost
d = cost + 1
g = g_score[current] + d
if g > max_cost:
continue
if neighbor not in open_set:
open_set.add(neighbor)
elif g >= g_score[neighbor]:
continue
came_from[neighbor] = current
g_score[neighbor] = g
h_score[neighbor] = heuristic(neighbor)
f_score[neighbor] = g_score[neighbor] + h_score[neighbor]
time_at[neighbor] = nt
c_score[neighbor] = c_score[current] + int(pos in conflicts_by_t[nt])
if current[agent_i] == neighbor[agent_i]:
extracted = raw_extracted
halite_left[agent_i] += extracted
halite_at[neighbor] = halite_left
extractions_at[neighbor] = extractions_at[current] + [(current[agent_i], nt, extracted)]
else:
halite_left[agent_i] -= raw_move_cost
halite_at[neighbor] = halite_left
extractions_at[neighbor] = deepcopy(extractions_at[current])
need ships in beginning
1546479533
1546523792
1546522752
opponent can bring reinforcements sooner
if there more than 1k halite, then should have multiple ships assigned
that garuntees every ship will have something
or even by doing space partitioning every possible way
captures that the opponent won't get it
just include halite / turns to drop in return if
if enemies are way closer, no point in going there
prioritize things where you have more ships than opponents
connected_components = []
component_by_pos = {p: None for p in OBSTACLES}
unclaimed = positions - OBSTACLES
while len(unclaimed) > 0:
open_set = {unclaimed.pop()}
closed_set = set()
component = len(connected_components)
while len(open_set) > 0:
current = open_set.pop()
closed_set.add(current)
component_by_pos[current] = component
unclaimed.discard(current)
for neighbor in cardinal_neighbors(current):
if neighbor in OBSTACLES or neighbor in closed_set:
continue
open_set.add(neighbor)
connected_components.append(closed_set)
amount_gained = -ship.halite_amount
opponent = gmap[destination].ship
other_allies = list(me.get_ships())
other_allies.remove(ship)
other_opponents = list(opponent_ships)
other_opponents.remove(opponent)
if len(other_allies) > 0:
closest_ally = min(other_allies, key=lambda s: gmap.dist(s.pos, destination))
ally_dist = gmap.dist(closest_ally.pos, destination)
if len(other_opponents) == 0:
closer_than_enemy = True
else:
closest_enemy = min(other_opponents, key=lambda s: gmap.dist(s.pos, destination))
enemy_dist = gmap.dist(closest_enemy.pos, destination)
closer_than_enemy = ally_dist < enemy_dist
if allies_nearby > opponents_nearby and closer_than_enemy:
amount_dropped = ship.halite_amount + opponent.halite_amount
amount_gained = min(constants.MAX_HALITE - closest_ally.halite_amount, amount_dropped)
amount_gained -= amount_dropped - amount_gained
turns_to_move += gmap.dist(closest_ally.pos, destination) + 1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.