Skip to content

Commit

Permalink
updated linting
Browse files Browse the repository at this point in the history
  • Loading branch information
gbomarito committed Sep 23, 2024
1 parent fa4f9e7 commit 4851b55
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 441 deletions.
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ good-names=i,
k,
ex,
Run,
_
_,
X

# Good variable names regexes, separated by a comma. If names match any regex,
# they will always be accepted
Expand Down
3 changes: 3 additions & 0 deletions bingo/local_optimizers/local_opt_fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
that will perform local optimization of a `Chromosome` as necessary
using a `LocalOptimizer` before evaluating it.
"""

from ..evaluation.fitness_function import FitnessFunction


Expand All @@ -29,7 +30,9 @@ class LocalOptFitnessFunction(FitnessFunction):
training_data : `TrainingData`
data that can be used in the wrapped fitness function
"""

def __init__(self, fitness_function, optimizer):
# pylint: disable=super-init-not-called
self._fitness_function = fitness_function
self.optimizer = optimizer

Expand Down
12 changes: 5 additions & 7 deletions bingo/local_optimizers/normalized_marginal_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
that will perform probabilistic local optimization of a `Chromosome` using
SMCPy. The normaized marginal likelihood from the SMC optimization is returned.
"""

import numpy as np
from bingo.local_optimizers.smcpy_optimizer import SmcpyOptimizer
from ..evaluation.fitness_function import FitnessFunction
Expand All @@ -12,7 +13,7 @@
class NormalizedMarginalLikelihood(FitnessFunction):
"""Normalized marginal likelihood calculation using SMCPy
A class for fitness evaluation of individuals that have local optimization
A class for fitness evaluation of individuals that have local optimization
parameters
Parameters
Expand All @@ -35,12 +36,9 @@ class NormalizedMarginalLikelihood(FitnessFunction):
"""

def __init__(
self,
fitness_function,
deterministic_optimizer,
log_scale=True,
**kwargs
self, fitness_function, deterministic_optimizer, log_scale=True, **kwargs
):
# pylint: disable=super-init-not-called
self._log_scale = log_scale
self.optimizer = SmcpyOptimizer(
fitness_function, deterministic_optimizer, **kwargs
Expand Down Expand Up @@ -70,7 +68,7 @@ def __call__(self, individual):
Parameters
----------
individual : `Chromosome`
Individual to calculate the normalized marginal likelihood of.
Individual to calculate the normalized marginal likelihood of.
Probabilistic local optimization is performed during evaluation.
Returns
Expand Down
20 changes: 20 additions & 0 deletions bingo/local_optimizers/smcpy_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"""A module for probabilistic calibration of parameters.
Probabilistic calibration of model parameters can be useful in cases where data
is sparse and/or noisy. Using a calibration of this type can allow for a better
estimate of true fitness while being a bit more robust to overfitting.
"""

import numpy as np
from scipy.stats import multivariate_normal as mvn
from scipy.stats import invgamma
Expand Down Expand Up @@ -264,6 +271,19 @@ def _get_samples_and_pdf(distributions, num_samples):
return pdf, samples

def evaluate_model(self, params, individual):
"""Evaluate an individual given a set of parameters
Parameters
----------
params : numpy array
parameters for which to evaluate the individual
individual : Equation
individual for which to evaluate fitness
Returns
-------
numpy array : fitness vector outputs for the individual w/ the params
"""
individual.set_local_optimization_params(params.T)
result = self._objective_fn.evaluate_fitness_vector(individual).T
if len(result.shape) < 2:
Expand Down
29 changes: 16 additions & 13 deletions bingo/selection/generalized_crowding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
algorithm in bingo analyses. The next generation is selected by pairing parents
with their offspring and selecting the most fit of the two.
"""

from abc import abstractmethod
from .selection import Selection


class GeneralizedCrowding(Selection):
"""The class that performs generalized crowding selection on a population
"""
"""The class that performs generalized crowding selection on a population"""

def __call__(self, population, target_population_size):
"""Performs selection on a population
Expand All @@ -29,30 +30,32 @@ def __call__(self, population, target_population_size):
The newly selected generation of chromosomes
"""
if (len(population) % 2) > 0 or (target_population_size % 2) > 0:
raise ValueError('Population must be of even length')
raise ValueError("Population must be of even length")

