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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue