NSGA-II Generator¶
NSGA2Generator
¶
Bases: GAGeneratorBase, StateOwner
Non-dominated Sorting Genetic Algorithm II (NSGA-II) generator. Implements the NSGA-II algorithm for multi-objective optimization as described in [1]. This generator accomdates user selected mutation and crossover operators and performs selection with non-dominated sorting and crowding distance.
References
[1] Deb, K., Pratap, A., Agarwal, S., & Meyarivan, T. (2002). A fast and elitist multiobjective genetic algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2). https://doi.org/10.1109/4235.996017
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_size
|
int
|
Size of the population maintained across generations. |
50
|
crossover_operator
|
SimulatedBinaryCrossover or DummyCrossover
|
Operator used to perform crossover between parent solutions. |
SimulatedBinaryCrossover()
|
mutation_operator
|
PolynomialMutation or DummyMutation
|
Operator used to perform mutation on offspring solutions. |
PolynomialMutation()
|
output_dir
|
str or PathLike
|
Directory to save algorithm state and population history. The path actually
written to, after expansion and collision avoidance, is |
required |
checkpoint_freq
|
int
|
Frequency (in generations) at which to save checkpoints. |
1
|
checkpoint_file
|
str
|
Path to checkpoint file to load from. If provided, the generator will be initialized from the checkpoint state. The user-provided VOCS must match the checkpoint VOCS exactly. User-specified parameters will override checkpoint values. |
required |
deduplicate_output
|
bool
|
Whether to ensure all generated candidates are unique. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
fevals |
int
|
Number of function evaluations performed so far. |
n_generations |
int
|
Number of generations completed. |
n_candidates |
int
|
Total number of candidate solutions generated. |
history_idx |
list of list of int
|
Xopt indices of individuals in each generation. |
pop |
list of dict
|
Current population of individuals. |
child |
list of dict
|
Buffer of evaluated offspring waiting to be incorporated into the population. |
Notes
When output_dir is set to a path, the populations and all evaluated individuals will be written to the
files "populations.csv" and "data.csv" respectively. Checkpoints are also saved every checkpoint_freq generation
to a subdirectory. If the output_dir already exists at the first time output is created in the generator's lifetime,
a number will be appended the output path to avoid overwriting previous data. output_dir itself is left as given,
with the path in use available from output_dir_resolved.
The population file contains all of the populations with an index "xopt_generation" to indicate with which generation each row is associated.
Source code in xopt/generators/ga/nsga2.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
output_dir_resolved
property
¶
Directory output is written to, empty until it has been created.
__init__(**kwargs)
¶
Initialize the generator.
Source code in xopt/generator.py
170 171 172 173 174 175 | |
close_log_file()
¶
Closes out the log file (if used)
Source code in xopt/generators/ga/base.py
232 233 234 235 236 237 238 239 | |
data_in_bounds(data)
¶
Returns true if every variable in the data dictionary is within bounds.
Source code in xopt/generators/ga/nsga2.py
457 458 459 460 461 462 463 464 | |
drop_resolved_output_dir(values)
classmethod
¶
Discard "output_dir_resolved" so a reloaded generator resolves its own.
Source code in xopt/generators/ga/base.py
57 58 59 60 61 62 63 64 | |
end_generation(generation_index, population)
¶
Record a completed generation, writing output and checkpoints as configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generation_index
|
int
|
Index of the generation which was just completed. |
required |
population
|
list of dict
|
The individuals making up the completed population. |
required |
Source code in xopt/generators/ga/base.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
generate(n_candidates)
¶
Generate the unique candidates.
If deduplication is enabled, ensures all returned candidates have unique decision variables that have not been seen before.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_candidates
|
int
|
Number of unique candidates to generate. |
required |
Returns:
| Type | Description |
|---|---|
list of dict
|
List of candidate solutions. |
Notes
When deduplication is enabled, the method may make multiple calls
to the underlying _generate method if duplicates are found, until
the requested number of unique candidates is obtained.
Source code in xopt/generators/deduplicated.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
load_from_checkpoint(values)
classmethod
¶
Load from checkpoint file if checkpoint_file is provided.
Source code in xopt/generators/checkpoints.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
model_dump(*args, **kwargs)
¶
overwrite model dump to remove faux class attrs
Source code in xopt/generator.py
203 204 205 206 207 208 209 210 211 | |
validate_output_dir(value)
classmethod
¶
Accept any os.PathLike, storing it as a string.
Source code in xopt/generators/ga/base.py
115 116 117 118 119 120 121 | |
vocs_compatible()
¶
Check that the VOCS object is compatible with our checkpoint For selection and the genetic operators to work correctly, all incoming variables, objectives, and constraints must exist as keys in pop/child
Source code in xopt/generators/ga/nsga2.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
yaml(**kwargs)
¶
serialize first then dump to yaml string
Source code in xopt/pydantic.py
295 296 297 298 299 300 301 302 | |
crowded_comparison_argsort(pop_f, pop_g=None)
¶
Sorts the objective functions by domination rank and then by crowding distance (crowded comparison operator). Indices to individuals are returned in order of increasing value of fitness by crowded comparison operator. That is, the least fit individuals are returned first.
Notes: NaN values are removed from the comparison and added back at the beginning (least fit direction) of the sorted indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop_f
|
ndarray
|
(N, M) numpy array where N is the number of individuals and M is the number of objectives |
required |
pop_g
|
ndarray
|
The constraints, by default None |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Numpy array of indices to sorted individuals |
Source code in xopt/generators/ga/nsga2.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
cull_population(pop_x, pop_f, pop_g, population_size)
¶
Reduce population size by selecting the best individuals based on crowded comparison.
Uses crowded comparison sorting to rank individuals in the population, then selects the top-ranked individuals to maintain the desired population size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop_x
|
ndarray
|
Decision variables of the population, shape (n_individuals, n_variables). |
required |
pop_f
|
ndarray / None
|
Objective function values of the population, shape (n_individuals, n_objectives), None if no constraints. |
required |
pop_g
|
ndarray
|
Constraint violation values of the population, shape (n_individuals, n_constraints). |
required |
population_size
|
int
|
Target size for the reduced population. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Indices of selected individuals, shape (population_size,). |
Source code in xopt/generators/ga/nsga2.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
generate_candidates_from_population(pop, vocs, n_candidates, mutation_operator, crossover_operator)
¶
Generate offspring from an existing population using binary tournament selection, crossover, and mutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop
|
list[dict]
|
Current population individuals. |
required |
vocs
|
VOCS
|
VOCS object defining variables, objectives, and constraints. |
required |
n_candidates
|
int
|
Number of offspring to produce. |
required |
mutation_operator
|
MutationOperator
|
Mutation operator to apply. |
required |
crossover_operator
|
CrossoverOperator
|
Crossover operator to apply. |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
Generated candidates with variable name keys. |
Source code in xopt/generators/ga/nsga2.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
generate_child_binary_tournament(pop_x, pop_f, pop_g, bounds, mutate, crossover, fitness=None)
¶
Creates a single child from the population using binary tournament selection, crossover, and mutation.
Selection is performed using binary tournament where 4 random individuals are chosen and the best from each pair becomes a parent. The two parents undergo crossover to produce a child, which is then mutated before being returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop_x
|
ndarray
|
Decision variables of the population, shape (n_individuals, n_variables). |
required |
pop_f
|
ndarray
|
Objective function values of the population, shape (n_individuals, n_objectives). |
required |
pop_g
|
ndarray / None
|
Constraint violation values of the population, shape (n_individuals, n_constraints). None if no constraints. |
required |
bounds
|
ndarray
|
Bounds for decision variables, shape (2, n_variables) where bounds[0] are lower bounds and bounds[1] are upper bounds. |
required |
mutate
|
MutationOperator
|
Mutation operator to apply to the child solution. |
required |
crossover
|
CrossoverOperator
|
Crossover operator to apply to the parent solutions. |
required |
fitness
|
ndarray
|
The fitness of each individual (or None to compute from objectives and constraints) |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The child solution with decision variables, shape (n_variables,). |
Source code in xopt/generators/ga/nsga2.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
get_crowding_distance(pop_f)
¶
Calculates NSGA-II style crowding distance as described in [1].
References
[1] Deb, K., Pratap, A., Agarwal, S., & Meyarivan, T. (2002). A fast and elitist multiobjective genetic algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2). https://doi.org/10.1109/4235.996017
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop_f
|
ndarray
|
(M, N) numpy array where N is the number of individuals and M is the number of objectives |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Numpy array of crowding distance for each individual |
Source code in xopt/generators/ga/nsga2.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
get_fitness(pop_f, pop_g=None)
¶
Get the "fitness" of each individual according to domination and crowding distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pop_f
|
ndarray
|
The objectives |
required |
pop_g
|
ndarray / None
|
The constraints, or None of no constraints |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The fitness of each individual |
Source code in xopt/generators/ga/nsga2.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
vocs_data_to_arr(data)
¶
Force data coming from VOCS object into 2D numpy array (or None) for compatibility with helper functions
Source code in xopt/generators/ga/nsga2.py
30 31 32 33 34 35 36 37 38 39 40 | |