half_pop_size = len(population) // 2
if target_population_size > half_pop_size:
raise ValueError('Target population size cannot be greater\
than the half of the population')
raise ValueError(
"Target population size cannot be greater\
than the half of the population"
)

offspring = population[half_pop_size:]
population = population[:half_pop_size]

for i in range(target_population_size // 2):
parent_1 = population[i*2]
parent_2 = population[i*2+1]
child_1 = offspring[i*2]
child_2 = offspring[i*2+1]
parent_1 = population[i * 2]
parent_2 = population[i * 2 + 1]
child_1 = offspring[i * 2]
child_2 = offspring[i * 2 + 1]

dist_a = parent_1.distance(child_1) + parent_2.distance(child_2)
dist_b = parent_1.distance(child_2) + parent_2.distance(child_1)
if dist_a <= dist_b:
population[i*2] = self._return_most_fit(child_1, parent_1)
population[i*2+1] = self._return_most_fit(child_2, parent_2)
population[i * 2] = self._return_most_fit(child_1, parent_1)
population[i * 2 + 1] = self._return_most_fit(child_2, parent_2)
else:
population[i*2] = self._return_most_fit(child_2, parent_1)
population[i*2+1] = self._return_most_fit(child_1, parent_2)
population[i * 2] = self._return_most_fit(child_2, parent_1)
population[i * 2 + 1] = self._return_most_fit(child_1, parent_2)

return population

Expand Down
4 changes: 2 additions & 2 deletions bingo/selection/probabilistic_crowding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
with their offspring and selecting the child with a probility that is related
to the fitness of the paired child and parent.
"""

import numpy as np
from .generalized_crowding import GeneralizedCrowding

Expand All @@ -21,7 +22,7 @@ class ProbabilisticCrowding(GeneralizedCrowding):
log_scale : bool
Whether fitnesses of the individuals is in log space. Default True.
negative : bool
Whether to invert the fitness of the individual (before log). Default
Whether to invert the fitness of the individual (before log). Default
True.
"""

Expand All @@ -48,4 +49,3 @@ def _return_most_fit(self, child, parent):
else:
prob = c_fit / (p_fit + c_fit)
return child if np.random.random() < prob else parent

2 changes: 1 addition & 1 deletion bingo/symbolic_regression/agraph/agraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
"""

import logging
import warnings
import numpy as np
from sympy.core import Expr
import warnings

from .string_parsing import eq_string_to_command_array_and_constants
from .string_generation import get_formatted_string
Expand Down
17 changes: 11 additions & 6 deletions bingo/symbolic_regression/agraph/crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
This module contains the implementation of single point crossover between
acyclic graph individuals.
"""

import numpy as np

from ...chromosomes.crossover import Crossover


class AGraphCrossover(Crossover):
"""Crossover between acyclic graph individuals
Attributes
----------
types : iterable of str
Expand All @@ -19,8 +20,10 @@ class AGraphCrossover(Crossover):
the crossover type (or None) that happened to create the first child
and second child, respectively
"""

def __init__(self):
self.types = ["default"]
self.last_crossover_types = (None, None)

def __call__(self, parent_1, parent_2):
"""Single point crossover.
Expand All @@ -41,11 +44,13 @@ def __call__(self, parent_1, parent_2):
child_2 = parent_2.copy()

ag_size = parent_1.command_array.shape[0]
cross_point = np.random.randint(1, ag_size-1)
child_1.mutable_command_array[cross_point:] = \
parent_2.command_array[cross_point:]
child_2.mutable_command_array[cross_point:] = \
parent_1.command_array[cross_point:]
cross_point = np.random.randint(1, ag_size - 1)
child_1.mutable_command_array[cross_point:] = parent_2.command_array[
cross_point:
]
child_2.mutable_command_array[cross_point:] = parent_1.command_array[
cross_point:
]

child_age = max(parent_1.genetic_age, parent_2.genetic_age)
child_1.genetic_age = child_age
Expand Down
Loading

0 comments on commit 4851b55

Please sign in to comment.