Added run attribute and check for the name of the first argument
This commit is contained in:
@ -1,4 +1,7 @@
|
|||||||
# Import square root function for ga.adapt()
|
# Import signature tool to check if functions start with self or ga
|
||||||
|
from inspect import signature
|
||||||
|
|
||||||
|
# Import square root function for ga.adapt() and ga.dist()
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
import random
|
import random
|
||||||
@ -43,6 +46,7 @@ class Attributes:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
|
run = 0,
|
||||||
chromosome_length = 10,
|
chromosome_length = 10,
|
||||||
population_size = 10,
|
population_size = 10,
|
||||||
chromosome_impl = None,
|
chromosome_impl = None,
|
||||||
@ -94,6 +98,9 @@ class Attributes:
|
|||||||
Graph = matplotlib_graph.Matplotlib_Graph
|
Graph = matplotlib_graph.Matplotlib_Graph
|
||||||
):
|
):
|
||||||
|
|
||||||
|
# Keep track of the current run
|
||||||
|
self.run = run
|
||||||
|
|
||||||
# Initilization variables
|
# Initilization variables
|
||||||
self.chromosome_length = chromosome_length
|
self.chromosome_length = chromosome_length
|
||||||
self.population_size = population_size
|
self.population_size = population_size
|
||||||
@ -169,7 +176,7 @@ class Attributes:
|
|||||||
|
|
||||||
which follows the following guidelines:
|
which follows the following guidelines:
|
||||||
- if self.name is a property, the specific property setter is used
|
- if self.name is a property, the specific property setter is used
|
||||||
- else if value is callable, self is passed in as the first parameter
|
- else if value is callable and the first parameter is either 'self' or 'ga', self is passed in as the first parameter
|
||||||
- else if value is not None or self.name is not set, assign it like normal
|
- else if value is not None or self.name is not set, assign it like normal
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -180,7 +187,7 @@ class Attributes:
|
|||||||
prop.fset(self, value)
|
prop.fset(self, value)
|
||||||
|
|
||||||
# Check for function
|
# Check for function
|
||||||
elif callable(value):
|
elif callable(value) and next(iter(signature(value).parameters), None) in ('self', 'ga'):
|
||||||
foo = lambda *args, **kwargs: value(self, *args, **kwargs)
|
foo = lambda *args, **kwargs: value(self, *args, **kwargs)
|
||||||
# Reassign name and doc-string for documentation
|
# Reassign name and doc-string for documentation
|
||||||
foo.__name__ = value.__name__
|
foo.__name__ = value.__name__
|
||||||
@ -329,63 +336,17 @@ class Attributes:
|
|||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def make_population(self):
|
def run(self):
|
||||||
"""Getter function for making populations."""
|
"""Getter function for the run counter."""
|
||||||
return self._make_population
|
return self._run
|
||||||
|
|
||||||
|
|
||||||
@make_population.setter
|
@run.setter
|
||||||
def make_population(self, method):
|
def run(self, value):
|
||||||
"""Setter function for making populations."""
|
"""Setter function for the run counter."""
|
||||||
self.__dict__['_make_population'] = method
|
if not(isinstance(value, int) and value >= 0):
|
||||||
|
raise ValueError("ga.run counter must be an integer greater than or equal to 0.")
|
||||||
|
self._run = value
|
||||||
@property
|
|
||||||
def make_chromosome(self):
|
|
||||||
"""Getter function for making chromosomes."""
|
|
||||||
return self._make_chromosome
|
|
||||||
|
|
||||||
|
|
||||||
@make_chromosome.setter
|
|
||||||
def make_chromosome(self, method):
|
|
||||||
"""Setter function for making chromosomes."""
|
|
||||||
self.__dict__['_make_chromosome'] = method
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def make_gene(self):
|
|
||||||
"""Getter function for making genes."""
|
|
||||||
return self._make_gene
|
|
||||||
|
|
||||||
|
|
||||||
@make_gene.setter
|
|
||||||
def make_gene(self, method):
|
|
||||||
"""Setter function for making genes."""
|
|
||||||
self.__dict__['_make_gene'] = method
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def gene_impl(self):
|
|
||||||
"""Getter function for making randomized genes."""
|
|
||||||
return self._gene_impl
|
|
||||||
|
|
||||||
|
|
||||||
@gene_impl.setter
|
|
||||||
def gene_impl(self, method):
|
|
||||||
"""Setter function for making randomized genes."""
|
|
||||||
self.__dict__['_gene_impl'] = method
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def chromosome_impl(self):
|
|
||||||
"""Getter function for making randomized chromosomes."""
|
|
||||||
return self._chromosome_impl
|
|
||||||
|
|
||||||
|
|
||||||
@chromosome_impl.setter
|
|
||||||
def chromosome_impl(self, method):
|
|
||||||
"""Setter function for making randomized chromosomes."""
|
|
||||||
self.__dict__['_chromosome_impl'] = method
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
Reference in New Issue
Block a user