Skip to content

Commit

Permalink
Update MPIUtil to singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
PytLab committed Nov 3, 2018
1 parent 6b3e043 commit 0cd2b7f
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 102 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Version: 0.5.5
### Date: 2018-10-25
1. Make MPIUtil a singleton and remove in-module instantiation.
2. Standardize docstring for Sphinx documentation

## Version: 0.5.4
### Date: 2018-01-30
1. Fixed important bugs in individual clone.
Expand Down
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 153 files
10 changes: 10 additions & 0 deletions gaft/analysis/console_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@


class ConsoleOutput(OnTheFlyAnalysis):
''' Built-in on-the-fly analysis plugin class for outputing log on console.
Attribute:
interval(:obj:`int`): The analysis interval in evolution iteration, default
value is 1 meaning analyze every step.
master_only(:obj:`bool`): Flag for if the analysis plugin is only effective
in master process. Default is True.
'''

# Analysis interval.
interval = 1
Expand Down
8 changes: 8 additions & 0 deletions gaft/analysis/fitness_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from ..plugin_interfaces.analysis import OnTheFlyAnalysis

class FitnessStore(OnTheFlyAnalysis):
''' Built-in on-the-fly analysis plugin class for storing fitness related data during iteration.
Attribute:
interval(:obj:`int`): The analysis interval in evolution iteration, default
value is 1 meaning analyze every step.
master_only(:obj:`bool`): Flag for if the analysis plugin is only effective
in master process. Default is True.
'''

# Analysis interval.
interval = 1
Expand Down
2 changes: 2 additions & 0 deletions gaft/operators/crossover/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
''' Package for built-in crossover operators '''

from .uniform_crossover import UniformCrossover

25 changes: 15 additions & 10 deletions gaft/operators/crossover/uniform_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@


class UniformCrossover(Crossover):
def __init__(self, pc, pe=0.5):
'''
Crossover operator with uniform crossover algorithm,
see https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)
''' Crossover operator with uniform crossover algorithm,
see https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)
:param pc: The probability of crossover (usaully between 0.25 ~ 1.0)
:type pc: float in (0.0, 1.0]
:param pc: The probability of crossover (usaully between 0.25 ~ 1.0)
:type pc: float in (0.0, 1.0]
:param pe: Gene exchange probability.
'''
:param pe: Gene exchange probability.
:type pe: float in range (0.0, 1.0]
'''
def __init__(self, pc, pe=0.5):
if pc <= 0.0 or pc > 1.0:
raise ValueError('Invalid crossover probability')
self.pc = pc
Expand All @@ -29,8 +29,13 @@ def __init__(self, pc, pe=0.5):
self.pe = pe

def cross(self, father, mother):
'''
Cross chromsomes of parent using uniform crossover method.
''' Cross chromsomes of parent using uniform crossover method.
:param population: Population where the selection operation occurs.
:type population: :obj:`gaft.components.Population`
:return: Selected parents (a father and a mother)
:rtype: list of :obj:`gaft.components.IndividualBase`
'''
do_cross = True if random() <= self.pc else False

Expand Down
2 changes: 2 additions & 0 deletions gaft/operators/mutation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

''' Package for built-in mutation operators '''

from .flip_bit_mutation import FlipBitMutation, FlipBitBigMutation

58 changes: 36 additions & 22 deletions gaft/operators/mutation/flip_bit_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@


class FlipBitMutation(Mutation):
def __init__(self, pm):
'''
Mutation operator with Flip Bit mutation implementation.
''' Mutation operator with Flip Bit mutation implementation.
:param pm: The probability of mutation (usually between 0.001 ~ 0.1)
:type pm: float in (0.0, 1.0]
'''
:param pm: The probability of mutation (usually between 0.001 ~ 0.1)
:type pm: float in range (0.0, 1.0]
'''
def __init__(self, pm):
if pm <= 0.0 or pm > 1.0:
raise ValueError('Invalid mutation probability')

self.pm = pm

def mutate(self, individual, engine):
'''
Mutate the individual.
''' Mutate the individual.
:param individual: The individual on which crossover operation occurs
:type individual: :obj:`gaft.components.IndividualBase`
:param engine: Current genetic algorithm engine
:type engine: :obj:`gaft.engine.GAEngine`
:return: A mutated individual
:rtype: :obj:`gaft.components.IndividualBase`
'''
do_mutation = True if random() <= self.pm else False

