Refactor genetic algorithm

This commit is contained in:
hal8174 2025-02-01 22:55:06 +01:00
parent 6fbff67e61
commit 0ab97485bb
9 changed files with 260 additions and 15 deletions

View file

@ -1,9 +1,8 @@
use factorio_blueprint::abstraction::{Blueprint, ElectricPoleType, Entity, InserterType};
use factorio_core::{beltoptions::Beltspeed, prelude::*};
pub fn assembly_line(assembly_machines: usize) -> Blueprint {
pub fn assembly_line(assembly_machines: usize, recipe: impl AsRef<str>) -> Blueprint {
let mut blueprint = Blueprint::new();
let mut last = None;
for i in 0..assembly_machines.div_ceil(3) {
let top = blueprint.add_entity(Entity::new_electric_pole(
@ -28,7 +27,7 @@ pub fn assembly_line(assembly_machines: usize) -> Blueprint {
for i in 0..assembly_machines {
blueprint.add_entity(Entity::new_production(
"assembling-machine-3",
"iron-gear-wheel",
recipe.as_ref(),
Position::new(3 + 6 * i as PositionType, 9),
Direction::Up,
Position::new(6, 6),

View file

@ -7,11 +7,14 @@ struct Args {
#[arg(short, long)]
json: bool,
assembly_machines: usize,
recipe: String,
}
fn main() {
let args = Args::parse();
let b = BlueprintString::Blueprint(assembly_line(args.assembly_machines).to_blueprint());
let b = BlueprintString::Blueprint(
assembly_line(args.assembly_machines, args.recipe).to_blueprint(),
);
if args.json {
println!("{}", serde_json::to_string_pretty(&b).unwrap());

View file

@ -2,7 +2,7 @@ use clap::Parser;
use factorio_blueprint::{BlueprintString, encode};
use factorio_blueprint_generator::factory::generate_factory;
use factorio_core::prelude::*;
use factorio_layout::valid_layout::ValidLayout;
use factorio_layout::{genetic_algorithm_v1::GeneticAlgorithm, valid_layout::ValidLayout};
use factorio_pathfinding::belt_finding::ConflictAvoidance;
use rand::{SeedableRng, rngs::SmallRng};
@ -19,10 +19,19 @@ fn main() {
let l = ValidLayout {
max_tries: 10,
retries: 10,
start_size: Position::new(100, 100),
retries: 20,
start_size: Position::new(20, 20),
growth: Position::new(5, 5),
};
let l = GeneticAlgorithm {
mutation_retries: 20,
population_size: 10,
population_keep: 2,
population_new: 2,
generations: 20,
valid_layout: l,
};
let p = ConflictAvoidance {
timeout: Some(std::time::Duration::from_millis(20)),
};

View file

@ -95,10 +95,10 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
.unwrap();
let blueprints = vec![
assembly_line(2),
assembly_line(3),
assembly_line(2),
assembly_line(2),
assembly_line(2, "iron-gear-wheel"),
assembly_line(3, "copper-cable"),
assembly_line(2, "electronic-circuit"),
assembly_line(2, "inserter"),
];
let mut b = Blueprint::new();