~ruther/ctu-fee-eoa

f1a6bbdb829d78985c738ddd16184fde2690ccff — Rutherther 6 days ago abba316
fix: use two objectives in nsga_constr
1 files changed, 20 insertions(+), 4 deletions(-)

M codes/constr_hw02/src/main.rs
M codes/constr_hw02/src/main.rs => codes/constr_hw02/src/main.rs +20 -4
@@ 831,7 831,7 @@ pub fn solve_with_nsga_constr<const DIM: usize, const CONSTRAINTS: usize>(
    iterations: usize,
    mutation_std_dev: f64,
    rng: &mut dyn RngCore,
) -> Result<(EvolutionResult<SVector<f64, DIM>, [f64; 1]>, Vec<f64>, Vec<f64>), Box<dyn std::error::Error>> {
) -> Result<(EvolutionResult<SVector<f64, DIM>, [f64; 2]>, Vec<f64>, Vec<f64>), Box<dyn std::error::Error>> {
    // Create initial population
    let initializer = RandomInitializer::new(
        Box::new(BoundedOVector::new(problem.bounds.0, problem.bounds.1))


@@ 853,9 853,25 @@ pub fn solve_with_nsga_constr<const DIM: usize, const CONSTRAINTS: usize>(
    // Clone the problem to access its fields
    let cloned_problem = problem.clone();

    // Create objectives array
    let objectives: [Box<dyn FitnessFunction<In = SVector<f64, DIM>, Out = f64, Err = Infallible>>; 1] = [
    let constraint_weights = [1.0; CONSTRAINTS];

    // Convert constraint array references
    let constraint_refs = problem.constraints.iter().collect::<Vec<_>>().try_into()
        .map_err(|_| "Failed to convert constraint references")?;

    let zero_fitness = ArbitraryFitness::zero();

    // Second objective: constraint violation only (zero fitness + constraints)
    let constrained_fitness_obj2 = ConstrainedFitnessFunction {
        fitness: &zero_fitness,
        constraints: constraint_refs,
        constraint_weights,
        capped: true
    };

    let objectives: [Box<dyn FitnessFunction<In = SVector<f64, DIM>, Out = f64, Err = Infallible>>; 2] = [
        Box::new(cloned_problem.objective),
        Box::new(constrained_fitness_obj2),
    ];

    // Convert constraints to boxed trait objects


@@ 866,7 882,7 @@ pub fn solve_with_nsga_constr<const DIM: usize, const CONSTRAINTS: usize>(
    let mut feasible_fractions = Vec::with_capacity(iterations);
    let mut avg_constraint_violations = Vec::with_capacity(iterations);

    let result = constrained_nsga_2::<1, CONSTRAINTS, SVector<f64, DIM>, f64, Infallible, 2, _, _, _>(
    let result = constrained_nsga_2::<2, CONSTRAINTS, SVector<f64, DIM>, f64, Infallible, 2, _, _, _>(
        initial_population,
        parents_count,
        objectives,