Expand Down Expand Up @@ -56,21 +63,20 @@ def mutate(self, individual, engine):


class FlipBitBigMutation(FlipBitMutation):
def __init__(self, pm, pbm, alpha):
'''
Mutation operator using Flip Bit mutation implementation with adaptive
big mutation rate to overcome premature or local-best solution.
''' Mutation operator using Flip Bit mutation implementation with adaptive
big mutation rate to overcome premature or local-best solution.
:param pm: The probability of mutation (usually between 0.001 ~ 0.1)
:type pm: float in (0.0, 1.0]
:param pm: The probability of mutation (usually between 0.001 ~ 0.1)
:type pm: float in (0.0, 1.0]
:param pbm: The probability of big mutation, usually more than 5 times
bigger than pm.
:type pbm: float
:param pbm: The probability of big mutation, usually more than 5 times
bigger than pm.
:type pbm: float
:param alpha: intensive factor
:type alpha: float, in range (0.5, 1)
'''
:param alpha: intensive factor
:type alpha: float, in range (0.5, 1)
'''
def __init__(self, pm, pbm, alpha):
super(self.__class__, self).__init__(pm)

if not (0.0 < pbm < 1.0):
Expand All @@ -85,8 +91,16 @@ def __init__(self, pm, pbm, alpha):
self.alpha = alpha

def mutate(self, individual, engine):
'''
Mutate the individual with adaptive big mutation rate.
''' Mutate the individual with adaptive big mutation rate.
:param individual: The individual on which crossover operation occurs
:type individual: :obj:`gaft.components.IndividualBase`
:param engine: Current genetic algorithm engine
:type engine: :obj:`gaft.engine.GAEngine`
:return: A mutated individual
:rtype: :obj:`gaft.components.IndividualBase`
'''
pm = self.pm

Expand Down
5 changes: 5 additions & 0 deletions gaft/operators/selection/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
''' Package for built-in selection operators '''

from .exponential_ranking_selection import ExponentialRankingSelection
from .linear_ranking_selection import LinearRankingSelection
from .roulette_wheel_selection import RouletteWheelSelection
from .tournament_selection import TournamentSelection

20 changes: 12 additions & 8 deletions gaft/operators/selection/exponential_ranking_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@


class ExponentialRankingSelection(Selection):
def __init__(self, base=0.5):
'''
Selection operator using Exponential Ranking selection method.
''' Selection operator using Exponential Ranking selection method.
:param base: The base of exponent
:type base: float in range (0.0, 1.0)
'''
:param base: The base of exponent
:type base: float in range (0.0, 1.0)
'''
def __init__(self, base=0.5):
if not (0.0 < base < 1.0):
raise ValueError('The base of exponent c must in range (0.0, 1.0)')

self.base = base

def select(self, population, fitness):
'''
Select a pair of parent individuals using exponential ranking method.
''' Select a pair of parent individuals using exponential ranking method.
:param population: Population where the selection operation occurs.
:type population: :obj:`gaft.components.Population`
:return: Selected parents (a father and a mother)
:rtype: list of :obj:`gaft.components.IndividualBase`
'''
# Individual number.
NP = len(population)
Expand Down
22 changes: 13 additions & 9 deletions gaft/operators/selection/linear_ranking_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@


class LinearRankingSelection(Selection):
def __init__(self, pmin=0.1, pmax=0.9):
'''
Selection operator using Linear Ranking selection method.
''' Selection operator using Linear Ranking selection method.
Reference: Baker J E. Adaptive selection methods for genetic
algorithms[C]//Proceedings of an International Conference on Genetic
Algorithms and their applications. 1985: 101-111.
'''
Reference: Baker J E. Adaptive selection methods for genetic
algorithms[C]//Proceedings of an International Conference on Genetic
Algorithms and their applications. 1985: 101-111.
'''
def __init__(self, pmin=0.1, pmax=0.9):
# Selection probabilities for the worst and best individuals.
self.pmin, self.pmax = pmin, pmax

