Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add baseline advisor for lr model #19

Open
wants to merge 45 commits into
base: add-baseline-advisors
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5cde120
Import `Sequence` from `typing`, not `collections.abc`
aphedges Nov 15, 2024
12c4b08
Run tests with Python 3.7 in GitHub Actions
aphedges Nov 19, 2024
5b721c7
Merge pull request #16 from ALLAN-DIP/fix-py3.7-compat
aphedges Nov 19, 2024
2db5a8f
Update code quality checks
aphedges Dec 5, 2024
2beb922
Update Python lockfile
aphedges Dec 5, 2024
f5e4605
Build x86_64 OCI images
aphedges Nov 11, 2024
471d376
Move `log/` directory into `data/` directory
aphedges Dec 4, 2024
73e0f70
Remove `*.sif` from `.gitignore`
aphedges Dec 6, 2024
c6a354d
Merge pull request #17 from ALLAN-DIP/misc-improvements-2024-12-05
aphedges Dec 6, 2024
349b9c5
Update `diplomacy`
aphedges Dec 5, 2024
838a481
Switch to using new JSON-based suggestion type
aphedges Nov 26, 2024
92d43c1
Merge pull request #18 from ALLAN-DIP/use-json-suggestion-api
aphedges Dec 9, 2024
9b8a1b6
modify knn baseline bot
gale2307 Dec 10, 2024
e75f7d1
add lr baseline bot
gale2307 Dec 10, 2024
f58d1c8
update requirements
gale2307 Dec 11, 2024
5257959
~
gale2307 Dec 11, 2024
6159312
Accept `Sequence`s as input to `suggest_orders()`
aphedges Dec 12, 2024
744fbc6
Fix `suggest_commentary()` error message
aphedges Dec 12, 2024
04c516b
Factor out actual sending of suggestions
aphedges Dec 10, 2024
a9fd7ea
Change to newer JSON suggestion format
aphedges Dec 10, 2024
25766c0
Send advisor name in suggestion messages
aphedges Dec 9, 2024
746f6bc
Update `diplomacy`
aphedges Dec 12, 2024
fec07a6
Add new method to predict opponent orders
aphedges Dec 12, 2024
02eecd4
Prohibit suggestions when bot does not declare support
aphedges Dec 12, 2024
77a4734
Refactor `get_random_orders()` to work for other powers
aphedges Dec 12, 2024
9e655e9
Use `get_random_orders()` everywhere we get random orders
aphedges Dec 12, 2024
36a77f7
Generate random opponent order predictions
aphedges Dec 12, 2024
da018ef
delete knn bot
gale2307 Dec 16, 2024
0282bcd
clean up lr bot
gale2307 Dec 16, 2024
e2570e7
update lrbot readme
gale2307 Dec 17, 2024
96489b5
Merge pull request #20 from ALLAN-DIP/predict-opponent-orders
aphedges Dec 18, 2024
feba699
Implement baseline `KnnBot` agent
DragonEntropy Nov 11, 2024
887cdaf
Improve `KnnBot`
aphedges Nov 11, 2024
2288c04
Update Python lockfile
aphedges Nov 11, 2024
c481eda
Update `run_game.sh` to work properly
aphedges Nov 12, 2024
1d9aef6
Merge branch 'add-baseline-advisors' of /~https://github.com/ALLAN-DIP/…
gale2307 Jan 13, 2025
06f69f8
modify knn baseline bot
gale2307 Dec 10, 2024
f1658ef
add lr baseline bot
gale2307 Dec 10, 2024
59f5329
update requirements
gale2307 Dec 11, 2024
1c21ef2
~
gale2307 Dec 11, 2024
f0db97c
delete knn bot
gale2307 Dec 16, 2024
24b3365
clean up lr bot
gale2307 Dec 16, 2024
b068558
update lrbot readme
gale2307 Dec 17, 2024
4035401
Merge branch 'add-baseline-advisors-lr' of /~https://github.com/ALLAN-D…
gale2307 Jan 13, 2025
89ce28f
update baseline_models version
gale2307 Jan 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements-lock.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
astroid==3.3.5
baseline_models @ git+https://git@github.com/ALLAN-DIP/baseline-models.git@ba5700a587ffab63e35a44d9ed96919a4ffee0cf
baseline_models @ git+https://git@github.com/ALLAN-DIP/baseline-models.git@367fc4bb269e37ddcd60978e3a11cf180106679c
bcrypt==4.2.0
certifi==2024.7.4
cfgv==3.4.0
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
baseline-models @ git+https://git@github.com/ALLAN-DIP/baseline-models.git@ba5700a587ffab63e35a44d9ed96919a4ffee0cf
baseline-models @ git+https://git@github.com/ALLAN-DIP/baseline-models.git@367fc4bb269e37ddcd60978e3a11cf180106679c
daidepp @ git+https://git@github.com/SHADE-AI/daidepp.git@7f6dcca60f2e6dd89c37862663be7b098cc03794
# Pinned `diplomacy` to `main`
diplomacy @ git+https://git@github.com/ALLAN-DIP/diplomacy.git@f4ec4517dc5ea3c3ed787d3ad483ce01f4f0e0ea
Expand Down
6 changes: 6 additions & 0 deletions src/chiron_utils/bots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
KnnAdvisor as KnnAdvisor,
KnnPlayer as KnnPlayer,
)
from chiron_utils.bots.lr_bot import (
LrAdvisor as LrAdvisor,
LrPlayer as LrPlayer,
)
from chiron_utils.bots.random_proposer_bot import (
RandomProposerAdvisor as RandomProposerAdvisor,
RandomProposerPlayer as RandomProposerPlayer,
Expand All @@ -15,6 +19,8 @@
BOTS: List[Type[BaselineBot]] = [
KnnPlayer,
KnnAdvisor,
LrPlayer,
LrAdvisor,
RandomProposerAdvisor,
RandomProposerPlayer,
]
Expand Down
53 changes: 17 additions & 36 deletions src/chiron_utils/bots/knn_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import pickle
from typing import Any, Dict, List, Sequence

from baseline_models.model_code.evaluation import infer
from baseline_models.model_code.preprocess import entry_to_vectors
from baseline_models.model_code.predict import predict
from diplomacy.utils import strings as diplomacy_strings
from diplomacy.utils.constants import SuggestionType

Expand All @@ -19,22 +18,20 @@

DEFAULT_COMM_STAGE_LENGTH = 300 # 5 minutes in seconds
COMM_STAGE_LENGTH = int(os.environ.get("COMM_STAGE_LENGTH", DEFAULT_COMM_STAGE_LENGTH))
MODEL_PATH = Path() / "baseline_knn_model.pkl"

POWER_TO_INDEX = {
"AUSTRIA": 0,
"ENGLAND": 1,
"FRANCE": 2,
"GERMANY": 3,
"ITALY": 4,
"RUSSIA": 5,
"TURKEY": 6,
}
MODEL_PATH = Path() / "knn_models"


@dataclass
class KnnBot(BaselineBot, ABC):
"""Currently a dictionary mapping phase type to a model
"""Baseline knn model

MODEL_PATH should point to folder containing model .pkl files

Each model corresponds to a (unit, location, phase) combination

Unit types are 'A', 'F'

Location types include all possible locations on the board, such as 'LON', 'STP_SC', 'BRE', etc

Phase types are 'SM', 'FM', 'WA, 'SR', 'FR', 'CD'
"""
Expand All @@ -46,37 +43,21 @@ def __post_init__(self) -> None:
self.models: Dict[str, Any] = pickle.load(model_file)

def get_orders(self) -> List[str]:
name = self.game.phase
units = self.game.map.units
centers = self.game.map.centers
homes = self.game.map.homes

influences = {}
for power, power_class in self.game.powers.items():
influences[power] = power_class.influence

vector, _, season = entry_to_vectors(
None,
include_orders=False,
name_data=name,
units_data=units,
centers_data=centers,
homes_data=homes,
influences_data=influences,
)
orders = infer(self.models[season], vector.reshape(1, -1))
orders = list()
state = self.game.get_state()
orders = predict(MODEL_PATH, state, self.power_name)

logger.info("Orders to suggest: %s", orders)

return orders

async def gen_orders(self) -> List[str]:
orders = self.get_orders()
power_orders = orders[POWER_TO_INDEX[self.power_name]]
if self.bot_type == BotType.ADVISOR:
await self.suggest_orders(power_orders)
await self.suggest_orders(orders)
elif self.bot_type == BotType.PLAYER:
await self.send_orders(orders, wait=True)
return power_orders
return orders

async def do_messaging_round(self, orders: Sequence[str]) -> List[str]:
return list(orders)
Expand Down
71 changes: 71 additions & 0 deletions src/chiron_utils/bots/lr_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Abstract base classes for bots."""

from abc import ABC
from dataclasses import dataclass
import os
from pathlib import Path
from typing import Any, Dict, List, Sequence

from baseline_models.model_code.predict import predict
from diplomacy.utils import strings as diplomacy_strings
from diplomacy.utils.constants import SuggestionType

from chiron_utils.bots.baseline_bot import BaselineBot, BotType
from chiron_utils.utils import return_logger

logger = return_logger(__name__)

DEFAULT_COMM_STAGE_LENGTH = 300 # 5 minutes in seconds
COMM_STAGE_LENGTH = int(os.environ.get("COMM_STAGE_LENGTH", DEFAULT_COMM_STAGE_LENGTH))
MODEL_PATH = Path() / "lr_models"


@dataclass
class LrBot(BaselineBot, ABC):
"""Baseline lr model

MODEL_PATH should point to folder containing model .pkl files

Each model corresponds to a (unit, location, phase) combination

Unit types are 'A', 'F'

Location types include all possible locations on the board, such as 'LON', 'STP_SC', 'BRE', etc

Phase types are 'SM', 'FM', 'WA, 'SR', 'FR', 'CD'
"""

player_type = diplomacy_strings.NO_PRESS_BOT

def get_orders(self) -> List[str]:
orders = list()
state = self.game.get_state()
orders = predict(MODEL_PATH, state, self.power_name)

logger.info("Orders to suggest: %s", orders)

return orders

async def gen_orders(self) -> List[str]:
orders = self.get_orders()
if self.bot_type == BotType.ADVISOR:
await self.suggest_orders(orders)
elif self.bot_type == BotType.PLAYER:
await self.send_orders(orders, wait=True)
return orders

async def do_messaging_round(self, orders: Sequence[str]) -> List[str]:
return list(orders)


class LrAdvisor(LrBot):
"""Advisor form of `LrBot`."""

bot_type = BotType.ADVISOR
suggestion_type = SuggestionType.MOVE


class LrPlayer(LrBot):
"""Player form of `LrBot`."""

bot_type = BotType.PLAYER