Use i64 instead of f64 for WheightedGraph

This commit is contained in:
hal8174 2025-03-26 17:22:37 +01:00
parent 068297e2c2
commit 6f74f1345e
6 changed files with 52 additions and 52 deletions

View file

@ -11,7 +11,7 @@ use super::*;
#[derive(Debug)] #[derive(Debug)]
struct PowerGraph { struct PowerGraph {
nodes: HashMap<(Position, ElectricPoleType), (Vec<(Position, ElectricPoleType)>, f64)>, nodes: HashMap<(Position, ElectricPoleType), (Vec<(Position, ElectricPoleType)>, i64)>,
} }
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
@ -30,7 +30,7 @@ struct Node {
impl WheightedGraph for PowerGraph { impl WheightedGraph for PowerGraph {
type Node = Node; type Node = Node;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
match node.node_type { match node.node_type {
NodeType::In => { NodeType::In => {
if num == 0 { if num == 0 {
@ -61,7 +61,7 @@ impl WheightedGraph for PowerGraph {
electric_pole_type, electric_pole_type,
node_type: NodeType::In, node_type: NodeType::In,
}, },
0.0, 0,
) )
}) })
}), }),
@ -144,15 +144,15 @@ impl Blueprint {
.placeable(Position::new(x, y), electric_pole_type.size()) .placeable(Position::new(x, y), electric_pole_type.size())
{ {
Some(match electric_pole_type { Some(match electric_pole_type {
ElectricPoleType::Small => 0.8, ElectricPoleType::Small => 8,
ElectricPoleType::Medium => 1.0, ElectricPoleType::Medium => 10,
ElectricPoleType::Big => 1.5, ElectricPoleType::Big => 15,
ElectricPoleType::Substation => todo!(), ElectricPoleType::Substation => todo!(),
}) })
} else if power_pole_map } else if power_pole_map
.contains_key(&(Position::new(x, y), electric_pole_type)) .contains_key(&(Position::new(x, y), electric_pole_type))
{ {
Some(0.0) Some(0)
} else { } else {
None None
}; };
@ -236,10 +236,10 @@ where
{ {
type Node = Option<G::Node>; type Node = Option<G::Node>;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
match node { match node {
Some(n) => self.graph.edge(n, num).map(|(e, w)| (Some(e), w)), Some(n) => self.graph.edge(n, num).map(|(e, w)| (Some(e), w)),
None => self.start_nodes.get(num).map(|n| (Some(n.clone()), 0.0)), None => self.start_nodes.get(num).map(|n| (Some(n.clone()), 0)),
} }
} }
} }
@ -346,15 +346,15 @@ impl Blueprint {
.placeable(Position::new(x, y), electric_pole_type.size()) .placeable(Position::new(x, y), electric_pole_type.size())
{ {
Some(match electric_pole_type { Some(match electric_pole_type {
ElectricPoleType::Small => 0.8, ElectricPoleType::Small => 8,
ElectricPoleType::Medium => 1.0, ElectricPoleType::Medium => 10,
ElectricPoleType::Big => 1.5, ElectricPoleType::Big => 15,
ElectricPoleType::Substation => todo!(), ElectricPoleType::Substation => todo!(),
}) })
} else if power_pole_map } else if power_pole_map
.contains_key(&(Position::new(x, y), electric_pole_type)) .contains_key(&(Position::new(x, y), electric_pole_type))
{ {
Some(0.0) Some(0)
} else { } else {
None None
}; };

View file

@ -2,7 +2,7 @@ use super::WheightedGraph;
pub struct WheightedAdjacencyList { pub struct WheightedAdjacencyList {
pub(crate) nodes: Vec<usize>, pub(crate) nodes: Vec<usize>,
pub(crate) edges: Vec<(usize, f64)>, pub(crate) edges: Vec<(usize, i64)>,
} }
impl WheightedGraph for WheightedAdjacencyList { impl WheightedGraph for WheightedAdjacencyList {
@ -17,7 +17,7 @@ impl WheightedGraph for WheightedAdjacencyList {
} }
} }
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
if num < self.num_edges(node) { if num < self.num_edges(node) {
Some(self.edges[self.nodes[*node] + num]) Some(self.edges[self.nodes[*node] + num])
} else { } else {

View file

@ -8,7 +8,7 @@ pub trait WheightedGraph: Sized {
self.edge_iter(node).count() self.edge_iter(node).count()
} }
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)>; fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)>;
fn edge_iter<'a, 'b>(&'a self, node: &'b Self::Node) -> WheightedGraphEdgeIter<'a, 'b, Self> { fn edge_iter<'a, 'b>(&'a self, node: &'b Self::Node) -> WheightedGraphEdgeIter<'a, 'b, Self> {
WheightedGraphEdgeIter::new(self, node) WheightedGraphEdgeIter::new(self, node)
@ -41,7 +41,7 @@ impl<G> Iterator for WheightedGraphEdgeIter<'_, '_, G>
where where
G: WheightedGraph, G: WheightedGraph,
{ {
type Item = (G::Node, f64); type Item = (G::Node, i64);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.count += 1; self.count += 1;

View file

@ -9,11 +9,11 @@ use super::WheightedGraph;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct QueueObject<N> { pub struct QueueObject<N> {
node: N, node: N,
score: f64, score: i64,
} }
impl<N> QueueObject<N> { impl<N> QueueObject<N> {
fn new(node: N, score: f64) -> Self { fn new(node: N, score: i64) -> Self {
Self { node, score } Self { node, score }
} }
} }
@ -42,12 +42,12 @@ where
#[derive(Debug)] #[derive(Debug)]
struct MapObject<N, H> { struct MapObject<N, H> {
parent: N, parent: N,
score: f64, score: i64,
key: Option<H>, key: Option<H>,
} }
impl<N, H> MapObject<N, H> { impl<N, H> MapObject<N, H> {
fn new(parent: N, score: f64, key: H) -> Self { fn new(parent: N, score: i64, key: H) -> Self {
Self { Self {
parent, parent,
score, score,
@ -72,7 +72,7 @@ where
let mut map: HashMap<G::Node, MapObject<G::Node, P::Handle>> = HashMap::new(); let mut map: HashMap<G::Node, MapObject<G::Node, P::Handle>> = HashMap::new();
let mut q = P::new(); let mut q = P::new();
q.insert(QueueObject::new(start.clone(), 0.0)); q.insert(QueueObject::new(start.clone(), 0));
let mut visited_nodes: usize = 1; let mut visited_nodes: usize = 1;
@ -137,10 +137,10 @@ struct GraphWrapper<'a, G, F> {
f: F, f: F,
} }
impl<G: WheightedGraph, F: Fn(&G::Node) -> f64> WheightedGraph for GraphWrapper<'_, G, F> { impl<G: WheightedGraph, F: Fn(&G::Node) -> i64> WheightedGraph for GraphWrapper<'_, G, F> {
type Node = G::Node; type Node = G::Node;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
self.g.edge(node, num).map(|(n, s)| { self.g.edge(node, num).map(|(n, s)| {
let s = s - (self.f)(node) + (self.f)(&n); let s = s - (self.f)(node) + (self.f)(&n);
(n, s) (n, s)
@ -155,7 +155,7 @@ where
G::Node: Eq + Hash + Clone + Debug, G::Node: Eq + Hash + Clone + Debug,
G: WheightedGraph, G: WheightedGraph,
E: Fn(&'_ G::Node) -> bool, E: Fn(&'_ G::Node) -> bool,
F: Fn(&G::Node) -> f64, F: Fn(&G::Node) -> i64,
{ {
let g = GraphWrapper { g: graph, f: dist }; let g = GraphWrapper { g: graph, f: dist };
@ -176,7 +176,7 @@ mod test {
fn trivial() { fn trivial() {
let a = WheightedAdjacencyList { let a = WheightedAdjacencyList {
nodes: vec![0, 1], nodes: vec![0, 1],
edges: vec![(1, 1.0)], edges: vec![(1, 1)],
}; };
assert_eq!( assert_eq!(
@ -190,17 +190,17 @@ mod test {
let a = WheightedAdjacencyList { let a = WheightedAdjacencyList {
nodes: vec![0, 2, 3, 5, 5, 7, 10], nodes: vec![0, 2, 3, 5, 5, 7, 10],
edges: vec![ edges: vec![
(1, 2.0), (1, 2),
(4, 10.0), (4, 10),
(2, 3.0), (2, 3),
(3, 2.0), (3, 2),
(5, 1.0), (5, 1),
(0, 4.0), (0, 4),
(2, 5.0), (2, 5),
(2, 9.0), (2, 9),
(3, 8.0), (3, 8),
(4, 0.0), (4, 0),
(5, 7.0), (5, 7),
], ],
}; };
@ -252,7 +252,7 @@ mod test {
c c
} }
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
dbg!(&node); dbg!(&node);
let edges = [ let edges = [
(node.0 > 0).then_some((node.0.saturating_sub(1), node.1)), (node.0 > 0).then_some((node.0.saturating_sub(1), node.1)),
@ -261,7 +261,7 @@ mod test {
(node.1 < self.height - 1).then_some((node.0, node.1 + 1)), (node.1 < self.height - 1).then_some((node.0, node.1 + 1)),
]; ];
edges.iter().flatten().nth(num).map(|&p| (p, 1.0)) edges.iter().flatten().nth(num).map(|&p| (p, 1))
} }
} }

View file

@ -20,10 +20,10 @@ where
{ {
type Node = Option<G::Node>; type Node = Option<G::Node>;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
match node { match node {
Some(n) => self.graph.edge(n, num).map(|(n, v)| (Some(n), v)), Some(n) => self.graph.edge(n, num).map(|(n, v)| (Some(n), v)),
None => self.start_nodes.get(num).cloned().map(|n| (Some(n), 0.0)), None => self.start_nodes.get(num).cloned().map(|n| (Some(n), 0)),
} }
} }
} }

View file

@ -66,7 +66,7 @@ impl SinglePathfinder for ConflictAvoidance {
#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy)] #[derive(Debug, Default, Serialize, Deserialize, Clone, Copy)]
pub struct Field { pub struct Field {
pub blocked: bool, pub blocked: bool,
weight: f64, weight: i64,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -108,10 +108,10 @@ 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 weight = 0.0; let mut weight = 0;
if self.map.get(x, y).blocked { if self.map.get(x, y).blocked {
weight += 10000.0; weight += 10000;
} }
self.map.get_mut(x, y).weight = weight; self.map.get_mut(x, y).weight = weight;
@ -121,7 +121,7 @@ impl Problem {
for (i, path) in self.path.iter().enumerate() { for (i, path) in self.path.iter().enumerate() {
if i != without && !path.is_empty() { if i != without && !path.is_empty() {
for p in &path[1..] { for p in &path[1..] {
let weight = 1000.0; let weight = 1000;
let x = p.0.x as usize; let x = p.0.x as usize;
let y = p.0.y as usize; let y = p.0.y as usize;
@ -226,7 +226,7 @@ impl WheightedGraph for MapInternal<'_> {
&self, &self,
node: &(Position, Direction), node: &(Position, Direction),
num: usize, num: usize,
) -> Option<((Position, Direction), f64)> { ) -> Option<((Position, Direction), i64)> {
let next = node.0.in_direction(&node.1, 1); let next = node.0.in_direction(&node.1, 1);
next.in_range( next.in_range(
&Position::new(0, 0), &Position::new(0, 0),
@ -241,9 +241,9 @@ impl WheightedGraph for MapInternal<'_> {
let penalty = self.map.get(next.x as usize, next.y as usize).weight; 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 + penalty)), 0 => Some(((next, node.1), 3 + penalty)),
1 => Some(((next, node.1.counter_clockwise()), 2.0 + penalty)), 1 => Some(((next, node.1.counter_clockwise()), 4 + penalty)),
2 => Some(((next, node.1.clockwise()), 2.0 + penalty)), 2 => Some(((next, node.1.clockwise()), 4 + penalty)),
_ => { _ => {
let mut count = 2; let mut count = 2;
for l in 2..=6 { for l in 2..=6 {
@ -259,7 +259,7 @@ impl WheightedGraph for MapInternal<'_> {
count += 1; count += 1;
if count == num { if count == num {
let penalty = penalty + self.map.get(n.x as usize, n.y as usize).weight; let penalty = penalty + self.map.get(n.x as usize, n.y as usize).weight;
return Some(((n, node.1), 35.0 + penalty)); return Some(((n, node.1), 35 + penalty));
} }
} }
} }
@ -285,9 +285,9 @@ impl Problem {
self.start[i], self.start[i],
|&n| n == self.end[i], |&n| n == self.end[i],
|&(p, _)| { |&(p, _)| {
1.5 * (PositionType::abs_diff(p.x, self.end[i].0.x) 3 * (PositionType::abs_diff(p.x, self.end[i].0.x)
+ PositionType::abs_diff(p.y, self.end[i].0.y)) + PositionType::abs_diff(p.y, self.end[i].0.y))
as f64 as i64
}, },
) )
}; };