Skip to content

Commit

Permalink
Add parallel Nash and Pareto methods
Browse files Browse the repository at this point in the history
  • Loading branch information
walkie committed May 26, 2024
1 parent 802d2cc commit 3f75296
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion t4t-games/src/rps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn fire_water() -> Normal<Move, i64, 2> {
///
/// Note that `rps1000` demonstrates that `Normal` can represent extremely large games---this game
/// has a payoff table with `3^1000` entries! Such large games can be represented and played
/// without issue, but any function that iterates over the outcomes (such as
/// without issue. However, any function that iterates over the outcomes (such as
/// [`is_zero_sum`](t4t::Normal::is_zero_sum) or any solution concept), will leave you waiting
/// beyond the heat death of the universe.
#[rustfmt::skip]
Expand Down
1 change: 1 addition & 0 deletions t4t/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ log = "0.4.21"
num = "0.4.3"
rand = "0.8.5"
rand_distr = "0.4.3"
rayon = "1.10.0"

[dev-dependencies]
env_logger = "0.11.3"
Expand Down
27 changes: 27 additions & 0 deletions t4t/src/normal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num::Zero;
use rayon::prelude::*;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::iter::Iterator;
Expand Down Expand Up @@ -587,6 +588,19 @@ impl<M: Move, U: Utility, const P: usize> Normal<M, U, P> {
nash
}

/// A variant of [`pure_nash_equilibria`] that analyzes the outcomes in parallel.
pub fn pure_nash_equlibria_parallel(&self) -> Vec<Profile<M, P>> {
let (sender, receiver) = std::sync::mpsc::channel();
self.possible_profiles()
.par_bridge()
.for_each_with(sender, |s, profile| {
if self.is_stable(profile) {
s.send(profile).unwrap();
}
});
receiver.iter().collect()
}

/// Return a new profile that represents a
/// [Pareto improvement](https://en.wikipedia.org/wiki/Pareto_efficiency)
/// on the given profile, if one exists.
Expand Down Expand Up @@ -634,6 +648,19 @@ impl<M: Move, U: Utility, const P: usize> Normal<M, U, P> {
pareto
}

/// A variant of [`pareto_optimal_solutions`] that analyzes the outcomes in parallel.
pub fn pareto_optimal_solutions_parallel(&self) -> Vec<Profile<M, P>> {
let (sender, receiver) = std::sync::mpsc::channel();
self.possible_profiles()
.par_bridge()
.for_each_with(sender, |s, profile| {
if self.is_pareto_optimal(profile) {
s.send(profile).unwrap();
}
});
receiver.iter().collect()
}

/// Get all dominated move relationships for the given player. If a move is dominated by
/// multiple different moves, it will contain multiple entries in the returned vector.
///
Expand Down

0 comments on commit 3f75296

Please sign in to comment.