Skip to content

Commit

Permalink
Refactor mate (#118)
Browse files Browse the repository at this point in the history
* misc

* separate selector from traverser

* resolver is subtrait of VCTHelper

* move field to analysis
  • Loading branch information
yubessy authored Aug 13, 2022
1 parent 5131f9d commit 8381959
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 528 deletions.
1 change: 1 addition & 0 deletions src/analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod field;
File renamed without changes.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod analysis;
pub mod board;
pub mod mate;
pub mod wasm;
Expand Down
2 changes: 1 addition & 1 deletion src/mate/vct.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod field;
mod generator;
mod helper;
mod proof;
mod resolver;
mod searcher;
mod selector;
mod solver;
mod state;
mod traverser;
Expand Down
6 changes: 2 additions & 4 deletions src/mate/vct/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use super::helper::VCFHelper;
use super::state::VCTState;
use crate::mate::game::*;
use crate::mate::mate::Mate;
use crate::mate::state::State;
use crate::mate::vct::proof::*;

pub trait Resolver: ProofTree {
fn solve_attacker_vcf(&mut self, state: &VCTState) -> Option<Mate>;
fn solve_attacker_threat(&mut self, state: &VCTState) -> Option<Mate>;

pub trait Resolver: ProofTree + VCFHelper {
fn resolve(&mut self, state: &mut VCTState) -> Option<Mate> {
self.resolve_attacks(state)
}
Expand Down
74 changes: 74 additions & 0 deletions src/mate/vct/selector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::board::Point;
use crate::mate::vct::proof::*;
use crate::mate::vct::state::VCTState;

pub struct Selection {
pub best: Option<Point>,
pub current: Node,
pub next1: Node,
pub next2: Node,
}

pub trait Selector: ProofTree {
fn select_attack(&mut self, state: &mut VCTState, attacks: &[Point]) -> Selection {
let limit = state.limit;
let mut best: Option<Point> = Some(attacks[0]);
let mut current = Node::zero_dn(limit);
let mut next1 = Node::zero_dn(limit);
let mut next2 = Node::zero_dn(limit);
let init = Node::unit_dn(attacks.len() as u32, limit); // trick
for &attack in attacks {
let maybe_child = self.attacker_table().lookup_next(state, Some(attack));
let child = maybe_child.unwrap_or(init);
current = current.min_pn_sum_dn(child);
if child.pn < next1.pn {
best.replace(attack);
next2 = next1;
next1 = child;
} else if child.pn < next2.pn {
next2 = child;
}
if current.pn == 0 {
current.dn = INF;
break;
}
}
Selection {
best: best,
current: current,
next1: next1,
next2: next2,
}
}

fn select_defence(&mut self, state: &mut VCTState, defences: &[Point]) -> Selection {
let limit = state.limit;
let mut best: Option<Point> = Some(defences[0]);
let mut current = Node::zero_pn(limit - 1);
let mut next1 = Node::zero_pn(limit - 1);
let mut next2 = Node::zero_pn(limit - 1);
let init = Node::unit_pn(defences.len() as u32, limit - 1); // trick
for &defence in defences {
let maybe_child = self.defender_table().lookup_next(state, Some(defence));
let child = maybe_child.unwrap_or(init);
current = current.min_dn_sum_pn(child);
if child.dn < next1.dn {
best.replace(defence);
next2 = next1;
next1 = child;
} else if child.dn < next2.dn {
next2 = child;
}
if current.dn == 0 {
current.pn = INF;
break;
}
}
Selection {
best: best,
current: current,
next1: next1,
next2: next2,
}
}
}
53 changes: 23 additions & 30 deletions src/mate/vct/solver/dfpns.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::board::Point;
use crate::mate::mate::Mate;
use crate::mate::vcf;
use crate::mate::vct::generator::*;
use crate::mate::vct::generator::Generator;
use crate::mate::vct::helper::VCFHelper;
use crate::mate::vct::proof::*;
use crate::mate::vct::resolver::Resolver;
use crate::mate::vct::searcher::Searcher;
use crate::mate::vct::selector::*;
use crate::mate::vct::solver::VCTSolver;
use crate::mate::vct::state::VCTState;
use crate::mate::vct::traverser::*;
use lru::LruCache;

Expand Down Expand Up @@ -51,13 +50,21 @@ impl Generator for DFPNSVCTSolver {
}
}

impl ProofTree for DFPNSVCTSolver {
fn attacker_table(&mut self) -> &mut Table {
&mut self.attacker_table
impl VCFHelper for DFPNSVCTSolver {
fn attacker_vcf_depth(&self) -> u8 {
self.attacker_vcf_depth
}

fn defender_table(&mut self) -> &mut Table {
&mut self.defender_table
fn defender_vcf_depth(&self) -> u8 {
self.defender_vcf_depth
}

fn attacker_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.attacker_vcf_solver
}

fn defender_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.defender_vcf_solver
}
}

Expand All @@ -73,30 +80,16 @@ impl Traverser for DFPNSVCTSolver {

impl DFPNSTraverser for DFPNSVCTSolver {}

impl Resolver for DFPNSVCTSolver {
fn solve_attacker_vcf(&mut self, state: &VCTState) -> Option<Mate> {
VCFHelper::solve_attacker_vcf(self, state)
}
impl Selector for DFPNSVCTSolver {}

fn solve_attacker_threat(&mut self, state: &VCTState) -> Option<Mate> {
VCFHelper::solve_attacker_threat(self, state)
}
}

impl VCFHelper for DFPNSVCTSolver {
fn attacker_vcf_depth(&self) -> u8 {
self.attacker_vcf_depth
}

fn defender_vcf_depth(&self) -> u8 {
self.defender_vcf_depth
}

fn attacker_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.attacker_vcf_solver
impl ProofTree for DFPNSVCTSolver {
fn attacker_table(&mut self) -> &mut Table {
&mut self.attacker_table
}

fn defender_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.defender_vcf_solver
fn defender_table(&mut self) -> &mut Table {
&mut self.defender_table
}
}

impl Resolver for DFPNSVCTSolver {}
53 changes: 23 additions & 30 deletions src/mate/vct/solver/dfs.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::board::Point;
use crate::mate::mate::Mate;
use crate::mate::vcf;
use crate::mate::vct::generator::*;
use crate::mate::vct::generator::Generator;
use crate::mate::vct::helper::VCFHelper;
use crate::mate::vct::proof::*;
use crate::mate::vct::resolver::Resolver;
use crate::mate::vct::searcher::Searcher;
use crate::mate::vct::selector::*;
use crate::mate::vct::solver::VCTSolver;
use crate::mate::vct::state::VCTState;
use crate::mate::vct::traverser::*;
use lru::LruCache;

Expand Down Expand Up @@ -51,13 +50,21 @@ impl Generator for DFSVCTSolver {
}
}

impl ProofTree for DFSVCTSolver {
fn attacker_table(&mut self) -> &mut Table {
&mut self.attacker_table
impl VCFHelper for DFSVCTSolver {
fn attacker_vcf_depth(&self) -> u8 {
self.attacker_vcf_depth
}

fn defender_table(&mut self) -> &mut Table {
&mut self.defender_table
fn defender_vcf_depth(&self) -> u8 {
self.defender_vcf_depth
}

fn attacker_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.attacker_vcf_solver
}

fn defender_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.defender_vcf_solver
}
}

Expand All @@ -73,30 +80,16 @@ impl Traverser for DFSVCTSolver {

impl DFSTraverser for DFSVCTSolver {}

impl Resolver for DFSVCTSolver {
fn solve_attacker_vcf(&mut self, state: &VCTState) -> Option<Mate> {
VCFHelper::solve_attacker_vcf(self, state)
}
impl Selector for DFSVCTSolver {}

fn solve_attacker_threat(&mut self, state: &VCTState) -> Option<Mate> {
VCFHelper::solve_attacker_threat(self, state)
}
}

impl VCFHelper for DFSVCTSolver {
fn attacker_vcf_depth(&self) -> u8 {
self.attacker_vcf_depth
}

fn defender_vcf_depth(&self) -> u8 {
self.defender_vcf_depth
}

fn attacker_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.attacker_vcf_solver
impl ProofTree for DFSVCTSolver {
fn attacker_table(&mut self) -> &mut Table {
&mut self.attacker_table
}

fn defender_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.defender_vcf_solver
fn defender_table(&mut self) -> &mut Table {
&mut self.defender_table
}
}

impl Resolver for DFSVCTSolver {}
54 changes: 23 additions & 31 deletions src/mate/vct/solver/pns.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::board::Point;
use crate::mate::mate::Mate;
use crate::mate::vcf;
use crate::mate::vct::generator::*;
use crate::mate::vct::generator::Generator;
use crate::mate::vct::helper::VCFHelper;
use crate::mate::vct::proof::*;
use crate::mate::vct::resolver::Resolver;
use crate::mate::vct::searcher::Searcher;
use crate::mate::vct::selector::*;
use crate::mate::vct::solver::VCTSolver;
use crate::mate::vct::state::VCTState;
use crate::mate::vct::traverser::*;
use lru::LruCache;

Expand Down Expand Up @@ -41,6 +40,8 @@ impl VCTSolver for PNSVCTSolver {}

impl Searcher for PNSVCTSolver {}

impl Selector for PNSVCTSolver {}

impl Generator for PNSVCTSolver {
fn attacks_cache(&mut self) -> &mut LruCache<u64, Result<Vec<Point>, Node>> {
&mut self.attacks_cache
Expand All @@ -51,16 +52,23 @@ impl Generator for PNSVCTSolver {
}
}

impl ProofTree for PNSVCTSolver {
fn attacker_table(&mut self) -> &mut Table {
&mut self.attacker_table
impl VCFHelper for PNSVCTSolver {
fn attacker_vcf_depth(&self) -> u8 {
self.attacker_vcf_depth
}

fn defender_table(&mut self) -> &mut Table {
&mut self.defender_table
fn defender_vcf_depth(&self) -> u8 {
self.defender_vcf_depth
}

fn attacker_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.attacker_vcf_solver
}
}

fn defender_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.defender_vcf_solver
}
}
impl Traverser for PNSVCTSolver {
fn next_threshold_attack(&self, selection: &Selection, threshold: Node) -> Node {
PNSTraverser::next_threshold_attack(self, selection, threshold)
Expand All @@ -73,30 +81,14 @@ impl Traverser for PNSVCTSolver {

impl PNSTraverser for PNSVCTSolver {}

impl Resolver for PNSVCTSolver {
fn solve_attacker_vcf(&mut self, state: &VCTState) -> Option<Mate> {
VCFHelper::solve_attacker_vcf(self, state)
impl ProofTree for PNSVCTSolver {
fn attacker_table(&mut self) -> &mut Table {
&mut self.attacker_table
}

fn solve_attacker_threat(&mut self, state: &VCTState) -> Option<Mate> {
VCFHelper::solve_attacker_threat(self, state)
fn defender_table(&mut self) -> &mut Table {
&mut self.defender_table
}
}

impl VCFHelper for PNSVCTSolver {
fn attacker_vcf_depth(&self) -> u8 {
self.attacker_vcf_depth
}

fn defender_vcf_depth(&self) -> u8 {
self.defender_vcf_depth
}

fn attacker_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.attacker_vcf_solver
}

fn defender_vcf_solver(&mut self) -> &mut vcf::IDDFSSolver {
&mut self.defender_vcf_solver
}
}
impl Resolver for PNSVCTSolver {}
Loading

0 comments on commit 8381959

Please sign in to comment.