From 6d1fc7dc0447da9f270fcee2870d97aa2296b426 Mon Sep 17 00:00:00 2001 From: SevgiAkten Date: Fri, 9 Aug 2024 16:25:00 +0300 Subject: [PATCH] (#18) edit optimizer and add usage examples to readme and manuscript --- README.md | 158 +++- cga/__init__.py | 3 - paper/paper.md | 146 ++- {cga/mutation => src}/__init__.py | 0 {cga => src}/byte_operators.py | 0 {cga => src}/db_utility.py | 0 {cga => src}/grid.py | 0 src/individual.py | 221 +++++ src/main.py | 231 +++++ {cga => src}/main_cga.py | 2 +- .../mutation}/__init__.py | 0 {cga => src}/mutation/bit_flip_mutation.py | 4 +- {cga => src}/mutation/byte_mutation.py | 4 +- {cga => src}/mutation/byte_mutation_random.py | 4 +- .../mutation/float_uniform_mutation.py | 4 +- {cga => src}/mutation/insertion_mutation.py | 4 +- {cga => src}/mutation/shuffle_mutation.py | 4 +- {cga => src}/mutation/swap_mutation.py | 4 +- {cga => src}/mutation/two_opt_mutation.py | 4 +- .../neighborhoods}/__init__.py | 0 {cga => src}/neighborhoods/compact_13.py | 0 {cga => src}/neighborhoods/compact_21.py | 0 {cga => src}/neighborhoods/compact_25.py | 0 {cga => src}/neighborhoods/compact_9.py | 0 {cga => src}/neighborhoods/linear_5.py | 0 {cga => src}/neighborhoods/linear_9.py | 0 src/optimizer.py | 883 ++++++++++++++++++ {cga => src}/optimizer_alpha_cga.py | 16 +- {cga => src}/optimizer_ccga.py | 19 +- {cga => src}/optimizer_cga.py | 15 +- {cga => src}/optimizer_mcccga.py | 21 +- {cga => src}/optimizer_sync_cga.py | 15 +- {cga => src}/population.py | 53 +- .../problems}/__init__.py | 0 {cga => src}/problems/abstract_problem.py | 0 .../problems/single_objective}/__init__.py | 0 .../single_objective/continuous}/__init__.py | 0 .../single_objective/continuous/ackley.py | 2 +- .../single_objective/continuous/bentcigar.py | 2 +- .../continuous/bohachevsky.py | 2 +- .../continuous/chichinadze.py | 2 +- .../single_objective/continuous/dropwave.py | 2 +- .../single_objective/continuous/fms.py | 2 +- .../single_objective/continuous/griewank.py | 2 +- .../single_objective/continuous/holzman.py | 2 +- .../single_objective/continuous/levy.py | 2 +- .../single_objective/continuous/matyas.py | 2 +- .../single_objective/continuous/pow.py | 2 +- .../single_objective/continuous/powell.py | 2 +- .../single_objective/continuous/rastrigin.py | 2 +- .../single_objective/continuous/rosenbrock.py | 2 +- .../continuous/rothellipsoid.py | 2 +- .../single_objective/continuous/schaffer.py | 2 +- .../single_objective/continuous/schaffer2.py | 2 +- .../single_objective/continuous/schwefel.py | 2 +- .../single_objective/continuous/sphere.py | 2 +- .../continuous/styblinskitang.py | 2 +- .../continuous/sumofdifferentpowers.py | 2 +- .../single_objective/continuous/threehumps.py | 2 +- .../single_objective/continuous/zakharov.py | 2 +- .../single_objective/continuous/zettle.py | 2 +- .../single_objective/discrete}/__init__.py | 0 .../discrete/binary}/__init__.py | 0 .../discrete/binary/count_sat.py | 2 +- .../single_objective/discrete/binary/ecc.py | 2 +- .../single_objective/discrete/binary/fms.py | 2 +- .../discrete/binary/maxcut100.py | 2 +- .../discrete/binary/maxcut20_01.py | 2 +- .../discrete/binary/maxcut20_09.py | 2 +- .../single_objective/discrete/binary/mmdp.py | 2 +- .../discrete/binary/one_max.py | 2 +- .../single_objective/discrete/binary/peak.py | 2 +- .../discrete/permutation}/__init__.py | 0 .../discrete/permutation/burma14.tsp.txt | 0 .../discrete/permutation/tsp.py | 2 +- .../recombination}/__init__.py | 0 .../recombination/arithmetic_crossover.py | 4 +- .../recombination/blxalpha_crossover.py | 4 +- .../recombination/byte_one_point_crossover.py | 4 +- .../recombination/byte_uniform_crossover.py | 4 +- {cga => src}/recombination/flat_crossover.py | 4 +- .../recombination/linear_crossover.py | 4 +- .../recombination/one_point_crossover.py | 4 +- {cga => src}/recombination/pmx_crossover.py | 4 +- .../recombination/two_point_crossover.py | 4 +- .../recombination/unfair_avarage_crossover.py | 4 +- .../recombination/uniform_crossover.py | 4 +- {cga/tests => src/selection}/__init__.py | 0 .../selection/roulette_wheel_selection.py | 2 +- .../selection/tournament_selection.py | 2 +- src/tests/__init__.py | 0 {cga => src}/tests/conftest.py | 2 +- {cga => src}/tests/noteontests.txt | 0 {cga => src}/tests/test_ackley.py | 2 +- .../tests/test_arithmetic_crossover.py | 6 +- {cga => src}/tests/test_bentcigar_function.py | 2 +- {cga => src}/tests/test_bit_flip_mutation.py | 6 +- {cga => src}/tests/test_blxalpha_crossover.py | 6 +- {cga => src}/tests/test_bohachevsky.py | 2 +- {cga => src}/tests/test_byte_mutation.py | 6 +- .../tests/test_byte_mutation_random.py | 6 +- .../tests/test_byte_one_point_crossover.py | 6 +- {cga => src}/tests/test_byte_operators.py | 2 +- .../tests/test_byte_uniform_crossover.py | 6 +- .../tests/test_chichinadze_function.py | 2 +- {cga => src}/tests/test_compact_13.py | 2 +- {cga => src}/tests/test_compact_21.py | 2 +- {cga => src}/tests/test_compact_25.py | 2 +- {cga => src}/tests/test_compact_9.py | 2 +- {cga => src}/tests/test_count_sat.py | 2 +- {cga => src}/tests/test_db_utility.py | 2 +- {cga => src}/tests/test_dropwave_function.py | 2 +- {cga => src}/tests/test_ecc.py | 2 +- {cga => src}/tests/test_flat_crossover.py | 6 +- .../tests/test_float_uniform_mutation.py | 6 +- {cga => src}/tests/test_fms.py | 4 +- {cga => src}/tests/test_grid.py | 2 +- {cga => src}/tests/test_griewank_function.py | 2 +- {cga => src}/tests/test_holzman_function.py | 2 +- {cga => src}/tests/test_individual.py | 2 +- {cga => src}/tests/test_insertion_mutation.py | 6 +- {cga => src}/tests/test_levy_function.py | 2 +- {cga => src}/tests/test_linear_5.py | 2 +- {cga => src}/tests/test_linear_9.py | 2 +- {cga => src}/tests/test_linear_crossover.py | 6 +- {cga => src}/tests/test_matyas_function.py | 2 +- {cga => src}/tests/test_maxcut100.py | 2 +- {cga => src}/tests/test_maxcut20_01.py | 2 +- {cga => src}/tests/test_maxcut20_09.py | 2 +- {cga => src}/tests/test_mmdp.py | 2 +- {cga => src}/tests/test_one_max.py | 2 +- .../tests/test_one_point_crossover.py | 6 +- {cga => src}/tests/test_optimizer.py | 12 +- .../tests/test_optimizer_alpha_cga.py | 16 +- {cga => src}/tests/test_optimizer_ccga.py | 18 +- {cga => src}/tests/test_optimizer_cga.py | 14 +- {cga => src}/tests/test_optimizer_sync_cga.py | 14 +- {cga => src}/tests/test_peak.py | 2 +- {cga => src}/tests/test_pmx_crossover.py | 6 +- {cga => src}/tests/test_population.py | 12 +- {cga => src}/tests/test_pow_function.py | 2 +- {cga => src}/tests/test_powell_function.py | 2 +- {cga => src}/tests/test_rastrigin.py | 2 +- {cga => src}/tests/test_rosenbrock.py | 2 +- .../tests/test_rothellipsoid_function.py | 2 +- .../tests/test_roulette_wheel_selection.py | 6 +- {cga => src}/tests/test_schaffer2_function.py | 2 +- {cga => src}/tests/test_schaffer_function.py | 2 +- {cga => src}/tests/test_schwefel.py | 2 +- {cga => src}/tests/test_shuffle_mutation.py | 6 +- {cga => src}/tests/test_sphere.py | 2 +- .../tests/test_styblinskitang_function.py | 2 +- .../test_sumofdifferentpowers_function.py | 2 +- {cga => src}/tests/test_swap_mutation.py | 6 +- .../tests/test_threehumps_function.py | 2 +- .../tests/test_tournament_selection.py | 6 +- {cga => src}/tests/test_tsp.py | 2 +- {cga => src}/tests/test_two_opt_mutation.py | 6 +- .../tests/test_two_point_crossover.py | 6 +- .../tests/test_unfair_average_crossover.py | 6 +- {cga => src}/tests/test_uniform_crossover.py | 6 +- {cga => src}/tests/test_zakharov_function.py | 2 +- {cga => src}/tests/test_zettle_function.py | 2 +- 163 files changed, 1928 insertions(+), 305 deletions(-) delete mode 100644 cga/__init__.py rename {cga/mutation => src}/__init__.py (100%) rename {cga => src}/byte_operators.py (100%) rename {cga => src}/db_utility.py (100%) rename {cga => src}/grid.py (100%) create mode 100644 src/individual.py create mode 100644 src/main.py rename {cga => src}/main_cga.py (99%) rename {cga/neighborhoods => src/mutation}/__init__.py (100%) rename {cga => src}/mutation/bit_flip_mutation.py (95%) rename {cga => src}/mutation/byte_mutation.py (97%) rename {cga => src}/mutation/byte_mutation_random.py (96%) rename {cga => src}/mutation/float_uniform_mutation.py (96%) rename {cga => src}/mutation/insertion_mutation.py (96%) rename {cga => src}/mutation/shuffle_mutation.py (97%) rename {cga => src}/mutation/swap_mutation.py (96%) rename {cga => src}/mutation/two_opt_mutation.py (96%) rename {cga/problems => src/neighborhoods}/__init__.py (100%) rename {cga => src}/neighborhoods/compact_13.py (100%) rename {cga => src}/neighborhoods/compact_21.py (100%) rename {cga => src}/neighborhoods/compact_25.py (100%) rename {cga => src}/neighborhoods/compact_9.py (100%) rename {cga => src}/neighborhoods/linear_5.py (100%) rename {cga => src}/neighborhoods/linear_9.py (100%) create mode 100644 src/optimizer.py rename {cga => src}/optimizer_alpha_cga.py (95%) rename {cga => src}/optimizer_ccga.py (94%) rename {cga => src}/optimizer_cga.py (94%) rename {cga => src}/optimizer_mcccga.py (93%) rename {cga => src}/optimizer_sync_cga.py (94%) rename {cga => src}/population.py (61%) rename {cga/problems/single_objective => src/problems}/__init__.py (100%) rename {cga => src}/problems/abstract_problem.py (100%) rename {cga/problems/single_objective/continuous => src/problems/single_objective}/__init__.py (100%) rename {cga/problems/single_objective/discrete => src/problems/single_objective/continuous}/__init__.py (100%) rename {cga => src}/problems/single_objective/continuous/ackley.py (95%) rename {cga => src}/problems/single_objective/continuous/bentcigar.py (95%) rename {cga => src}/problems/single_objective/continuous/bohachevsky.py (95%) rename {cga => src}/problems/single_objective/continuous/chichinadze.py (95%) rename {cga => src}/problems/single_objective/continuous/dropwave.py (96%) rename {cga => src}/problems/single_objective/continuous/fms.py (96%) rename {cga => src}/problems/single_objective/continuous/griewank.py (94%) rename {cga => src}/problems/single_objective/continuous/holzman.py (94%) rename {cga => src}/problems/single_objective/continuous/levy.py (95%) rename {cga => src}/problems/single_objective/continuous/matyas.py (95%) rename {cga => src}/problems/single_objective/continuous/pow.py (95%) rename {cga => src}/problems/single_objective/continuous/powell.py (95%) rename {cga => src}/problems/single_objective/continuous/rastrigin.py (94%) rename {cga => src}/problems/single_objective/continuous/rosenbrock.py (94%) rename {cga => src}/problems/single_objective/continuous/rothellipsoid.py (95%) rename {cga => src}/problems/single_objective/continuous/schaffer.py (96%) rename {cga => src}/problems/single_objective/continuous/schaffer2.py (95%) rename {cga => src}/problems/single_objective/continuous/schwefel.py (95%) rename {cga => src}/problems/single_objective/continuous/sphere.py (94%) rename {cga => src}/problems/single_objective/continuous/styblinskitang.py (95%) rename {cga => src}/problems/single_objective/continuous/sumofdifferentpowers.py (92%) rename {cga => src}/problems/single_objective/continuous/threehumps.py (95%) rename {cga => src}/problems/single_objective/continuous/zakharov.py (95%) rename {cga => src}/problems/single_objective/continuous/zettle.py (94%) rename {cga/problems/single_objective/discrete/binary => src/problems/single_objective/discrete}/__init__.py (100%) rename {cga/problems/single_objective/discrete/permutation => src/problems/single_objective/discrete/binary}/__init__.py (100%) rename {cga => src}/problems/single_objective/discrete/binary/count_sat.py (96%) rename {cga => src}/problems/single_objective/discrete/binary/ecc.py (97%) rename {cga => src}/problems/single_objective/discrete/binary/fms.py (98%) rename {cga => src}/problems/single_objective/discrete/binary/maxcut100.py (99%) rename {cga => src}/problems/single_objective/discrete/binary/maxcut20_01.py (98%) rename {cga => src}/problems/single_objective/discrete/binary/maxcut20_09.py (98%) rename {cga => src}/problems/single_objective/discrete/binary/mmdp.py (97%) rename {cga => src}/problems/single_objective/discrete/binary/one_max.py (93%) rename {cga => src}/problems/single_objective/discrete/binary/peak.py (96%) rename {cga/recombination => src/problems/single_objective/discrete/permutation}/__init__.py (100%) rename {cga => src}/problems/single_objective/discrete/permutation/burma14.tsp.txt (100%) rename {cga => src}/problems/single_objective/discrete/permutation/tsp.py (98%) rename {cga/selection => src/recombination}/__init__.py (100%) rename {cga => src}/recombination/arithmetic_crossover.py (96%) rename {cga => src}/recombination/blxalpha_crossover.py (97%) rename {cga => src}/recombination/byte_one_point_crossover.py (97%) rename {cga => src}/recombination/byte_uniform_crossover.py (97%) rename {cga => src}/recombination/flat_crossover.py (96%) rename {cga => src}/recombination/linear_crossover.py (97%) rename {cga => src}/recombination/one_point_crossover.py (96%) rename {cga => src}/recombination/pmx_crossover.py (98%) rename {cga => src}/recombination/two_point_crossover.py (96%) rename {cga => src}/recombination/unfair_avarage_crossover.py (96%) rename {cga => src}/recombination/uniform_crossover.py (96%) rename {cga/tests => src/selection}/__init__.py (100%) rename {cga => src}/selection/roulette_wheel_selection.py (98%) rename {cga => src}/selection/tournament_selection.py (98%) create mode 100644 src/tests/__init__.py rename {cga => src}/tests/conftest.py (82%) rename {cga => src}/tests/noteontests.txt (100%) rename {cga => src}/tests/test_ackley.py (95%) rename {cga => src}/tests/test_arithmetic_crossover.py (93%) rename {cga => src}/tests/test_bentcigar_function.py (92%) rename {cga => src}/tests/test_bit_flip_mutation.py (92%) rename {cga => src}/tests/test_blxalpha_crossover.py (93%) rename {cga => src}/tests/test_bohachevsky.py (95%) rename {cga => src}/tests/test_byte_mutation.py (93%) rename {cga => src}/tests/test_byte_mutation_random.py (92%) rename {cga => src}/tests/test_byte_one_point_crossover.py (93%) rename {cga => src}/tests/test_byte_operators.py (93%) rename {cga => src}/tests/test_byte_uniform_crossover.py (93%) rename {cga => src}/tests/test_chichinadze_function.py (94%) rename {cga => src}/tests/test_compact_13.py (97%) rename {cga => src}/tests/test_compact_21.py (97%) rename {cga => src}/tests/test_compact_25.py (97%) rename {cga => src}/tests/test_compact_9.py (97%) rename {cga => src}/tests/test_count_sat.py (94%) rename {cga => src}/tests/test_db_utility.py (97%) rename {cga => src}/tests/test_dropwave_function.py (95%) rename {cga => src}/tests/test_ecc.py (95%) rename {cga => src}/tests/test_flat_crossover.py (93%) rename {cga => src}/tests/test_float_uniform_mutation.py (92%) rename {cga => src}/tests/test_fms.py (91%) rename {cga => src}/tests/test_grid.py (97%) rename {cga => src}/tests/test_griewank_function.py (95%) rename {cga => src}/tests/test_holzman_function.py (95%) rename {cga => src}/tests/test_individual.py (98%) rename {cga => src}/tests/test_insertion_mutation.py (91%) rename {cga => src}/tests/test_levy_function.py (95%) rename {cga => src}/tests/test_linear_5.py (97%) rename {cga => src}/tests/test_linear_9.py (97%) rename {cga => src}/tests/test_linear_crossover.py (94%) rename {cga => src}/tests/test_matyas_function.py (94%) rename {cga => src}/tests/test_maxcut100.py (92%) rename {cga => src}/tests/test_maxcut20_01.py (95%) rename {cga => src}/tests/test_maxcut20_09.py (95%) rename {cga => src}/tests/test_mmdp.py (93%) rename {cga => src}/tests/test_one_max.py (92%) rename {cga => src}/tests/test_one_point_crossover.py (91%) rename {cga => src}/tests/test_optimizer.py (62%) rename {cga => src}/tests/test_optimizer_alpha_cga.py (83%) rename {cga => src}/tests/test_optimizer_ccga.py (88%) rename {cga => src}/tests/test_optimizer_cga.py (85%) rename {cga => src}/tests/test_optimizer_sync_cga.py (85%) rename {cga => src}/tests/test_peak.py (93%) rename {cga => src}/tests/test_pmx_crossover.py (90%) rename {cga => src}/tests/test_population.py (88%) rename {cga => src}/tests/test_pow_function.py (95%) rename {cga => src}/tests/test_powell_function.py (95%) rename {cga => src}/tests/test_rastrigin.py (93%) rename {cga => src}/tests/test_rosenbrock.py (93%) rename {cga => src}/tests/test_rothellipsoid_function.py (94%) rename {cga => src}/tests/test_roulette_wheel_selection.py (91%) rename {cga => src}/tests/test_schaffer2_function.py (94%) rename {cga => src}/tests/test_schaffer_function.py (94%) rename {cga => src}/tests/test_schwefel.py (94%) rename {cga => src}/tests/test_shuffle_mutation.py (91%) rename {cga => src}/tests/test_sphere.py (94%) rename {cga => src}/tests/test_styblinskitang_function.py (94%) rename {cga => src}/tests/test_sumofdifferentpowers_function.py (93%) rename {cga => src}/tests/test_swap_mutation.py (91%) rename {cga => src}/tests/test_threehumps_function.py (94%) rename {cga => src}/tests/test_tournament_selection.py (91%) rename {cga => src}/tests/test_tsp.py (94%) rename {cga => src}/tests/test_two_opt_mutation.py (92%) rename {cga => src}/tests/test_two_point_crossover.py (91%) rename {cga => src}/tests/test_unfair_average_crossover.py (93%) rename {cga => src}/tests/test_uniform_crossover.py (91%) rename {cga => src}/tests/test_zakharov_function.py (93%) rename {cga => src}/tests/test_zettle_function.py (95%) diff --git a/README.md b/README.md index a10d717..06a16ac 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,14 @@ ## Features -- **Cellular Genetic Algorithms (CGAs)**: Efficient implementation of CGAs with various built-in functions. -- **Machine-Coded Operators**: Includes byte-based and machine-coded operators for efficient numerical data handling. -- **Customizable**: Various customization options to suit different optimization problems. -- **Alpha-male CGA**: A specialized CGA variant. -- **Machine Coded Compact CGA**: Optimized for compact representations. -- **Improved CGA with Machine Coded Operators**: Enhanced performance for real-valued optimization problems. +- **Cellular Genetic Algorithm (`cga`)**: Efficient implementation of CGAs with various built-in functions for diverse applications. +- **Improved CGA with Machine-Coded Operators**: Enhanced performance in real-valued optimization problems through the use of `machine-coded` `byte operators`. +- **Synchronous Cellular Genetic Algorithm (`sync_cga`)**: Simultaneous update of all individuals (cells) in each iteration for synchronized evolution. +- **Alpha Male Cellular Genetic Algorithm (`alpha_cga`)**: Population divided into social groups, with each group consisting of females selecting the same alpha male. +- **Compact Cellular Genetic Algorithm (`ccga`)**: Integrates the principles of Cellular Genetic Algorithms with those of Compact Genetic Algorithms for memory efficiency. +- **Machine-Coded Compact Cellular Genetic Algorithm (`mcccga`)**: Applies machine-coded compact GA to a cellular structure for optimizing real-valued problems. +- **Customizable**: Offers various customization options to adapt to different optimization problems. + ## Installation @@ -23,20 +25,148 @@ pip install pycellga Comprehensive documentation is available on the official documentation site. -## Example +## Usage Examples + +In this section, we'll explain what each method in the optimizer does and provide examples of how to use them. The package includes various ready-to-use crossover and mutation operators, along with Real-valued, Binary, and Permutation functions that you can run directly —examples are available in the `main.py` file. Please configure the `gen_type` attribute as `Real-valued`, `Binary`, or `Permutation` according to the type of problem you want to solve. + +### 1. **cga (Cellular Genetic Algorithm)** + +**cga** is a type of genetic algorithm where the population is structured as a grid (or other topologies) and each individual interacts only with its neighbors. This structure helps maintain diversity in the population and can prevent premature convergence. To convert it from classcical cga into a specialized algorithm, ICGA (Improved CGA) with machine-coded representation for real-valued problems, you should use byte operators.The formulation for encoding and decoding numbers was created by an algorithm specified in the IEEE 754 – IEEE Standards for Floating-Point Arithmetic. This approach yields better results for continuous functions. + +**Usage:** + +```python +result = optimizer.cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.ByteOnePointCrossover, + mutation = optimizer.ByteMutationRandom +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 0.0 +# Best solution chromosome: [0.0, 0.0, 0.0, 0.0, 0.0] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 2. **sync_cga (Synchronous Cellular Genetic Algorithm)** + +In the **sync_cga** all individuals (cells) are updated simultaneously in each iteration (generation). This means that the algorithm evaluates the fitness of all individuals, selects parents, and applies genetic operators (crossover and mutation) in parallel, updating the entire population at once before moving to the next generation. + +**Usage:** + +```python +result = optimizer.sync_cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.BlxalphaCrossover, + mutation = optimizer.FloatUniformMutation +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 0.0 +# Best solution chromosome: [0.0001, 0.00012, 0.00022, 5e-05, -5e-05] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 3. **alpha_cga (Alpha Male Cellular Genetic Algorithm)** + +**alpha_cga** is an extension of the Cellular Genetic Algorithm that involves dividing the population into social groups, where each group consists of females selecting the same alpha male. Within each group, one individual is labeled as the alpha male, and the rest are productive females. + +**Usage:** + +```python +result = optimizer.alpha_cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 10, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.BlxalphaCrossover, + mutation = optimizer.FloatUniformMutation +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 0.0 +# Best solution chromosome: [0.0, 0.0, 0.0, 8e-05, -6e-05, 0.00015, 0.0, -0.00014, -0.0, 0.0] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 4. **ccga (Compact Cellular Genetic Algorithm)** -Can we put a small code snipped here, for example finding the global minimum of Ackley function or something more interesting? It would also be nice to see more than one feature of the package with combining different algorithms and cases, e.g. different operators, byte-bit encodings, permutation, binary, or real, etc. +**ccga** combines the principles of Cellular Genetic Algorithms with those of Compact Genetic Algorithms. This approach leverages the structured population model of Cellular GAs and the memory efficiency of Compact GAs. + +**Usage:** ```python -# The function to be minimized -def f(x): - return (x[0] - 3.14159264)**2 + (x[1] - 2.71828)**2 +result = optimizer.ccga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 10, + gen_type = "Binary", + problem = optimizer.OneMax(), + selection = optimizer.TournamentSelection +) + +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 8 +# Best solution chromosome: [0, 1, 0, 1, 1, 1, 1, 1, 1, 1] + +# Note that the result could be different because the algorithm includes randomness. +``` -result = optimizer(f, ....) +### 5. **mcccga (Machine-Coded Compact Cellular Genetic Algorithm)** + +**mcccga** is the adaptation of the machine-coded compact GA for real-valued optimization problems has been applied to a cellular structure. Real-valued variables are converted into a binary format using the IEEE-754 standard. Encoding and decoding processes are performed bidirectionally using the same standard when necessary. Also the initial vector is generated within a narrowed range according to the variable's definition domain. + +**Usage:** + +```python +result = optimizer.mcccga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + min_value = -32.768, + max_value = 32.768 +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) # The result is -# x[0] = 3.14159265 -# x[1] = 2.71828 +# Best solution: 11.334 +# Best solution chromosome: [2.201, 18.259, -11.774, 0.0, 20.0] + +# Note that the result could be different because the algorithm includes randomness. ``` ## Contributing diff --git a/cga/__init__.py b/cga/__init__.py deleted file mode 100644 index b28b04f..0000000 --- a/cga/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/paper/paper.md b/paper/paper.md index 3eb8cf9..a2ad21c 100644 --- a/paper/paper.md +++ b/paper/paper.md @@ -18,9 +18,8 @@ affiliations: index: 2 -date: 6 Aug 2024 +date: 9 Aug 2024 bibliography: paper.bib -csl: apa.csl --- # Summary @@ -54,6 +53,149 @@ The need for `pycellga` arises from the increasing complexity of optimization pr ```python pip install pycellga ``` +## Usage Examples + +In this section, we'll explain what each method in the optimizer does and provide examples of how to use them. The package includes various ready-to-use crossover and mutation operators, along with Real-valued, Binary, and Permutation functions that you can run directly —examples are available in the `main.py` file. Please configure the `gen_type` attribute as `Real-valued`, `Binary`, or `Permutation` according to the type of problem you want to solve. + +### 1. **cga (Cellular Genetic Algorithm)** + +**cga** is a type of genetic algorithm where the population is structured as a grid (or other topologies) and each individual interacts only with its neighbors. This structure helps maintain diversity in the population and can prevent premature convergence. To convert it from classcical cga into a specialized algorithm, ICGA (Improved CGA) with machine-coded representation for real-valued problems, you should use byte operators. The formulation for encoding and decoding numbers was created by an algorithm specified in the IEEE 754 – IEEE Standards for Floating-Point Arithmetic. This approach yields better results for continuous functions. + +**Usage:** + +```python +result = optimizer.cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.ByteOnePointCrossover, + mutation = optimizer.ByteMutationRandom +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 0.0 +# Best solution chromosome: [0.0, 0.0, 0.0, 0.0, 0.0] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 2. **sync_cga (Synchronous Cellular Genetic Algorithm)** + +In the **sync_cga** all individuals (cells) are updated simultaneously in each iteration (generation). This means that the algorithm evaluates the fitness of all individuals, selects parents, and applies genetic operators (crossover and mutation) in parallel, updating the entire population at once before moving to the next generation. + +**Usage:** + +```python +result = optimizer.sync_cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.BlxalphaCrossover, + mutation = optimizer.FloatUniformMutation +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 0.0 +# Best solution chromosome: [0.0001, 0.00012, 0.00022, 5e-05, -5e-05] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 3. **alpha_cga (Alpha Male Cellular Genetic Algorithm)** + +**alpha_cga** is an extension of the Cellular Genetic Algorithm that involves dividing the population into social groups, where each group consists of females selecting the same alpha male. Within each group, one individual is labeled as the alpha male, and the rest are productive females. + +**Usage:** + +```python +result = optimizer.alpha_cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 10, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.BlxalphaCrossover, + mutation = optimizer.FloatUniformMutation +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 0.0 +# Best solution chromosome: [0.0, 0.0, 0.0, 8e-05, -6e-05, 0.00015, 0.0, -0.00014, -0.0, 0.0] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 4. **ccga (Compact Cellular Genetic Algorithm)** + +**ccga** combines the principles of Cellular Genetic Algorithms with those of Compact Genetic Algorithms. This approach leverages the structured population model of Cellular GAs and the memory efficiency of Compact GAs. + +**Usage:** + +```python +result = optimizer.ccga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 10, + gen_type = "Binary", + problem = optimizer.OneMax(), + selection = optimizer.TournamentSelection +) + +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 8 +# Best solution chromosome: [0, 1, 0, 1, 1, 1, 1, 1, 1, 1] + +# Note that the result could be different because the algorithm includes randomness. +``` + +### 5. **mcccga (Machine-Coded Compact Cellular Genetic Algorithm)** + +**mcccga** is the adaptation of the machine-coded compact GA for real-valued optimization problems has been applied to a cellular structure. Real-valued variables are converted into a binary format using the IEEE-754 standard. Encoding and decoding processes are performed bidirectionally using the same standard when necessary. Also the initial vector is generated within a narrowed range according to the variable's definition domain. + +**Usage:** + +```python +result = optimizer.mcccga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + min_value = -32.768, + max_value = 32.768 +) +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) + +# The result is +# Best solution: 11.334 +# Best solution chromosome: [2.201, 18.259, -11.774, 0.0, 20.0] + +# Note that the result could be different because the algorithm includes randomness. +``` # References diff --git a/cga/mutation/__init__.py b/src/__init__.py similarity index 100% rename from cga/mutation/__init__.py rename to src/__init__.py diff --git a/cga/byte_operators.py b/src/byte_operators.py similarity index 100% rename from cga/byte_operators.py rename to src/byte_operators.py diff --git a/cga/db_utility.py b/src/db_utility.py similarity index 100% rename from cga/db_utility.py rename to src/db_utility.py diff --git a/cga/grid.py b/src/grid.py similarity index 100% rename from cga/grid.py rename to src/grid.py diff --git a/src/individual.py b/src/individual.py new file mode 100644 index 0000000..2f884e3 --- /dev/null +++ b/src/individual.py @@ -0,0 +1,221 @@ +from numpy import random +import random as rd +from problems.abstract_problem import AbstractProblem + + + +class Individual: + """ + A class to represent an individual in an evolutionary algorithm. + + Attributes + ---------- + chromosome : list + The chromosome representing the individual. + fitness_value : float + The fitness value of the individual. + position : tuple + The position of the individual, represented as a tuple (x, y). + neighbors_positions : list or None + The positions of the individual's neighbors. + neighbors : list or None + The list of neighbors for the individual. + gen_type : str + The type of genome representation ("Binary", "Permutation", "Real-valued"). + ch_size : int + The size of the chromosome. + """ + + def __init__(self, + gen_type: str = "", + ch_size: int = 0, + problem: AbstractProblem = None): + """ + Initialize an Individual with a specific genome type and chromosome size. + + Parameters + ---------- + gen_type : str, optional + The type of genome representation. Must be one of "Binary", "Permutation", or "Real-valued". (default is "Binary") + ch_size : int + The size of the chromosome. + """ + self.gen_type = gen_type + self.ch_size = ch_size + self.problem = problem + self.chromosome = [] + self.fitness_value = 0 + self.position = (0, 0) + self.neighbors_positions = None + self.neighbors = None + + + def randomize(self): + """ + Randomly initialize the chromosome based on the genome type. + + Returns + ------- + list + The randomly generated chromosome. + + Raises + ------ + NotImplementedError + If the genome type is not implemented. + """ + problem_name = self.problem.__class__.__name__ + + if self.gen_type == "Binary": + + # CountSat, Fms, Mmdp, OneMax, Ecc, Maxcut20_01, Maxcut20_09, Maxcut100 + if problem_name in ["CountSat", "Fms", "Mmdp", "OneMax", "Ecc", "Maxcut20_01", "Maxcut20_09", "Maxcut100"]: + self.chromosome = [random.randint(2) for i in range(self.ch_size)] + # Peak + elif problem_name == "Peak": + self.chromosome = [[random.randint(2) for g in range(self.ch_size)] for h in range(self.ch_size)] + + elif self.gen_type == "Permutation": + + # Tsp + if problem_name == "Tsp": + self.chromosome = list(rd.sample(range(1, 15), self.ch_size)) + + elif self.gen_type == "Real-valued": + + # Ackley + if problem_name == "Ackley": + self.chromosome = [round(rd.uniform(-32.768, 32.768), 3) for i in range(self.ch_size)] + + # Bohachevsky + elif problem_name == "Bohachevsky": + self.chromosome = [random.randint(-15.0, 16.0)for i in range(self.ch_size)] + + # Fms + elif problem_name == "Fms": + self.chromosome = [round(rd.uniform(-6.4, 6.35), 3) for i in range(self.ch_size)] + + # Griewank + elif problem_name == "Griewank": + self.chromosome = [round(rd.uniform(-600, 600), 3) for i in range(self.ch_size)] + + # Rastrigin + elif problem_name == "Rastrigin": + self.chromosome = [round(rd.uniform(-5.12, 5.13), 2) for i in range(self.ch_size)] + + # Rosenbrock + elif problem_name == "Rosenbrock": + self.chromosome = [random.randint(-5.0, 11.0) for i in range(self.ch_size)] + + # Schaffer and Schaffer2, Bentcigar, Rothellipsoid + elif problem_name in ["Schaffer", "Schaffer2", "Bentcigar", "Rothellipsoid"]: + self.chromosome = [round(rd.uniform(-100, 100), 3) for i in range(self.ch_size)] + + # Matyas, Sumofdifferentpowers + elif problem_name in ["Matyas", "Sumofdifferentpowers", "Holzman"]: + self.chromosome = [round(rd.uniform(-10.0, 10.0), 3) for i in range(self.ch_size)] + + # Powell + elif problem_name == "Powell": + self.chromosome = [round(rd.uniform(-4.0, 5.0), 3) for i in range(self.ch_size)] + + # Chichinadze + elif problem_name == "Chichinadze": + self.chromosome = [round(rd.uniform(-30, 30), 5) for i in range(self.ch_size)] + + # Levy + elif problem_name == "Levy": + self.chromosome = [round(rd.uniform(-10.0, 10.0), 2) for i in range(self.ch_size)] + + # Zettle + elif problem_name == "Zettle": + self.chromosome = [round(rd.uniform(-5.0, 5.0), 4) for i in range(self.ch_size)] + + # Dropwave, Sphere + elif problem_name in ["Dropwave", "Sphere"]: + self.chromosome = [round(rd.uniform(-5.12, 5.12), 3) for i in range(self.ch_size)] + + # # StyblinskiTang + elif problem_name == "StyblinskiTang": + self.chromosome = [round(rd.uniform(-5.0, 5.0), 6) for i in range(self.ch_size)] + + # # Threehumps + elif problem_name == "Threehumps": + self.chromosome = [round(rd.uniform(-5.0, 5.0), 3) for i in range(self.ch_size)] + + # # Zakharov + elif problem_name == "Zakharov": + self.chromosome = [round(rd.uniform(-5.0, 10.0), 3) for i in range(self.ch_size)] + + # # Schwefel + elif problem_name == "Schwefel": + self.chromosome = [round(rd.uniform(-500.0, 500.0), 3) for i in range(self.ch_size)] + + # # Pow + elif problem_name == "Pow": + self.chromosome = [round(rd.uniform(-5.0, 15.0), 2) for i in range(self.ch_size)] + + else: + raise NotImplementedError(self.gen_type + " not implemented yet.") + return self.chromosome + + def generate_candidate(self, vector: list) -> list: + """ + Generate a candidate chromosome based on the given probability vector. + + Parameters + ---------- + vector : list of float + The probability vector used to generate the candidate chromosome. + + Returns + ------- + list + The generated candidate chromosome as a list of 0s and 1s. + """ + ind = [1 if random.rand() < p else 0 for p in vector] + return ind + + def getneighbors_positions(self) -> list: + """ + Get the positions of the individual's neighbors. + + Returns + ------- + list or None + The positions of the individual's neighbors. + """ + return self.neighbors_positions + + def setneighbors_positions(self, positions: list) -> None: + """ + Set the positions of the individual's neighbors. + + Parameters + ---------- + positions : list + The positions to set for the individual's neighbors. + """ + self.neighbors_positions = positions + + def getneighbors(self) -> list: + """ + Get the list of neighbors for the individual. + + Returns + ------- + list or None + The list of neighbors for the individual. + """ + return self.neighbors + + def setneighbors(self, neighbors: list) -> None: + """ + Set the list of neighbors for the individual. + + Parameters + ---------- + neighbors : list + The list of neighbors to set for the individual. + """ + self.neighbors = list(neighbors) \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..017b3c6 --- /dev/null +++ b/src/main.py @@ -0,0 +1,231 @@ +import optimizer + +# --------------------------------Cellular Genetic Algorithm (cga)------------------------------------------ # + +result = optimizer.cga( + n_cols = 5, + n_rows = 5, + n_gen = 100, + ch_size = 5, + gen_type = "Real-valued", + p_crossover = 0.9, + p_mutation = 0.2, + problem = optimizer.Ackley(), + selection = optimizer.TournamentSelection, + recombination = optimizer.ByteOnePointCrossover, + mutation = optimizer.ByteMutationRandom) + +""" +Optimize using the Cellular Genetic Algorithm (cga). + +Parameters +---------- +n_cols : int + Number of columns in the cellular grid. +n_rows : int + Number of rows in the cellular grid. +n_gen : int + Number of generations to run the algorithm. +ch_size : int + Chromosome size. +gen_type : str + Type of the genetic representation, e.g., 'Real-valued'. +p_crossover : float + Probability of crossover. +p_mutation : float + Probability of mutation. +problem : problem class + Instance of the optimization problem, e.g., Ackley. +selection : selection function + Selection method, e.g., TournamentSelection. +recombination : recombination function + Crossover method, e.g., ByteOnePointCrossover. +mutation : mutation function + Mutation method, e.g., ByteMutationRandom. + +Returns +------- +tuple + Best solution found and its corresponding chromosome. +""" + +# --------------------------------Synchronous Cellular Genetic Algorithm (sync_cga)------------------------------------------ # + +# result = optimizer.sync_cga( +# n_cols = 5, +# n_rows = 5, +# n_gen = 100, +# ch_size = 5, +# gen_type = "Real-valued", +# p_crossover = 0.9, +# p_mutation = 0.2, +# problem = optimizer.Ackley(), +# selection = optimizer.TournamentSelection, +# recombination = optimizer.BlxalphaCrossover, +# mutation = optimizer.FloatUniformMutation) + +""" +Optimize using the Synchronous Cellular Genetic Algorithm (sync_cga). + +Parameters +---------- +n_cols : int + Number of columns in the cellular grid. +n_rows : int + Number of rows in the cellular grid. +n_gen : int + Number of generations to run the algorithm. +ch_size : int + Chromosome size. +gen_type : str + Type of the genetic representation, e.g., 'Real-valued'. +p_crossover : float + Probability of crossover. +p_mutation : float + Probability of mutation. +problem : problem class + Instance of the optimization problem, e.g., Ackley. +selection : selection function + Selection method, e.g., TournamentSelection. +recombination : recombination function + Crossover method, e.g., BlxalphaCrossover. +mutation : mutation function + Mutation method, e.g., FloatUniformMutation. + +Returns +------- +tuple + Best solution found and its corresponding chromosome. +""" + +# --------------------------------Alpha Male Cellular Genetic Algorithm (alpha_cga)------------------------------------------ # + +# result = optimizer.alpha_cga( +# n_cols = 5, +# n_rows = 5, +# n_gen = 100, +# ch_size = 10, +# gen_type = "Real-valued", +# p_crossover = 0.9, +# p_mutation = 0.2, +# problem = optimizer.Ackley(), +# selection = optimizer.TournamentSelection, +# recombination = optimizer.BlxalphaCrossover, +# mutation = optimizer.FloatUniformMutation) + +""" +Optimize using the Alpha Male Cellular Genetic Algorithm (alpha_cga). + +Parameters +---------- +n_cols : int + Number of columns in the cellular grid. +n_rows : int + Number of rows in the cellular grid. +n_gen : int + Number of generations to run the algorithm. +ch_size : int + Chromosome size. +gen_type : str + Type of the genetic representation, e.g., 'Real-valued'. +p_crossover : float + Probability of crossover. +p_mutation : float + Probability of mutation. +problem : problem class + Instance of the optimization problem, e.g., Ackley. +selection : selection function + Selection method, e.g., TournamentSelection. +recombination : recombination function + Crossover method, e.g., BlxalphaCrossover. +mutation : mutation function + Mutation method, e.g., FloatUniformMutation. + +Returns +------- +tuple + Best solution found and its corresponding chromosome. +""" +# --------------------------------Compact Cellular Genetic Algorithm (ccga)------------------------------------------ # + +# result = optimizer.ccga( +# n_cols = 5, +# n_rows = 5, +# n_gen = 100, +# ch_size = 10, +# gen_type = "Binary", +# problem = optimizer.OneMax(), +# selection = optimizer.TournamentSelection) + +""" +Optimize using the Compact Cellular Genetic Algorithm (ccga). + +Parameters +---------- +n_cols : int + Number of columns in the cellular grid. +n_rows : int + Number of rows in the cellular grid. +n_gen : int + Number of generations to run the algorithm. +ch_size : int + Chromosome size. +gen_type : str + Type of the genetic representation, e.g., 'Binary'. +problem : problem class + Instance of the optimization problem, e.g., OneMax. +selection : selection function + Selection method, e.g., TournamentSelection. + +Returns +------- +tuple + Best solution found and its corresponding chromosome. +""" + +# --------------------------------Machine-Coded Compact Cellular Genetic Algorithm (mcccga)------------------------------------------ # + +# result = optimizer.mcccga( +# n_cols = 5, +# n_rows = 5, +# n_gen = 100, +# ch_size = 5, +# gen_type = "Real-valued", +# problem = optimizer.Ackley(), +# selection = optimizer.TournamentSelection, +# min_value = -32.768, +# max_value = 32.768) + +""" +Optimize using the Machine-Coded Compact Cellular Genetic Algorithm (mcccga). + +Parameters +---------- +n_cols : int + Number of columns in the cellular grid. +n_rows : int + Number of rows in the cellular grid. +n_gen : int + Number of generations to run the algorithm. +ch_size : int + Chromosome size. +gen_type : str + Type of the genetic representation, e.g., 'Real-valued'. +problem : problem class + Instance of the optimization problem, e.g., Ackley. +selection : selection function + Selection method, e.g., TournamentSelection. +min_value : float + Minimum value of the real-valued variables. +max_value : float + Maximum value of the real-valued variables. + +Returns +------- +tuple + Best solution found and its corresponding chromosome. +""" + + + +print("Best solution:", result[1], "\nBest solution chromosome:", result[0]) diff --git a/cga/main_cga.py b/src/main_cga.py similarity index 99% rename from cga/main_cga.py rename to src/main_cga.py index f20bebb..5ac1d61 100644 --- a/cga/main_cga.py +++ b/src/main_cga.py @@ -89,7 +89,7 @@ def runSimulation() -> None: Run the genetic algorithm simulation, record results in the database, and plot the optimization progress. """ - result_tuple = optimize( + result_tuple = cga( n_cols=10, n_rows=10, n_gen=500, diff --git a/cga/neighborhoods/__init__.py b/src/mutation/__init__.py similarity index 100% rename from cga/neighborhoods/__init__.py rename to src/mutation/__init__.py diff --git a/cga/mutation/bit_flip_mutation.py b/src/mutation/bit_flip_mutation.py similarity index 95% rename from cga/mutation/bit_flip_mutation.py rename to src/mutation/bit_flip_mutation.py index ddc8410..9cd8f94 100644 --- a/cga/mutation/bit_flip_mutation.py +++ b/src/mutation/bit_flip_mutation.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem class BitFlipMutation: """ diff --git a/cga/mutation/byte_mutation.py b/src/mutation/byte_mutation.py similarity index 97% rename from cga/mutation/byte_mutation.py rename to src/mutation/byte_mutation.py index 4d97a68..d0fed96 100644 --- a/cga/mutation/byte_mutation.py +++ b/src/mutation/byte_mutation.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem import struct class ByteMutation: diff --git a/cga/mutation/byte_mutation_random.py b/src/mutation/byte_mutation_random.py similarity index 96% rename from cga/mutation/byte_mutation_random.py rename to src/mutation/byte_mutation_random.py index e06927e..dab46bf 100644 --- a/cga/mutation/byte_mutation_random.py +++ b/src/mutation/byte_mutation_random.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem import struct class ByteMutationRandom: diff --git a/cga/mutation/float_uniform_mutation.py b/src/mutation/float_uniform_mutation.py similarity index 96% rename from cga/mutation/float_uniform_mutation.py rename to src/mutation/float_uniform_mutation.py index 6ac69c0..14d9809 100644 --- a/cga/mutation/float_uniform_mutation.py +++ b/src/mutation/float_uniform_mutation.py @@ -1,6 +1,6 @@ import random -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem class FloatUniformMutation: """ diff --git a/cga/mutation/insertion_mutation.py b/src/mutation/insertion_mutation.py similarity index 96% rename from cga/mutation/insertion_mutation.py rename to src/mutation/insertion_mutation.py index b35c6f7..54130da 100644 --- a/cga/mutation/insertion_mutation.py +++ b/src/mutation/insertion_mutation.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem class InsertionMutation: """ diff --git a/cga/mutation/shuffle_mutation.py b/src/mutation/shuffle_mutation.py similarity index 97% rename from cga/mutation/shuffle_mutation.py rename to src/mutation/shuffle_mutation.py index 2a72247..8e1cc81 100644 --- a/cga/mutation/shuffle_mutation.py +++ b/src/mutation/shuffle_mutation.py @@ -1,7 +1,7 @@ import numpy as np import random as rd -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem class ShuffleMutation: """ diff --git a/cga/mutation/swap_mutation.py b/src/mutation/swap_mutation.py similarity index 96% rename from cga/mutation/swap_mutation.py rename to src/mutation/swap_mutation.py index 467f3f4..6425cbd 100644 --- a/cga/mutation/swap_mutation.py +++ b/src/mutation/swap_mutation.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem class SwapMutation: """ diff --git a/cga/mutation/two_opt_mutation.py b/src/mutation/two_opt_mutation.py similarity index 96% rename from cga/mutation/two_opt_mutation.py rename to src/mutation/two_opt_mutation.py index ed50158..1783ac5 100644 --- a/cga/mutation/two_opt_mutation.py +++ b/src/mutation/two_opt_mutation.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem class TwoOptMutation: """ diff --git a/cga/problems/__init__.py b/src/neighborhoods/__init__.py similarity index 100% rename from cga/problems/__init__.py rename to src/neighborhoods/__init__.py diff --git a/cga/neighborhoods/compact_13.py b/src/neighborhoods/compact_13.py similarity index 100% rename from cga/neighborhoods/compact_13.py rename to src/neighborhoods/compact_13.py diff --git a/cga/neighborhoods/compact_21.py b/src/neighborhoods/compact_21.py similarity index 100% rename from cga/neighborhoods/compact_21.py rename to src/neighborhoods/compact_21.py diff --git a/cga/neighborhoods/compact_25.py b/src/neighborhoods/compact_25.py similarity index 100% rename from cga/neighborhoods/compact_25.py rename to src/neighborhoods/compact_25.py diff --git a/cga/neighborhoods/compact_9.py b/src/neighborhoods/compact_9.py similarity index 100% rename from cga/neighborhoods/compact_9.py rename to src/neighborhoods/compact_9.py diff --git a/cga/neighborhoods/linear_5.py b/src/neighborhoods/linear_5.py similarity index 100% rename from cga/neighborhoods/linear_5.py rename to src/neighborhoods/linear_5.py diff --git a/cga/neighborhoods/linear_9.py b/src/neighborhoods/linear_9.py similarity index 100% rename from cga/neighborhoods/linear_9.py rename to src/neighborhoods/linear_9.py diff --git a/src/optimizer.py b/src/optimizer.py new file mode 100644 index 0000000..c8f8556 --- /dev/null +++ b/src/optimizer.py @@ -0,0 +1,883 @@ + +# --------------------------------- problems -------------------------------- # +# Binary +from problems.single_objective.discrete.binary.count_sat import CountSat +# from problems.single_objective.discrete.binary.fms import Fms +from problems.single_objective.discrete.binary.mmdp import Mmdp +from problems.single_objective.discrete.binary.one_max import OneMax +from problems.single_objective.discrete.binary.peak import Peak +from problems.single_objective.discrete.binary.ecc import Ecc +from problems.single_objective.discrete.binary.maxcut20_01 import Maxcut20_01 +from problems.single_objective.discrete.binary.maxcut20_09 import Maxcut20_09 +from problems.single_objective.discrete.binary.maxcut100 import Maxcut100 +# Permutation +from problems.single_objective.discrete.permutation.tsp import Tsp +# Real-valued +from problems.single_objective.continuous.ackley import Ackley +from problems.single_objective.continuous.bohachevsky import Bohachevsky +from problems.single_objective.continuous.fms import Fms +from problems.single_objective.continuous.griewank import Griewank +from problems.single_objective.continuous.holzman import Holzman +from problems.single_objective.continuous.rastrigin import Rastrigin +from problems.single_objective.continuous.rosenbrock import Rosenbrock +from problems.single_objective.continuous.schaffer import Schaffer +from problems.single_objective.continuous.schaffer2 import Schaffer2 +from problems.single_objective.continuous.schaffer2 import Schaffer2 +from problems.single_objective.continuous.matyas import Matyas +from problems.single_objective.continuous.bentcigar import Bentcigar +from problems.single_objective.continuous.sumofdifferentpowers import Sumofdifferentpowers +from problems.single_objective.continuous.sumofdifferentpowers import Sumofdifferentpowers +from problems.single_objective.continuous.powell import Powell +from problems.single_objective.continuous.rothellipsoid import Rothellipsoid +from problems.single_objective.continuous.levy import Levy +from problems.single_objective.continuous.zettle import Zettle +from problems.single_objective.continuous.dropwave import Dropwave +from problems.single_objective.continuous.styblinskitang import StyblinskiTang +from problems.single_objective.continuous.threehumps import Threehumps +from problems.single_objective.continuous.zakharov import Zakharov +from problems.single_objective.continuous.sphere import Sphere +from problems.single_objective.continuous.pow import Pow +# -------------------------------------------------------------------------- # + +# ------------------------------ selection --------------------------------- # +from selection.roulette_wheel_selection import RouletteWheelSelection +from selection.tournament_selection import TournamentSelection +# -------------------------------------------------------------------------- # + +# ------------------------------ recombination ----------------------------- # +from recombination.one_point_crossover import OnePointCrossover +from recombination.pmx_crossover import PMXCrossover +from recombination.two_point_crossover import TwoPointCrossover +from recombination.uniform_crossover import UniformCrossover +from recombination.byte_uniform_crossover import ByteUniformCrossover +from recombination.byte_one_point_crossover import ByteOnePointCrossover +from recombination.byte_one_point_crossover import ByteOnePointCrossover +from recombination.flat_crossover import FlatCrossover +from recombination.arithmetic_crossover import ArithmeticCrossover +from recombination.blxalpha_crossover import BlxalphaCrossover +from recombination.linear_crossover import LinearCrossover +from recombination.unfair_avarage_crossover import UnfairAvarageCrossover + +# -------------------------------------------------------------------------- # + +# -------------------------------- mutation -------------------------------- # +from mutation.bit_flip_mutation import BitFlipMutation +from mutation.byte_mutation import ByteMutation +from mutation.byte_mutation_random import ByteMutationRandom +from mutation.insertion_mutation import InsertionMutation +from mutation.shuffle_mutation import ShuffleMutation +from mutation.swap_mutation import SwapMutation +from mutation.two_opt_mutation import TwoOptMutation +from mutation.float_uniform_mutation import FloatUniformMutation +# -------------------------------------------------------------------------- # + + +import random +import numpy as np +import byte_operators +from population import * +from individual import * +from mutation.bit_flip_mutation import * +from selection.tournament_selection import * +from recombination.one_point_crossover import * +from problems.single_objective.discrete.binary.one_max import * + +from typing import Callable, List, Tuple +from collections.abc import Callable + + + +def cga( + n_cols: int, + n_rows: int, + n_gen: int, + ch_size: int, + gen_type: str, + p_crossover: float, + p_mutation: float, + problem: AbstractProblem, + selection: Callable, + recombination: Callable, + mutation: Callable +) -> List: + """ + Optimize the given problem using a genetic algorithm. + + Parameters + ---------- + n_cols : int + Number of columns in the population grid. + n_rows : int + Number of rows in the population grid. + n_gen : int + Number of generations to evolve. + ch_size : int + Size of the chromosome. + gen_type : str + Type of the genome representation (e.g., 'Binary', 'Permutation', 'Real'). + p_crossover : float + Probability of crossover (between 0 and 1). + p_mutation : float + Probability of mutation (between 0 and 1). + known_best : float + Known best solution for gap calculation. + k_tournament : int + Size of the tournament for selection. + problem : AbstractProblem + The problem instance used for fitness evaluation. + selection : Callable + Function or class used for selecting parents. + recombination : Callable + Function or class used for recombination (crossover). + mutation : Callable + Function or class used for mutation. + + Returns + ------- + List + A list containing the best solution found during the optimization process, + where the first element is the chromosome, the second is the fitness value, + and the third is the generation at which it was found. + """ + + pop_size = n_cols * n_rows + best_solutions = [] + best_objectives = [] + best_ever_solution = [] + avg_objectives = [] + method_name = "cga" + + # Generate Initial Population + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem).initial_population() + + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + 0, + ] + + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + # Evolutionary Algorithm Loop + generation = 1 + while generation != n_gen + 1: + for c in range(pop_size): + offsprings = [] + parents = selection(pop_list, c).get_parents() + rnd = np.random.rand() + + if rnd < p_crossover: + offsprings = recombination( + parents, problem).get_recombinations() + else: + offsprings = parents + + for p in range(len(offsprings)): + + mutation_cand = offsprings[p] + + # for byte_mutation_dynamic and byte_mutation_random_dynamic + # p_mutation = p_mutation - ((g/n_gen)*p_mutation) + + rnd = np.random.rand() + + if rnd < p_mutation: + mutated = mutation(mutation_cand, problem).mutate() + offsprings[p] = mutated + else: + pass + + # Replacement: Replace if better + if offsprings[p].fitness_value < parents[p].fitness_value: + index = pop_list.index(parents[p]) + new_p = offsprings[p] + old_p = pop_list[index] + pop_list[index] = new_p + + else: + pass + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + + if (pop_list_ordered[0].fitness_value) < best_ever_solution[1]: + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + generation, + ] + else: + pass + + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + generation += 1 + + return best_ever_solution + +def sync_cga( + n_cols: int, + n_rows: int, + n_gen: int, + ch_size: int, + gen_type: str, + p_crossover: float, + p_mutation: float, + problem: Callable[[List[float]], float], + selection: Callable, + recombination: Callable, + mutation: Callable +) -> List: + """ + Optimize the given problem using a synchronous cellular genetic algorithm (Sync-CGA). + + Parameters + ---------- + n_cols : int + Number of columns in the population grid. + n_rows : int + Number of rows in the population grid. + n_gen : int + Number of generations to evolve. + ch_size : int + Size of the chromosome. + gen_type : str + Type of the genome representation (e.g., 'Binary', 'Permutation', 'Real'). + p_crossover : float + Probability of crossover between parents. + p_mutation : float + Probability of mutation in offspring. + problem : Callable[[List[float]], float] + Function to evaluate the fitness of a solution. Takes a list of floats and returns a float. + selection : Callable + Function or class used for selecting parents. + recombination : Callable + Function or class used for recombination (crossover). + mutation : Callable + Function or class used for mutation. + + Returns + ------- + List + A list containing the best solution found during the optimization process, + where the first element is the chromosome, the second is the fitness value, + and the third is the generation at which it was found. + """ + + pop_size = n_cols * n_rows + best_solutions = [] + best_objectives = [] + best_ever_solution = [] + avg_objectives = [] + method_name = "sync_cga" + + + # Generate Initial Population + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem).initial_population() + + # Sort population by fitness value + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + # Track the best solution in the initial population + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + 0, + ] + + # Calculate the mean fitness for the initial population + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + # Evolutionary Algorithm Loop + generation = 1 + aux_poplist = [] + + while generation != n_gen + 1: + aux_poplist = pop_list.copy() # Create a copy of the population for synchronous update + + for c in range(pop_size): + offsprings = [] + # Select parents + parents = selection(pop_list, c).get_parents() + rnd = np.random.rand() + + # Apply crossover with probability p_crossover + if rnd < p_crossover: + offsprings = recombination(parents, problem).get_recombinations() + else: + offsprings = parents + + for p in range(len(offsprings)): + mutation_cand = offsprings[p] + rnd = np.random.rand() + + # Apply mutation with probability p_mutation + if rnd < p_mutation: + mutated = mutation(mutation_cand, problem).mutate() + offsprings[p] = mutated + + # Replacement: Replace if offspring is better + if offsprings[p].fitness_value < parents[p].fitness_value: + index = pop_list.index(parents[p]) + aux_poplist[index] = offsprings[p] + + # Update population synchronously + pop_list = aux_poplist + pop_list_ordered = sorted(pop_list, key=lambda x: x.fitness_value) + + # Track best solutions + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + + # Update best ever solution if the current solution is better + if pop_list_ordered[0].fitness_value < best_ever_solution[1]: + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + generation, + ] + + # Calculate the mean fitness for the current generation + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + generation += 1 + + return best_ever_solution + +def alpha_cga( + n_cols: int, + n_rows: int, + n_gen: int, + ch_size: int, + gen_type: str, + p_crossover: float, + p_mutation: float, + problem: AbstractProblem, + selection: Callable, + recombination: Callable, + mutation: Callable +) -> List: + """ + Optimize a problem using an evolutionary algorithm with an alpha-male exchange mechanism. + + Parameters + ---------- + n_cols : int + Number of columns in the grid for the population. + n_rows : int + Number of rows in the grid for the population. + n_gen : int + Number of generations to run the optimization. + ch_size : int + Size of the chromosome. + gen_type : str + Type of genome representation ("Binary", "Permutation", "Real-valued"). + p_crossover : float + Probability of crossover, should be between 0 and 1. + p_mutation : float + Probability of mutation, should be between 0 and 1. + known_best : float + Known best solution value for gap calculation. + k_tournament : int + Tournament size for selection. + problem : AbstractProblem + The problem instance used to evaluate fitness. + selection : Callable + Function used for selection in the evolutionary algorithm. + recombination : Callable + Function used for recombination (crossover) in the evolutionary algorithm. + mutation : Callable + Function used for mutation in the evolutionary algorithm. + + Returns + ------- + List + A list containing the best solution found during the optimization process, + where the first element is the chromosome, the second is the fitness value, + and the third is the generation at which it was found. + """ + + pop_size = n_cols * n_rows + best_solutions = [] + best_objectives = [] + best_ever_solution = [] + avg_objectives = [] + method_name = "alpha_cga" + + + # Generate Initial Population + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem).initial_population() + + # Sort population by fitness value + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + # Initialize tracking of best solutions + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + 0, + ] + + # Calculate mean fitness for the initial population + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + # Optimization loop + generation = 1 + + while generation != n_gen + 1: + for c in range(0, pop_size, n_cols): + # Alpha-male exchange mechanism + if generation % 10 == 0: + rnd1 = rd.randrange(1, pop_size + 1, n_cols) + rnd2 = rd.randrange(1, pop_size + 1, n_cols) + while rnd1 == rnd2: + rnd2 = np.random.randint(1, n_cols + 1) + + alpha_male1 = pop_list[rnd1] + alpha_male2 = pop_list[rnd2] + + pop_list[rnd2] = alpha_male1 + pop_list[rnd1] = alpha_male2 + + for n in range(n_cols): + offsprings = [] + parents = [] + + p1 = pop_list[c] + p2 = pop_list[c + n] + parents.append(p1) + parents.append(p2) + + rnd = np.random.rand() + + # Apply crossover with probability p_crossover + if rnd < p_crossover: + offsprings = recombination( + parents, problem).get_recombinations() + else: + offsprings = parents + + for p in range(len(offsprings)): + + mutation_cand = offsprings[p] + rnd = np.random.rand() + + # Apply mutation with probability p_mutation + if rnd < p_mutation: + mutated = mutation(mutation_cand, problem).mutate() + offsprings[p] = mutated + + # Replacement: Replace if offspring is better + if offsprings[p].fitness_value < parents[1].fitness_value: + try: + index = pop_list.index(parents[1]) + except ValueError: + continue + new_p = offsprings[p] + pop_list[index] = new_p + + # Sort population and update best solutions + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + + # Update best ever solution if current solution is better + if pop_list_ordered[0].fitness_value < best_ever_solution[1]: + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + generation, + ] + + # Update average objectives + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + generation += 1 + + return best_ever_solution + +def ccga( + n_cols: int, + n_rows: int, + n_gen: int, + ch_size: int, + gen_type: str, + problem: AbstractProblem, + selection: Callable, +) -> List: + """ + Perform optimization using a coevolutionary genetic algorithm (CCGA). + + Parameters + ---------- + n_cols : int + Number of columns in the grid for the population. + n_rows : int + Number of rows in the grid for the population. + n_gen : int + Number of generations to run the optimization. + ch_size : int + Size of the chromosome. + gen_type : str + Type of genome representation ("Binary", "Permutation", "Real-valued"). + problem : AbstractProblem + The problem instance used to evaluate fitness. + selection : Callable + Function used for selection in the evolutionary algorithm. + + Returns + ------- + List + A list containing the best solution found during the optimization process, + where the first element is the chromosome, the second is the fitness value, + and the third is the generation at which it was found. + """ + + pop_size = n_cols * n_rows + best_solutions = [] + best_objectives = [] + best_ever_solution = [] + avg_objectives = [] + vector = [0.5 for _ in range(ch_size)] + method_name = "ccga" + + + # Generate Initial Population + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem, vector).initial_population() + + # Sort population by fitness value + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + # Initialize tracking of best solutions + best_solutions.append(pop_list_ordered[0].chromosome) + best_objectives.append(pop_list_ordered[0].fitness_value) + best_ever_solution = [ + pop_list_ordered[0].chromosome, + pop_list_ordered[0].fitness_value, + 0, + ] + + # Calculate mean fitness for the initial population + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + best = pop_list_ordered[0] + + # Evolutionary Algorithm Loop + generation = 1 + while generation != n_gen + 1: + for c in range(pop_size): + # Select parents and determine the winner and loser + parents = selection(pop_list, c).get_parents() + p1 = parents[0] + p2 = parents[1] + + winner, loser = compete(p1, p2) + + if winner.fitness_value > best.fitness_value: + best = winner + + # Update the vector based on the winner and loser + update_vector(vector, winner, loser, pop_size) + + # Re-generate the population based on the updated vector + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem, vector).initial_population() + + # Update best ever solution if current solution is better + if best.fitness_value > best_ever_solution[1]: + best_ever_solution = [ + best.chromosome, + best.fitness_value, + generation, + ] + + # Update average objectives + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + + generation += 1 + + return best_ever_solution + +def mcccga( + n_cols: int, + n_rows: int, + n_gen: int, + ch_size: int, + gen_type: str, + problem: Callable[[List[float]], float], + selection: Callable, + min_value: float, + max_value: float +) -> List: + """ + Optimize the given problem using a multi-population coevolutionary genetic algorithm (MCCGA). + + Parameters + ---------- + n_cols : int + Number of columns in the population grid. + n_rows : int + Number of rows in the population grid. + n_gen : int + Number of generations to evolve. + ch_size : int + Size of the chromosome. + gen_type : str + Type of the genome representation (e.g., 'Binary', 'Permutation', 'Real'). + problem : Callable[[List[float]], float] + Function to evaluate the fitness of a solution. Takes a list of floats and returns a float. + selection : Callable + Function or class used for selecting parents. + mins : List[float] + List of minimum values for the probability vector generation. + maxs : List[float] + List of maximum values for the probability vector generation. + + Returns + ------- + List + A list containing the best solution found during the optimization process, + where the first element is the chromosome, the second is the fitness value, + and the third is the generation at which it was found. + """ + + pop_size = n_cols * n_rows + best_objectives = [] + best_ever_solution = [] + avg_objectives = [] + method_name = "mcccga" + mins = [min_value for i in range(ch_size)] # chromosome number of min values in the list + maxs = [max_value for i in range(ch_size)] # chromosome number of max values in the list + + + # Generate initial probability vector + vector = generate_probability_vector(mins, maxs, pop_size) + + # Generate Initial Population + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem, vector).initial_population() + + # Sort population by fitness value + pop_list_ordered = sorted( + pop_list, key=lambda x: x.fitness_value) + + # Track the best solution in the initial population + best_objectives.append(pop_list_ordered[0].fitness_value) + best_byte_ch = byte_operators.bits_to_floats( + pop_list_ordered[0].chromosome) + best_ever_solution = [ + best_byte_ch, + pop_list_ordered[0].fitness_value, + 1, + ] + + # Calculate the mean fitness for the initial population + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + best = pop_list_ordered[0] + + # Evolutionary Algorithm Loop + generation = 1 + while generation != n_gen + 1: + for c in range(pop_size): + + # Select parents from the population + parents = selection(pop_list, c).get_parents() + p1 = parents[0] + p2 = parents[1] + + # Compete parents and identify the winner and loser + winner, loser = compete(p1, p2) + if winner.fitness_value < best.fitness_value: + best = winner + + # Update the probability vector based on the competition result + update_vector(vector, winner, loser, pop_size) + + # Re-generate the population based on the updated vector + pop_list = Population(method_name, ch_size, n_rows, n_cols, + gen_type, problem, vector).initial_population() + + # Track the best fitness value and update the best solution if necessary + best_objectives.append(best.fitness_value) + best_byte_ch = byte_operators.bits_to_floats( + pop_list_ordered[0].chromosome) + + if best.fitness_value < best_ever_solution[1]: + best_ever_solution = [ + best_byte_ch, + best.fitness_value, + generation, + ] + + # Calculate the mean fitness for the current generation + mean = sum(map(lambda x: x.fitness_value, pop_list)) / len(pop_list) + avg_objectives.append(mean) + best_byte_ch = byte_operators.bits_to_floats(best.chromosome) + + generation += 1 + + # Evaluate the final solution sampled from the probability vector + best_byte_ch = byte_operators.bits_to_floats(sample(vector)) + best_byte_result = problem.f(best_byte_ch) + + # Update the best-ever solution if the sampled solution is better + if best_byte_result <= best_ever_solution[1]: + best_ever_solution[0] = best_byte_ch + best_ever_solution[1] = best_byte_result + + return best_ever_solution + + +def compete(p1: Individual, p2: Individual) -> Tuple[Individual, Individual]: + """ + Compete between two individuals to determine the better one. + + Parameters + ---------- + p1 : Individual + First individual. + p2 : Individual + Second individual. + + Returns + ------- + Tuple[Individual, Individual] + The better individual and the loser. + """ + if p1.fitness_value < p2.fitness_value: + return p1, p2 + else: + return p2, p1 + + +def update_vector(vector: List[float], winner: Individual, loser: Individual, pop_size: int): + """ + Update the probability vector based on the winner and loser individuals. + + Parameters + ---------- + vector : List[float] + Probability vector to be updated. + winner : Individual + The winning individual. + loser : Individual + The losing individual. + pop_size : int + Size of the population. + """ + for i in range(len(vector)): + if winner.chromosome[i] != loser.chromosome[i]: + if winner.chromosome[i] == 1: + vector[i] += round((1.0 / float(pop_size)), 3) + else: + vector[i] -= round((1.0 / float(pop_size)), 3) + + +def random_vector_between(mins: List[float], maxs: List[float]) -> List[float]: + """ + Generate a random vector of floats between the given minimum and maximum values. + + Parameters + ---------- + mins : List[float] + List of minimum values. + maxs : List[float] + List of maximum values. + + Returns + ------- + List[float] + Randomly generated vector. + """ + n = len(mins) + result = [0.0] * n + + for i in range(n): + result[i] = mins[i] + random.random() * (maxs[i] - mins[i]) + + return result + + +def generate_probability_vector(mins: List[float], maxs: List[float], ntries: int) -> List[float]: + """ + Generate a probability vector based on the given minimum and maximum values. + + Parameters + ---------- + mins : List[float] + List of minimum values. + maxs : List[float] + List of maximum values. + ntries : int + Number of trials for generating the probability vector. + + Returns + ------- + List[float] + Probability vector. + """ + nbits = len(mins) * 32 + mutrate = 1.0 / ntries + probvector = [0.0] * nbits + + for _ in range(ntries): + floats = random_vector_between(mins, maxs) + floatbits = byte_operators.floats_to_bits(floats) + for k in range(nbits): + if floatbits[k] == 1: + probvector[k] = probvector[k] + mutrate + + return probvector + + +def sample(probvector: List[float]) -> List[int]: + """ + Sample a vector based on the provided probability vector. + + Parameters + ---------- + probvector : List[float] + Probability vector for sampling. + + Returns + ------- + List[int] + Sampled binary vector. + """ + n = len(probvector) + newvector = [0] * n + + for i in range(n): + if random.random() < probvector[i]: + newvector[i] = 1 + + return newvector + diff --git a/cga/optimizer_alpha_cga.py b/src/optimizer_alpha_cga.py similarity index 95% rename from cga/optimizer_alpha_cga.py rename to src/optimizer_alpha_cga.py index e3d4d01..69ec89b 100644 --- a/cga/optimizer_alpha_cga.py +++ b/src/optimizer_alpha_cga.py @@ -1,15 +1,15 @@ import numpy as np import random as rd -from cga.population import Population -from cga.selection.tournament_selection import * -from cga.recombination.one_point_crossover import * -from cga.mutation.bit_flip_mutation import * -from cga.problems.single_objective.discrete.binary.one_max import OneMax +from population import Population +from selection.tournament_selection import * +from recombination.one_point_crossover import * +from mutation.bit_flip_mutation import * +from problems.single_objective.discrete.binary.one_max import OneMax import time from typing import Callable, Tuple, List -def optimize( +def alpha_cga( n_cols: int, n_rows: int, n_gen: int, @@ -77,9 +77,11 @@ def optimize( best_ever_solution = [] avg_objectives = [] start_time = time.time() + method_name = "alpha_cga" + # Generate Initial Population - pop_list = Population(ch_size, n_rows, n_cols, + pop_list = Population(method_name, ch_size, n_rows, n_cols, gen_type, problem).initial_population() # Sort population by fitness value diff --git a/cga/optimizer_ccga.py b/src/optimizer_ccga.py similarity index 94% rename from cga/optimizer_ccga.py rename to src/optimizer_ccga.py index 639dc8f..565d597 100644 --- a/cga/optimizer_ccga.py +++ b/src/optimizer_ccga.py @@ -1,14 +1,14 @@ -from cga.population import * -from cga.individual import * -from cga.selection.tournament_selection import * -from cga.recombination.one_point_crossover import * -from cga.mutation.bit_flip_mutation import * -from cga.problems.single_objective.discrete.binary.one_max import OneMax +from population import * +from individual import * +from selection.tournament_selection import * +from recombination.one_point_crossover import * +from mutation.bit_flip_mutation import * +from problems.single_objective.discrete.binary.one_max import OneMax import time from typing import Callable, List, Tuple -def optimize( +def ccga( n_cols: int, n_rows: int, n_gen: int, @@ -77,9 +77,10 @@ def optimize( avg_objectives = [] start_time = time.time() vector = [0.5 for p in range(ch_size)] + method_name = "ccga" # Generate Initial Population - pop_list = Population(ch_size, n_rows, n_cols, + pop_list = Population(method_name, ch_size, n_rows, n_cols, gen_type, problem, vector).initial_population() pop_list_ordered = sorted( @@ -115,7 +116,7 @@ def optimize( vector best.ch_size = len(best.chromosome) - pop_list = Population(ch_size, n_rows, n_cols, + pop_list = Population(method_name, ch_size, n_rows, n_cols, gen_type, problem, vector).initial_population() if (best.fitness_value) > best_ever_solution[1]: diff --git a/cga/optimizer_cga.py b/src/optimizer_cga.py similarity index 94% rename from cga/optimizer_cga.py rename to src/optimizer_cga.py index 9b30d47..a8d49cc 100644 --- a/cga/optimizer_cga.py +++ b/src/optimizer_cga.py @@ -1,14 +1,14 @@ import numpy as np -from cga.population import * -from cga.selection.tournament_selection import * -from cga.recombination.one_point_crossover import * -from cga.mutation.bit_flip_mutation import * -from cga.problems.single_objective.discrete.binary.one_max import OneMax +from population import * +from selection.tournament_selection import * +from recombination.one_point_crossover import * +from mutation.bit_flip_mutation import * +from problems.single_objective.discrete.binary.one_max import OneMax import time from typing import Callable, List, Tuple -def optimize( +def cga( n_cols: int, n_rows: int, n_gen: int, @@ -76,9 +76,10 @@ def optimize( best_ever_solution = [] avg_objectives = [] start_time = time.time() + method_name = "cga" # Generate Initial Population - pop_list = Population(ch_size, n_rows, n_cols, + pop_list = Population(method_name, ch_size, n_rows, n_cols, gen_type, problem).initial_population() pop_list_ordered = sorted( diff --git a/cga/optimizer_mcccga.py b/src/optimizer_mcccga.py similarity index 93% rename from cga/optimizer_mcccga.py rename to src/optimizer_mcccga.py index 69038f6..14db776 100644 --- a/cga/optimizer_mcccga.py +++ b/src/optimizer_mcccga.py @@ -1,16 +1,16 @@ from collections.abc import Callable import random import byte_operators -from cga.population import Population -from cga.individual import Individual -from cga.selection.tournament_selection import * -from cga.recombination.one_point_crossover import * -from cga.mutation.bit_flip_mutation import * +from population import Population +from individual import Individual +from selection.tournament_selection import * +from recombination.one_point_crossover import * +from mutation.bit_flip_mutation import * import time from typing import List, Tuple -def optimize( +def mcccga( n_cols: int, n_rows: int, n_gen: int, @@ -20,8 +20,8 @@ def optimize( k_tournament: int, problem: Callable[[List[float]], float], selection: Callable, - mins: List[float], - maxs: List[float] + min_value: float, + max_value: float ) -> Tuple[dict, dict, List[float], List[float], float]: """ Optimize the given problem using a genetic algorithm. @@ -71,11 +71,14 @@ def optimize( best_ever_solution = [] avg_objectives = [] start_time = time.time() + mins = [min_value for i in range(ch_size)], # chromosome number of min values in the list + maxs = [max_value for i in range(ch_size)] # chromosome number of max values in the list # vector = [0.5 for p in range(ch_size)] vector = generate_probability_vector(mins, maxs, pop_size) + method_name = "mcccga" # Generate Initial Population - pop_list = Population(ch_size, n_rows, n_cols, + pop_list = Population(method_name, ch_size, n_rows, n_cols, gen_type, problem, vector).initial_population() pop_list_ordered = sorted( diff --git a/cga/optimizer_sync_cga.py b/src/optimizer_sync_cga.py similarity index 94% rename from cga/optimizer_sync_cga.py rename to src/optimizer_sync_cga.py index fd54c90..12d9d09 100644 --- a/cga/optimizer_sync_cga.py +++ b/src/optimizer_sync_cga.py @@ -1,14 +1,14 @@ import numpy as np -from cga.population import * -from cga.selection.tournament_selection import * -from cga.recombination.one_point_crossover import * -from cga.mutation.bit_flip_mutation import * -from cga.problems.single_objective.discrete.binary.one_max import OneMax +from population import * +from selection.tournament_selection import * +from recombination.one_point_crossover import * +from mutation.bit_flip_mutation import * +from problems.single_objective.discrete.binary.one_max import OneMax import time from typing import Callable, List, Tuple -def optimize( +def sync_cga( n_cols: int, n_rows: int, n_gen: int, @@ -74,9 +74,10 @@ def optimize( best_ever_solution = [] avg_objectives = [] start_time = time.time() + method_name = "sync_cga" # Generate Initial Population - pop_list = Population(ch_size, n_rows, n_cols, + pop_list = Population(method_name, ch_size, n_rows, n_cols, gen_type, problem).initial_population() pop_list_ordered = sorted( diff --git a/cga/population.py b/src/population.py similarity index 61% rename from cga/population.py rename to src/population.py index 9b4b968..257b3e6 100644 --- a/cga/population.py +++ b/src/population.py @@ -1,15 +1,16 @@ -from cga.individual import * -from cga.grid import * -from cga.neighborhoods.linear_5 import Linear5 -from cga.neighborhoods.linear_9 import Linear9 -from cga.neighborhoods.compact_9 import Compact9 -from cga.neighborhoods.compact_13 import Compact13 -from cga.neighborhoods.compact_21 import Compact21 -from cga.neighborhoods.compact_25 import Compact25 -from cga.byte_operators import * - -from cga.problems.abstract_problem import AbstractProblem from typing import List +from individual import * +from grid import * +from neighborhoods.linear_5 import Linear5 +from neighborhoods.linear_9 import Linear9 +from neighborhoods.compact_9 import Compact9 +from neighborhoods.compact_13 import Compact13 +from neighborhoods.compact_21 import Compact21 +from neighborhoods.compact_25 import Compact25 +from byte_operators import * + +from problems.abstract_problem import AbstractProblem + class Population: @@ -31,7 +32,14 @@ class Population: vector : list A list used to generate candidates for the population (relevant for MCCCGA). """ - def __init__(self, ch_size: int = 0, n_rows: int = 0, n_cols: int = 0, gen_type: str = "", problem: AbstractProblem = None, vector: list = []): + def __init__(self, + method_name: str = "", + ch_size: int = 0, + n_rows: int = 0, + n_cols: int = 0, + gen_type: str = "", + problem: AbstractProblem = None, + vector: list = []): """ Initialize the Population with the specified parameters. @@ -50,6 +58,7 @@ def __init__(self, ch_size: int = 0, n_rows: int = 0, n_cols: int = 0, gen_type: vector : list, optional A list used to generate candidates (default is an empty list). """ + self.method_name = method_name self.ch_size = ch_size self.n_rows = n_rows self.n_cols = n_cols @@ -72,16 +81,18 @@ def initial_population(self) -> List[Individual]: grid = Grid(self.n_rows, self.n_cols).make_2d_grid() for i in range(pop_size): - ind = Individual(gen_type=self.gen_type, ch_size=self.ch_size) - - # Initialize chromosome and evaluate fitness for CGA - ind.chromosome = ind.randomize() - ind.fitness_value = self.problem.f(ind.chromosome) + ind = Individual(gen_type = self.gen_type, ch_size = self.ch_size, problem = self.problem) + + # Initialize chromosome and evaluate fitness for cga, syn_cga and alpha_cga + if self.method_name in ["cga", "sync_cga", "alpha_cga", "ccga"]: + ind.chromosome = ind.randomize() + ind.fitness_value = self.problem.f(ind.chromosome) - # Initialize chromosome and evaluate fitness for MCCGA - # ind.chromosome = ind.generate_candidate(self.vector) - # ind_byte_ch = bits_to_floats(ind.chromosome) - # ind.fitness_value = self.problem.f(ind_byte_ch) + # Initialize chromosome and evaluate fitness for cga and mcccga + elif self.method_name in ["mcccga"]: + ind.chromosome = ind.generate_candidate(self.vector) + ind_byte_ch = bits_to_floats(ind.chromosome) + ind.fitness_value = self.problem.f(ind_byte_ch) ind.position = grid[i] ind.neighbors_positions = Linear9( diff --git a/cga/problems/single_objective/__init__.py b/src/problems/__init__.py similarity index 100% rename from cga/problems/single_objective/__init__.py rename to src/problems/__init__.py diff --git a/cga/problems/abstract_problem.py b/src/problems/abstract_problem.py similarity index 100% rename from cga/problems/abstract_problem.py rename to src/problems/abstract_problem.py diff --git a/cga/problems/single_objective/continuous/__init__.py b/src/problems/single_objective/__init__.py similarity index 100% rename from cga/problems/single_objective/continuous/__init__.py rename to src/problems/single_objective/__init__.py diff --git a/cga/problems/single_objective/discrete/__init__.py b/src/problems/single_objective/continuous/__init__.py similarity index 100% rename from cga/problems/single_objective/discrete/__init__.py rename to src/problems/single_objective/continuous/__init__.py diff --git a/cga/problems/single_objective/continuous/ackley.py b/src/problems/single_objective/continuous/ackley.py similarity index 95% rename from cga/problems/single_objective/continuous/ackley.py rename to src/problems/single_objective/continuous/ackley.py index 3ce370b..5d34537 100644 --- a/cga/problems/single_objective/continuous/ackley.py +++ b/src/problems/single_objective/continuous/ackley.py @@ -1,5 +1,5 @@ from numpy import pi, e, cos, sqrt, exp -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Ackley(AbstractProblem): """ Ackley function implementation for optimization problems. diff --git a/cga/problems/single_objective/continuous/bentcigar.py b/src/problems/single_objective/continuous/bentcigar.py similarity index 95% rename from cga/problems/single_objective/continuous/bentcigar.py rename to src/problems/single_objective/continuous/bentcigar.py index 539860a..28287cc 100644 --- a/cga/problems/single_objective/continuous/bentcigar.py +++ b/src/problems/single_objective/continuous/bentcigar.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Bentcigar(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/bohachevsky.py b/src/problems/single_objective/continuous/bohachevsky.py similarity index 95% rename from cga/problems/single_objective/continuous/bohachevsky.py rename to src/problems/single_objective/continuous/bohachevsky.py index 044ad0f..0d4b432 100644 --- a/cga/problems/single_objective/continuous/bohachevsky.py +++ b/src/problems/single_objective/continuous/bohachevsky.py @@ -1,7 +1,7 @@ from numpy import cos from numpy import pi from numpy import power as pw -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Bohachevsky(AbstractProblem): """ diff --git a/cga/problems/single_objective/continuous/chichinadze.py b/src/problems/single_objective/continuous/chichinadze.py similarity index 95% rename from cga/problems/single_objective/continuous/chichinadze.py rename to src/problems/single_objective/continuous/chichinadze.py index 848bc1f..8d0c064 100644 --- a/cga/problems/single_objective/continuous/chichinadze.py +++ b/src/problems/single_objective/continuous/chichinadze.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import numpy as np class Chichinadze(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/dropwave.py b/src/problems/single_objective/continuous/dropwave.py similarity index 96% rename from cga/problems/single_objective/continuous/dropwave.py rename to src/problems/single_objective/continuous/dropwave.py index 3ed0c74..bb13d59 100644 --- a/cga/problems/single_objective/continuous/dropwave.py +++ b/src/problems/single_objective/continuous/dropwave.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import math from numpy import * diff --git a/cga/problems/single_objective/continuous/fms.py b/src/problems/single_objective/continuous/fms.py similarity index 96% rename from cga/problems/single_objective/continuous/fms.py rename to src/problems/single_objective/continuous/fms.py index 9f9154d..2a1407e 100644 --- a/cga/problems/single_objective/continuous/fms.py +++ b/src/problems/single_objective/continuous/fms.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import pi, sin, random class Fms(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/griewank.py b/src/problems/single_objective/continuous/griewank.py similarity index 94% rename from cga/problems/single_objective/continuous/griewank.py rename to src/problems/single_objective/continuous/griewank.py index f5e93d3..fc88c56 100644 --- a/cga/problems/single_objective/continuous/griewank.py +++ b/src/problems/single_objective/continuous/griewank.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import math class Griewank(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/holzman.py b/src/problems/single_objective/continuous/holzman.py similarity index 94% rename from cga/problems/single_objective/continuous/holzman.py rename to src/problems/single_objective/continuous/holzman.py index 4a12ddc..91cb408 100644 --- a/cga/problems/single_objective/continuous/holzman.py +++ b/src/problems/single_objective/continuous/holzman.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Holzman(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/levy.py b/src/problems/single_objective/continuous/levy.py similarity index 95% rename from cga/problems/single_objective/continuous/levy.py rename to src/problems/single_objective/continuous/levy.py index 960dd04..8ba3d1a 100644 --- a/cga/problems/single_objective/continuous/levy.py +++ b/src/problems/single_objective/continuous/levy.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import math from numpy import power as pw diff --git a/cga/problems/single_objective/continuous/matyas.py b/src/problems/single_objective/continuous/matyas.py similarity index 95% rename from cga/problems/single_objective/continuous/matyas.py rename to src/problems/single_objective/continuous/matyas.py index 3277dad..4e2d53b 100644 --- a/cga/problems/single_objective/continuous/matyas.py +++ b/src/problems/single_objective/continuous/matyas.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Matyas(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/pow.py b/src/problems/single_objective/continuous/pow.py similarity index 95% rename from cga/problems/single_objective/continuous/pow.py rename to src/problems/single_objective/continuous/pow.py index 0760d77..85a533c 100644 --- a/cga/problems/single_objective/continuous/pow.py +++ b/src/problems/single_objective/continuous/pow.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Pow(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/powell.py b/src/problems/single_objective/continuous/powell.py similarity index 95% rename from cga/problems/single_objective/continuous/powell.py rename to src/problems/single_objective/continuous/powell.py index c1abc05..95a6198 100644 --- a/cga/problems/single_objective/continuous/powell.py +++ b/src/problems/single_objective/continuous/powell.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import numpy as np class Powell(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/rastrigin.py b/src/problems/single_objective/continuous/rastrigin.py similarity index 94% rename from cga/problems/single_objective/continuous/rastrigin.py rename to src/problems/single_objective/continuous/rastrigin.py index fed95d1..ada8f7c 100644 --- a/cga/problems/single_objective/continuous/rastrigin.py +++ b/src/problems/single_objective/continuous/rastrigin.py @@ -1,5 +1,5 @@ from numpy import cos, pi -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Rastrigin(AbstractProblem): """ Rastrigin function implementation for optimization problems. diff --git a/cga/problems/single_objective/continuous/rosenbrock.py b/src/problems/single_objective/continuous/rosenbrock.py similarity index 94% rename from cga/problems/single_objective/continuous/rosenbrock.py rename to src/problems/single_objective/continuous/rosenbrock.py index 085cde2..9c0a96b 100644 --- a/cga/problems/single_objective/continuous/rosenbrock.py +++ b/src/problems/single_objective/continuous/rosenbrock.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Rosenbrock(AbstractProblem): """ diff --git a/cga/problems/single_objective/continuous/rothellipsoid.py b/src/problems/single_objective/continuous/rothellipsoid.py similarity index 95% rename from cga/problems/single_objective/continuous/rothellipsoid.py rename to src/problems/single_objective/continuous/rothellipsoid.py index 450875b..39f2ba8 100644 --- a/cga/problems/single_objective/continuous/rothellipsoid.py +++ b/src/problems/single_objective/continuous/rothellipsoid.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import numpy as np class Rothellipsoid(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/schaffer.py b/src/problems/single_objective/continuous/schaffer.py similarity index 96% rename from cga/problems/single_objective/continuous/schaffer.py rename to src/problems/single_objective/continuous/schaffer.py index 47d471b..d533bf9 100644 --- a/cga/problems/single_objective/continuous/schaffer.py +++ b/src/problems/single_objective/continuous/schaffer.py @@ -1,6 +1,6 @@ import numpy as np from numpy import power as pw -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Schaffer(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/schaffer2.py b/src/problems/single_objective/continuous/schaffer2.py similarity index 95% rename from cga/problems/single_objective/continuous/schaffer2.py rename to src/problems/single_objective/continuous/schaffer2.py index 0b62339..a478d23 100644 --- a/cga/problems/single_objective/continuous/schaffer2.py +++ b/src/problems/single_objective/continuous/schaffer2.py @@ -1,6 +1,6 @@ import numpy as np from numpy import power as pw -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Schaffer2(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/schwefel.py b/src/problems/single_objective/continuous/schwefel.py similarity index 95% rename from cga/problems/single_objective/continuous/schwefel.py rename to src/problems/single_objective/continuous/schwefel.py index a33f5f7..adc2368 100644 --- a/cga/problems/single_objective/continuous/schwefel.py +++ b/src/problems/single_objective/continuous/schwefel.py @@ -1,5 +1,5 @@ from numpy import sin, sqrt -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Schwefel(AbstractProblem): """ diff --git a/cga/problems/single_objective/continuous/sphere.py b/src/problems/single_objective/continuous/sphere.py similarity index 94% rename from cga/problems/single_objective/continuous/sphere.py rename to src/problems/single_objective/continuous/sphere.py index d616a47..fb20a03 100644 --- a/cga/problems/single_objective/continuous/sphere.py +++ b/src/problems/single_objective/continuous/sphere.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Sphere(AbstractProblem): """ diff --git a/cga/problems/single_objective/continuous/styblinskitang.py b/src/problems/single_objective/continuous/styblinskitang.py similarity index 95% rename from cga/problems/single_objective/continuous/styblinskitang.py rename to src/problems/single_objective/continuous/styblinskitang.py index e2a7952..dcc8fcf 100644 --- a/cga/problems/single_objective/continuous/styblinskitang.py +++ b/src/problems/single_objective/continuous/styblinskitang.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class StyblinskiTang(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/sumofdifferentpowers.py b/src/problems/single_objective/continuous/sumofdifferentpowers.py similarity index 92% rename from cga/problems/single_objective/continuous/sumofdifferentpowers.py rename to src/problems/single_objective/continuous/sumofdifferentpowers.py index 4fb87bc..4a82ebf 100644 --- a/cga/problems/single_objective/continuous/sumofdifferentpowers.py +++ b/src/problems/single_objective/continuous/sumofdifferentpowers.py @@ -1,5 +1,5 @@ import numpy as np -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Sumofdifferentpowers(AbstractProblem): """ diff --git a/cga/problems/single_objective/continuous/threehumps.py b/src/problems/single_objective/continuous/threehumps.py similarity index 95% rename from cga/problems/single_objective/continuous/threehumps.py rename to src/problems/single_objective/continuous/threehumps.py index f4542d5..5cff4d0 100644 --- a/cga/problems/single_objective/continuous/threehumps.py +++ b/src/problems/single_objective/continuous/threehumps.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Threehumps(AbstractProblem): diff --git a/cga/problems/single_objective/continuous/zakharov.py b/src/problems/single_objective/continuous/zakharov.py similarity index 95% rename from cga/problems/single_objective/continuous/zakharov.py rename to src/problems/single_objective/continuous/zakharov.py index 78daf35..a1b1871 100644 --- a/cga/problems/single_objective/continuous/zakharov.py +++ b/src/problems/single_objective/continuous/zakharov.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Zakharov(AbstractProblem): """ diff --git a/cga/problems/single_objective/continuous/zettle.py b/src/problems/single_objective/continuous/zettle.py similarity index 94% rename from cga/problems/single_objective/continuous/zettle.py rename to src/problems/single_objective/continuous/zettle.py index eeb0a41..f982621 100644 --- a/cga/problems/single_objective/continuous/zettle.py +++ b/src/problems/single_objective/continuous/zettle.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Zettle(AbstractProblem): diff --git a/cga/problems/single_objective/discrete/binary/__init__.py b/src/problems/single_objective/discrete/__init__.py similarity index 100% rename from cga/problems/single_objective/discrete/binary/__init__.py rename to src/problems/single_objective/discrete/__init__.py diff --git a/cga/problems/single_objective/discrete/permutation/__init__.py b/src/problems/single_objective/discrete/binary/__init__.py similarity index 100% rename from cga/problems/single_objective/discrete/permutation/__init__.py rename to src/problems/single_objective/discrete/binary/__init__.py diff --git a/cga/problems/single_objective/discrete/binary/count_sat.py b/src/problems/single_objective/discrete/binary/count_sat.py similarity index 96% rename from cga/problems/single_objective/discrete/binary/count_sat.py rename to src/problems/single_objective/discrete/binary/count_sat.py index 6a16f81..93502e0 100644 --- a/cga/problems/single_objective/discrete/binary/count_sat.py +++ b/src/problems/single_objective/discrete/binary/count_sat.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class CountSat(AbstractProblem): """ CountSat function implementation for optimization problems. diff --git a/cga/problems/single_objective/discrete/binary/ecc.py b/src/problems/single_objective/discrete/binary/ecc.py similarity index 97% rename from cga/problems/single_objective/discrete/binary/ecc.py rename to src/problems/single_objective/discrete/binary/ecc.py index 62fee8e..7301faa 100644 --- a/cga/problems/single_objective/discrete/binary/ecc.py +++ b/src/problems/single_objective/discrete/binary/ecc.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Ecc(AbstractProblem): """ diff --git a/cga/problems/single_objective/discrete/binary/fms.py b/src/problems/single_objective/discrete/binary/fms.py similarity index 98% rename from cga/problems/single_objective/discrete/binary/fms.py rename to src/problems/single_objective/discrete/binary/fms.py index 0b87f20..493007f 100644 --- a/cga/problems/single_objective/discrete/binary/fms.py +++ b/src/problems/single_objective/discrete/binary/fms.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import pi, sin, random class Fms(AbstractProblem): """ diff --git a/cga/problems/single_objective/discrete/binary/maxcut100.py b/src/problems/single_objective/discrete/binary/maxcut100.py similarity index 99% rename from cga/problems/single_objective/discrete/binary/maxcut100.py rename to src/problems/single_objective/discrete/binary/maxcut100.py index ecf70b0..1d7af2e 100644 --- a/cga/problems/single_objective/discrete/binary/maxcut100.py +++ b/src/problems/single_objective/discrete/binary/maxcut100.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Maxcut100(AbstractProblem): """ diff --git a/cga/problems/single_objective/discrete/binary/maxcut20_01.py b/src/problems/single_objective/discrete/binary/maxcut20_01.py similarity index 98% rename from cga/problems/single_objective/discrete/binary/maxcut20_01.py rename to src/problems/single_objective/discrete/binary/maxcut20_01.py index 6253eb1..85a68ac 100644 --- a/cga/problems/single_objective/discrete/binary/maxcut20_01.py +++ b/src/problems/single_objective/discrete/binary/maxcut20_01.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Maxcut20_01(AbstractProblem): """ Maximum Cut (MAXCUT) function implementation for optimization problems. diff --git a/cga/problems/single_objective/discrete/binary/maxcut20_09.py b/src/problems/single_objective/discrete/binary/maxcut20_09.py similarity index 98% rename from cga/problems/single_objective/discrete/binary/maxcut20_09.py rename to src/problems/single_objective/discrete/binary/maxcut20_09.py index 2d2243d..345ec7b 100644 --- a/cga/problems/single_objective/discrete/binary/maxcut20_09.py +++ b/src/problems/single_objective/discrete/binary/maxcut20_09.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Maxcut20_09(AbstractProblem): """ Maximum Cut (MAXCUT) function implementation for optimization problems. diff --git a/cga/problems/single_objective/discrete/binary/mmdp.py b/src/problems/single_objective/discrete/binary/mmdp.py similarity index 97% rename from cga/problems/single_objective/discrete/binary/mmdp.py rename to src/problems/single_objective/discrete/binary/mmdp.py index 5825d8c..85a7cb3 100644 --- a/cga/problems/single_objective/discrete/binary/mmdp.py +++ b/src/problems/single_objective/discrete/binary/mmdp.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class Mmdp(AbstractProblem): """ diff --git a/cga/problems/single_objective/discrete/binary/one_max.py b/src/problems/single_objective/discrete/binary/one_max.py similarity index 93% rename from cga/problems/single_objective/discrete/binary/one_max.py rename to src/problems/single_objective/discrete/binary/one_max.py index 0096a12..2327f90 100644 --- a/cga/problems/single_objective/discrete/binary/one_max.py +++ b/src/problems/single_objective/discrete/binary/one_max.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem class OneMax(AbstractProblem): """ diff --git a/cga/problems/single_objective/discrete/binary/peak.py b/src/problems/single_objective/discrete/binary/peak.py similarity index 96% rename from cga/problems/single_objective/discrete/binary/peak.py rename to src/problems/single_objective/discrete/binary/peak.py index 7a30921..bf088d4 100644 --- a/cga/problems/single_objective/discrete/binary/peak.py +++ b/src/problems/single_objective/discrete/binary/peak.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import random class Peak(AbstractProblem): diff --git a/cga/recombination/__init__.py b/src/problems/single_objective/discrete/permutation/__init__.py similarity index 100% rename from cga/recombination/__init__.py rename to src/problems/single_objective/discrete/permutation/__init__.py diff --git a/cga/problems/single_objective/discrete/permutation/burma14.tsp.txt b/src/problems/single_objective/discrete/permutation/burma14.tsp.txt similarity index 100% rename from cga/problems/single_objective/discrete/permutation/burma14.tsp.txt rename to src/problems/single_objective/discrete/permutation/burma14.tsp.txt diff --git a/cga/problems/single_objective/discrete/permutation/tsp.py b/src/problems/single_objective/discrete/permutation/tsp.py similarity index 98% rename from cga/problems/single_objective/discrete/permutation/tsp.py rename to src/problems/single_objective/discrete/permutation/tsp.py index a8c68f6..9d4888b 100644 --- a/cga/problems/single_objective/discrete/permutation/tsp.py +++ b/src/problems/single_objective/discrete/permutation/tsp.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem import tsplib95 from math import sqrt import pandas as pd diff --git a/cga/selection/__init__.py b/src/recombination/__init__.py similarity index 100% rename from cga/selection/__init__.py rename to src/recombination/__init__.py diff --git a/cga/recombination/arithmetic_crossover.py b/src/recombination/arithmetic_crossover.py similarity index 96% rename from cga/recombination/arithmetic_crossover.py rename to src/recombination/arithmetic_crossover.py index 818b8d0..26b3d51 100644 --- a/cga/recombination/arithmetic_crossover.py +++ b/src/recombination/arithmetic_crossover.py @@ -1,6 +1,6 @@ import random -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class ArithmeticCrossover: diff --git a/cga/recombination/blxalpha_crossover.py b/src/recombination/blxalpha_crossover.py similarity index 97% rename from cga/recombination/blxalpha_crossover.py rename to src/recombination/blxalpha_crossover.py index 6f5c389..c7ad66d 100644 --- a/cga/recombination/blxalpha_crossover.py +++ b/src/recombination/blxalpha_crossover.py @@ -1,6 +1,6 @@ import random -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class BlxalphaCrossover: diff --git a/cga/recombination/byte_one_point_crossover.py b/src/recombination/byte_one_point_crossover.py similarity index 97% rename from cga/recombination/byte_one_point_crossover.py rename to src/recombination/byte_one_point_crossover.py index c0150a4..73e08db 100644 --- a/cga/recombination/byte_one_point_crossover.py +++ b/src/recombination/byte_one_point_crossover.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem +from individual import Individual +from problems.abstract_problem import AbstractProblem from typing import List import struct diff --git a/cga/recombination/byte_uniform_crossover.py b/src/recombination/byte_uniform_crossover.py similarity index 97% rename from cga/recombination/byte_uniform_crossover.py rename to src/recombination/byte_uniform_crossover.py index 283e15f..e884855 100644 --- a/cga/recombination/byte_uniform_crossover.py +++ b/src/recombination/byte_uniform_crossover.py @@ -1,6 +1,6 @@ import numpy.random as randomgenerator -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List import struct diff --git a/cga/recombination/flat_crossover.py b/src/recombination/flat_crossover.py similarity index 96% rename from cga/recombination/flat_crossover.py rename to src/recombination/flat_crossover.py index 20e5347..14e20a7 100644 --- a/cga/recombination/flat_crossover.py +++ b/src/recombination/flat_crossover.py @@ -1,6 +1,6 @@ import random -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class FlatCrossover: diff --git a/cga/recombination/linear_crossover.py b/src/recombination/linear_crossover.py similarity index 97% rename from cga/recombination/linear_crossover.py rename to src/recombination/linear_crossover.py index a3e8755..62ecad7 100644 --- a/cga/recombination/linear_crossover.py +++ b/src/recombination/linear_crossover.py @@ -1,6 +1,6 @@ import random -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class LinearCrossover: diff --git a/cga/recombination/one_point_crossover.py b/src/recombination/one_point_crossover.py similarity index 96% rename from cga/recombination/one_point_crossover.py rename to src/recombination/one_point_crossover.py index 241be47..a3f8af3 100644 --- a/cga/recombination/one_point_crossover.py +++ b/src/recombination/one_point_crossover.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class OnePointCrossover: diff --git a/cga/recombination/pmx_crossover.py b/src/recombination/pmx_crossover.py similarity index 98% rename from cga/recombination/pmx_crossover.py rename to src/recombination/pmx_crossover.py index 2a4c867..0dd110a 100644 --- a/cga/recombination/pmx_crossover.py +++ b/src/recombination/pmx_crossover.py @@ -1,5 +1,5 @@ -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class PMXCrossover: diff --git a/cga/recombination/two_point_crossover.py b/src/recombination/two_point_crossover.py similarity index 96% rename from cga/recombination/two_point_crossover.py rename to src/recombination/two_point_crossover.py index fa9b4d3..a0bcb5a 100644 --- a/cga/recombination/two_point_crossover.py +++ b/src/recombination/two_point_crossover.py @@ -1,6 +1,6 @@ import numpy as np -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem +from individual import Individual +from problems.abstract_problem import AbstractProblem from typing import List class TwoPointCrossover: diff --git a/cga/recombination/unfair_avarage_crossover.py b/src/recombination/unfair_avarage_crossover.py similarity index 96% rename from cga/recombination/unfair_avarage_crossover.py rename to src/recombination/unfair_avarage_crossover.py index e7b89cd..19cfcea 100644 --- a/cga/recombination/unfair_avarage_crossover.py +++ b/src/recombination/unfair_avarage_crossover.py @@ -1,6 +1,6 @@ import random -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class UnfairAvarageCrossover: diff --git a/cga/recombination/uniform_crossover.py b/src/recombination/uniform_crossover.py similarity index 96% rename from cga/recombination/uniform_crossover.py rename to src/recombination/uniform_crossover.py index 819ae8d..68953f1 100644 --- a/cga/recombination/uniform_crossover.py +++ b/src/recombination/uniform_crossover.py @@ -1,6 +1,6 @@ import numpy.random as randomgenerator -from cga.individual import * -from cga.problems.abstract_problem import AbstractProblem +from individual import * +from problems.abstract_problem import AbstractProblem from typing import List class UniformCrossover: diff --git a/cga/tests/__init__.py b/src/selection/__init__.py similarity index 100% rename from cga/tests/__init__.py rename to src/selection/__init__.py diff --git a/cga/selection/roulette_wheel_selection.py b/src/selection/roulette_wheel_selection.py similarity index 98% rename from cga/selection/roulette_wheel_selection.py rename to src/selection/roulette_wheel_selection.py index f154d74..1652463 100644 --- a/cga/selection/roulette_wheel_selection.py +++ b/src/selection/roulette_wheel_selection.py @@ -1,5 +1,5 @@ from typing import List -from cga.individual import Individual +from individual import Individual import random class RouletteWheelSelection: diff --git a/cga/selection/tournament_selection.py b/src/selection/tournament_selection.py similarity index 98% rename from cga/selection/tournament_selection.py rename to src/selection/tournament_selection.py index 1e01a27..37aa016 100644 --- a/cga/selection/tournament_selection.py +++ b/src/selection/tournament_selection.py @@ -1,5 +1,5 @@ from typing import List -from cga.individual import Individual +from individual import Individual import numpy as np class TournamentSelection: diff --git a/src/tests/__init__.py b/src/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cga/tests/conftest.py b/src/tests/conftest.py similarity index 82% rename from cga/tests/conftest.py rename to src/tests/conftest.py index 1fab1b1..acade58 100644 --- a/cga/tests/conftest.py +++ b/src/tests/conftest.py @@ -4,7 +4,7 @@ # Calculate the project root directory current_dir = os.path.dirname(os.path.abspath(__file__)) -project_root = os.path.abspath(os.path.join(current_dir, '../..')) # Adjusted path +project_root = os.path.abspath(os.path.join(current_dir, '../')) # Adjusted path # Print the calculated project root directory for debugging print("Calculated project root:", project_root) diff --git a/cga/tests/noteontests.txt b/src/tests/noteontests.txt similarity index 100% rename from cga/tests/noteontests.txt rename to src/tests/noteontests.txt diff --git a/cga/tests/test_ackley.py b/src/tests/test_ackley.py similarity index 95% rename from cga/tests/test_ackley.py rename to src/tests/test_ackley.py index 71bd6db..241615f 100644 --- a/cga/tests/test_ackley.py +++ b/src/tests/test_ackley.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.continuous.ackley import Ackley +from problems.single_objective.continuous.ackley import Ackley import numpy as np def test_ackley(): diff --git a/cga/tests/test_arithmetic_crossover.py b/src/tests/test_arithmetic_crossover.py similarity index 93% rename from cga/tests/test_arithmetic_crossover.py rename to src/tests/test_arithmetic_crossover.py index 8b60f09..21b2c85 100644 --- a/cga/tests/test_arithmetic_crossover.py +++ b/src/tests/test_arithmetic_crossover.py @@ -1,8 +1,8 @@ import pytest import random -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.arithmetic_crossover import ArithmeticCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.arithmetic_crossover import ArithmeticCrossover # Replace with the actual path if different class MockProblem(AbstractProblem): """ diff --git a/cga/tests/test_bentcigar_function.py b/src/tests/test_bentcigar_function.py similarity index 92% rename from cga/tests/test_bentcigar_function.py rename to src/tests/test_bentcigar_function.py index ec1c027..919a563 100644 --- a/cga/tests/test_bentcigar_function.py +++ b/src/tests/test_bentcigar_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.bentcigar import Bentcigar # Replace with the actual path if different +from problems.single_objective.continuous.bentcigar import Bentcigar # Replace with the actual path if different @pytest.fixture def setup_bentcigar(): diff --git a/cga/tests/test_bit_flip_mutation.py b/src/tests/test_bit_flip_mutation.py similarity index 92% rename from cga/tests/test_bit_flip_mutation.py rename to src/tests/test_bit_flip_mutation.py index f30a90f..b6b97a7 100644 --- a/cga/tests/test_bit_flip_mutation.py +++ b/src/tests/test_bit_flip_mutation.py @@ -1,8 +1,8 @@ import numpy as np import pytest -from cga.mutation.bit_flip_mutation import BitFlipMutation -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.individual import Individual +from mutation.bit_flip_mutation import BitFlipMutation +from problems.single_objective.discrete.binary.one_max import OneMax +from individual import Individual def test_bit_flip_mutation(): """ diff --git a/cga/tests/test_blxalpha_crossover.py b/src/tests/test_blxalpha_crossover.py similarity index 93% rename from cga/tests/test_blxalpha_crossover.py rename to src/tests/test_blxalpha_crossover.py index bc2f362..e023c09 100644 --- a/cga/tests/test_blxalpha_crossover.py +++ b/src/tests/test_blxalpha_crossover.py @@ -1,8 +1,8 @@ import pytest import random -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.blxalpha_crossover import BlxalphaCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.blxalpha_crossover import BlxalphaCrossover # Replace with the actual path if different class MockProblem(AbstractProblem): """ diff --git a/cga/tests/test_bohachevsky.py b/src/tests/test_bohachevsky.py similarity index 95% rename from cga/tests/test_bohachevsky.py rename to src/tests/test_bohachevsky.py index b7d12dd..cab62a3 100644 --- a/cga/tests/test_bohachevsky.py +++ b/src/tests/test_bohachevsky.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.continuous.bohachevsky import Bohachevsky +from problems.single_objective.continuous.bohachevsky import Bohachevsky def test_bohachevsky(): """ diff --git a/cga/tests/test_byte_mutation.py b/src/tests/test_byte_mutation.py similarity index 93% rename from cga/tests/test_byte_mutation.py rename to src/tests/test_byte_mutation.py index 15a79db..523dce3 100644 --- a/cga/tests/test_byte_mutation.py +++ b/src/tests/test_byte_mutation.py @@ -1,8 +1,8 @@ import pytest import numpy as np -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.mutation.byte_mutation import ByteMutation # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from mutation.byte_mutation import ByteMutation # Replace with the actual path if different import struct class MockProblem(AbstractProblem): diff --git a/cga/tests/test_byte_mutation_random.py b/src/tests/test_byte_mutation_random.py similarity index 92% rename from cga/tests/test_byte_mutation_random.py rename to src/tests/test_byte_mutation_random.py index eb162ab..dc2ca54 100644 --- a/cga/tests/test_byte_mutation_random.py +++ b/src/tests/test_byte_mutation_random.py @@ -1,8 +1,8 @@ import pytest import numpy as np -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.mutation.byte_mutation_random import ByteMutationRandom # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from mutation.byte_mutation_random import ByteMutationRandom # Replace with the actual path if different import struct class MockProblem(AbstractProblem): diff --git a/cga/tests/test_byte_one_point_crossover.py b/src/tests/test_byte_one_point_crossover.py similarity index 93% rename from cga/tests/test_byte_one_point_crossover.py rename to src/tests/test_byte_one_point_crossover.py index 51ac920..6221afb 100644 --- a/cga/tests/test_byte_one_point_crossover.py +++ b/src/tests/test_byte_one_point_crossover.py @@ -1,8 +1,8 @@ import pytest import numpy as np -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.byte_one_point_crossover import ByteOnePointCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.byte_one_point_crossover import ByteOnePointCrossover # Replace with the actual path if different import struct class MockProblem(AbstractProblem): diff --git a/cga/tests/test_byte_operators.py b/src/tests/test_byte_operators.py similarity index 93% rename from cga/tests/test_byte_operators.py rename to src/tests/test_byte_operators.py index 919d4d5..8c15d80 100644 --- a/cga/tests/test_byte_operators.py +++ b/src/tests/test_byte_operators.py @@ -1,5 +1,5 @@ import pytest -from cga.byte_operators import float_to_bits, bits_to_float, floats_to_bits, bits_to_floats +from byte_operators import float_to_bits, bits_to_float, floats_to_bits, bits_to_floats def test_float_to_bits(): """ diff --git a/cga/tests/test_byte_uniform_crossover.py b/src/tests/test_byte_uniform_crossover.py similarity index 93% rename from cga/tests/test_byte_uniform_crossover.py rename to src/tests/test_byte_uniform_crossover.py index a7e6074..dd9168c 100644 --- a/cga/tests/test_byte_uniform_crossover.py +++ b/src/tests/test_byte_uniform_crossover.py @@ -1,8 +1,8 @@ import pytest import numpy.random as randomgenerator -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.byte_uniform_crossover import ByteUniformCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.byte_uniform_crossover import ByteUniformCrossover # Replace with the actual path if different import struct class MockProblem(AbstractProblem): diff --git a/cga/tests/test_chichinadze_function.py b/src/tests/test_chichinadze_function.py similarity index 94% rename from cga/tests/test_chichinadze_function.py rename to src/tests/test_chichinadze_function.py index 01091e7..d3e1e48 100644 --- a/cga/tests/test_chichinadze_function.py +++ b/src/tests/test_chichinadze_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.chichinadze import Chichinadze +from problems.single_objective.continuous.chichinadze import Chichinadze @pytest.fixture def setup_chichinadze(): diff --git a/cga/tests/test_compact_13.py b/src/tests/test_compact_13.py similarity index 97% rename from cga/tests/test_compact_13.py rename to src/tests/test_compact_13.py index cd5d622..901f403 100644 --- a/cga/tests/test_compact_13.py +++ b/src/tests/test_compact_13.py @@ -1,4 +1,4 @@ -from cga.neighborhoods.compact_13 import Compact13 +from neighborhoods.compact_13 import Compact13 def test_compact_13(): """ diff --git a/cga/tests/test_compact_21.py b/src/tests/test_compact_21.py similarity index 97% rename from cga/tests/test_compact_21.py rename to src/tests/test_compact_21.py index c7e11f1..fcc1bb8 100644 --- a/cga/tests/test_compact_21.py +++ b/src/tests/test_compact_21.py @@ -1,4 +1,4 @@ -from cga.neighborhoods.compact_21 import Compact21 +from neighborhoods.compact_21 import Compact21 def test_compact_21(): """ diff --git a/cga/tests/test_compact_25.py b/src/tests/test_compact_25.py similarity index 97% rename from cga/tests/test_compact_25.py rename to src/tests/test_compact_25.py index 3cba948..49d4e83 100644 --- a/cga/tests/test_compact_25.py +++ b/src/tests/test_compact_25.py @@ -1,4 +1,4 @@ -from cga.neighborhoods.compact_25 import Compact25 +from neighborhoods.compact_25 import Compact25 def test_compact_25(): """ diff --git a/cga/tests/test_compact_9.py b/src/tests/test_compact_9.py similarity index 97% rename from cga/tests/test_compact_9.py rename to src/tests/test_compact_9.py index 4feebbe..c5d0002 100644 --- a/cga/tests/test_compact_9.py +++ b/src/tests/test_compact_9.py @@ -1,4 +1,4 @@ -from cga.neighborhoods.compact_9 import Compact9 +from neighborhoods.compact_9 import Compact9 def test_compact_9(): """ diff --git a/cga/tests/test_count_sat.py b/src/tests/test_count_sat.py similarity index 94% rename from cga/tests/test_count_sat.py rename to src/tests/test_count_sat.py index 1c71646..74a119f 100644 --- a/cga/tests/test_count_sat.py +++ b/src/tests/test_count_sat.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.discrete.binary.count_sat import CountSat +from problems.single_objective.discrete.binary.count_sat import CountSat def test_count_sat(): """ diff --git a/cga/tests/test_db_utility.py b/src/tests/test_db_utility.py similarity index 97% rename from cga/tests/test_db_utility.py rename to src/tests/test_db_utility.py index 252c97e..d0a7da4 100644 --- a/cga/tests/test_db_utility.py +++ b/src/tests/test_db_utility.py @@ -1,4 +1,4 @@ -from cga.db_utility import DBUtility +from db_utility import DBUtility import os def test_db_utility(): diff --git a/cga/tests/test_dropwave_function.py b/src/tests/test_dropwave_function.py similarity index 95% rename from cga/tests/test_dropwave_function.py rename to src/tests/test_dropwave_function.py index be34d76..32de540 100644 --- a/cga/tests/test_dropwave_function.py +++ b/src/tests/test_dropwave_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.dropwave import Dropwave +from problems.single_objective.continuous.dropwave import Dropwave @pytest.fixture def setup_dropwave(): diff --git a/cga/tests/test_ecc.py b/src/tests/test_ecc.py similarity index 95% rename from cga/tests/test_ecc.py rename to src/tests/test_ecc.py index ea692f6..c094642 100644 --- a/cga/tests/test_ecc.py +++ b/src/tests/test_ecc.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.discrete.binary.ecc import Ecc +from problems.single_objective.discrete.binary.ecc import Ecc @pytest.fixture def ecc_instance(): diff --git a/cga/tests/test_flat_crossover.py b/src/tests/test_flat_crossover.py similarity index 93% rename from cga/tests/test_flat_crossover.py rename to src/tests/test_flat_crossover.py index 5be2558..5021441 100644 --- a/cga/tests/test_flat_crossover.py +++ b/src/tests/test_flat_crossover.py @@ -1,8 +1,8 @@ import pytest import random -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.flat_crossover import FlatCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.flat_crossover import FlatCrossover # Replace with the actual path if different class MockProblem(AbstractProblem): """ diff --git a/cga/tests/test_float_uniform_mutation.py b/src/tests/test_float_uniform_mutation.py similarity index 92% rename from cga/tests/test_float_uniform_mutation.py rename to src/tests/test_float_uniform_mutation.py index 0b35ab4..97ac2f4 100644 --- a/cga/tests/test_float_uniform_mutation.py +++ b/src/tests/test_float_uniform_mutation.py @@ -1,8 +1,8 @@ import pytest import random -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.mutation.float_uniform_mutation import FloatUniformMutation # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from mutation.float_uniform_mutation import FloatUniformMutation # Replace with the actual path if different class MockProblem(AbstractProblem): """ diff --git a/cga/tests/test_fms.py b/src/tests/test_fms.py similarity index 91% rename from cga/tests/test_fms.py rename to src/tests/test_fms.py index ae016ac..dfc3676 100644 --- a/cga/tests/test_fms.py +++ b/src/tests/test_fms.py @@ -1,6 +1,6 @@ import pytest -from cga.problems.abstract_problem import AbstractProblem -from cga.problems.single_objective.continuous.fms import Fms # Replace with the actual path if different +from problems.abstract_problem import AbstractProblem +from problems.single_objective.continuous.fms import Fms # Replace with the actual path if different from numpy import random @pytest.fixture diff --git a/cga/tests/test_grid.py b/src/tests/test_grid.py similarity index 97% rename from cga/tests/test_grid.py rename to src/tests/test_grid.py index bc37851..a751bb8 100644 --- a/cga/tests/test_grid.py +++ b/src/tests/test_grid.py @@ -1,4 +1,4 @@ -from cga.grid import Grid +from grid import Grid def test_grid(): """ diff --git a/cga/tests/test_griewank_function.py b/src/tests/test_griewank_function.py similarity index 95% rename from cga/tests/test_griewank_function.py rename to src/tests/test_griewank_function.py index a0f52f9..cb3ab21 100644 --- a/cga/tests/test_griewank_function.py +++ b/src/tests/test_griewank_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.griewank import Griewank +from problems.single_objective.continuous.griewank import Griewank @pytest.fixture def setup_griewank(): diff --git a/cga/tests/test_holzman_function.py b/src/tests/test_holzman_function.py similarity index 95% rename from cga/tests/test_holzman_function.py rename to src/tests/test_holzman_function.py index 6a5b0c9..315614f 100644 --- a/cga/tests/test_holzman_function.py +++ b/src/tests/test_holzman_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.holzman import Holzman +from problems.single_objective.continuous.holzman import Holzman @pytest.fixture def setup_holzman(): diff --git a/cga/tests/test_individual.py b/src/tests/test_individual.py similarity index 98% rename from cga/tests/test_individual.py rename to src/tests/test_individual.py index 3058333..0830372 100644 --- a/cga/tests/test_individual.py +++ b/src/tests/test_individual.py @@ -1,7 +1,7 @@ import pytest from numpy import random import random as rd -from cga.individual import Individual +from individual import Individual @pytest.fixture def setup_individual(): diff --git a/cga/tests/test_insertion_mutation.py b/src/tests/test_insertion_mutation.py similarity index 91% rename from cga/tests/test_insertion_mutation.py rename to src/tests/test_insertion_mutation.py index 1b37298..a38780f 100644 --- a/cga/tests/test_insertion_mutation.py +++ b/src/tests/test_insertion_mutation.py @@ -1,6 +1,6 @@ -from cga.mutation.insertion_mutation import InsertionMutation -from cga.problems.single_objective.discrete.permutation.tsp import Tsp -from cga.individual import Individual +from mutation.insertion_mutation import InsertionMutation +from problems.single_objective.discrete.permutation.tsp import Tsp +from individual import Individual import random def test_insertion_mutation(): diff --git a/cga/tests/test_levy_function.py b/src/tests/test_levy_function.py similarity index 95% rename from cga/tests/test_levy_function.py rename to src/tests/test_levy_function.py index a2aa7ac..7acaf41 100644 --- a/cga/tests/test_levy_function.py +++ b/src/tests/test_levy_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.levy import Levy +from problems.single_objective.continuous.levy import Levy @pytest.fixture def setup_levy(): diff --git a/cga/tests/test_linear_5.py b/src/tests/test_linear_5.py similarity index 97% rename from cga/tests/test_linear_5.py rename to src/tests/test_linear_5.py index 5679c15..8dfea57 100644 --- a/cga/tests/test_linear_5.py +++ b/src/tests/test_linear_5.py @@ -1,4 +1,4 @@ -from cga.neighborhoods.linear_5 import Linear5 +from neighborhoods.linear_5 import Linear5 def test_linear_5(): """ diff --git a/cga/tests/test_linear_9.py b/src/tests/test_linear_9.py similarity index 97% rename from cga/tests/test_linear_9.py rename to src/tests/test_linear_9.py index 8e4e258..8b6c7a0 100644 --- a/cga/tests/test_linear_9.py +++ b/src/tests/test_linear_9.py @@ -1,4 +1,4 @@ -from cga.neighborhoods.linear_9 import Linear9 +from neighborhoods.linear_9 import Linear9 def test_linear_9(): """ diff --git a/cga/tests/test_linear_crossover.py b/src/tests/test_linear_crossover.py similarity index 94% rename from cga/tests/test_linear_crossover.py rename to src/tests/test_linear_crossover.py index 76b84a0..216048a 100644 --- a/cga/tests/test_linear_crossover.py +++ b/src/tests/test_linear_crossover.py @@ -1,8 +1,8 @@ import pytest import random -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.linear_crossover import LinearCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.linear_crossover import LinearCrossover # Replace with the actual path if different class MockProblem(AbstractProblem): """ diff --git a/cga/tests/test_matyas_function.py b/src/tests/test_matyas_function.py similarity index 94% rename from cga/tests/test_matyas_function.py rename to src/tests/test_matyas_function.py index 9b2ff47..5f1d70e 100644 --- a/cga/tests/test_matyas_function.py +++ b/src/tests/test_matyas_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.matyas import Matyas +from problems.single_objective.continuous.matyas import Matyas @pytest.fixture def setup_matyas(): diff --git a/cga/tests/test_maxcut100.py b/src/tests/test_maxcut100.py similarity index 92% rename from cga/tests/test_maxcut100.py rename to src/tests/test_maxcut100.py index 21a87f3..72386be 100644 --- a/cga/tests/test_maxcut100.py +++ b/src/tests/test_maxcut100.py @@ -1,6 +1,6 @@ import pytest -from cga.problems.single_objective.discrete.binary.maxcut100 import Maxcut100 +from problems.single_objective.discrete.binary.maxcut100 import Maxcut100 @pytest.fixture diff --git a/cga/tests/test_maxcut20_01.py b/src/tests/test_maxcut20_01.py similarity index 95% rename from cga/tests/test_maxcut20_01.py rename to src/tests/test_maxcut20_01.py index 2195b74..63e76b4 100644 --- a/cga/tests/test_maxcut20_01.py +++ b/src/tests/test_maxcut20_01.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.discrete.binary.maxcut20_01 import Maxcut20_01 +from problems.single_objective.discrete.binary.maxcut20_01 import Maxcut20_01 @pytest.fixture def maxcut_instance(): diff --git a/cga/tests/test_maxcut20_09.py b/src/tests/test_maxcut20_09.py similarity index 95% rename from cga/tests/test_maxcut20_09.py rename to src/tests/test_maxcut20_09.py index 1cc69d2..cc1cb88 100644 --- a/cga/tests/test_maxcut20_09.py +++ b/src/tests/test_maxcut20_09.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.discrete.binary.maxcut20_09 import Maxcut20_09 +from problems.single_objective.discrete.binary.maxcut20_09 import Maxcut20_09 @pytest.fixture def maxcut_instance(): diff --git a/cga/tests/test_mmdp.py b/src/tests/test_mmdp.py similarity index 93% rename from cga/tests/test_mmdp.py rename to src/tests/test_mmdp.py index 97d7283..66694b7 100644 --- a/cga/tests/test_mmdp.py +++ b/src/tests/test_mmdp.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.discrete.binary.mmdp import Mmdp # Replace with the actual path if different +from problems.single_objective.discrete.binary.mmdp import Mmdp # Replace with the actual path if different @pytest.fixture def mmdp_instance(): diff --git a/cga/tests/test_one_max.py b/src/tests/test_one_max.py similarity index 92% rename from cga/tests/test_one_max.py rename to src/tests/test_one_max.py index 69ca713..47b823b 100644 --- a/cga/tests/test_one_max.py +++ b/src/tests/test_one_max.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.discrete.binary.one_max import OneMax +from problems.single_objective.discrete.binary.one_max import OneMax def test_one_max(): """ diff --git a/cga/tests/test_one_point_crossover.py b/src/tests/test_one_point_crossover.py similarity index 91% rename from cga/tests/test_one_point_crossover.py rename to src/tests/test_one_point_crossover.py index 7d78134..7629571 100644 --- a/cga/tests/test_one_point_crossover.py +++ b/src/tests/test_one_point_crossover.py @@ -1,6 +1,6 @@ -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.recombination.one_point_crossover import OnePointCrossover -from cga.individual import Individual +from problems.single_objective.discrete.binary.one_max import OneMax +from recombination.one_point_crossover import OnePointCrossover +from individual import Individual def test_one_point_crossover(): """ diff --git a/cga/tests/test_optimizer.py b/src/tests/test_optimizer.py similarity index 62% rename from cga/tests/test_optimizer.py rename to src/tests/test_optimizer.py index 6be0ada..1a85649 100644 --- a/cga/tests/test_optimizer.py +++ b/src/tests/test_optimizer.py @@ -1,13 +1,13 @@ -from cga.optimizer_cga import * -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.selection.tournament_selection import TournamentSelection -from cga.recombination.one_point_crossover import OnePointCrossover -from cga.mutation.bit_flip_mutation import BitFlipMutation +from optimizer_cga import * +from problems.single_objective.discrete.binary.one_max import OneMax +from selection.tournament_selection import TournamentSelection +from recombination.one_point_crossover import OnePointCrossover +from mutation.bit_flip_mutation import BitFlipMutation def test_optimizer_cga(): - res = optimize( + res = cga( n_cols=10, n_rows=10, n_gen=2, diff --git a/cga/tests/test_optimizer_alpha_cga.py b/src/tests/test_optimizer_alpha_cga.py similarity index 83% rename from cga/tests/test_optimizer_alpha_cga.py rename to src/tests/test_optimizer_alpha_cga.py index 9eae370..335e1eb 100644 --- a/cga/tests/test_optimizer_alpha_cga.py +++ b/src/tests/test_optimizer_alpha_cga.py @@ -1,13 +1,13 @@ import pytest -from cga.population import Population -from cga.selection.tournament_selection import TournamentSelection -from cga.recombination.one_point_crossover import OnePointCrossover -from cga.mutation.bit_flip_mutation import BitFlipMutation -from cga.problems.single_objective.continuous.ackley import Ackley -from cga.problems.abstract_problem import AbstractProblem +from population import Population +from selection.tournament_selection import TournamentSelection +from recombination.one_point_crossover import OnePointCrossover +from mutation.bit_flip_mutation import BitFlipMutation +from problems.single_objective.continuous.ackley import Ackley +from problems.abstract_problem import AbstractProblem from typing import Callable, Tuple, List import time -from cga.optimizer_alpha_cga import * +from optimizer_alpha_cga import * def test_optimizer_alpha_cga(): """ @@ -30,7 +30,7 @@ def test_optimizer_alpha_cga(): recombination = OnePointCrossover mutation = BitFlipMutation - optimizer_result, parameters, best_objectives, avg_objectives, elapsed_time = optimize( + optimizer_result, parameters, best_objectives, avg_objectives, elapsed_time = alpha_cga( n_cols, n_rows, n_gen, diff --git a/cga/tests/test_optimizer_ccga.py b/src/tests/test_optimizer_ccga.py similarity index 88% rename from cga/tests/test_optimizer_ccga.py rename to src/tests/test_optimizer_ccga.py index fae4777..0508358 100644 --- a/cga/tests/test_optimizer_ccga.py +++ b/src/tests/test_optimizer_ccga.py @@ -1,13 +1,13 @@ import pytest -from cga.individual import Individual -from cga.population import Population -from cga.selection.tournament_selection import TournamentSelection -from cga.recombination.one_point_crossover import OnePointCrossover -from cga.mutation.bit_flip_mutation import BitFlipMutation -from cga.problems.single_objective.continuous.ackley import Ackley -from cga.problems.abstract_problem import AbstractProblem +from individual import Individual +from population import Population +from selection.tournament_selection import TournamentSelection +from recombination.one_point_crossover import OnePointCrossover +from mutation.bit_flip_mutation import BitFlipMutation +from problems.single_objective.continuous.ackley import Ackley +from problems.abstract_problem import AbstractProblem from typing import Callable, List, Tuple -from cga.optimizer_ccga import * +from optimizer_ccga import * def test_optimizer_ccga(): @@ -31,7 +31,7 @@ def test_optimizer_ccga(): recombination = OnePointCrossover mutation = BitFlipMutation - optimizer_result, parameters, best_objectives, avg_objectives, elapsed_time = optimize( + optimizer_result, parameters, best_objectives, avg_objectives, elapsed_time = ccga( n_cols, n_rows, n_gen, diff --git a/cga/tests/test_optimizer_cga.py b/src/tests/test_optimizer_cga.py similarity index 85% rename from cga/tests/test_optimizer_cga.py rename to src/tests/test_optimizer_cga.py index a9b7b56..96e7f5a 100644 --- a/cga/tests/test_optimizer_cga.py +++ b/src/tests/test_optimizer_cga.py @@ -1,12 +1,12 @@ import pytest import numpy as np -from cga.population import Population -from cga.selection.tournament_selection import TournamentSelection -from cga.recombination.one_point_crossover import OnePointCrossover -from cga.mutation.bit_flip_mutation import BitFlipMutation -from cga.problems.single_objective.continuous.ackley import Ackley +from population import Population +from selection.tournament_selection import TournamentSelection +from recombination.one_point_crossover import OnePointCrossover +from mutation.bit_flip_mutation import BitFlipMutation +from problems.single_objective.continuous.ackley import Ackley -from cga.optimizer_cga import * +from optimizer_cga import * @pytest.fixture def optimization_parameters(): @@ -27,7 +27,7 @@ def optimization_parameters(): } def test_optimizer_cga(optimization_parameters): - result, params, best_objectives, avg_objectives, elapsed_time = optimize( + result, params, best_objectives, avg_objectives, elapsed_time = cga( optimization_parameters['n_cols'], optimization_parameters['n_rows'], optimization_parameters['n_gen'], diff --git a/cga/tests/test_optimizer_sync_cga.py b/src/tests/test_optimizer_sync_cga.py similarity index 85% rename from cga/tests/test_optimizer_sync_cga.py rename to src/tests/test_optimizer_sync_cga.py index ba640b5..584e5db 100644 --- a/cga/tests/test_optimizer_sync_cga.py +++ b/src/tests/test_optimizer_sync_cga.py @@ -1,12 +1,12 @@ import pytest import numpy as np -from cga.population import Population -from cga.selection.tournament_selection import TournamentSelection -from cga.recombination.one_point_crossover import OnePointCrossover -from cga.mutation.bit_flip_mutation import BitFlipMutation -from cga.problems.single_objective.continuous.ackley import Ackley +from population import Population +from selection.tournament_selection import TournamentSelection +from recombination.one_point_crossover import OnePointCrossover +from mutation.bit_flip_mutation import BitFlipMutation +from problems.single_objective.continuous.ackley import Ackley -from cga.optimizer_sync_cga import * +from optimizer_sync_cga import * @pytest.fixture def optimization_parameters(): @@ -27,7 +27,7 @@ def optimization_parameters(): } def test_optimizer_cga(optimization_parameters): - result, params, best_objectives, avg_objectives, elapsed_time = optimize( + result, params, best_objectives, avg_objectives, elapsed_time = sync_cga( optimization_parameters['n_cols'], optimization_parameters['n_rows'], optimization_parameters['n_gen'], diff --git a/cga/tests/test_peak.py b/src/tests/test_peak.py similarity index 93% rename from cga/tests/test_peak.py rename to src/tests/test_peak.py index 82b492c..09dd6c0 100644 --- a/cga/tests/test_peak.py +++ b/src/tests/test_peak.py @@ -1,6 +1,6 @@ import pytest from numpy import random -from cga.problems.single_objective.discrete.binary.peak import Peak # Replace with the actual path if different +from problems.single_objective.discrete.binary.peak import Peak @pytest.fixture def peak_instance(): diff --git a/cga/tests/test_pmx_crossover.py b/src/tests/test_pmx_crossover.py similarity index 90% rename from cga/tests/test_pmx_crossover.py rename to src/tests/test_pmx_crossover.py index 3d2d83a..38da84c 100644 --- a/cga/tests/test_pmx_crossover.py +++ b/src/tests/test_pmx_crossover.py @@ -1,6 +1,6 @@ -from cga.problems.single_objective.discrete.permutation.tsp import Tsp -from cga.recombination.pmx_crossover import PMXCrossover -from cga.individual import Individual +from problems.single_objective.discrete.permutation.tsp import Tsp +from recombination.pmx_crossover import PMXCrossover +from individual import Individual import random def test_pmx_crossover(): diff --git a/cga/tests/test_population.py b/src/tests/test_population.py similarity index 88% rename from cga/tests/test_population.py rename to src/tests/test_population.py index 044db55..52020d7 100644 --- a/cga/tests/test_population.py +++ b/src/tests/test_population.py @@ -1,10 +1,10 @@ import pytest -from cga.individual import Individual -from cga.population import Population -from cga.grid import Grid -from cga.neighborhoods.linear_9 import Linear9 -from cga.byte_operators import * -from cga.problems.abstract_problem import AbstractProblem +from individual import Individual +from population import Population +from grid import Grid +from neighborhoods.linear_9 import Linear9 +from byte_operators import * +from problems.abstract_problem import AbstractProblem from typing import List class MockProblem(AbstractProblem): diff --git a/cga/tests/test_pow_function.py b/src/tests/test_pow_function.py similarity index 95% rename from cga/tests/test_pow_function.py rename to src/tests/test_pow_function.py index 2a8408b..cf49f19 100644 --- a/cga/tests/test_pow_function.py +++ b/src/tests/test_pow_function.py @@ -1,4 +1,4 @@ -from cga.problems.abstract_problem import AbstractProblem +from problems.abstract_problem import AbstractProblem from numpy import power as pw class Pow(AbstractProblem): diff --git a/cga/tests/test_powell_function.py b/src/tests/test_powell_function.py similarity index 95% rename from cga/tests/test_powell_function.py rename to src/tests/test_powell_function.py index 38b4009..52df8af 100644 --- a/cga/tests/test_powell_function.py +++ b/src/tests/test_powell_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.powell import Powell +from problems.single_objective.continuous.powell import Powell @pytest.fixture def setup_powell(): diff --git a/cga/tests/test_rastrigin.py b/src/tests/test_rastrigin.py similarity index 93% rename from cga/tests/test_rastrigin.py rename to src/tests/test_rastrigin.py index 3e88465..e992ac1 100644 --- a/cga/tests/test_rastrigin.py +++ b/src/tests/test_rastrigin.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.continuous.rastrigin import Rastrigin +from problems.single_objective.continuous.rastrigin import Rastrigin def test_rastrigin(): """ diff --git a/cga/tests/test_rosenbrock.py b/src/tests/test_rosenbrock.py similarity index 93% rename from cga/tests/test_rosenbrock.py rename to src/tests/test_rosenbrock.py index 389099d..f66940a 100644 --- a/cga/tests/test_rosenbrock.py +++ b/src/tests/test_rosenbrock.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.continuous.rosenbrock import Rosenbrock +from problems.single_objective.continuous.rosenbrock import Rosenbrock def test_rosenbrock(): """ diff --git a/cga/tests/test_rothellipsoid_function.py b/src/tests/test_rothellipsoid_function.py similarity index 94% rename from cga/tests/test_rothellipsoid_function.py rename to src/tests/test_rothellipsoid_function.py index 43bb764..0d7e33f 100644 --- a/cga/tests/test_rothellipsoid_function.py +++ b/src/tests/test_rothellipsoid_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.rothellipsoid import Rothellipsoid +from problems.single_objective.continuous.rothellipsoid import Rothellipsoid @pytest.fixture def setup_rothellipsoid(): diff --git a/cga/tests/test_roulette_wheel_selection.py b/src/tests/test_roulette_wheel_selection.py similarity index 91% rename from cga/tests/test_roulette_wheel_selection.py rename to src/tests/test_roulette_wheel_selection.py index adc534e..1ad43ac 100644 --- a/cga/tests/test_roulette_wheel_selection.py +++ b/src/tests/test_roulette_wheel_selection.py @@ -1,6 +1,6 @@ -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.selection.roulette_wheel_selection import RouletteWheelSelection -from cga.population import Population +from problems.single_objective.discrete.binary.one_max import OneMax +from selection.roulette_wheel_selection import RouletteWheelSelection +from population import Population def test_roulette_wheel_selection(): """ diff --git a/cga/tests/test_schaffer2_function.py b/src/tests/test_schaffer2_function.py similarity index 94% rename from cga/tests/test_schaffer2_function.py rename to src/tests/test_schaffer2_function.py index c4bb42e..902d185 100644 --- a/cga/tests/test_schaffer2_function.py +++ b/src/tests/test_schaffer2_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.schaffer2 import Schaffer2 +from problems.single_objective.continuous.schaffer2 import Schaffer2 @pytest.fixture def setup_schaffer2(): diff --git a/cga/tests/test_schaffer_function.py b/src/tests/test_schaffer_function.py similarity index 94% rename from cga/tests/test_schaffer_function.py rename to src/tests/test_schaffer_function.py index 16bb533..483e3e0 100644 --- a/cga/tests/test_schaffer_function.py +++ b/src/tests/test_schaffer_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.schaffer import Schaffer +from problems.single_objective.continuous.schaffer import Schaffer @pytest.fixture def setup_schaffer(): diff --git a/cga/tests/test_schwefel.py b/src/tests/test_schwefel.py similarity index 94% rename from cga/tests/test_schwefel.py rename to src/tests/test_schwefel.py index 7b4ed2b..56173da 100644 --- a/cga/tests/test_schwefel.py +++ b/src/tests/test_schwefel.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.continuous.schwefel import Schwefel +from problems.single_objective.continuous.schwefel import Schwefel def test_schwefel(): """ diff --git a/cga/tests/test_shuffle_mutation.py b/src/tests/test_shuffle_mutation.py similarity index 91% rename from cga/tests/test_shuffle_mutation.py rename to src/tests/test_shuffle_mutation.py index 950d1fc..c886ada 100644 --- a/cga/tests/test_shuffle_mutation.py +++ b/src/tests/test_shuffle_mutation.py @@ -1,6 +1,6 @@ -from cga.mutation.shuffle_mutation import ShuffleMutation -from cga.problems.single_objective.discrete.permutation.tsp import Tsp -from cga.individual import Individual +from mutation.shuffle_mutation import ShuffleMutation +from problems.single_objective.discrete.permutation.tsp import Tsp +from individual import Individual import random def test_shuffle_mutation(): diff --git a/cga/tests/test_sphere.py b/src/tests/test_sphere.py similarity index 94% rename from cga/tests/test_sphere.py rename to src/tests/test_sphere.py index 48f0d0d..c210a32 100644 --- a/cga/tests/test_sphere.py +++ b/src/tests/test_sphere.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.continuous.sphere import Sphere +from problems.single_objective.continuous.sphere import Sphere def test_sphere(): """ diff --git a/cga/tests/test_styblinskitang_function.py b/src/tests/test_styblinskitang_function.py similarity index 94% rename from cga/tests/test_styblinskitang_function.py rename to src/tests/test_styblinskitang_function.py index 3e78b04..dfb4c8e 100644 --- a/cga/tests/test_styblinskitang_function.py +++ b/src/tests/test_styblinskitang_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.styblinskitang import StyblinskiTang +from problems.single_objective.continuous.styblinskitang import StyblinskiTang @pytest.fixture def setup_styblinski_tang(): diff --git a/cga/tests/test_sumofdifferentpowers_function.py b/src/tests/test_sumofdifferentpowers_function.py similarity index 93% rename from cga/tests/test_sumofdifferentpowers_function.py rename to src/tests/test_sumofdifferentpowers_function.py index f0cc8c8..17be239 100644 --- a/cga/tests/test_sumofdifferentpowers_function.py +++ b/src/tests/test_sumofdifferentpowers_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.sumofdifferentpowers import Sumofdifferentpowers +from problems.single_objective.continuous.sumofdifferentpowers import Sumofdifferentpowers @pytest.fixture def setup_sumofdifferentpowers(): diff --git a/cga/tests/test_swap_mutation.py b/src/tests/test_swap_mutation.py similarity index 91% rename from cga/tests/test_swap_mutation.py rename to src/tests/test_swap_mutation.py index 36bd4be..635c3dc 100644 --- a/cga/tests/test_swap_mutation.py +++ b/src/tests/test_swap_mutation.py @@ -1,6 +1,6 @@ -from cga.mutation.swap_mutation import SwapMutation -from cga.problems.single_objective.discrete.permutation.tsp import Tsp -from cga.individual import Individual +from mutation.swap_mutation import SwapMutation +from problems.single_objective.discrete.permutation.tsp import Tsp +from individual import Individual import random def test_swap_mutation(): diff --git a/cga/tests/test_threehumps_function.py b/src/tests/test_threehumps_function.py similarity index 94% rename from cga/tests/test_threehumps_function.py rename to src/tests/test_threehumps_function.py index d4e1d58..4e05d41 100644 --- a/cga/tests/test_threehumps_function.py +++ b/src/tests/test_threehumps_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.threehumps import Threehumps +from problems.single_objective.continuous.threehumps import Threehumps @pytest.fixture def setup_threehumps(): diff --git a/cga/tests/test_tournament_selection.py b/src/tests/test_tournament_selection.py similarity index 91% rename from cga/tests/test_tournament_selection.py rename to src/tests/test_tournament_selection.py index 4091417..50d6112 100644 --- a/cga/tests/test_tournament_selection.py +++ b/src/tests/test_tournament_selection.py @@ -1,6 +1,6 @@ -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.selection.tournament_selection import TournamentSelection -from cga.population import Population +from problems.single_objective.discrete.binary.one_max import OneMax +from selection.tournament_selection import TournamentSelection +from population import Population def test_tournament_selection(): """ diff --git a/cga/tests/test_tsp.py b/src/tests/test_tsp.py similarity index 94% rename from cga/tests/test_tsp.py rename to src/tests/test_tsp.py index 7f249ae..4b6a937 100644 --- a/cga/tests/test_tsp.py +++ b/src/tests/test_tsp.py @@ -1,4 +1,4 @@ -from cga.problems.single_objective.discrete.permutation.tsp import Tsp +from problems.single_objective.discrete.permutation.tsp import Tsp import random def test_tsp(): diff --git a/cga/tests/test_two_opt_mutation.py b/src/tests/test_two_opt_mutation.py similarity index 92% rename from cga/tests/test_two_opt_mutation.py rename to src/tests/test_two_opt_mutation.py index 7c3f863..676d397 100644 --- a/cga/tests/test_two_opt_mutation.py +++ b/src/tests/test_two_opt_mutation.py @@ -1,6 +1,6 @@ -from cga.mutation.two_opt_mutation import TwoOptMutation -from cga.problems.single_objective.discrete.permutation.tsp import Tsp -from cga.individual import Individual +from mutation.two_opt_mutation import TwoOptMutation +from problems.single_objective.discrete.permutation.tsp import Tsp +from individual import Individual import random def test_two_opt_mutation(): diff --git a/cga/tests/test_two_point_crossover.py b/src/tests/test_two_point_crossover.py similarity index 91% rename from cga/tests/test_two_point_crossover.py rename to src/tests/test_two_point_crossover.py index 2e8e8d6..43edf99 100644 --- a/cga/tests/test_two_point_crossover.py +++ b/src/tests/test_two_point_crossover.py @@ -1,7 +1,7 @@ import numpy as np -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.recombination.two_point_crossover import TwoPointCrossover -from cga.individual import Individual +from problems.single_objective.discrete.binary.one_max import OneMax +from recombination.two_point_crossover import TwoPointCrossover +from individual import Individual def test_two_point_crossover(): """ diff --git a/cga/tests/test_unfair_average_crossover.py b/src/tests/test_unfair_average_crossover.py similarity index 93% rename from cga/tests/test_unfair_average_crossover.py rename to src/tests/test_unfair_average_crossover.py index fff5a2e..1fe692e 100644 --- a/cga/tests/test_unfair_average_crossover.py +++ b/src/tests/test_unfair_average_crossover.py @@ -1,8 +1,8 @@ import pytest import random -from cga.individual import Individual -from cga.problems.abstract_problem import AbstractProblem -from cga.recombination.unfair_avarage_crossover import UnfairAvarageCrossover # Replace with the actual path if different +from individual import Individual +from problems.abstract_problem import AbstractProblem +from recombination.unfair_avarage_crossover import UnfairAvarageCrossover # Replace with the actual path if different class MockProblem(AbstractProblem): """ diff --git a/cga/tests/test_uniform_crossover.py b/src/tests/test_uniform_crossover.py similarity index 91% rename from cga/tests/test_uniform_crossover.py rename to src/tests/test_uniform_crossover.py index d8a675b..8c6e410 100644 --- a/cga/tests/test_uniform_crossover.py +++ b/src/tests/test_uniform_crossover.py @@ -1,6 +1,6 @@ -from cga.problems.single_objective.discrete.binary.one_max import OneMax -from cga.recombination.uniform_crossover import UniformCrossover -from cga.individual import Individual +from problems.single_objective.discrete.binary.one_max import OneMax +from recombination.uniform_crossover import UniformCrossover +from individual import Individual def test_uniform_crossover(): """ diff --git a/cga/tests/test_zakharov_function.py b/src/tests/test_zakharov_function.py similarity index 93% rename from cga/tests/test_zakharov_function.py rename to src/tests/test_zakharov_function.py index 0e66543..e87e454 100644 --- a/cga/tests/test_zakharov_function.py +++ b/src/tests/test_zakharov_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.zakharov import Zakharov +from problems.single_objective.continuous.zakharov import Zakharov def test_zakharov_function(): diff --git a/cga/tests/test_zettle_function.py b/src/tests/test_zettle_function.py similarity index 95% rename from cga/tests/test_zettle_function.py rename to src/tests/test_zettle_function.py index 78c770e..fe3100b 100644 --- a/cga/tests/test_zettle_function.py +++ b/src/tests/test_zettle_function.py @@ -1,5 +1,5 @@ import pytest -from cga.problems.single_objective.continuous.zettle import Zettle +from problems.single_objective.continuous.zettle import Zettle @pytest.fixture def setup_zettle():