Skip to content

Commit

Permalink
Merge pull request #318 from ujh/no-large-patterns
Browse files Browse the repository at this point in the history
Turn off large patterns
  • Loading branch information
ujh committed Nov 15, 2016
2 parents 33fd809 + 11fd2d9 commit d6052cc
Show file tree
Hide file tree
Showing 26 changed files with 12 additions and 1,030,195 deletions.
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ num = "*"
num_cpus = "*"
quicksort = "*"
rand = "*"
rayon = "*"
regex = "*"
smallvec = "*"
time = "*"
Expand Down
16 changes: 0 additions & 16 deletions src/board/coord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,6 @@ impl Coord {
1 <= self.col && self.col <= board_size && 1 <= self.row && self.row <= board_size
}

pub fn offset_by(&self, offset: (isize, isize), board_size: u8) -> Option<Coord> {
let (offset_col, offset_row) = offset;
let new_col = self.col as isize + offset_col;
let new_row = self.row as isize + offset_row;
if new_col <= 0 || new_row <= 0 {
None
} else {
let new_coord = Self::new(new_col as u8, new_row as u8);
if new_coord.is_inside(board_size) {
Some(new_coord)
} else {
None
}
}
}

pub fn distance_to_border(&self, board_size: u8) -> u8 {
*[self.col-1, self.row-1, board_size - self.col, board_size - self.row]
.iter()
Expand Down
7 changes: 0 additions & 7 deletions src/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,6 @@ impl Board {
self.board[c.to_index(self.size)].color
}

pub fn coord_with_offset_from(&self, c: &Coord, offset: (isize, isize)) -> Option<Color> {
match c.offset_by(offset, self.size) {
Some(coord) => Some(self.color(&coord)),
None => None
}
}

/// Remove dead stone for scoring. Board becomes unplayable after
/// doing this!
pub fn remove_dead_stone(&mut self, c: &Coord) {
Expand Down
3 changes: 0 additions & 3 deletions src/config/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
atari_check = 0.743349
captures_probability = 0.44147
ladder_check = 0.702563
large_pattern_factor = 315.093
large_pattern_probability = 0.386782
last_moves_for_heuristics = 2
pattern_probability = 0.868737
play_in_middle_of_eye = 1.0
Expand All @@ -15,7 +13,6 @@ best_move_factor = 1.0
capture_many = 30
capture_one = 15
empty = 20
large_pattern_factor = 143
neutral_plays = 10
neutral_wins = 5
self_atari = 10
Expand Down
18 changes: 0 additions & 18 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ pub struct PriorsConfig {
/// moves on the third line. This is only applied if the area
/// around the move of a Manhattan distance of three is empty.
pub empty: usize,
/// Factor to multiply the probability of the matching large
/// pattern by.
pub large_pattern_factor: f32,
/// The number of prior plays to start with. This is useful to
/// simplify the calculations as we can avoid 0 values.
pub neutral_plays: usize,
Expand Down Expand Up @@ -164,7 +161,6 @@ impl PriorsConfig {
capture_many: Self::as_integer(&table, "capture_many"),
capture_one: Self::as_integer(&table, "capture_one"),
empty: Self::as_integer(&table, "empty"),
large_pattern_factor: Self::as_float(&table, "large_pattern_factor"),
neutral_plays: Self::as_integer(&table, "neutral_plays"),
neutral_wins: Self::as_integer(&table, "neutral_wins"),
self_atari: Self::as_integer(&table, "self_atari"),
Expand All @@ -185,12 +181,6 @@ impl PriorsConfig {
pub fn use_small_patterns(&self) -> bool {
self.small_patterns > 0
}

/// Returns false if the large pattern factor is zero, which then turns
/// off the code that calculates the large patterns prior.
pub fn use_large_patterns(&self) -> bool {
self.large_pattern_factor > 0.0
}
}

impl FromToml for PriorsConfig {
Expand Down Expand Up @@ -252,12 +242,6 @@ pub struct PlayoutConfig {
/// expensive) during atari resolution. Set to 1.0 to always use
/// it.
pub ladder_check: f32,
/// Multiplication factor of the probability of a matched large
/// pattern. The larger the value the more likely it is that a
/// matched move is being played.
pub large_pattern_factor: f32,
/// Probability of playing a move found by matching large patterns.
pub large_pattern_probability: f32,
/// The number of most recently played moves to consider when
/// selecting moves based on heuristics.
pub last_moves_for_heuristics: usize,
Expand All @@ -282,8 +266,6 @@ impl PlayoutConfig {
atari_check: Self::as_float(&table, "atari_check"),
captures_probability: Self::as_float(&table, "captures_probability"),
ladder_check: Self::as_float(&table, "ladder_check"),
large_pattern_factor: Self::as_float(&table, "large_pattern_factor"),
large_pattern_probability: Self::as_float(&table, "large_pattern_probability"),
last_moves_for_heuristics: Self::as_integer(&table, "last_moves_for_heuristics"),
pattern_probability: Self::as_float(&table, "pattern_probability"),
play_in_middle_of_eye: Self::as_float(&table, "play_in_middle_of_eye"),
Expand Down
7 changes: 1 addition & 6 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use board::White;
use config::Config;
use game::Game;
use ownership::OwnershipStatistics;
use patterns::LargePatternMatcher;
use patterns::SmallPatternMatcher;
use playout::Playout;
use ruleset::KgsChinese;
Expand Down Expand Up @@ -77,7 +76,6 @@ pub struct Engine {
config: Arc<Config>,
direct_message_senders: Vec<Sender<DirectMessage>>,
id: usize,
large_pattern_matcher: Arc<LargePatternMatcher>,
ownership: OwnershipStatistics,
playout: Arc<Playout>,
previous_node_count: usize,
Expand All @@ -90,17 +88,15 @@ pub struct Engine {

impl Engine {

pub fn new(config: Arc<Config>, small_pattern_matcher: Arc<SmallPatternMatcher>, large_pattern_matcher: Arc<LargePatternMatcher>) -> Engine {
pub fn new(config: Arc<Config>, small_pattern_matcher: Arc<SmallPatternMatcher>) -> Engine {
let (send_to_main, receive_from_threads) = channel();
let mut engine = Engine {
config: config.clone(),
direct_message_senders: vec!(),
id: 0,
large_pattern_matcher: large_pattern_matcher.clone(),
ownership: OwnershipStatistics::new(config.clone(), 0, 0.0),
playout: Arc::new(Playout::new(
config.clone(),
large_pattern_matcher.clone(),
small_pattern_matcher.clone()
)),
previous_node_count: 0,
Expand Down Expand Up @@ -330,7 +326,6 @@ impl Engine {
&self.config,
&self.playout,
&self.small_pattern_matcher,
&self.large_pattern_matcher,
&self.send_to_main
);
let (send_direct_message, receive_direct_message) = channel();
Expand Down
23 changes: 5 additions & 18 deletions src/engine/prior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,26 @@ use board::Board;
use board::Empty;
use board::Move;
use config::Config;
use patterns::LargePatternMatcher;
use patterns::SmallPatternMatcher;

use std::sync::Arc;

pub struct Prior {
large_pattern_matched: bool,
m: Move,
plays: usize,
wins: usize,
}

impl Prior {

pub fn new(board: &Board, m: &Move, small_pattern_matcher: &Arc<SmallPatternMatcher>, large_pattern_matcher: &Arc<LargePatternMatcher>, config: Arc<Config>) -> Prior {
pub fn new(board: &Board, m: &Move, small_pattern_matcher: &Arc<SmallPatternMatcher>, config: Arc<Config>) -> Prior {
let mut prior = Prior {
large_pattern_matched: false,
m: *m,
plays: 0,
wins: 0,
};
if !m.is_pass() {
prior.calculate(board, m, small_pattern_matcher, large_pattern_matcher, &config);
prior.calculate(board, m, small_pattern_matcher, &config);
}
prior
}
Expand All @@ -58,7 +55,7 @@ impl Prior {
self.wins
}

fn calculate(&mut self, board: &Board, m: &Move, small_pattern_matcher: &Arc<SmallPatternMatcher>, large_pattern_matcher: &Arc<LargePatternMatcher>, config: &Arc<Config>) {
fn calculate(&mut self, board: &Board, m: &Move, small_pattern_matcher: &Arc<SmallPatternMatcher>, config: &Arc<Config>) {
if !board.is_not_self_atari(m) {
let value = config.priors.self_atari;
self.record_negative_prior(value);
Expand All @@ -79,12 +76,6 @@ impl Prior {
let prior = count * config.priors.small_patterns;
self.record_even_prior(prior);
}
if config.priors.use_large_patterns() {
let probability = self.large_pattern_probability(board, m, large_pattern_matcher);
self.large_pattern_matched = probability > 0.0;
let prior = config.priors.large_pattern_factor * probability;
self.record_even_prior(prior.round() as usize);
}
}

fn in_empty_area(&self, board: &Board, m: &Move) -> bool {
Expand All @@ -97,10 +88,6 @@ impl Prior {
matcher.pattern_count(board, &m.coord())
}

fn large_pattern_probability(&self, board: &Board, m: &Move, matcher: &Arc<LargePatternMatcher>) -> f32 {
matcher.pattern_probability(board, &m.coord())
}

fn record_priors(&mut self, plays: usize, wins: usize) {
self.plays += plays;
self.wins += wins;
Expand All @@ -115,9 +102,9 @@ impl Prior {
}
}

pub fn calculate(board: Board, child_moves: Vec<Move>, small_pattern_matcher: &Arc<SmallPatternMatcher>, large_pattern_matcher: &Arc<LargePatternMatcher>, config: &Arc<Config>) -> Vec<Prior> {
pub fn calculate(board: Board, child_moves: Vec<Move>, small_pattern_matcher: &Arc<SmallPatternMatcher>, config: &Arc<Config>) -> Vec<Prior> {
let mut priors: Vec<Prior> = child_moves.iter()
.map(|m| Prior::new(&board, m, small_pattern_matcher, large_pattern_matcher, config.clone()))
.map(|m| Prior::new(&board, m, small_pattern_matcher, config.clone()))
.collect();
let color = board.next_player().opposite();
let in_danger = board.chains().iter()
Expand Down
7 changes: 2 additions & 5 deletions src/engine/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use board::Board;
use board::Move;
use config::Config;
use patterns::LargePatternMatcher;
use patterns::SmallPatternMatcher;
use playout::Playout;
use playout::PlayoutResult;
Expand Down Expand Up @@ -75,7 +74,6 @@ pub struct Worker {
board: Option<Board>,
config: Arc<Config>,
id: Option<usize>,
large_pattern_matcher: Arc<LargePatternMatcher>,
playout: Arc<Playout>,
rng: XorShiftRng,
send_to_main: Sender<Response>,
Expand All @@ -85,13 +83,12 @@ pub struct Worker {

impl Worker {

pub fn new(config: &Arc<Config>, playout: &Arc<Playout>, small_pattern_matcher: &Arc<SmallPatternMatcher>, large_pattern_matcher: &Arc<LargePatternMatcher>, send_to_main: &Sender<Response>) -> Worker {
pub fn new(config: &Arc<Config>, playout: &Arc<Playout>, small_pattern_matcher: &Arc<SmallPatternMatcher>, send_to_main: &Sender<Response>) -> Worker {
let rng = weak_rng();
Worker {
board: None,
config: config.clone(),
id: None,
large_pattern_matcher: large_pattern_matcher.clone(),
playout: playout.clone(),
rng: rng,
send_to_main: send_to_main.clone(),
Expand Down Expand Up @@ -159,7 +156,7 @@ impl Worker {
for &m in moves.iter() {
b.play_legal_move(m);
}
let priors = prior::calculate(b, child_moves, &self.small_pattern_matcher, &self.large_pattern_matcher, &self.config);
let priors = prior::calculate(b, child_moves, &self.small_pattern_matcher, &self.config);
let answer = Answer::CalculatePriors {
moves: moves,
path: path,
Expand Down
7 changes: 2 additions & 5 deletions src/gtp/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

pub use config::Config;
pub use engine::Engine;
pub use patterns::LargePatternMatcher;
pub use patterns::SmallPatternMatcher;
pub use ruleset::CGOS;
pub use ruleset::KgsChinese;
Expand All @@ -51,8 +50,7 @@ describe! interpreter {
c.ruleset = CGOS;
let config = Arc::new(c);
let small_pattern_matcher = Arc::new(SmallPatternMatcher::new());
let large_pattern_matcher = Arc::new(LargePatternMatcher::test());
let engine = Engine::new(config.clone(), small_pattern_matcher, large_pattern_matcher);
let engine = Engine::new(config.clone(), small_pattern_matcher);
let mut interpreter = GTPInterpreter::new(config.clone(), engine);
}

Expand Down Expand Up @@ -441,8 +439,7 @@ describe! interpreter {
c.ruleset = KgsChinese;
let config = Arc::new(c);
let small_pattern_matcher = Arc::new(SmallPatternMatcher::new());
let large_pattern_matcher = Arc::new(LargePatternMatcher::test());
let engine = Engine::new(config.clone(), small_pattern_matcher, large_pattern_matcher);
let engine = Engine::new(config.clone(), small_pattern_matcher);
let mut interpreter = GTPInterpreter::new(config.clone(), engine);
}

Expand Down
4 changes: 0 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ extern crate num;
extern crate num_cpus;
extern crate quicksort;
extern crate rand;
extern crate rayon;
extern crate regex;
extern crate smallvec;
extern crate test;
Expand All @@ -46,7 +45,6 @@ extern crate toml;
pub use config::*;
use engine::Engine;
use gtp::driver::Driver;
use patterns::LargePatternMatcher;
use patterns::SmallPatternMatcher;
use ruleset::Ruleset;

Expand Down Expand Up @@ -149,12 +147,10 @@ fn main() {
// Instantiate only one matcher as it does a lot of computation
// during setup.
let small_pattern_matcher = Arc::new(SmallPatternMatcher::new());
let large_pattern_matcher = Arc::new(LargePatternMatcher::new(config.clone()));

let engine = Engine::new(
config.clone(),
small_pattern_matcher,
large_pattern_matcher,
);

config.log(format!("Current configuration: {:#?}", config));
Expand Down
Loading

0 comments on commit d6052cc

Please sign in to comment.