|
| 1 | +# Benchmark Problems |
| 2 | + |
| 3 | +PyGAD ships a small collection of standard benchmark problems under `pygad.benchmarks`. Each problem is a class that can be called with the PyGAD fitness signature `(ga, solution, sol_idx)` and returns a fitness value in PyGAD's maximization format (the original minimization values are negated for you). |
| 4 | + |
| 5 | +Each class also exposes the attributes you usually need to set up the GA: |
| 6 | + |
| 7 | +- `num_genes`: number of decision variables. |
| 8 | +- `num_objectives`: number of objectives. `1` for single-objective problems. |
| 9 | +- `bounds`: `(low, high)` tuple of variable bounds. |
| 10 | + |
| 11 | +For ZDT problems and ZDT4 / ZDT6, the class also has a `pareto_front(num_points)` method that returns reference points on the true Pareto front. Pass these to the IGD or GD indicators as the `reference_front` argument. |
| 12 | + |
| 13 | +## Single-Objective Problems |
| 14 | + |
| 15 | +Available in `pygad.benchmarks.classic`: |
| 16 | + |
| 17 | +| Class | Global minimum | Bounds | |
| 18 | +|---|---|---| |
| 19 | +| `Sphere` | f(0, ..., 0) = 0 | `(-5.12, 5.12)` | |
| 20 | +| `Rastrigin` | f(0, ..., 0) = 0 | `(-5.12, 5.12)` | |
| 21 | +| `Rosenbrock` | f(1, ..., 1) = 0 | `(-5.0, 10.0)` | |
| 22 | +| `Griewank` | f(0, ..., 0) = 0 | `(-600.0, 600.0)` | |
| 23 | +| `Schwefel` | f(420.97, ..., 420.97) ≈ 0 | `(-500.0, 500.0)` | |
| 24 | +| `Ackley` | f(0, ..., 0) = 0 | `(-32.768, 32.768)` | |
| 25 | +| `Himmelblau` | four equal minima at f = 0 (2D only) | `(-5.0, 5.0)` | |
| 26 | + |
| 27 | +## Multi-Objective Problems (ZDT family) |
| 28 | + |
| 29 | +Available in `pygad.benchmarks.zdt`. All ZDT problems have two objectives and variables in `[0, 1]` (except ZDT4 which uses `[-5, 5]` for the rest of the variables). |
| 30 | + |
| 31 | +| Class | Pareto front shape | |
| 32 | +|---|---| |
| 33 | +| `ZDT1` | convex | |
| 34 | +| `ZDT2` | non-convex | |
| 35 | +| `ZDT3` | disconnected (five pieces) | |
| 36 | +| `ZDT4` | convex, many local minima in the search space | |
| 37 | +| `ZDT6` | non-uniform | |
| 38 | + |
| 39 | +## Many-Objective Problems (DTLZ family) |
| 40 | + |
| 41 | +Available in `pygad.benchmarks.dtlz`. All DTLZ problems support an arbitrary number of objectives `M`. The number of decision variables is `M + k - 1` where `k` is a "distance" variable count. |
| 42 | + |
| 43 | +| Class | Default M | Pareto front shape | |
| 44 | +|---|---|---| |
| 45 | +| `DTLZ1` | 3 | linear hyperplane (`sum(f_i) = 0.5`) | |
| 46 | +| `DTLZ2` | 3 | unit sphere first orthant | |
| 47 | +| `DTLZ3` | 3 | unit sphere with hard multimodal g-function | |
| 48 | +| `DTLZ4` | 3 | unit sphere with strong bias toward one corner | |
| 49 | + |
| 50 | +## Combinatorial Problems |
| 51 | + |
| 52 | +Available in `pygad.benchmarks.knapsack`. The 0/1 `Knapsack` class takes three arguments: a 1D array of item `weights`, a 1D array of item `values`, and a numeric `capacity`. A solution is a binary vector where a 1 means the item is picked. The fitness is the total value when the candidate is within the capacity, and a negative penalty scaled by how much the candidate is over the limit otherwise. |
| 53 | + |
| 54 | +The class exposes `gene_space=[0, 1]` and `gene_type=int` so you can plug it directly into PyGAD: |
| 55 | + |
| 56 | +```python |
| 57 | +import pygad |
| 58 | +from pygad.benchmarks.knapsack import Knapsack |
| 59 | + |
| 60 | +problem = Knapsack(weights=[2, 3, 4, 5], |
| 61 | + values=[3, 4, 5, 6], |
| 62 | + capacity=5) |
| 63 | + |
| 64 | +ga = pygad.GA( |
| 65 | + num_generations=50, |
| 66 | + num_parents_mating=10, |
| 67 | + fitness_func=problem, |
| 68 | + sol_per_pop=30, |
| 69 | + num_genes=problem.num_genes, |
| 70 | + gene_space=problem.gene_space, |
| 71 | + gene_type=problem.gene_type, |
| 72 | +) |
| 73 | +ga.run() |
| 74 | +``` |
| 75 | + |
| 76 | +## Example: SOO |
| 77 | + |
| 78 | +```python |
| 79 | +import pygad |
| 80 | +from pygad.benchmarks.classic import Sphere |
| 81 | + |
| 82 | +problem = Sphere(num_genes=10) |
| 83 | + |
| 84 | +ga = pygad.GA( |
| 85 | + num_generations=100, |
| 86 | + num_parents_mating=10, |
| 87 | + fitness_func=problem, |
| 88 | + sol_per_pop=20, |
| 89 | + num_genes=problem.num_genes, |
| 90 | + init_range_low=problem.bounds[0], |
| 91 | + init_range_high=problem.bounds[1], |
| 92 | + crossover_type='sbx', |
| 93 | + sbx_crossover_eta=30, |
| 94 | + mutation_type='polynomial', |
| 95 | + polynomial_mutation_eta=20, |
| 96 | +) |
| 97 | +ga.run() |
| 98 | +``` |
| 99 | + |
| 100 | +## Example: MOO |
| 101 | + |
| 102 | +```python |
| 103 | +import pygad |
| 104 | +from pygad.benchmarks.zdt import ZDT1 |
| 105 | +from pygad.utils.indicators import inverted_generational_distance |
| 106 | + |
| 107 | +problem = ZDT1(num_genes=10) |
| 108 | + |
| 109 | +ga = pygad.GA( |
| 110 | + num_generations=200, |
| 111 | + num_parents_mating=20, |
| 112 | + fitness_func=problem, |
| 113 | + sol_per_pop=30, |
| 114 | + num_genes=problem.num_genes, |
| 115 | + init_range_low=problem.bounds[0], |
| 116 | + init_range_high=problem.bounds[1], |
| 117 | + parent_selection_type='nsga2', |
| 118 | + crossover_type='sbx', |
| 119 | + sbx_crossover_eta=30, |
| 120 | + mutation_type='polynomial', |
| 121 | + polynomial_mutation_eta=20, |
| 122 | +) |
| 123 | +ga.run() |
| 124 | + |
| 125 | +# Measure how close the final population is to the true Pareto front |
| 126 | +true_front = problem.pareto_front(num_points=100) |
| 127 | +igd = inverted_generational_distance(ga.last_generation_fitness, true_front) |
| 128 | +print(f'IGD = {igd}') |
| 129 | +``` |
0 commit comments