Skill Tracer
Skills › Scientific Agent Skills › pymoo › many_objective_example.py

many_objective_example.py

75 lines · skills/pymoo/scripts/many_objective_example.py at 5f2bc03

1"""
2Many-objective optimization example using pymoo.
3
4This script demonstrates many-objective optimization (4+ objectives)
5using NSGA-III on the DTLZ2 benchmark problem.
6"""
7
8from pymoo.algorithms.moo.nsga3 import NSGA3
9from pymoo.problems import get_problem
10from pymoo.optimize import minimize
11from pymoo.util.ref_dirs import get_reference_directions
12from pymoo.visualization.pcp import PCP
13import numpy as np
16def run_many_objective_optimization():
17 """Run many-objective optimization example."""
19 # Define the problem - DTLZ2 with 5 objectives
20 n_obj = 5
21 problem = get_problem("dtlz2", n_obj=n_obj)
23 # Generate reference directions for NSGA-III
24 # Das-Dennis simplex lattice: C(3 + 5 - 1, 5 - 1) = 35 directions.
25 # This bounded demo does not establish convergence or full front coverage.
26 # The dimension is positional: the factory's first argument is n_dim, and
27 # passing it as n_obj= raises TypeError.
28 ref_dirs = get_reference_directions("das-dennis", n_obj, n_partitions=3)
30 print(f"Number of reference directions: {len(ref_dirs)}")
32 # Configure NSGA-III algorithm
33 algorithm = NSGA3(
34 ref_dirs=ref_dirs,
35 eliminate_duplicates=True
36 )
38 # Run optimization
39 result = minimize(
40 problem,
41 algorithm,
42 ('n_gen', 60),
43 seed=1,
44 verbose=True
45 )
47 # Print results summary
48 print("\n" + "="*60)
49 print("MANY-OBJECTIVE OPTIMIZATION RESULTS")
50 print("="*60)
51 print(f"Number of objectives: {n_obj}")
52 print(f"Number of solutions: {len(result.F)}")
53 print(f"Completed generations: {result.algorithm.n_gen - 1}")
54 print(f"Number of function evaluations: {result.algorithm.evaluator.n_eval}")
56 # Show objective space statistics
57 print("\nObjective space statistics:")
58 print(f"Minimum values per objective: {result.F.min(axis=0)}")
59 print(f"Maximum values per objective: {result.F.max(axis=0)}")
60 print("="*60)
62 # Visualize using Parallel Coordinate Plot
63 plot = PCP(
64 title=f"DTLZ2 ({n_obj} objectives) - NSGA-III Results",
65 labels=[f"f{i+1}" for i in range(n_obj)],
66 normalize_each_axis=True
67 )
68 plot.add(result.F, alpha=0.3, color="blue")
69 plot.show()
71 return result
74if __name__ == "__main__":
75 result = run_many_objective_optimization()