summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 5a3c7982f5777962ae620cbcf36e1c940c5ce118 (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::{path::PathBuf, time::Duration};

use clap::{Args, Parser, Subcommand, ValueEnum};

use crate::{board::Outcome, claude_player::ClaudePlayer, game::GameHandler, local_player::LocalPlayer, network::Network, neural_player::NeuralPlayer, player::Player};

mod board;
mod player;
mod game;
#[path = "board-view.rs"]
mod board_view;
#[path = "local-player.rs"]
mod local_player;
#[path = "claude-player.rs"]
mod claude_player;
mod bench;
mod network;
#[path = "neural-player.rs"]
mod neural_player;
mod train;

/// Tigers and Sheep, a hunt game on a 5x5 board. With no command, plays a game in the terminal.
#[derive(Parser)]
#[command(version, args_conflicts_with_subcommands = true)]
struct Cli
{
	#[command(subcommand)]
	command: Option<Command>,
	#[command(flatten)]
	play: PlayArgs,
}

#[derive(Subcommand)]
enum Command
{
	/// Play a game in the terminal (the default)
	Play(PlayArgs),
	/// Play against a trained network
	Neural
	{
		/// Weights file written by `train`
		weights: PathBuf,
		/// The side you play
		#[arg(value_enum, default_value_t = Side::Sheep)]
		side: Side,
		/// Seconds the network may think per move
		#[arg(short, long, default_value_t = 2.0)]
		time: f64,
	},
	/// Time the computer player's search on fixed positions, without the TUI
	Bench
	{
		/// Search depth
		#[arg(default_value_t = 6)]
		depth: usize,
	},
	/// Train a network by self-play
	///
	/// Starts from `--from`, or else the newest gen-NNN.bin in the out dir, or else random weights.
	Train(TrainArgs),
}

#[derive(Args)]
struct PlayArgs
{
	/// Who plays the sheep: `human`, `claude`, or a weights file for a neural player
	#[arg(short = 'S', long, default_value = "claude", value_parser = parse_player)]
	sheep: PlayerKind,
	/// Who plays the tigers: `human`, `claude`, or a weights file for a neural player
	#[arg(short = 'T', long, default_value = "claude", value_parser = parse_player)]
	tiger: PlayerKind,
	/// Seconds each computer player may think per move
	#[arg(short, long, default_value_t = 2.0)]
	time: f64,
}

#[derive(Clone)]
enum PlayerKind
{
	Human,
	Claude,
	Neural(PathBuf),
}

fn parse_player(s: &str) -> Result<PlayerKind, String>
{
	match s
	{
		"human" => Ok(PlayerKind::Human),
		"claude" => Ok(PlayerKind::Claude),
		path if PathBuf::from(path).is_file() => Ok(PlayerKind::Neural(path.into())),
		_ => Err(format!("expected `human`, `claude` or a weights file, but `{s}` is none of these")),
	}
}

#[derive(Clone, Copy, ValueEnum)]
enum Side
{
	Sheep,
	Tiger,
}

#[derive(Args)]
struct TrainArgs
{
	/// Generations to train
	#[arg(default_value_t = train::Config::default().generations)]
	generations: usize,
	/// Self-play games per generation
	#[arg(default_value_t = train::Config::default().games_per_generation)]
	games: usize,
	/// Where each generation's weights are saved
	#[arg(default_value = "weights")]
	out_dir: PathBuf,
	/// Start from this weights file
	#[arg(long)]
	from: Option<PathBuf>,
	/// Search depth for every move in self-play and matches
	#[arg(long, default_value_t = train::Config::default().depth)]
	depth: usize,
	/// A game this many plies long is a draw
	#[arg(long, default_value_t = train::Config::default().ply_cap)]
	ply_cap: usize,
	/// Plies of random moves at the start of each self-play game
	#[arg(long, default_value_t = train::Config::default().random_plies)]
	random_plies: usize,
	/// Chance of a random move after that
	#[arg(long, default_value_t = train::Config::default().epsilon)]
	epsilon: f32,
	/// TD(λ): 1 trains on each game's result alone, smaller trusts later search scores more
	#[arg(long, default_value_t = train::Config::default().lambda)]
	lambda: f32,
	/// Train on the samples of this many of the latest generations
	#[arg(long, default_value_t = train::Config::default().buffer_generations)]
	buffer_generations: usize,
	/// Don't balance positions from sheep-win and tiger-win games
	#[arg(long)]
	no_balance: bool,
	/// Passes over the samples per generation
	#[arg(long, default_value_t = train::Config::default().epochs)]
	epochs: usize,
	/// Positions per optimiser step
	#[arg(long, default_value_t = train::Config::default().batch_size)]
	batch_size: usize,
	/// Adam's learning rate
	#[arg(long, default_value_t = train::Config::default().learning_rate)]
	learning_rate: f32,
	/// Games against ClaudePlayer, and against the previous generation, after each generation
	#[arg(long, default_value_t = train::Config::default().match_games)]
	match_games: usize,
}

impl From<TrainArgs> for train::Config
{
	fn from(args: TrainArgs) -> Self
	{
		Self
		{
			generations: args.generations,
			games_per_generation: args.games,
			out_dir: args.out_dir,
			from: args.from,
			depth: args.depth,
			ply_cap: args.ply_cap,
			random_plies: args.random_plies,
			epsilon: args.epsilon,
			lambda: args.lambda,
			buffer_generations: args.buffer_generations,
			balance_outcomes: !args.no_balance,
			epochs: args.epochs,
			batch_size: args.batch_size,
			learning_rate: args.learning_rate,
			match_games: args.match_games,
		}
	}
}

fn main() -> std::io::Result<()>
{
	let cli = Cli::parse();
	match cli.command
	{
		Some(Command::Bench { depth }) =>
		{
			bench::run(depth);
			Ok(())
		}
		Some(Command::Train(args)) => train::run(&args.into()),
		Some(Command::Neural { weights, side, time }) =>
		{
			let human = PlayerKind::Human;
			let computer = PlayerKind::Neural(weights);
			let (sheep, tiger) = match side
			{
				Side::Sheep => (human, computer),
				Side::Tiger => (computer, human),
			};
			play(PlayArgs { sheep, tiger, time })
		}
		Some(Command::Play(args)) => play(args),
		None => play(cli.play),
	}
}

fn make_player(kind: PlayerKind, time_limit: Duration) -> std::io::Result<Box<dyn Player>>
{
	Ok(match kind
	{
		PlayerKind::Human => Box::new(LocalPlayer::default()),
		PlayerKind::Claude => Box::new(ClaudePlayer::new(time_limit)),
		PlayerKind::Neural(path) => Box::new(NeuralPlayer::new(Network::load(&path)?, time_limit, 20)),
	})
}

fn play(args: PlayArgs) -> std::io::Result<()>
{
	let time_limit = Duration::from_secs_f64(args.time);
	let mut sheep = make_player(args.sheep, time_limit)?;
	let mut tiger = make_player(args.tiger, time_limit)?;
	let mut game = GameHandler::default();
	match game.play_game(sheep.as_mut(), tiger.as_mut())?
	{
		Some(Outcome::Tiger) => println!("Tigers win!"),
		Some(Outcome::Sheep) => println!("Sheep win!"),
		Some(Outcome::Draw) => println!("Draw by repetition."),
		None => (),
	}
	Ok(())
}