|
1 | | -from __future__ import print_function |
2 | 1 | from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
3 | 4 |
|
4 | 5 | import re |
5 | 6 | import random |
6 | 7 | import json |
7 | 8 | import copy |
8 | 9 |
|
9 | 10 |
|
10 | | -__author__ = [ 'Tomas Mendez Echenagucia <[email protected]>'] |
11 | | -__copyright__ = 'Copyright 2017, Block Research Group - ETH Zurich' |
12 | | -__license__ = 'MIT License' |
13 | | - |
| 11 | +__author__ = [ 'Tomas Mendez Echenagucia <[email protected]>'] |
| 12 | +__copyright__ = 'Copyright 2017, Block Research Group - ETH Zurich' |
| 13 | +__license__ = 'MIT License' |
| 14 | + |
14 | 15 |
|
15 | 16 |
|
16 | 17 | __all__ = [ |
@@ -60,7 +61,6 @@ def ga(fit_function, |
60 | 61 | output_path=None, |
61 | 62 | input_path=None): |
62 | 63 |
|
63 | | - ga_ = GA() |
64 | 64 | """Genetic Algorithm optimisation (Holland 1975). |
65 | 65 |
|
66 | 66 | Parameters |
@@ -181,6 +181,7 @@ def foo(X): |
181 | 181 | ================================================================================ |
182 | 182 |
|
183 | 183 | """ |
| 184 | + ga_ = GA() |
184 | 185 |
|
185 | 186 | ga_.fit_name = fit_name or fit_function.__name__ |
186 | 187 | ga_.fit_type = fit_type |
@@ -448,7 +449,6 @@ def scale_population(self, decoded_pop): |
448 | 449 | scaled_pop: list |
449 | 450 | The scaled ppopulation list. |
450 | 451 | """ |
451 | | - |
452 | 452 | scaled_pop = [[[]] * self.num_var for i in range(self.num_pop)] |
453 | 453 | for j in range(self.num_pop): |
454 | 454 | for i in range(self.num_var): |
@@ -496,7 +496,6 @@ def select_elite_pop(self, pop, num_elite=None): |
496 | 496 | elite_pop: dict |
497 | 497 | The elite population dictionary. |
498 | 498 | """ |
499 | | - |
500 | 499 | if self.fit_type == 'min': |
501 | 500 | sorted_ind = self.get_sorting_indices(self.current_pop['fit_value']) |
502 | 501 | elif self.fit_type == 'max': |
@@ -565,7 +564,6 @@ def simple_crossover(self): |
565 | 564 | combined with individuals in ``GA.mating_pool_b`` using a single, randomly selected |
566 | 565 | crossover point. |
567 | 566 | """ |
568 | | - |
569 | 567 | self.current_pop = {'binary': [], 'decoded': [], 'scaled': [], 'fit_value': []} |
570 | 568 | self.current_pop['binary'] = [[[]] * self.num_var for i in range(self.num_pop)] |
571 | 569 | for j in range((self.num_pop - self.num_elite) / 2): |
@@ -603,7 +601,7 @@ def code_decoded(self, decoded_pop): |
603 | 601 | Parameters |
604 | 602 | ---------- |
605 | 603 | decoded_pop: dict |
606 | | - The decoded population dictionary to be coded |
| 604 | + The decoded population dictionary to be coded |
607 | 605 |
|
608 | 606 | Returns |
609 | 607 | ------- |
@@ -844,6 +842,10 @@ def get_best_individual_index(self): |
844 | 842 | self.best_individual_index = indices[0] |
845 | 843 |
|
846 | 844 |
|
| 845 | +# ============================================================================== |
| 846 | +# Main |
| 847 | +# ============================================================================== |
| 848 | + |
847 | 849 | if __name__ == '__main__': |
848 | 850 |
|
849 | 851 | import os |
|
0 commit comments