|
| 1 | +# Benchmark Problems |
| 2 | + |
| 3 | +PyGAD bundles common benchmark problems under `pygad.benchmarks`. Each problem is a class callable with `(ga, solution, sol_idx)` and returns a fitness in PyGAD's maximisation format. Minimisation values are negated. |
| 4 | + |
| 5 | +Class attributes for setting up the GA: |
| 6 | + |
| 7 | +- `num_genes`: number of decision variables. |
| 8 | +- `num_objectives`: number of objectives (`1` for single-objective). |
| 9 | +- `bounds`: `(low, high)` tuple of variable bounds. |
| 10 | + |
| 11 | +ZDT classes also have a `pareto_front(num_points)` method that returns true-front reference points. Pass these to the IGD or GD indicators as `reference_front`. |
| 12 | + |
| 13 | +A runnable example per benchmark lives under `examples/benchmarks/`. |
| 14 | + |
| 15 | +## Single-Objective Problems |
| 16 | + |
| 17 | +Available in `pygad.benchmarks.classic`: |
| 18 | + |
| 19 | +| Class | Global minimum | Bounds | |
| 20 | +|---|---|---| |
| 21 | +| `Sphere` | f(0, ..., 0) = 0 | `(-5.12, 5.12)` | |
| 22 | +| `Rastrigin` | f(0, ..., 0) = 0 | `(-5.12, 5.12)` | |
| 23 | +| `Rosenbrock` | f(1, ..., 1) = 0 | `(-5.0, 10.0)` | |
| 24 | +| `Griewank` | f(0, ..., 0) = 0 | `(-600.0, 600.0)` | |
| 25 | +| `Schwefel` | f(420.97, ..., 420.97) ≈ 0 | `(-500.0, 500.0)` | |
| 26 | +| `Ackley` | f(0, ..., 0) = 0 | `(-32.768, 32.768)` | |
| 27 | +| `Himmelblau` | four equal minima at f = 0 (2D only) | `(-5.0, 5.0)` | |
| 28 | + |
| 29 | +## Multi-Objective Problems (ZDT family) |
| 30 | + |
| 31 | +In `pygad.benchmarks.zdt`. Two objectives, variables in `[0, 1]` (ZDT4 uses `[-5, 5]` for the rest). |
| 32 | + |
| 33 | +| Class | Pareto front shape | |
| 34 | +|---|---| |
| 35 | +| `ZDT1` | convex | |
| 36 | +| `ZDT2` | non-convex | |
| 37 | +| `ZDT3` | disconnected (five pieces) | |
| 38 | +| `ZDT4` | convex, many local minima in the search space | |
| 39 | +| `ZDT6` | non-uniform | |
| 40 | + |
| 41 | +## Many-Objective Problems (DTLZ family) |
| 42 | + |
| 43 | +In `pygad.benchmarks.dtlz`. Any number of objectives `M`. Decision variables: `M + k - 1`, where `k` is the distance-variable count. |
| 44 | + |
| 45 | +| Class | Default M | Pareto front shape | |
| 46 | +|---|---|---| |
| 47 | +| `DTLZ1` | 3 | linear hyperplane (`sum(f_i) = 0.5`) | |
| 48 | +| `DTLZ2` | 3 | unit sphere first orthant | |
| 49 | +| `DTLZ3` | 3 | unit sphere with hard multimodal g-function | |
| 50 | +| `DTLZ4` | 3 | unit sphere with strong bias toward one corner | |
| 51 | + |
| 52 | +## Combinatorial Problems |
| 53 | + |
| 54 | +Two combinatorial benchmarks: 0/1 `Knapsack` and `TSP`. |
| 55 | + |
| 56 | +### Knapsack |
| 57 | + |
| 58 | +In `pygad.benchmarks.knapsack`. `Knapsack` takes three arguments: 1D arrays of `weights` and `values`, and a numeric `capacity`. A solution is a binary vector (1 = pick the item). Fitness is the total value within capacity, or a negative penalty scaled by the overweight amount. |
| 59 | + |
| 60 | +Class attributes `gene_space=[0, 1]` and `gene_type=int` plug into PyGAD as is: |
| 61 | + |
| 62 | +```python |
| 63 | +import pygad |
| 64 | +from pygad.benchmarks.knapsack import Knapsack |
| 65 | + |
| 66 | +problem = Knapsack(weights=[2, 3, 4, 5], |
| 67 | + values=[3, 4, 5, 6], |
| 68 | + capacity=5) |
| 69 | + |
| 70 | +ga = pygad.GA( |
| 71 | + num_generations=50, |
| 72 | + num_parents_mating=10, |
| 73 | + fitness_func=problem, |
| 74 | + sol_per_pop=30, |
| 75 | + num_genes=problem.num_genes, |
| 76 | + gene_space=problem.gene_space, |
| 77 | + gene_type=problem.gene_type, |
| 78 | +) |
| 79 | +ga.run() |
| 80 | +``` |
| 81 | + |
| 82 | +### Travelling Salesman Problem |
| 83 | + |
| 84 | +In `pygad.benchmarks.tsp`. Build `TSP` from either a 2D `coordinates` array or a square `distance_matrix`. A solution is a permutation of city indices and the fitness is the negative tour length (the tour closes back to the start). Non-permutation candidates get a large negative penalty. |
| 85 | + |
| 86 | +Class attributes `gene_space=list(range(num_cities))`, `gene_type=int`, and `allow_duplicate_genes=False` keep the permutation constraint: |
| 87 | + |
| 88 | +```python |
| 89 | +import pygad |
| 90 | +from pygad.benchmarks.tsp import TSP |
| 91 | + |
| 92 | +problem = TSP(coordinates=[[0.0, 0.0], |
| 93 | + [1.0, 0.0], |
| 94 | + [1.0, 1.0], |
| 95 | + [0.0, 1.0]]) |
| 96 | + |
| 97 | +ga = pygad.GA( |
| 98 | + num_generations=200, |
| 99 | + num_parents_mating=10, |
| 100 | + fitness_func=problem, |
| 101 | + sol_per_pop=30, |
| 102 | + num_genes=problem.num_genes, |
| 103 | + gene_space=problem.gene_space, |
| 104 | + gene_type=problem.gene_type, |
| 105 | + allow_duplicate_genes=problem.allow_duplicate_genes, |
| 106 | +) |
| 107 | +ga.run() |
| 108 | +``` |
| 109 | + |
| 110 | +## Example: SOO |
| 111 | + |
| 112 | +```python |
| 113 | +import pygad |
| 114 | +from pygad.benchmarks.classic import Sphere |
| 115 | + |
| 116 | +problem = Sphere(num_genes=10) |
| 117 | + |
| 118 | +ga = pygad.GA( |
| 119 | + num_generations=100, |
| 120 | + num_parents_mating=10, |
| 121 | + fitness_func=problem, |
| 122 | + sol_per_pop=20, |
| 123 | + num_genes=problem.num_genes, |
| 124 | + init_range_low=problem.bounds[0], |
| 125 | + init_range_high=problem.bounds[1], |
| 126 | + crossover_type='sbx', |
| 127 | + sbx_crossover_eta=30, |
| 128 | + mutation_type='polynomial', |
| 129 | + polynomial_mutation_eta=20, |
| 130 | +) |
| 131 | +ga.run() |
| 132 | +``` |
| 133 | + |
| 134 | +## Example: MOO |
| 135 | + |
| 136 | +```python |
| 137 | +import pygad |
| 138 | +from pygad.benchmarks.zdt import ZDT1 |
| 139 | +from pygad.utils.quality_indicators import inverted_generational_distance |
| 140 | + |
| 141 | +problem = ZDT1(num_genes=10) |
| 142 | + |
| 143 | +ga = pygad.GA( |
| 144 | + num_generations=200, |
| 145 | + num_parents_mating=20, |
| 146 | + fitness_func=problem, |
| 147 | + sol_per_pop=30, |
| 148 | + num_genes=problem.num_genes, |
| 149 | + init_range_low=problem.bounds[0], |
| 150 | + init_range_high=problem.bounds[1], |
| 151 | + parent_selection_type='nsga2', |
| 152 | + crossover_type='sbx', |
| 153 | + sbx_crossover_eta=30, |
| 154 | + mutation_type='polynomial', |
| 155 | + polynomial_mutation_eta=20, |
| 156 | +) |
| 157 | +ga.run() |
| 158 | + |
| 159 | +# IGD against the true front. |
| 160 | +true_front = problem.pareto_front(num_points=100) |
| 161 | +igd = inverted_generational_distance(ga.last_generation_fitness, true_front) |
| 162 | +print(f'IGD = {igd}') |
| 163 | +``` |
0 commit comments