From cc1ffddbcc258069dfafa714f60b6b2cd5df1326 Mon Sep 17 00:00:00 2001 From: Geoffrey Pruvost Date: Fri, 4 Dec 2020 14:44:21 +0100 Subject: [PATCH] V0.5.7 : refactoring params genetic_operator --- .../core/genetic_operator/abstract_operator.py | 3 +-- .../genetic_operator/combinatorial/cross_mut.py | 13 ++++++++++++- .../genetic_operator/combinatorial/crossover.py | 7 +++++++ .../core/genetic_operator/combinatorial/mutation.py | 9 ++++++++- moead_framework/problem/combinatorial/knapsack.py | 2 +- setup.py | 2 +- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/moead_framework/core/genetic_operator/abstract_operator.py b/moead_framework/core/genetic_operator/abstract_operator.py index 565839e..0914074 100644 --- a/moead_framework/core/genetic_operator/abstract_operator.py +++ b/moead_framework/core/genetic_operator/abstract_operator.py @@ -3,9 +3,8 @@ class GeneticOperator(ABC): - def __init__(self, solutions, crossover_points=1): + def __init__(self, solutions, **kwargs): self.solutions = solutions - self.crossover_points = crossover_points @abstractmethod def run(self): diff --git a/moead_framework/core/genetic_operator/combinatorial/cross_mut.py b/moead_framework/core/genetic_operator/combinatorial/cross_mut.py index df7b454..28acfbe 100644 --- a/moead_framework/core/genetic_operator/combinatorial/cross_mut.py +++ b/moead_framework/core/genetic_operator/combinatorial/cross_mut.py @@ -4,11 +4,22 @@ class CrossoverAndMutation(GeneticOperator): + def __init__(self, solutions, **kwargs): + super().__init__(solutions, **kwargs) + if kwargs.get("crossover_points") is None: + self.crossover_points = 1 + else: + self.crossover_points = int(kwargs.get("crossover_points")) + + if kwargs.get("mutation_probability") is None: + self.mutation_probability = 1 + else: + self.mutation_probability = int(kwargs.get("mutation_probability")) def run(self): self.number_of_solution_is_correct(n=2) child = Crossover(solutions=self.solutions, crossover_points=self.crossover_points).run() - child = BinaryMutation(solutions=[child]).run() + child = BinaryMutation(solutions=[child], mutation_probability=self.mutation_probability).run() return child diff --git a/moead_framework/core/genetic_operator/combinatorial/crossover.py b/moead_framework/core/genetic_operator/combinatorial/crossover.py index fcb7e3d..d859538 100644 --- a/moead_framework/core/genetic_operator/combinatorial/crossover.py +++ b/moead_framework/core/genetic_operator/combinatorial/crossover.py @@ -5,6 +5,13 @@ class Crossover(GeneticOperator): + def __init__(self, solutions, **kwargs): + super().__init__(solutions, **kwargs) + if kwargs.get("crossover_points") is None: + self.crossover_points = 1 + else: + self.crossover_points = int(kwargs.get("crossover_points")) + def run(self): self.number_of_solution_is_correct(n=2) solution1 = self.solutions[0] diff --git a/moead_framework/core/genetic_operator/combinatorial/mutation.py b/moead_framework/core/genetic_operator/combinatorial/mutation.py index ddfb43b..ebda78a 100644 --- a/moead_framework/core/genetic_operator/combinatorial/mutation.py +++ b/moead_framework/core/genetic_operator/combinatorial/mutation.py @@ -4,6 +4,13 @@ class BinaryMutation(GeneticOperator): + def __init__(self, solutions, **kwargs): + super().__init__(solutions, **kwargs) + if kwargs.get("mutation_probability") is None: + self.mutation_probability = 1 + else: + self.mutation_probability = int(kwargs.get("mutation_probability")) + def run(self): self.number_of_solution_is_correct(n=1) @@ -14,7 +21,7 @@ def run(self): for i in range(len(solution)): probability = random.randint(0, n) - if probability < (1 / n): + if probability < (self.mutation_probability / n): solution[i] = abs(solution[i]-1) return solution diff --git a/moead_framework/problem/combinatorial/knapsack.py b/moead_framework/problem/combinatorial/knapsack.py index 76e1ee8..f64090a 100644 --- a/moead_framework/problem/combinatorial/knapsack.py +++ b/moead_framework/problem/combinatorial/knapsack.py @@ -23,7 +23,7 @@ def __init__(self, objective_number, instance_file): indexes_to_split.append(index_to_split_one * (i+1) + 1) kps = np.split(np.array(file_content), indexes_to_split) - print(len(kps)) + for kp in kps: # print(kp) if kp[0] == "=": diff --git a/setup.py b/setup.py index a3325d0..f67c62b 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="moead-framework", - version="0.5.6", + version="0.5.7", author="Geoffrey Pruvost", author_email="geoffrey@pruvost.xyz", description="MOEA/D Framework in Python 3",