Parameterize priority queue

This commit is contained in:
hal8174 2025-03-27 20:25:10 +01:00
parent 6f74f1345e
commit ffe51bede9
14 changed files with 276 additions and 183 deletions

View file

@ -8,6 +8,7 @@ factorio-pathfinding = { path = "../factorio-pathfinding" }
factorio-layout = { path = "../factorio-layout" }
factorio-core = { path = "../factorio-core" }
factorio-blueprint = { path = "../factorio-blueprint" }
factorio-graph = { path = "../factorio-graph" }
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.135"
serde_yaml = "0.9.34"

View file

@ -1,11 +1,21 @@
use clap::Parser;
use clap::{Parser, ValueEnum};
use factorio_blueprint::{BlueprintString, encode};
use factorio_blueprint_generator::factory::{FactoryGraph, generate_factory};
use factorio_core::{prelude::*, visualize::Visualize};
use factorio_layout::{genetic_algorithm_v1::GeneticAlgorithm, valid_layout::ValidLayout};
use factorio_pathfinding::belt_finding::ConflictAvoidance;
use factorio_graph::{
priority_queue::{
PriorityQueue,
binary_heap::{BinaryHeap, FastBinaryHeap},
},
wheighted_graph::shortest_path::QueueObject,
};
use factorio_layout::{
Layouter, genetic_algorithm_v1::GeneticAlgorithm, valid_layout::ValidLayout,
};
use factorio_pathfinding::{Pathfinder, belt_finding::ConflictAvoidance};
use rand::{SeedableRng, rngs::SmallRng};
use std::path::PathBuf;
use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan};
#[derive(Parser)]
struct Args {
@ -15,19 +25,42 @@ struct Args {
json: bool,
path: PathBuf,
#[arg(short, long, default_value = "none")]
tracing: Tracing,
#[arg(long, default_value = "ca-fbh")]
pathfinder: PathfinderArg,
}
#[derive(ValueEnum, Clone, Copy)]
enum Tracing {
Perfetto,
Stdio,
None,
}
#[derive(ValueEnum, Clone, Copy)]
enum PathfinderArg {
CaFbh,
CaBh,
}
fn main() {
let args = Args::parse();
// tracing_subscriber::fmt::fmt()
// .with_env_filter(EnvFilter::from_default_env())
// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
// .init();
// tracing_perfetto::install_perfetto_subscriber();
let text = std::fs::File::open(&args.path).unwrap();
let factory_graph: FactoryGraph = serde_yaml::from_reader(text).unwrap();
match args.tracing {
Tracing::Stdio => {
tracing_subscriber::fmt::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.init();
}
Tracing::Perfetto => {
tracing_perfetto::install_perfetto_subscriber();
}
Tracing::None => (),
}
// dbg!(&factory_graph);
@ -40,16 +73,49 @@ fn main() {
let l = GeneticAlgorithm {
mutation_retries: 20,
population_size: 10,
population_size: 6,
population_keep: 2,
population_new: 2,
generations: 4,
generations: 2,
valid_layout: l,
};
let p = ConflictAvoidance {
timeout: Some(std::time::Duration::from_millis(100)),
};
execute_without_pathfinder::<_>(args, l);
}
fn execute_without_pathfinder<L>(args: Args, l: L)
where
L: Layouter,
{
match args.pathfinder {
PathfinderArg::CaFbh => {
let p = ConflictAvoidance {
timeout: Some(std::time::Duration::from_millis(100)),
priority_queue: std::marker::PhantomData::<
FastBinaryHeap<QueueObject<(Position, Direction)>>,
>,
};
execute::<_, _>(args, l, p);
}
PathfinderArg::CaBh => {
let p = ConflictAvoidance {
timeout: Some(std::time::Duration::from_millis(100)),
priority_queue: std::marker::PhantomData::<
BinaryHeap<QueueObject<(Position, Direction)>>,
>,
};
execute::<_, _>(args, l, p);
}
}
}
fn execute<L, P>(args: Args, l: L, p: P)
where
P: Pathfinder + Sync,
L: Layouter,
{
let text = std::fs::File::open(&args.path).unwrap();
let factory_graph: FactoryGraph = serde_yaml::from_reader(text).unwrap();
let mut rng = SmallRng::seed_from_u64(args.seed);
let b = generate_factory(&l, &p, factory_graph, &mut rng);