def select(self, population, fitness):
'''
Select a pair of parent individuals using linear ranking method.
''' Select a pair of parent individuals using linear ranking method.
:param population: Population where the selection operation occurs.
:type population: :obj:`gaft.components.Population`
:return: Selected parents (a father and a mother)
:rtype: list of :obj:`gaft.components.IndividualBase`
'''
# Individual number.
NP = len(population)
Expand Down
16 changes: 10 additions & 6 deletions gaft/operators/selection/roulette_wheel_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
from ...plugin_interfaces.operators.selection import Selection

class RouletteWheelSelection(Selection):
''' Selection operator with fitness proportionate selection(FPS) or
so-called roulette-wheel selection implementation.
'''
def __init__(self):
'''
Selection operator with fitness proportionate selection(FPS) or
so-called roulette-wheel selection implementation.
'''
pass

def select(self, population, fitness):
'''
Select a pair of parent using FPS algorithm.
''' Select a pair of parent using FPS algorithm.
:param population: Population where the selection operation occurs.
:type population: :obj:`gaft.components.Population`
:return: Selected parents (a father and a mother)
:rtype: list of :obj:`gaft.components.IndividualBase`
'''
# Normalize fitness values for all individuals.
fit = population.all_fits(fitness)
Expand Down
19 changes: 13 additions & 6 deletions gaft/operators/selection/tournament_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@


class TournamentSelection(Selection):
''' Selection operator using Tournament Strategy with tournament size equals
to two by default.
:param tournament_size: Individual number in one tournament
:type tournament_size: int
'''
def __init__(self, tournament_size=2):
'''
Selection operator using Tournament Strategy with tournament size equals
to two by default.
'''
self.tournament_size = tournament_size

def select(self, population, fitness):
'''
Select a pair of parent using Tournament strategy.
''' Select a pair of parent using Tournament strategy.
:param population: Population where the selection operation occurs.
:type population: :obj:`gaft.components.Population`
:return: Selected parents (a father and a mother)
:rtype: list of :obj:`gaft.components.IndividualBase`
'''
# Competition function.
all_fits = population.all_fits(fitness)
Expand Down
20 changes: 13 additions & 7 deletions gaft/plugin_interfaces/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@


class OnTheFlyAnalysis(metaclass=AnalysisMeta):
'''
Class for providing an interface to easily extend and customize the behavior
''' Class for providing an interface to easily extend and customize the behavior
of the on-the-fly analysis functionality of gapy.
Attribute:
interval(:obj:`int`): The analysis interval in evolution iteration, default
value is 1 meaning analyze every step.
master_only(:obj:`bool`): Flag for if the analysis plugin is only effective
in master process. Default is True.
'''
# Only used in master process?
master_only = False
Expand All @@ -16,15 +23,14 @@ class OnTheFlyAnalysis(metaclass=AnalysisMeta):
interval = 1

def setup(self, ng, engine):
'''
Function called right before the start of genetic algorithm main iteration
''' Function called right before the start of genetic algorithm main iteration
to allow for custom setup of the analysis object.
:param ng: The number of generation.
:type ng: int
:param engine: The current GAEngine where the analysis is running.
:type engine: GAEngine
:type engine: gaft.engine.GAEngine
'''
raise NotImplementedError

Expand All @@ -39,7 +45,7 @@ def register_step(self, g, population, engine):
:type population: Population
:param engine: The current GAEngine where the analysis is running.
:type engine: GAEngine
:type engine: gaft.engine.GAEngine
'''
raise NotImplementedError

Expand All @@ -52,7 +58,7 @@ def finalize(self, population, engine):
:type population: Population
:param engine: The current GAEngine where the analysis is running.
:type engine: GAEngine
:type engine: gaft.engine.GAEngine
'''
raise NotImplementedError

Loading

0 comments on commit 0cd2b7f

Please sign in to comment.