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