Improve tracing output
This commit is contained in:
parent
cebdee4ec7
commit
81edc8e67a
6 changed files with 53 additions and 24 deletions
|
|
@ -24,7 +24,7 @@ fn main() {
|
|||
let problem = serde_yaml::from_reader(file).unwrap();
|
||||
|
||||
let l = ValidLayout {
|
||||
max_tries: 100,
|
||||
size_increases: 100,
|
||||
retries: 10,
|
||||
start_size: Position::new(10, 10),
|
||||
growth: Position::new(2, 2),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use tracing::{Level, info, span, trace, warn};
|
||||
use tracing::{Level, field::Empty, info, span, trace, warn};
|
||||
|
||||
use crate::{
|
||||
LayoutResult, Layouter,
|
||||
|
|
@ -22,7 +22,6 @@ impl Layouter for GeneticAlgorithm {
|
|||
pathfinder: &P,
|
||||
rng: &mut R,
|
||||
) -> Option<crate::LayoutResult> {
|
||||
assert!(self.population_new > 0);
|
||||
assert!(self.population_new + self.population_keep <= self.population_size);
|
||||
let _complete_span = span!(Level::TRACE, "genetic_algorithm_v1").entered();
|
||||
|
||||
|
|
@ -47,7 +46,8 @@ impl Layouter for GeneticAlgorithm {
|
|||
let mut best_result = population[0].clone();
|
||||
|
||||
for g in 0..self.generations {
|
||||
let _generation_span = span!(Level::TRACE, "generation", g).entered();
|
||||
let generation_span =
|
||||
span!(Level::TRACE, "generation", generation = g, score = Empty).entered();
|
||||
|
||||
{
|
||||
let _mutate_span = span!(Level::TRACE, "mutate").entered();
|
||||
|
|
@ -99,7 +99,8 @@ impl Layouter for GeneticAlgorithm {
|
|||
if population[0].1 < best_result.1 {
|
||||
best_result = population[0].clone();
|
||||
}
|
||||
info!("completed generation {g} best score: {}", population[0].1);
|
||||
generation_span.record("score", population[0].1);
|
||||
println!("completed generation {g} best score: {}", population[0].1);
|
||||
}
|
||||
|
||||
Some(best_result.0)
|
||||
|
|
|
|||
|
|
@ -40,22 +40,42 @@ fn place_block(
|
|||
let dir = rng.random::<Direction>();
|
||||
|
||||
let pos = match dir {
|
||||
Direction::Up => Position::new(
|
||||
rng.random_range(0..=(size.x - b.size.x)),
|
||||
rng.random_range(0..=(size.y - b.size.y)),
|
||||
),
|
||||
Direction::Right => Position::new(
|
||||
rng.random_range((b.size.y - 1)..size.x),
|
||||
rng.random_range(0..=(size.y - b.size.x)),
|
||||
),
|
||||
Direction::Down => Position::new(
|
||||
rng.random_range((b.size.x - 1)..size.x),
|
||||
rng.random_range((b.size.y - 1)..size.y),
|
||||
),
|
||||
Direction::Left => Position::new(
|
||||
rng.random_range(0..=(size.x - b.size.y)),
|
||||
rng.random_range((b.size.x - 1)..size.y),
|
||||
),
|
||||
Direction::Up => {
|
||||
if size.x - b.size.x < 0 || size.y - b.size.y < 0 {
|
||||
continue;
|
||||
}
|
||||
Position::new(
|
||||
rng.random_range(0..=(size.x - b.size.x)),
|
||||
rng.random_range(0..=(size.y - b.size.y)),
|
||||
)
|
||||
}
|
||||
Direction::Right => {
|
||||
if size.y - b.size.x < 0 || size.x - b.size.y < 0 {
|
||||
continue;
|
||||
}
|
||||
Position::new(
|
||||
rng.random_range((b.size.y - 1)..size.x),
|
||||
rng.random_range(0..=(size.y - b.size.x)),
|
||||
)
|
||||
}
|
||||
Direction::Down => {
|
||||
if size.x - b.size.x < 0 || size.y - b.size.y < 0 {
|
||||
continue;
|
||||
}
|
||||
Position::new(
|
||||
rng.random_range((b.size.x - 1)..size.x),
|
||||
rng.random_range((b.size.y - 1)..size.y),
|
||||
)
|
||||
}
|
||||
Direction::Left => {
|
||||
if size.y - b.size.x < 0 || size.x - b.size.y < 0 {
|
||||
continue;
|
||||
}
|
||||
Position::new(
|
||||
rng.random_range(0..=(size.x - b.size.y)),
|
||||
rng.random_range((b.size.x - 1)..size.y),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let current = Block::new(pos, dir, b.size);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub struct ValidLayout {
|
||||
pub max_tries: usize,
|
||||
pub size_increases: usize,
|
||||
pub retries: usize,
|
||||
pub start_size: Position,
|
||||
pub growth: Position,
|
||||
|
|
@ -21,7 +21,7 @@ impl Layouter for ValidLayout {
|
|||
pathfinder: &P,
|
||||
rng: &mut R,
|
||||
) -> Option<LayoutResult> {
|
||||
for i in 0..self.max_tries {
|
||||
for i in 0..self.size_increases {
|
||||
let size = self.start_size + i as PositionType * self.growth;
|
||||
|
||||
if let Some(blocks) = initally_set_blocks(input, size, self.retries, rng) {
|
||||
|
|
|
|||
|
|
@ -276,7 +276,6 @@ impl Problem {
|
|||
end: self.end[i],
|
||||
};
|
||||
let p = {
|
||||
let _pathfinding_span = span!(Level::TRACE, "graph").entered();
|
||||
// dijkstra::<MapInternal, FastBinaryHeap<_>>(&m, self.start[i], self.end[i])
|
||||
a_star::<MapInternal, FastBinaryHeap<_>, _>(
|
||||
&m,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use std::{collections::HashMap, fmt::Debug, hash::Hash, hash::Hasher};
|
||||
|
||||
use tracing::{field::Empty, trace, trace_span};
|
||||
|
||||
use crate::priority_queue::PriorityQueue;
|
||||
|
||||
use super::WheightedGraph;
|
||||
|
|
@ -61,6 +63,7 @@ where
|
|||
G::Node: Eq + Hash + Clone + Debug,
|
||||
G: WheightedGraph,
|
||||
{
|
||||
let span = trace_span!("graph", seen_nodes = Empty, visited_nodes = Empty).entered();
|
||||
if start == end {
|
||||
return Some(vec![start]);
|
||||
}
|
||||
|
|
@ -70,7 +73,10 @@ where
|
|||
let mut q = P::new();
|
||||
q.insert(QueueObject::new(start.clone(), 0.0));
|
||||
|
||||
let mut visited_nodes: usize = 1;
|
||||
|
||||
while let Some(o) = q.pop_min() {
|
||||
visited_nodes += 1;
|
||||
if let Some(m) = map.get_mut(&o.node) {
|
||||
m.key = None;
|
||||
}
|
||||
|
|
@ -97,6 +103,9 @@ where
|
|||
// dbg!(&q);
|
||||
}
|
||||
|
||||
span.record("seen_nodes", map.len());
|
||||
span.record("visited_nodes", visited_nodes);
|
||||
|
||||
map.get(&end)?;
|
||||
|
||||
let mut result = vec![end];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue