use mop_scripting::{ genetic_algorithm::{ operators::{ crossover::MultiPoint, mating_selection::RouletteWheel, mutation::RandomDomainAssignments, survivor_selection::Generational, }, spea2::Spea2Adapter, GeneticAlgorithmParamsBuilder, }, initial_solutions::RandomInitialSolutions, lua::LuaScript, opt_problem::OptProblem, prelude::Script, OptFacadeBuilder, }; pub fn main() { let mut lua = LuaScript::::new(); lua.load( r#" function f1 (s) local x1 = s:value(0, 0) return x1 end function f2 (s) local x1 = s:value(0, 0) local x2 = s:value(0, 1) return (1.0 + x2) / x1 end function g1_with_reasons (s) local x1 = s:value(0, 0) local x2 = s:value(0, 1) local cr = cstr_result() if x2 + 9.0 * x1 < 6.0 then local str = "G1 is unfeasible. x1: " .. x1 .. ", x2: " .. x2 cr:push_reason_and_add_single_weight(str) end return cr end function g1_without_reasons (s) local x1 = s:value(0, 0) local x2 = s:value(0, 1) local cr = cstr_result() if x2 + 9.0 * x1 < 6.0 then cr:add_single_weight() end return cr end function g2_with_reasons (s) local x1 = s:value(0, 0) local x2 = s:value(0, 1) local cr = cstr_result() if -x2 + 9.0 * x1 < 1.0 then cr:add_single_weight() end return cr end function g2_without_reasons (s) local x1 = s:value(0, 0) local x2 = s:value(0, 1) local cr = cstr_result() if -x2 + 9.0 * x1 < 1.0 then local str = "G2 is unfeasible. x1: " .. x1 .. ", x2: " .. x2 cr:push_reason_and_add_single_weight(str) end return cr end objs = { { "min", f1 }, { "min", f2 } } hard_cstrs = { { g1_with_reasons, g1_without_reasons }, { g2_with_reasons, g2_without_reasons } } "#, ); OptFacadeBuilder::new() .initial_solutions(RandomInitialSolutions::new()) .max_duration(30) .max_iterations(500) .opt_problem(OptProblem::with_definitions( lua.definitions_with_criterias() .num_of_evts_for_each_rslt(1..=1) .num_of_vars_for_each_evt(2..=2) .results_num(50) .vars_domains(vec![0.1..1.0, 0.0..5.0]) .build(), )).stagnation_percentage(2.0) .stagnation_threshold(30) .validate_cstrs(true) .build() .solve_with(Spea2Adapter::new( 2.0, GeneticAlgorithmParamsBuilder::new() .crossover(MultiPoint::new(1, 50.0)) .mating_selection(RouletteWheel::new()) .mutation(RandomDomainAssignments::new(50.0)) .survivor_selection(Generational::new()) .build(), )).opt_problem() .results() .iter() .enumerate() .for_each(|(idx, result)| println!("Result #{}: {:?}", idx, result)); }