Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sink_states termination strategy #15

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ rand = "0.8"
[[example]]
name = "eucdist"
path = "src/examples/eucdist.rs"


[[example]]
name = "weightedcoin"
path = "src/examples/weightedcoin.rs"
80 changes: 80 additions & 0 deletions src/examples/weightedcoin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use rurel::mdp::{Agent, State};
use rurel::strategy::explore::RandomExploration;
use rurel::strategy::learn::QLearning;
use rurel::strategy::terminate::SinkStates;
use rurel::AgentTrainer;

const TARGET : i32 = 100;
const WEIGHT : u8 = 100; //portion of 255

#[derive(PartialEq, Eq, Hash, Clone)]
struct CoinState{
balance : i32
}

#[derive(PartialEq, Eq, Hash, Clone)]
struct CoinAction{
bet : i32
}

impl State for CoinState {
type A = CoinAction;

fn reward(&self) -> f64 {
if self.balance>=TARGET {1.0}
else {0.0}
}

fn actions(&self) -> Vec<CoinAction> {
let bet_range = {
milanboers marked this conversation as resolved.
Show resolved Hide resolved
if self.balance<TARGET/2 {1..self.balance+1}
else {1..(TARGET-self.balance)+1}
};
return bet_range.map(|bet|CoinAction{bet:bet}).collect();
}
}

struct CoinAgent{state:CoinState}
impl Agent<CoinState> for CoinAgent{
fn current_state(&self) -> &CoinState {
&self.state
}
fn take_action(&mut self, action: &CoinAction) -> () {
//Update the state to:
self.state = CoinState { balance :
if rand::random::<u8>() <= WEIGHT {self.state.balance+action.bet}
//If the coin is heads, balance + bet
else {self.state.balance-action.bet}
//If the coin is tails, balance - bet
}
}
}

fn main() {
const TRIALS:usize=1000000;
let mut trainer=AgentTrainer::new();
for trial in 0..TRIALS{
let mut agent = CoinAgent {state: CoinState{balance:((1+trial%98) as i32)}};
trainer.train(
&mut agent,
&QLearning::new(0.2,1.0,0.0),
&mut SinkStates{},
&RandomExploration::new()
);
}

println!("Balance\tBet\tQ-value");
for balance in 1..TARGET{
let state = CoinState{balance:balance};
let action = trainer.best_action(&state).unwrap();
println!("{}\t{}\t{}",
balance,
action.bet,
trainer.expected_value(&state,&action).unwrap()
);
}
}
2 changes: 2 additions & 0 deletions src/strategy/terminate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
//! Module containing termination strategies.

pub use self::fixed_iterations::FixedIterations;
pub use self::sink_states::SinkStates;
use crate::mdp::State;

pub mod fixed_iterations;
pub mod sink_states;

/// A termination strategy decides when to end training.
pub trait TerminationStrategy<S: State> {
Expand Down
16 changes: 16 additions & 0 deletions src/strategy/terminate/sink_states.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Module for the fixed iterations strategy.

use crate::mdp::State;
use crate::strategy::terminate::TerminationStrategy;

/// The termination strategy that ends if it's at a terminal state (no actions)
pub struct SinkStates {}
impl<S: State> TerminationStrategy<S> for SinkStates {
fn should_stop(&mut self, state: &S) -> bool {
state.actions().is_empty()
}
}