Allele¶
Base Allele class¶
-
class
sgx.allele.base.
Allele
¶ Abstract class for Allele.
An allele must be Hashable (ie. non modifiable)
-
abstract
describe
() → str¶ Pretty describes the current allele.
-
property
mode
¶ Returns the most frequent allele.
-
property
possible_values
¶ Possible values of the allele. None if not reasonably applicable (eg. a float)
-
abstract
sample
(sample_type: Optional[str] = 'sample') → Hashable¶ Sample.
- Parameters
sample_type – ‘sample’ (default): random value according to the current probability distribution ‘uniform’: random value according to a uniform probability distribution (ie. completely random) ‘mode’: most common value according to the current probability distribution
-
abstract
update
(winner: Hashable, loser: Hashable) → None¶ Updates the winner and the loser Genotype, so as to modify the new Learning Rate.
- Parameters
winner – The genotype of the better solution.
loser – The genotype of the worse solution.
-
abstract
Boolean Allele class¶
-
class
sgx.allele.boolean.
Boolean
(learning_rate: float = 0.001)¶ -
describe
() → str¶ Pretty describes the current boolean allele.
-
is_valid
(value: Hashable) → bool¶ Checks if the allele is boolean.
-
run_paranoia_checks
() → bool¶ Returns True, if all tests are passed.
-
sample
(sample_type: Optional[str] = 'sample') → Hashable¶ Sample.
- Parameters
sample_type – The type of sample (sample (default), uniform, mode).
-
static
sigmoid
(x: float, k: Optional[float] = 1) → float¶ Logistic function with given logistic growth (
k
). See https://en.wikipedia.org/wiki/Logistic_function- Parameters
x – A float number.
k – Logistic Growth.
- Returns
The result probability of the sigmoid function.
-
update
(winner: Hashable, loser: Hashable) → None¶ Updates the winner and the loser Genotype, so as to modify the new Learning Rate.
- Parameters
winner – The genotype of the better solution.
loser – The genotype of the worse solution.
-
Categorical Allele class¶
-
class
sgx.allele.categorical.
Categorical
(alternatives: Sequence[Hashable], weights: Optional[Union[Sequence[float], dict]] = None, learning_rate: Optional[float] = None)¶ -
describe
() → str¶ Pretty describes the current categorical allele.
-
is_valid
(value: Hashable) → bool¶ Checks if the allele is categorical.
-
property
possible_values
¶ Possible values of the allele. None if not reasonably applicable (eg. a float)
-
run_paranoia_checks
() → bool¶ Returns True, if all tests are passed.
-
sample
(sample_type: Optional[str] = 'sample') → Hashable¶ Sample.
- Parameters
sample_type – The type of sample (sample (default), uniform, mode).
-
update
(winner: Hashable, loser: Hashable) → None¶ Updates the winner and the loser Genotype, so as to modify the new Learning Rate.
- Parameters
winner – The genotype of the better solution.
loser – The genotype of the worse solution.
-
Fitness¶
Base fitness class¶
-
class
sgx.fitness.base.
Fitness
¶ Fitness of a phenotype, handle multiple formats (eg. scalar, tuple).
The class also redefines the relational operator in order to handle different types of optimization (eg. maximization, minimization) and to provide limited support to more complex scenarios (eg. multi-objective optimization)
Equalities (‘==’ and ‘!=’) are based on
is_distinguishable
.Single angular-bracket operators (‘>’, ‘<’, ‘>=’, and ‘<=’) are based on
is_fitter
and may be randomized (the result may not be reproducible).Double angular-bracket operators (‘>>’ and ‘<<’) are based on
is_dominant
and the result is stable. By defaultis_dominant
is defined asis_fitter
.When subclassing, one should only redefine
is_fitter
, and optionallyis_distinguishable
andis_dominant
;is_dominant
must be changed ifis_fitter
is randomized (the result is uncertain).Additional sanity checks should be added to
check_comparable
. Subclasses may redefine thedecorate
method to change the values appearance.-
__eq__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness is equal (==) to the other.
-
__ge__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness is greater or equal (>=) to the other.
-
__gt__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness is greater (>) than the other.
-
__le__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness is less or equal (<=) to the other.
-
__lshift__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness does not dominates (<<) the other.
-
__lt__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness is less (<) than the other.
-
__ne__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness is not equal (!=) to the other.
-
__rshift__
(other: sgx.fitness.base.Fitness) → bool¶ Returns True if one fitness dominates (>>) the other.
-
check_comparable
(other: sgx.fitness.base.Fitness)¶ Checks if the fitness is able to be compared.
-
decorate
() → str¶ Represent the individual fitness value with a nice string.
-
is_distinguishable
(other: sgx.fitness.base.Fitness) → bool¶ Check whether some differences from the other Fitness may be perceived.
-
is_dominant
(other: sgx.fitness.base.Fitness) → bool¶ Check whether dominates the other (result is certain).
-
is_fitter
(other: sgx.fitness.base.Fitness) → bool¶ Check whether fitter than the other (result may be accidental).
-
is_valid
(fitness: sgx.fitness.base.Fitness) → bool¶ Returns True if the fitness is able to be compared, otherwise Raises an Assertion Error.
-
run_paranoia_checks
() → bool¶ Returns True if checks are successful.
-
-
sgx.fitness.base.
reversed
(fitness_class: sgx.fitness.base.Fitness) → sgx.fitness.base.Fitness¶ Reverse fitness class turning a maximization problem into a minimization one.
Fitness Function class¶
sgx.fitness.function
Multi-Objective class¶
-
class
sgx.fitness.multi_objective.
Lexicase
(value: Sequence, fitness_type: Type[sgx.fitness.base.Fitness] = <class 'sgx.fitness.simple.Scalar'>, **kwargs)¶ Pseudo-MO through Lexicase selection (DOI:10.1109/TEVC.2014.2362729).
-
is_fitter
(other: sgx.fitness.multi_objective.Lexicase) → bool¶ Check whether fitter than the other.
-
-
class
sgx.fitness.multi_objective.
MultiObjective
(value: Sequence, fitness_type: Type[sgx.fitness.base.Fitness] = <class 'sgx.fitness.simple.Scalar'>, **kwargs)¶ Abstract class for handling Multi-Objective problems.
-
is_dominant
(other: sgx.fitness.multi_objective.Lexicase) → bool¶ Check whether dominates the other (result is certain).
-
abstract
is_fitter
(other: sgx.fitness.multi_objective.Lexicase) → bool¶ Check whether fitter than the other.
-
Simple class¶
-
class
sgx.fitness.simple.
Approximate
(argument, rel_tol: float = 1e-09, abs_tol: float = 0)¶ A single, floating-point value with approximate equality – Larger is better.
-
check_comparable
(other: sgx.fitness.simple.Approximate)¶ Checks if the fitness is able to be compared.
-
decorate
() → str¶ Represent the individual fitness value with a nice string.
-
is_distinguishable
(other: sgx.fitness.base.Fitness) → bool¶ Check whether some differences from the other Fitness may be perceived.
-
is_fitter
(other: sgx.fitness.base.Fitness) → bool¶ Check whether fitter than the other.
-
-
class
sgx.fitness.simple.
Integer
¶ A single numeric value – Larger is better.
-
class
sgx.fitness.simple.
Scalar
(x=0, /)¶ A single numeric value – Larger is better.
-
class
sgx.fitness.simple.
Vector
(value: Sequence, fitness_type: Type[sgx.fitness.base.Fitness] = <class 'sgx.fitness.simple.Scalar'>, **kwargs)¶ A generic vector of Fitness values.
fitness_type is the subtype, **kwargs are passed to fitness init
Examples
f1 = sgx.fitness.Vector([23, 10], fitness_type=Approximate, abs_tol=.1) f2 = sgx.fitness.Vector([23, 10], fitness_type=Approximate, abs_tol=.001)
f1 > sgx.fitness.Vector([23, 9.99], fitness_type=Approximate, abs_tol=.1) is False f2 > sgx.fitness.Vector([23, 9.99], fitness_type=Approximate, abs_tol=.001) is True
-
check_comparable
(other: sgx.fitness.simple.Vector)¶ Checks if the fitness is able to be compared.
-
static
compare_vectors
(v1: Sequence[sgx.fitness.base.Fitness], v2: Sequence[sgx.fitness.base.Fitness]) → int¶ Compare Fitness values in v1 and v2.
- Parameters
v1 – The first fitness vector.
v2 – The second fitness vector.
- Returns
-1 if v1 < v2; +1 if v1 > v2; 0 if v1 == v2
-
decorate
() → str¶ Represent the individual fitness value with a nice string.
-
is_distinguishable
(other: sgx.fitness.simple.Vector) → bool¶ Check whether some differences from the other Fitness may be perceived.
-
is_fitter
(other: sgx.fitness.base.Fitness) → bool¶ Check whether fitter than the other.
-
Utils¶
CPU_time¶
Jupyter Support¶
-
sgx.utils.jupyter_support.
is_notebook
() → bool¶ Check if running inside a notebooks
Logging¶
-
sgx.utils.logging.
log_cpu
(level: int = 20, msg: str = '', *args, **kwargs) → None¶ Like log(), but including cpu time.
Random class¶
Archive class¶
sgx.archive
Base class¶
-
class
sgx.base.
Genome
(*args)¶ A tuple of Alleles, each one specifying a set of alternative genes.
-
is_valid
(genotype: sgx.base.Genotype) → bool¶ Check an object against a specification.
The function may be used to check a value against a parameter definition, a node against a section definition).
- Returns
True if the object is valid, False otherwise.
-
run_paranoia_checks
() → bool¶ Check the internal consistency of a “paranoid” object.
The function should be overridden by the sub-classes to implement the required, specific checks. It always returns
True
, but throws an exception whenever an inconsistency is detected.Notez bien: Sanity checks may be computationally intensive, paranoia checks are not supposed to be used in production environments (i.e., when
-O
is used for compiling). Their typical usage is:assert foo.run_paranoia_checks()
- Returns
True (always)
- Raises
AssertionError if some internal data structure is incoherent –
-
-
class
sgx.base.
Genotype
(*args)¶ A tuple containing the organism’s actual genes (their values).
-
run_paranoia_checks
() → bool¶ Check the internal consistency of a “paranoid” object.
The function should be overridden by the sub-classes to implement the required, specific checks. It always returns
True
, but throws an exception whenever an inconsistency is detected.Notez bien: Sanity checks may be computationally intensive, paranoia checks are not supposed to be used in production environments (i.e., when
-O
is used for compiling). Their typical usage is:assert foo.run_paranoia_checks()
- Returns
True (always)
- Raises
AssertionError if some internal data structure is incoherent –
-
-
class
sgx.base.
Paranoid
¶ Abstract class: Paranoid classes do implement
run_paranoia_checks()
.-
run_paranoia_checks
() → bool¶ Check the internal consistency of a “paranoid” object.
The function should be overridden by the sub-classes to implement the required, specific checks. It always returns
True
, but throws an exception whenever an inconsistency is detected.Notez bien: Sanity checks may be computationally intensive, paranoia checks are not supposed to be used in production environments (i.e., when
-O
is used for compiling). Their typical usage is:assert foo.run_paranoia_checks()
- Returns
True (always)
- Raises
AssertionError if some internal data structure is incoherent –
-
-
class
sgx.base.
Pedantic
¶ Abstract class: Pedantic classes do implement
is_valid()
.-
abstract
is_valid
(obj: Any) → bool¶ Check an object against a specification.
The function may be used to check a value against a parameter definition, a node against a section definition).
- Returns
True if the object is valid, False otherwise.
-
abstract
Species class¶
-
class
sgx.species.
Species
(genome: Sequence[Any], fitness_function: sgx.fitness.function.FitnessFunction, mutation_rate: Optional[float] = None)¶ Missing
-
evaluate
(genotype: sgx.base.Genotype) → sgx.fitness.base.Fitness¶ Evaluates a genotype according to a specific Fitness Function.
- Parameters
genotype – The Genotype which is going to be evaluated.
- Returns
The fitness calculated from a Fitness Function.
-
sample
(sample_type: Optional[str] = 'sample') → sgx.base.Genotype¶ Sampling the genotypes with a specific method.
- Parameters
sample_type – The type of sampling is going to be used.
- Returns
The candidate’s Genotype after sampling.
-
update
(winner: sgx.base.Genotype, loser: sgx.base.Genotype)¶ Updates the winner and the loser Genotype, so as to modify the new Learning Rate.
- Parameters
winner – The genotype of the better solution.
loser – The genotype of the worse solution.
-
Modular Design Rationale¶
…?