From 40626d04948a7c73ccbdbee22b5c568eec8302ba Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 5 Dec 2025 17:17:16 +0100 Subject: [PATCH] feat(constr_hw02): g04, g05, g09 and g21 --- codes/constr_hw02/src/main.rs | 409 +++++++++++++++++++++++++++++++++- 1 file changed, 400 insertions(+), 9 deletions(-) diff --git a/codes/constr_hw02/src/main.rs b/codes/constr_hw02/src/main.rs index 155c3ee870504abef864f724764cb2df2dff240c..0f0e1c62d645e15499bccf2e48f962c9fa0cb9e7 100644 --- a/codes/constr_hw02/src/main.rs +++ b/codes/constr_hw02/src/main.rs @@ -76,6 +76,109 @@ pub struct NsgaConfig { pub mutation_std_dev: f64, } +fn problem_g04() -> ConstrainedProblem<5, 6> { + ConstrainedProblem::new(Rc::new(|| + ConstrainedProblem { + name: "g04".to_string(), + objective: ArbitraryFitness::new( + Box::new(|vec| { + 5.3578547 * vec[2].powi(2) + 0.8356891 * vec[0] * vec[4] + + 37.293239 * vec[0] - 40792.141 + }) + ), + constraints: [ + LowerThanConstraintFunction::new( + Box::new(|vec| { + 85.334407 + 0.0056858 * vec[1] * vec[4] + 0.0006262 * vec[0] * vec[3] + - 0.0022053 * vec[2] * vec[4] - 92.0 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + -85.334407 - 0.0056858 * vec[1] * vec[4] - 0.0006262 * vec[0] * vec[3] + + 0.0022053 * vec[2] * vec[4] + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + 80.51249 + 0.0071317 * vec[1] * vec[4] + 0.0029955 * vec[0] * vec[1] + + 0.0021813 * vec[2].powi(2) - 110.0 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + -80.51249 - 0.0071317 * vec[1] * vec[4] - 0.0029955 * vec[0] * vec[1] + - 0.0021813 * vec[2].powi(2) + 90.0 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + 9.300961 + 0.0047026 * vec[2] * vec[4] + 0.0012547 * vec[0] * vec[2] + + 0.0019085 * vec[2] * vec[3] - 25.0 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + -9.300961 - 0.0047026 * vec[2] * vec[4] - 0.0012547 * vec[0] * vec[2] + - 0.0019085 * vec[2] * vec[3] + 20.0 + }) + ), + ], + bounds: ( + SVector::::from([78.0, 33.0, 27.0, 27.0, 27.0]), // min bounds + SVector::::from([102.0, 45.0, 45.0, 45.0, 45.0]) // max bounds + ), + optimal_value: -30665.53867178333, + instantiate_fn: None + })) +} + +fn problem_g05() -> ConstrainedProblem<4, 5> { + ConstrainedProblem::new(Rc::new(|| + ConstrainedProblem { + name: "g05".to_string(), + objective: ArbitraryFitness::new( + Box::new(|vec| { + 3.0 * vec[0] + 0.000001 * vec[0].powi(3) + 2.0 * vec[1] + + (0.000002 / 3.0) * vec[1].powi(3) + }) + ), + constraints: [ + LowerThanConstraintFunction::new( + Box::new(|vec| { + -vec[3] + vec[2] - 0.55 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + -vec[2] + vec[3] - 0.55 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + 1000.0 * (-vec[2] - 0.25).sin() + 1000.0 * (-vec[3] - 0.25) - vec[0] + 894.8 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + 1000.0 * (vec[2] - 0.25).sin() + 1000.0 * (vec[2] - vec[3] - 0.25) - vec[1] + 894.8 + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + 1000.0 * (vec[2] - 0.25).sin() + 1000.0 * (vec[3] - vec[2] - 0.25) + 1294.8 + }) + ), + ], + bounds: ( + SVector::::from([0.0, 0.0, -0.55, -0.55]), // min bounds + SVector::::from([1200.0, 1200.0, 0.55, 0.55]) // max bounds + ), + optimal_value: 5126.4967140071, + instantiate_fn: None + })) +} + fn problem_g06() -> ConstrainedProblem<2, 2> { ConstrainedProblem::new(Rc::new(|| ConstrainedProblem { @@ -136,6 +239,54 @@ fn problem_g08() -> ConstrainedProblem<2, 2> { })) } +fn problem_g09() -> ConstrainedProblem<7, 4> { + ConstrainedProblem::new(Rc::new(|| + ConstrainedProblem { + name: "g09".to_string(), + objective: ArbitraryFitness::new( + Box::new(|vec| { + (vec[0] - 10.0).powi(2) + 5.0 * (vec[1] - 12.0).powi(2) + + vec[2].powi(4) + 3.0 * (vec[3] - 11.0).powi(2) + + 10.0 * vec[4].powi(6) + 7.0 * vec[5].powi(2) + + vec[6].powi(4) - 4.0 * vec[5] * vec[6] + - 10.0 * vec[5] - 8.0 * vec[6] + }) + ), + constraints: [ + LowerThanConstraintFunction::new( + Box::new(|vec| { + -127.0 + 2.0 * vec[0].powi(2) + 3.0 * vec[1].powi(4) + + vec[2] + 4.0 * vec[3].powi(2) + 5.0 * vec[4] + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + -282.0 + 7.0 * vec[0] + 3.0 * vec[1] + 10.0 * vec[2].powi(2) + + vec[3] - vec[4] + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + -196.0 + 23.0 * vec[0] + vec[1].powi(2) + 6.0 * vec[5].powi(2) + - 8.0 * vec[6] + }) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| { + 4.0 * vec[0].powi(2) + vec[1].powi(2) - 3.0 * vec[0] * vec[1] + + 2.0 * vec[2].powi(2) + 5.0 * vec[5] - 11.0 * vec[6] + }) + ), + ], + bounds: ( + SVector::::from([-10.0; 7]), // min bounds (all -10) + SVector::::from([10.0; 7]) // max bounds (all 10) + ), + optimal_value: 680.6300573745, + instantiate_fn: None + })) +} + pub fn problem_g11(eps: f64) -> ConstrainedProblem<2, 1> { let problem = ConstrainedProblem::new(Rc::new(move || ConstrainedProblem { name: "g11".to_string(), @@ -166,6 +317,43 @@ pub fn problem_g11(eps: f64) -> ConstrainedProblem<2, 1> { problem } +fn problem_g21() -> ConstrainedProblem<7, 6> { + ConstrainedProblem::new(Rc::new(|| ConstrainedProblem { + name: "g21".to_string(), + objective: ArbitraryFitness::new(Box::new(|vec| vec[0])), + constraints: [ + LowerThanConstraintFunction::new(Box::new(|vec| { + -vec[0] + 35.0 * vec[1].powf(0.6) + 35.0 * vec[2].powf(0.6) + })), + LowerThanConstraintFunction::new(Box::new(|vec| { + -300.0 * vec[2] + 7500.0 * vec[4] - 7500.0 * vec[5] + - 25.0 * vec[3] * vec[4] + 25.0 * vec[3] * vec[5] + vec[2] * vec[3] + })), + LowerThanConstraintFunction::new(Box::new(|vec| { + 100.0 * vec[1] + 155.365 * vec[3] + 2500.0 * vec[6] + - vec[1] * vec[3] - 25.0 * vec[3] * vec[6] - 15536.5 + })), + LowerThanConstraintFunction::new(Box::new(|vec| { + -vec[4] + (-vec[3] + 900.0).ln() + })), + LowerThanConstraintFunction::new(Box::new(|vec| { + -vec[5] + (vec[3] + 300.0).ln() + })), + LowerThanConstraintFunction::new(Box::new(|vec| { + -vec[6] + (- 2.0 * vec[3] + 700.0).ln() + })), + ], + bounds: ( + // x1..x7 Min + SVector::::from([0.0, 0.0, 0.0, 100.0, 6.3, 5.9, 4.5]), + // x1..x7 Max + SVector::::from([1000.0, 40.0, 40.0, 300.0, 6.7, 6.4, 6.25]) + ), + optimal_value: 193.724510070035, + instantiate_fn: None + })) +} + pub fn problem_g24() -> ConstrainedProblem<2, 2> { ConstrainedProblem::new(Rc::new(|| ConstrainedProblem { name: "g24".to_string(), @@ -479,7 +667,7 @@ pub fn solve_with_nsga_multi 0.0) { + if evaluation.evaluations.iter().all(|&eval| eval > 0.0) { return false; } @@ -534,17 +722,64 @@ fn handle_g11_srank() -> Result<(), Box> { iterations: ITERATIONS, n_param: POPULATION * 2, - // population_size: POPULATION, - // parents_count: PARENTS_COUNT, - // iterations: ITERATIONS, - // n_param: 2 * POPULATION, - - p_param: 0.05, + p_param: 0.45, mutation_std_dev: 0.01 / 50.0, }; run_stochastic_ranking(problem, config) } +fn handle_g04_srank() -> Result<(), Box> { + let problem = problem_g04(); + let config = StochasticRankingConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + n_param: 2 * POPULATION, + p_param: 0.45, + mutation_std_dev: 1.0, + }; + run_stochastic_ranking(problem, config) +} + +fn handle_g05_srank() -> Result<(), Box> { + let problem = problem_g05(); + let config = StochasticRankingConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + n_param: 2 * POPULATION, + p_param: 0.45, + mutation_std_dev: 10.0, + }; + run_stochastic_ranking(problem, config) +} + +fn handle_g09_srank() -> Result<(), Box> { + let problem = problem_g09(); + let config = StochasticRankingConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + n_param: 2 * POPULATION, + p_param: 0.45, + mutation_std_dev: 1.0, + }; + run_stochastic_ranking(problem, config) +} + +fn handle_g21_srank() -> Result<(), Box> { + let problem = problem_g21(); + let config = StochasticRankingConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + n_param: 2 * POPULATION, + p_param: 0.45, + mutation_std_dev: 10.0, + }; + run_stochastic_ranking(problem, config) +} + fn handle_g24_srank() -> Result<(), Box> { let problem = problem_g24(); let config = StochasticRankingConfig { @@ -790,6 +1025,50 @@ fn handle_g11_nsga() -> Result<(), Box> { run_nsga_ii(problem, config) } +fn handle_g04_nsga() -> Result<(), Box> { + let problem = problem_g04(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 1.0, + }; + run_nsga_ii(problem, config) +} + +fn handle_g05_nsga() -> Result<(), Box> { + let problem = problem_g05(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 10.0, + }; + run_nsga_ii(problem, config) +} + +fn handle_g09_nsga() -> Result<(), Box> { + let problem = problem_g09(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 1.0, + }; + run_nsga_ii(problem, config) +} + +fn handle_g21_nsga() -> Result<(), Box> { + let problem = problem_g21(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 10.0, + }; + run_nsga_ii(problem, config) +} + fn handle_g24_nsga() -> Result<(), Box> { let problem = problem_g24(); let config = NsgaConfig { @@ -874,6 +1153,102 @@ fn handle_nsga_multi_g11(capped: bool) -> Result<(), Box> run_nsga_multi(problem, evolution_result, feasible_fractions, capped) } +fn handle_nsga_multi_g04(capped: bool) -> Result<(), Box> { + let mut rng = rand::rng(); + let problem = problem_g04(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 1.0, + }; + + let result = solve_with_nsga_multi::<5, 6, 7>( + problem.clone(), + config.population_size, + config.parents_count, + config.iterations, + config.mutation_std_dev, + &mut rng, + capped + )?; + + let (evolution_result, feasible_fractions) = result; + run_nsga_multi(problem, evolution_result, feasible_fractions, capped) +} + +fn handle_nsga_multi_g05(capped: bool) -> Result<(), Box> { + let mut rng = rand::rng(); + let problem = problem_g05(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 10.0, + }; + + let result = solve_with_nsga_multi::<4, 5, 6>( + problem.clone(), + config.population_size, + config.parents_count, + config.iterations, + config.mutation_std_dev, + &mut rng, + capped + )?; + + let (evolution_result, feasible_fractions) = result; + run_nsga_multi(problem, evolution_result, feasible_fractions, capped) +} + +fn handle_nsga_multi_g09(capped: bool) -> Result<(), Box> { + let mut rng = rand::rng(); + let problem = problem_g09(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 1.0, + }; + + let result = solve_with_nsga_multi::<7, 4, 5>( + problem.clone(), + config.population_size, + config.parents_count, + config.iterations, + config.mutation_std_dev, + &mut rng, + capped + )?; + + let (evolution_result, feasible_fractions) = result; + run_nsga_multi(problem, evolution_result, feasible_fractions, capped) +} + +fn handle_nsga_multi_g21(capped: bool) -> Result<(), Box> { + let mut rng = rand::rng(); + let problem = problem_g21(); + let config = NsgaConfig { + population_size: POPULATION, + parents_count: PARENTS_COUNT, + iterations: ITERATIONS, + mutation_std_dev: 10.0, + }; + + let result = solve_with_nsga_multi::<7, 6, 7>( + problem.clone(), + config.population_size, + config.parents_count, + config.iterations, + config.mutation_std_dev, + &mut rng, + capped + )?; + + let (evolution_result, feasible_fractions) = result; + run_nsga_multi(problem, evolution_result, feasible_fractions, capped) +} + fn handle_nsga_multi_g24(capped: bool) -> Result<(), Box> { let mut rng = rand::rng(); let problem = problem_g24(); @@ -904,7 +1279,7 @@ fn main() { if args.len() != 3 { eprintln!("Usage: {} ", args[0]); eprintln!("Methods: srank, nsga, nsga_multi, nsga_multi_noncapped"); - eprintln!("Problems: g06, g08, g11, g24"); + eprintln!("Problems: g04, g05, g06, g08, g09, g11, g21, g24"); std::process::exit(1); } @@ -912,26 +1287,42 @@ fn main() { let problem = &args[2]; let result = match (method.as_str(), problem.as_str()) { + ("srank", "g04") => handle_g04_srank(), + ("srank", "g05") => handle_g05_srank(), ("srank", "g06") => handle_g06_srank(), ("srank", "g08") => handle_g08_srank(), + ("srank", "g09") => handle_g09_srank(), ("srank", "g11") => handle_g11_srank(), + ("srank", "g21") => handle_g21_srank(), ("srank", "g24") => handle_g24_srank(), + ("nsga", "g04") => handle_g04_nsga(), + ("nsga", "g05") => handle_g05_nsga(), ("nsga", "g06") => handle_g06_nsga(), ("nsga", "g08") => handle_g08_nsga(), + ("nsga", "g09") => handle_g09_nsga(), ("nsga", "g11") => handle_g11_nsga(), + ("nsga", "g21") => handle_g21_nsga(), ("nsga", "g24") => handle_g24_nsga(), + ("nsga_multi", "g04") => handle_nsga_multi_g04(true), + ("nsga_multi", "g05") => handle_nsga_multi_g05(true), ("nsga_multi", "g06") => handle_nsga_multi_g06(true), ("nsga_multi", "g08") => handle_nsga_multi_g08(true), + ("nsga_multi", "g09") => handle_nsga_multi_g09(true), ("nsga_multi", "g11") => handle_nsga_multi_g11(true), + ("nsga_multi", "g21") => handle_nsga_multi_g21(true), ("nsga_multi", "g24") => handle_nsga_multi_g24(true), + ("nsga_multi_noncapped", "g04") => handle_nsga_multi_g04(false), + ("nsga_multi_noncapped", "g05") => handle_nsga_multi_g05(false), ("nsga_multi_noncapped", "g06") => handle_nsga_multi_g06(false), ("nsga_multi_noncapped", "g08") => handle_nsga_multi_g08(false), + ("nsga_multi_noncapped", "g09") => handle_nsga_multi_g09(false), ("nsga_multi_noncapped", "g11") => handle_nsga_multi_g11(false), + ("nsga_multi_noncapped", "g21") => handle_nsga_multi_g21(false), ("nsga_multi_noncapped", "g24") => handle_nsga_multi_g24(false), (_, _) => { eprintln!("Invalid method '{}' or problem '{}'", method, problem); eprintln!("Methods: srank, nsga, nsga_multi, nsga_multi_noncapped"); - eprintln!("Problems: g06, g08, g11, g24"); + eprintln!("Problems: g04, g05, g06, g08, g09, g11, g21, g24"); std::process::exit(1); } };