-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
122 lines (93 loc) · 3.45 KB
/
Copy pathcontroller.py
File metadata and controls
122 lines (93 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import random
import sys
from typing import List, Optional
from galactic_chess.ai.agent import AIAgent
from galactic_chess.ai.learning import SelfPlayLearner
from galactic_chess.game.game_state import GameState
from galactic_chess.ui.renderer import GalacticChessUI
from galactic_chess.utils.constants import FACTION_NAMES
def run_ai_vs_ai_match(
agents: List[AIAgent],
max_turns: int = 200,
verbose: bool = True,
) -> Optional[int]:
"""Run a match between AI agents"""
state = GameState()
for turn in range(max_turns):
if state.is_game_over():
winner = state.get_winner()
if verbose:
print(
f"\nGame Over! {FACTION_NAMES[winner or 0]} wins after {turn} turns!"
)
return winner
current = state.current_player
if current in state.eliminated_players:
state.advance_turn()
continue
agent = agents[current]
move = agent.get_move(state)
if move:
if verbose and turn % 10 == 0:
print(f"Turn {turn}: {FACTION_NAMES[current]} plays {move}")
state = state.make_move(move)
state.advance_turn()
if verbose:
print(f"\nGame ended in draw after {max_turns} turns")
return None
def benchmark_algorithms() -> None:
"""Compare Max-N vs Paranoid performance"""
print("=" * 60)
print("Benchmarking Max-N vs Paranoid Search")
print("=" * 60)
results = {"maxn": 0, "paranoid": 0, "draws": 0}
for game in range(5):
print(f"\nGame {game + 1}/5")
algorithms = ["maxn", "paranoid", "maxn"]
random.shuffle(algorithms)
agents = [AIAgent(i, algorithms[i], depth=2) for i in range(3)]
winner = run_ai_vs_ai_match(agents, max_turns=100, verbose=False)
if winner is not None:
winning_algo = algorithms[winner]
results[winning_algo] += 1
print(f" Winner: {FACTION_NAMES[winner]} ({winning_algo})")
else:
results["draws"] += 1
print(" Draw")
print("\n" + "=" * 60)
print("Results:")
print(f" Max-N wins: {results['maxn']}")
print(f" Paranoid wins: {results['paranoid']}")
print(f" Draws: {results['draws']}")
print("=" * 60)
def main() -> None:
"""Main entry point"""
if len(sys.argv) > 1:
if sys.argv[1] == "--benchmark":
benchmark_algorithms()
return
if sys.argv[1] == "--selfplay":
print("Running self-play learning...")
learner = SelfPlayLearner(games_per_generation=5, population_size=4)
for gen in range(3):
print(f"\nGeneration {gen + 1}")
weights = learner.run_generation()
print(f" Best weights: {weights.to_dict()}")
learner.save_weights("learned_weights.json")
print("\nWeights saved to learned_weights.json")
return
if sys.argv[1] == "--nogui":
print("Running AI vs AI match (no GUI)...")
agents = [
AIAgent(0, "maxn", depth=3),
AIAgent(1, "paranoid", depth=3),
AIAgent(2, "maxn", depth=3),
]
run_ai_vs_ai_match(agents, verbose=True)
return
print("Usage: python -m galactic_chess.main [--benchmark|--selfplay|--nogui]")
return
ui = GalacticChessUI()
ui.run()
if __name__ == "__main__":
main()