Add weights for dijkstra.

This commit is contained in:
hal8174 2024-03-21 23:41:26 +01:00
parent ec1a255898
commit a145c4b70c

View file

@ -16,7 +16,7 @@ pub mod conflict_avoidance;
#[derive(Default, Clone, Copy)] #[derive(Default, Clone, Copy)]
pub struct Field { pub struct Field {
pub blocked: bool, pub blocked: bool,
wheight: f64, weight: f64,
} }
pub struct Problem { pub struct Problem {
@ -55,36 +55,36 @@ impl Problem {
} }
fn calculate_wheights(&mut self, without: usize) { fn calculate_wheights(&mut self, without: usize) {
// for x in 0..self.map.width { for x in 0..self.map.width {
// for y in 0..self.map.height { for y in 0..self.map.height {
// let mut wheight = 1.0; let mut weight = 0.0;
// if self.map.get(x, y).blocked { if self.map.get(x, y).blocked {
// wheight += 100.0; weight += 100.0;
// } }
// self.map.get_mut(x, y).wheight = wheight; self.map.get_mut(x, y).weight = weight;
// } }
// } }
// for (i, path) in self.path.iter().enumerate() { for (i, path) in self.path.iter().enumerate() {
// if i != without { if i != without {
// for p in path { for p in path {
// let weight = 50.0; let weight = 0.5;
// let x = p.x as usize; let x = p.0.x as usize;
// let y = p.y as usize; let y = p.0.y as usize;
// self.map.get_mut(x, y).wheight += weight; self.map.get_mut(x, y).weight += weight;
// } }
// } }
// } }
// for p in &self.start { // for p in &self.start {
// self.map.get_mut(p.0.x as usize, p.0.y as usize).wheight = f64::INFINITY; // self.map.get_mut(p.0.x as usize, p.0.y as usize).weight = f64::INFINITY;
// } // }
// for p in &self.end { // for p in &self.end {
// self.map.get_mut(p.0.x as usize, p.0.y as usize).wheight = f64::INFINITY; // self.map.get_mut(p.0.x as usize, p.0.y as usize).weight = f64::INFINITY;
// } // }
} }
@ -173,10 +173,12 @@ impl<'a> WheightedGraph for MapInternal<'a> {
if self.map.get(next.x as usize, next.y as usize).blocked && self.end != (next, node.1) { if self.map.get(next.x as usize, next.y as usize).blocked && self.end != (next, node.1) {
return None; return None;
} }
let penalty = self.map.get(next.x as usize, next.y as usize).weight;
match num { match num {
0 => Some(((next, node.1), 1.5)), 0 => Some(((next, node.1), 1.5 + penalty)),
1 => Some(((next, node.1.counter_clockwise()), 1.5)), 1 => Some(((next, node.1.counter_clockwise()), 1.5 + penalty)),
2 => Some(((next, node.1.clockwise()), 1.5)), 2 => Some(((next, node.1.clockwise()), 1.5 + penalty)),
_ => { _ => {
let mut count = 2; let mut count = 2;
for l in 2..=6 { for l in 2..=6 {
@ -195,7 +197,8 @@ impl<'a> WheightedGraph for MapInternal<'a> {
if !self.map.get(n.x as usize, n.y as usize).blocked { if !self.map.get(n.x as usize, n.y as usize).blocked {
count += 1; count += 1;
if count == num { if count == num {
return Some(((n, node.1), 17.5)); let penalty = penalty + self.map.get(n.x as usize, n.y as usize).weight;
return Some(((n, node.1), 17.5 + penalty));
} }
} }
} }