fix pyr agent and state (#3)

This commit is contained in:
Eason
2024-05-02 00:10:52 +08:00
parent 833fcb8978
commit a977669056
5 changed files with 30 additions and 26 deletions

View File

@ -64,26 +64,27 @@ impl AIAgent {
}
fn get_reward(&self, new_state: &AIState) -> f64 {
let old_state = self.old_state.as_ref().unwrap();
let new_positive_distance = new_state
.get_postivie_food()
.map(|food| food.x + food.y)
.unwrap_or(0.0);
let old_positive_distance = old_state
.get_postivie_food()
.map(|food| food.x + food.y)
.unwrap_or(0.0);
let new_negative_distance = new_state
.get_negative_food()
.map(|food| food.x + food.y)
.unwrap_or(0.0);
let old_negative_distance = old_state
.get_negative_food()
.map(|food| food.x + food.y)
.unwrap_or(0.0);
// let new_positive_distance = new_state
// .get_postivie_food()
// .map(|food| food.x + food.y)
// .unwrap_or(0.0);
// let old_positive_distance = old_state
// .get_postivie_food()
// .map(|food| food.x + food.y)
// .unwrap_or(0.0);
// let new_negative_distance = new_state
// .get_negative_food()
// .map(|food| food.x + food.y)
// .unwrap_or(0.0);
// let old_negative_distance = old_state
// .get_negative_food()
// .map(|food| food.x + food.y)
// .unwrap_or(0.0);
return (old_positive_distance - new_positive_distance) as f64
+ (new_negative_distance - old_negative_distance) as f64
+ 100.0*(new_state.player.score - old_state.player.score) as f64;
return
// (old_positive_distance - new_positive_distance) as f64
// + (new_negative_distance - old_negative_distance) as f64
100.0*(new_state.player.score - old_state.player.score) as f64;
}
pub fn tick(&mut self, state: AIState) -> AIAction {
self.step += 1;

View File

@ -29,7 +29,7 @@ fn food_distance<'a>(player: &'a Player) -> impl FnMut(&&Food) -> i32 + 'a {
move |food: &&Food| {
let dx = player.x - food.x;
let dy = player.y - food.y;
((dx + dy) * 100.0) as i32
((dx.abs() + dy.abs()) * 100.0) as i32
}
}
impl AIState {