Skip to content

Commit

Permalink
Tested zero-by on 2 players; added 10 player option
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierrog committed Nov 5, 2023
1 parent 1a214f7 commit 8bb58c0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/database/bpdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<const N: usize> Database<N> for BPDatabase<N>

fn put(&mut self, state: State, record: Record<N>)
{
self.mem.insert(state, Mutex::new(Record::<N>::default()));
self.mem.insert(state, Mutex::new(record));
}

fn get(&self, state: State) -> Option<Record<N>>
Expand Down
4 changes: 2 additions & 2 deletions src/database/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl<const N: usize> Display for Record<N>
{
write!(
f,
"{} {}\n{} {}\n{} {}\n{} {}",
"Utility:".green().bold(),
"{}\n{}{} {}\n{} {}\n{} {}",
"Utility vector:".green().bold(),
self.util,
"Remoteness:".bold(),
self.rem,
Expand Down
68 changes: 63 additions & 5 deletions src/games/zero_by/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,20 @@ impl Game for Session

fn solvers(&self) -> HashMap<String, Solver<Self>>
{
let acyclic: Solver<Self> = <Self as AcyclicSolver<2>>::solve;
collection! {
<Self as AcyclicSolver<2>>::name() => acyclic,
match self.players {
2 => {
let acyclic: Solver<Self> = <Self as AcyclicSolver<2>>::solve;
collection! {
<Self as AcyclicSolver<2>>::name() => acyclic,
}
},
10 => {
let acyclic: Solver<Self> = <Self as AcyclicSolver<10>>::solve;
collection! {
<Self as AcyclicSolver<10>>::name() => acyclic,
}
}
_ => todo!()
}
}
}
Expand All @@ -109,9 +120,16 @@ impl Automaton<State> for Session
self.by
.iter()
.cloned()
.map(|choice| if state <= choice { state } else { choice })
.filter(|&choice| state >= choice)
.map(|choice| state - choice)
.map(|output| utils::pack_turn(output, turn, self.players))
.map(|output| {
utils::pack_turn(
output,
(turn + 1) % self.players,
self.players
)
})
.collect::<Vec<State>>()
}

Expand All @@ -125,7 +143,8 @@ impl Automaton<State> for Session
/* SOLVABLE DECLARATIONS */

implement! { for Session =>
AcyclicallySolvable<2>
AcyclicallySolvable<2>,
AcyclicallySolvable<10>
}

impl Solvable<2> for Session
Expand Down Expand Up @@ -155,3 +174,42 @@ impl Solvable<2> for Session
)
}
}

impl Solvable<10> for Session
{
fn weights(&self) -> SMatrix<Utility, 10, 10>
{
SMatrix::<Utility, 10, 10>::identity()
}

fn utility(&self, state: State) -> Option<SVector<Utility, 10>>
{
let (state, turn) = utils::unpack_turn(state, 10);
if !self.accepts(state) {
None
} else {
let mut result: SVector<Utility, 10> = SVector::<Utility, 10>::zeros();
for i in 0..10 {
if turn == i {
result[i as usize] = -9;
} else {
result[i as usize] = 1;
}
}
Some(result)
}
}

fn coalesce(&self, state: State) -> SVector<Utility, 10> {
let (_, turn) = utils::unpack_turn(state, 10);
let mut result: SVector<Utility, 10> = SVector::<Utility, 10>::zeros();
for i in 0..10 {
if turn == i {
result[i as usize] = 1;
} else {
result[i as usize] = 0;
}
}
result
}
}
9 changes: 4 additions & 5 deletions src/solvers/acyclic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ fn traverse<G: AcyclicallySolvable<N>, const N: usize>(
)
}
let mut available: HashSet<Record<N>> = HashSet::new();
for state in game.transition(state) {
if let Some(out) = db.get(state) {
for next in game.transition(state) {
if let Some(out) = db.get(next) {
available.insert(out);
} else {
let out = traverse(state, game, db);
let out = traverse(next, game, db);
available.insert(out);
db.put(state, out);
}
}
let matrix = game.weights();
Expand Down Expand Up @@ -98,7 +97,7 @@ fn select_record<const N: usize>(
let curr_dot = (matrix * r.util).dot(&coalition);
if curr_dot > dot || (curr_dot == dot && rem > r.rem) {
result.util = r.util;
result.rem = rem + 1;
result.rem = r.rem + 1;
dot = curr_dot;
rem = r.rem;
}
Expand Down

0 comments on commit 8bb58c0

Please sign in to comment.