beagle package¶
Submodules¶
beagle.individual module¶
-
class
beagle.individual.
Individual
(vector)[source]¶ Bases:
object
Class definition for Individual objects.
An Individual describes a single potential solution within the problem vector space.
Example:
>>> vector = np.array( [ 1, 0, 1, 0 ] ) >>> Individual( vector ) Individual([1 0 1 0])
-
constrain
(target)[source]¶ This method will attempt to constrain an Individual vector to match the composition specified by target. Elements that appear with too high frequency are replaced at random with elements that appear with too low frequency.
Example
>>> ind = Individual( np.array( [ 1, 0, 2 ] ) ) >>> target = { 0: 0, 1: 1, 2: 2 } >>> ind.constrain( target ) >>> ind Individual([1 2 2])
-
fitness_score
(fitness_function, use_saved_value=True)[source]¶ Returns the fitness score of this Individual, evaluated with a particular objective function.
- Example::
>>> vector = np.array( [ 1, 0, 1, 0 ] ) >>> ind = Individual( vector ) >>> objective_function = lambda x: sum( x ) >>> ind.fitness_score( objective_function ) 2
Parameters: - fitness_function (function) – The objective function, f(x), where x is the vector for this Individual.
- (optional (use_saved_value) – bool): The first time fitness_score() is called, the score is saved. If use_saved_value is True, subsequent calls will return the saved value instead of recalculating f(x). To force recalculation of f(x), set use_saved_value=False. Default: True.
Returns: The fitness score of this Individual.
Return type: (float)
-
off_target
(target)[source]¶ Returns the difference between the counts of appearances of integers for this Individual vector and a target count. For example, an Individual with vector [1, 0, 1, 0] contains 1 twice and 0 twice. If the target composition is 1 four times, and 0 none, this method will return the difference: {1: -2, 0: 2}.
Example
>>> ind = Individual( np.array( [ 1, 0, 1, 0 ] ) ) >>> target = { 1: 4, 0: 0 } >>> output = ind.off_target( target ) >>> output[0] 2 >>> output[1] -2
-
score
¶ Returns the fitness score of this Individual, providing this has already been calculated by passing the objective function to fitness_score( f(x) ). If the score has not yet been evaluated, trying to access this attribute will raise an AtttributeError.
- Example::
>>> ind = Individual( np.array( [ 1, 0, 1, 0 ] ) ) >>> objective_function = lambda x: sum( x ) >>> ind.fitness_score( objective_function ) 2 >>> ind.score 2
Parameters: None – Returns: The fitness score of this Individual. Return type: (float) Raises: AttributeError
– If the score for this individual has not previously been evaluated.
-
-
beagle.individual.
matches
(vector, a)[source]¶ Returns indices where the elements of a vector match some value.
Parameters: - vector (ndarray(int)) – A 1D numpy array describing a vector.
- a (int) – The value to match.
Returns: A list of indices for matching elements.
Return type: list(int)
Example:
>>> vector = np.array( [ 1, 0, 1, 0 ] ) >>> matches( vector, 0 ) [1, 3]
-
beagle.individual.
mutate
(i, mutator)[source]¶ Return a new Individual, generated by mutating a starting Individual.
Parameters: - i (Individual) – The Individual to be mutated.
- mutator (func) – A function that takes a 1D numpy array as an argument, and returns a “mutated” new 1D numpy array.
Returns: (Individual)
Example:
>>> vector = np.array( [ 1, 0, 1, 0 ] ) >>> i = Individual( vector ) >>> m = lambda x: 1-x # element-wise 1 <--> 0 >>> mutate(i, m) Individual([0 1 0 1])