Skip to content

Commit

Permalink
V0.5.7 : refactoring params genetic_operator
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreyp committed Dec 4, 2020
1 parent 03e66a0 commit cc1ffdd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
3 changes: 1 addition & 2 deletions moead_framework/core/genetic_operator/abstract_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 12 additions & 1 deletion moead_framework/core/genetic_operator/combinatorial/cross_mut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion moead_framework/problem/combinatorial/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] == "=":
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit cc1ffdd

Please sign in to comment.