-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use crate::*; | ||
use noisy_float::prelude::*; | ||
|
||
#[derive(Builder, Copy, Clone, PartialEq)] | ||
pub struct FullSearchStrategy { | ||
#[builder(default = "n64(1.0)")] | ||
win_reward: N64, | ||
|
||
#[builder(default = "n64(0.2)")] | ||
draw_reward: N64, | ||
|
||
#[builder(default = "n64(0.0)")] | ||
loose_reward: N64, | ||
} | ||
|
||
impl FullSearchStrategy { | ||
pub fn new() -> FullSearchStrategy { | ||
Default::default() | ||
} | ||
|
||
fn rate_action<S: State>(&self, state: &mut S, action: &S::Action, player: S::Player) -> N64 { | ||
state | ||
.action_effects(action) | ||
.map(|effect| { | ||
let possibility = n64(state.apply_effect(&effect)); | ||
let rating = self.rate_state(state, player); | ||
state.unapply_effect(&effect); | ||
possibility * rating | ||
}) | ||
.sum() | ||
} | ||
|
||
fn rate_state<S: State>(&self, state: &mut S, player: S::Player) -> N64 { | ||
if let Some(result) = state.winner() { | ||
match result { | ||
Winner::Draw => return self.draw_reward, | ||
Winner::Player(winner) => { | ||
return | ||
if winner == player { | ||
self.win_reward | ||
} else { | ||
self.loose_reward | ||
} | ||
} | ||
} | ||
} | ||
|
||
let try_win = state.player() == player; | ||
|
||
let actions = state.possible_actions(); | ||
let mut best = | ||
if try_win { | ||
-N64::infinity() | ||
} else { | ||
N64::infinity() | ||
}; | ||
assert!(!actions.is_empty()); | ||
for action in actions { | ||
let rate = self.rate_action(state, &action, player); | ||
let is_better = | ||
if try_win { | ||
rate > best | ||
} else { | ||
rate < best | ||
}; | ||
if is_better { | ||
best = rate; | ||
} | ||
} | ||
best | ||
} | ||
} | ||
|
||
|
||
impl <S: State> Strategy<S> for FullSearchStrategy { | ||
type Rating = N64; | ||
|
||
fn rated_actions(&self, state: &S) -> Vec<(S::Action, Self::Rating)> { | ||
let mut mut_state = state.clone(); | ||
state | ||
.possible_actions() | ||
.into_iter() | ||
.map(|action| { | ||
let rating = self.rate_action(&mut mut_state, &action, state.player()); | ||
(action, rating) | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
impl Default for FullSearchStrategy { | ||
fn default() -> FullSearchStrategy { | ||
FullSearchStrategyBuilder::default() | ||
.build() | ||
.unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod random_strategy; | ||
mod full_search_strategy; | ||
|
||
pub use random_strategy::RandomStrategy; | ||
pub use random_strategy::RandomStrategy; | ||
pub use full_search_strategy::FullSearchStrategy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters