milanboers / rurel Goto Github PK
View Code? Open in Web Editor NEWFlexible, reusable reinforcement learning (Q learning) implementation in Rust
License: Mozilla Public License 2.0
Flexible, reusable reinforcement learning (Q learning) implementation in Rust
License: Mozilla Public License 2.0
My guy
please document your code
it is very cool and interesting
but I cannot understand it
peace out
I think the library should offer a way to dump the learned state to file (basically just the HashMap of the AgentTrainer) to save the learned state, checkpoint or continue learning an earlier state. This could be hacked from the outside with some unsafe code, though I think offering access to q
within AgentTrainer would be useful.
Hey! cool crate, I'm playing around with it a bit. I realized that I can't seem to represent a state from which there are no actions, I'm training a model to play a game and in this game there are some "end states" like the end of the game where you're scored on your performance, but if I have my State::actions function return an empty vec I get a div/0 error here :https://github.com/milanboers/rurel/blob/40d0fa7116c528953780b74e0a19756182a70a72/src/mdp/mod.rs#L22C59-L22C59
since there are no actions. Maybe I'm just misunderstanding how this is supposed to work :)
It seems that if the State::actions()
returns an empty vector, the whole system crashes. I am new to reinforcement learning, so I might be using it incorrectly. My setup:
actions()
generates a list of actions available for the current board stateQuestion... can this library be used for something like Pong - i.e. where the reward isn't known right away, but rather becomes eventually known and somehow backpropogated?
I guess the reward could sortof be immediately known, by checking y-distance from the ball.. but that's not a huge win over just manually making it chase the ball. It'd be nicer to have the AI figure out other things - like trying to hit the ball such that it makes the opponent chase it a longer distance.
Sorry for the naive question - I haven't jumped into ML yet and I'm just tinkering around with a webassembly pong thing and thought this library might be a nice way to drive the AI :)
Running the example and adding some debugging code, I'm finding that the neural network is not learning anything at all.
let mut trainer = AgentTrainer::new();
let mut agent = MyAgent {
state: MyState { x: 0, y: 0 },
};
trainer.train(
&mut agent,
&QLearning::new(0.2, 0.01, 2.),
&mut FixedIterations::new(10000000),
&RandomExploration::new(),
);
let state1 = MyState { x: 1, y: 0 };
let state2 = MyState { x: 0, y: 1 };
let actions = vec![MyAction { dx: 0, dy: -1 }, MyAction { dx: -1, dy: 0 }];
for action in actions {
println!(
"1: {:?} {:?} {:?}",
state1,
action,
trainer.expected_value(&state1, &action),
);
println!(
"2: {:?} {:?} {:?}",
state2,
action,
trainer.expected_value(&state2, &action),
);
println!();
}
1: MyState { x: 1, y: 0 } MyAction { dx: 0, dy: -1 } Some(-13.582118848154376)
2: MyState { x: 0, y: 1 } MyAction { dx: 0, dy: -1 } Some(-14.27795681221249)
1: MyState { x: 1, y: 0 } MyAction { dx: -1, dy: 0 } Some(-14.27795681221249)
2: MyState { x: 0, y: 1 } MyAction { dx: -1, dy: 0 } Some(-13.582118848154376)
It seems that it hasn't learned that even with x:1 and y:0, dx:-1 and dy:0 is the best move. Am I misunderstanding the example or anything here?
I want to use this with an MDP that has sink states. That is, states with reward and no possible actions. Can I do so with this, and if so, how?
I've modified the eucdist example to add Display for MyAction which prints an arrow based on the action.
And added a function entry_to_action
which gets the most likely action from a given state (if I'm not wrong):
fn entry_to_action(entry: &HashMap<MyAction, f64>) -> Option<&MyAction> {
entry
.iter()
.max_by(|(_, v1), (_, v2)| v1.partial_cmp(v2).unwrap_or(Ordering::Equal))
.map(|(a, _)| a)
}
And after running the example, it prints this:
→ → → → → → → → → → → → → → → → → → → → ↑
↓ → → → → → → → → → → → → → → → → → → ↑ ↑
↓ ↓ → → → → → → → → → → → → → → → → ↑ ↑ ↑
↓ ↓ ↓ ↓ → → → → → → → → → → → → → ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ → → → → → → → → → → → → ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ → → → → → → → → → → ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ → → → → → → → ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ → → → → → → ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ → → → → ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ → → → ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ← ← ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ← ← ← ← ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ↓ ← ← ← ← ← ← ← ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ← ← ← ← ← ← ← ← ↑ ↑ ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ↓ ↓ ← ← ← ← ← ← ← ← ← ← ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ← ← ← ← ← ← ← ← ← ← ← ← ↑ ↑ ↑ ↑ ↑
↓ ↓ ↓ ↓ ← ← ← ← ← ← ← ← ← ← ← ← ← ← ↑ ↑ ↑
↓ ↓ ↓ ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ↑ ↑ ↑
↓ ↓ ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ↑ ↑
← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ←
I expected that the arrows would all point toward the center right?
Heres the code for printing the arrows:
impl fmt::Display for MyAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
MyAction::Move { dx, dy } => {
match (dx, dy) {
(-1, 0) => write!(f, "{LEFT_ARROW}"),
(1, 0) => write!(f, "{RIGHT_ARROW}"),
(0, -1) => write!(f, "{UP_ARROW}"),
(0, 1) => write!(f, "{DOWN_ARROW}"),
_ => unreachable!()
}
}
}
}
}
I really love the approach of this library, but can't seem to figure out how to make program play with itself. Can I exchange states between 2 agents in any way, or has this not been accounted for yet?
Is it possible to train a single step at a time, so I can have it run within something like a bevy system?
It seems like the current train method is usually done with a certain amount of iterations, but perhaps a function step
or train_step
would be convenient?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.