Skip to content

Commit

Permalink
Merge pull request #194 from ALLAN-DIP/misc-improvements-2024-04-22
Browse files Browse the repository at this point in the history
Miscellaneous improvements (2024-04-22)
  • Loading branch information
aphedges authored Apr 22, 2024
2 parents 8f1913e + 1f9c8d9 commit 1381335
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 90 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Use the command `hadolint Dockerfile` to test
# Adding Hadolint to `pre-commit` is non-trivial, so the command must be run manually

FROM allanumd/allan_bots:model_zip as model_zip

FROM pcpaquette/tensorflow-serving:20190226 AS base

WORKDIR /model/src/model_server
Expand All @@ -14,10 +16,9 @@ RUN apt-get -y update \
&& rm -rf /var/lib/apt/lists/*

# Copy SL model
RUN wget --progress=dot:giga https://f002.backblazeb2.com/file/ppaquette-public/benchmarks/neurips2019-sl_model.zip \
&& mkdir /model/src/model_server/bot_neurips2019-sl_model \
&& unzip neurips2019-sl_model.zip -d /model/src/model_server/bot_neurips2019-sl_model \
&& rm neurips2019-sl_model.zip \
RUN --mount=from=model_zip,target=/model_zip \
mkdir /model/src/model_server/bot_neurips2019-sl_model \
&& unzip /model_zip/neurips2019-sl_model.zip -d /model/src/model_server/bot_neurips2019-sl_model \
&& chmod -R 777 /model/src/model_server/bot_neurips2019-sl_model

# Clone and prepare research repo
Expand Down
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.PHONY: default
default:
@echo "an explicit target is required"

SHELL=/usr/bin/env bash

export PYTHONPATH := $(shell realpath .)

.PHONY: precommit
precommit:
pre-commit run --all-files

.PHONY: test
test:
docker build --target test_ci --tag ci_image .
docker run --rm ci_image

.PHONY: check
check: precommit test

.PHONY: build
build:
docker build \
--target allan_dip_bot \
--tag allan_dip_bot \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from allanumd/allan_bots:base-latest \
.
20 changes: 16 additions & 4 deletions docs/source/bots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ Bots
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.loyal_bot
.. automodule:: baseline_bots.bots.dipnet_bot
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.no_press_bot
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.pushover_bot
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.random_allier_proposer_bot
.. automodule:: baseline_bots.bots.random_proposer_bot
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.selectively_transparent_bot
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.smart_order_accepter_bot
:members:
:undoc-members:
:members:
:undoc-members:

.. automodule:: baseline_bots.bots.transparent_bot
:members:
:undoc-members:
5 changes: 0 additions & 5 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,3 @@
# a list of builtin themes.
#
html_theme = "karma_sphinx_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
2 changes: 1 addition & 1 deletion docs/source/random_order_mod.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Random Order Modification
==============
=========================

.. automodule:: baseline_bots.randomize_order
:members:
Expand Down
4 changes: 4 additions & 0 deletions docs/source/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ Utilities
.. automodule:: baseline_bots.utils
:members:
:undoc-members:

.. automodule:: baseline_bots.parsing_utils
:members:
:undoc-members:
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
daidepp @ git+https://git@github.com/SHADE-AI/daidepp.git@d55a2090c261783e5128fc7c58b5cb137a7653a6
diplomacy @ git+https://git@github.com/SHADE-AI/diplomacy.git@ef624e31839de8aaa8b0d62ebd4d394a5178d54e
gym==0.18.3
karma_sphinx_theme==0.0.8
karma-sphinx-theme==0.0.8
numpy==1.21.6
pre-commit==2.21.0
pytest==7.3.1
Expand Down
22 changes: 20 additions & 2 deletions src/baseline_bots/bots/dipnet_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


from abc import ABC
from typing import List
from typing import List, Optional, Tuple

from diplomacy import Game
from diplomacy_research.players.benchmark_player import DipNetRLPlayer, DipNetSLPlayer
Expand All @@ -29,6 +29,24 @@ def __init__(
else:
self.brain = DipNetRLPlayer()

async def get_brain_orders(
self, game: Optional[Game] = None, power_name: Optional[str] = None
) -> List[str]:
if game is None:
game = self.game
if power_name is None:
power_name = self.power_name
return await self.brain.get_orders(game, power_name)

async def get_brain_beam_orders(
self, game: Optional[Game] = None, power_name: Optional[str] = None
) -> Tuple[List[str], List[float]]:
if game is None:
game = self.game
if power_name is None:
power_name = self.power_name
return await self.brain.get_beam_orders(game, power_name)

async def gen_orders(self) -> List[str]:
"""finalizes moves"""
return await self.brain.get_orders(self.game, self.power_name)
return await self.get_brain_orders()
2 changes: 1 addition & 1 deletion src/baseline_bots/bots/pushover_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def gen_messages(self, rcvd_messages: List[Message]) -> MessagesData:
self.orders = OrdersData()
reply_obj = MessagesData()

orders = await self.brain.get_orders(self.game, self.power_name)
orders = await self.get_brain_orders()
self.orders.add_orders(orders)

if len(rcvd_messages) == 0:
Expand Down
13 changes: 8 additions & 5 deletions src/baseline_bots/bots/smart_order_accepter_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)
from baseline_bots.randomize_order import random_list_orders
from baseline_bots.utils import (
DEBUG_MODE,
USE_LIMITED_DAIDE,
MessagesData,
OrdersData,
Expand Down Expand Up @@ -657,7 +658,7 @@ async def get_non_aggressive_order(
"""
unit = dipnetify_unit(order.unit)
list_order, _ = await self.brain.get_beam_orders(self.game, self.power_name)
list_order, _ = await self.get_brain_beam_orders()

if len(list_order) > 1:
for i in range(1, len(list_order)):
Expand Down Expand Up @@ -710,13 +711,13 @@ async def replace_aggressive_order_to_allies(self) -> None:
order, daide_orders, self.allies
)
await self.send_intent_log(
f"Replacing order {daide_to_dipnet_parsing(str(order))[0]!r} "
f"with {daide_to_dipnet_parsing(str(new_order))[0]!r} because "
f"Replacing order {daide_to_dipnet_parsing(order)[0]!r} "
f"with {daide_to_dipnet_parsing(new_order)[0]!r} because "
"we should not be aggressive to allies."
)
else:
new_order = order
final_orders.append(daide_to_dipnet_parsing(str(new_order))[0])
final_orders.append(daide_to_dipnet_parsing(new_order)[0])

orders_data = OrdersData()
orders_data.add_orders(final_orders)
Expand Down Expand Up @@ -750,6 +751,8 @@ async def send_fake_orders_to_foes(
print(f"Raised {type(e).__name__} in order randomization code block")
print(e)
print("Catching the error and resuming operations")
if DEBUG_MODE:
raise e

async def do_messaging_round(
self,
Expand Down Expand Up @@ -860,7 +863,7 @@ def gen_relation_msg(powers: Sequence[str]) -> str:

async def __call__(self) -> List[str]:
# get dipnet order
orders = await self.brain.get_orders(self.game, self.power_name)
orders = await self.get_brain_orders()
orders_data = OrdersData()
orders_data.add_orders(orders)
await self.send_intent_log(
Expand Down
4 changes: 2 additions & 2 deletions src/baseline_bots/bots/transparent_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def gen_messages(self, rcvd_messages: List[Message]) -> MessagesData:
return comms_obj
parsed_orders = self.parse_messages(rcvd_messages)
parsed_orders = [
list(daide_to_dipnet_parsing(order))[0]
list(daide_to_dipnet_parsing(parse_daide(order)))[0]
for order in parsed_orders
if daide_to_dipnet_parsing(order)
if daide_to_dipnet_parsing(parse_daide(order))
]

# My orders' messages if not already sent
Expand Down
49 changes: 28 additions & 21 deletions src/baseline_bots/parsing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from diplomacy import Game, Message

from baseline_bots.utils import (
DEBUG_MODE,
get_order_tokens,
parse_alliance_proposal,
parse_arrangement,
Expand Down Expand Up @@ -232,6 +233,8 @@ def dipnet_to_daide_parsing(
f"\tSet of orders: {dipnet_style_order_strs}"
)
print(e)
if DEBUG_MODE:
raise e
continue
return daide_orders

Expand Down Expand Up @@ -265,47 +268,45 @@ def dipnetify_unit(unit: Unit) -> str:
return f"{unit_type} {location}"


def daide_to_dipnet_parsing(daide_style_order_str: str) -> Tuple[str, str]:
def daide_to_dipnet_parsing(daide_order: Command) -> Tuple[str, str]:
"""Convert single DAIDE-style order to DipNet-style order
More details here: https://docs.google.com/document/d/16RODa6KDX7vNNooBdciI4NqSVN31lToto3MLTNcEHk0/edit?usp=sharing
:param daide_style_order_str: DAIDE-style string to be converted to DipNet style
:param daide_order: DAIDE-style order to be converted to DipNet style
:return: DipNet-style order string and unit's power name
"""

try:
parsed_order: Command = parse_daide(daide_style_order_str)

# Dipnetify source unit
acting_unit = dipnetify_unit(parsed_order.unit)
unit_power = parsed_order.unit.power
if isinstance(parsed_order, SUP):
acting_unit = dipnetify_unit(daide_order.unit)
unit_power = daide_order.unit.power
if isinstance(daide_order, SUP):
# Support order
supported_unit = dipnetify_unit(parsed_order.supported_unit)
supported_unit = dipnetify_unit(daide_order.supported_unit)
dipnet_order = f"{acting_unit} S {supported_unit}"
if parsed_order.province_no_coast_location is not None:
prov = dipnetify_location(parsed_order.province_no_coast_location)
if daide_order.province_no_coast_location is not None:
prov = dipnetify_location(daide_order.province_no_coast_location)
dipnet_order += f" - {prov}"
elif isinstance(parsed_order, HLD):
elif isinstance(daide_order, HLD):
# Hold order
dipnet_order = f"{acting_unit} H"
elif isinstance(parsed_order, MoveByCVY):
elif isinstance(daide_order, MoveByCVY):
# CTO order
prov = dipnetify_location(parsed_order.province)
prov = dipnetify_location(daide_order.province)
dipnet_order = f"{acting_unit} - {prov} VIA"
elif isinstance(parsed_order, CVY):
elif isinstance(daide_order, CVY):
# Convoy order
convoyed_unit = dipnetify_unit(parsed_order.convoyed_unit)
prov = dipnetify_location(parsed_order.province)
convoyed_unit = dipnetify_unit(daide_order.convoyed_unit)
prov = dipnetify_location(daide_order.province)
dipnet_order = f"{acting_unit} C {convoyed_unit} - {prov}"
elif isinstance(parsed_order, MTO):
elif isinstance(daide_order, MTO):
# Move orders
prov = dipnetify_location(parsed_order.location)
prov = dipnetify_location(daide_order.location)
dipnet_order = f"{acting_unit} - {prov}"
else:
raise NotImplementedError(
f"Conversion for {type(parsed_order).__name__} commands has not been implemented yet"
f"Conversion for {type(daide_order).__name__} commands has not been implemented yet"
)

return dipnet_order, unit_power
Expand All @@ -314,9 +315,11 @@ def daide_to_dipnet_parsing(daide_style_order_str: str) -> Tuple[str, str]:
except Exception as e:
print(
f"ALLAN: error from {__name__}.{daide_to_dipnet_parsing.__name__}\n"
f"\tCould not convert DAIDE command {daide_style_order_str!r} to DipNet format"
f"\tCould not convert DAIDE command {str(daide_order)!r} to DipNet format"
)
print(e)
if DEBUG_MODE:
raise e
return None


Expand Down Expand Up @@ -365,7 +368,7 @@ def parse_proposal_messages(
]
for order in daide_style_orders:
if isinstance(order, XDO):
temp_message = daide_to_dipnet_parsing(str(order.order))
temp_message = daide_to_dipnet_parsing(order.order)
if temp_message:
proposals[order_msg.sender].append(temp_message)
# from RY: I think this parsing is problematic though..
Expand All @@ -390,6 +393,8 @@ def parse_proposal_messages(
f"\tUnexpected proposal message format: {order_msg.message!r}"
)
print(e)
if DEBUG_MODE:
raise e
continue

# Generate set of possible orders for the given power
Expand Down Expand Up @@ -443,6 +448,8 @@ def parse_proposal_messages(
f"\tReceived messages: {rcvd_messages}"
)
print(e)
if DEBUG_MODE:
raise e
return {
"valid_proposals": {},
"invalid_proposals": {},
Expand Down
Loading

0 comments on commit 1381335

Please sign in to comment.