Skip to content

Commit

Permalink
new example
Browse files Browse the repository at this point in the history
  • Loading branch information
Freakwill committed Dec 14, 2023
1 parent 7d576c2 commit 8d67a00
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/source/Install.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

**Documentation as PDF**: download latest

`pyrimidine` is a framework for genetic algorithm.
`pyrimidine` stands as a versatile framework designed for GAs, offering exceptional extensibility for a wide array of evolutionary algorithms, including particle swarm optimization and difference evolution.

## Install pyrimidine

Expand Down
1 change: 0 additions & 1 deletion examples/comparison0.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""An easy knapsack problem
Expand Down
46 changes: 28 additions & 18 deletions pyrimidine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,31 @@ def __str__(self):
def random(cls, size=None):
raise NotImplementedError

def copy(self, *args, **kwargs):
raise NotImplementedError

def transition(self, *args, **kwargs):
self.mutate()

def x(self, other):
# alias for cross
return self.cross(other)

def cross(self, other):
# crossover operation
raise NotImplementedError

def merge(self, *other):
raise NotImplementedError

@side_effect
def mutate(self):
# mutation operation
raise NotImplementedError

def replicate(self):
# Replication operation of a chromosome
ind = self.copy()
ind.mutate()
return ind

def decode(self):
"""Decoding of the chromesome
Translate the chromesome to (part of) solution, maybe a number.
Expand All @@ -142,15 +154,6 @@ def encode(cls, x):
def equal(self, other):
return np.array_equal(self, other)

def copy(self, *args, **kwargs):
raise NotImplementedError

def replicate(self):
# Replication operation of a chromosome
ind = self.copy()
ind.mutate()
return ind


class BaseIndividual(FitnessMixin, metaclass=MetaContainer):
"""Base class of individuals
Expand Down Expand Up @@ -203,6 +206,9 @@ def copy(self, type_=None, *args, **kwargs):
else:
return type_([c.copy(type_=type_.element_class) for c in self])

def transition(self, *args, **kwargs):
self.mutate()

def cross(self, other):
# Cross operation of two individual
return self.__class__([chromosome.cross(other_c) for chromosome, other_c in zip(self, other)])
Expand Down Expand Up @@ -258,6 +264,7 @@ def __sub__(self, other):
return self.__class__([this - that for this, that in zip(self.chromosomes, other.chromosomes)])

def __rmul__(self, other):
# assert isinstance(other, np.number)
return self.__class__([other * this for this in self.chromosomes])


Expand Down Expand Up @@ -479,19 +486,22 @@ class BaseMultiPopulation(PopulationMixin, metaclass=MetaHighContainer):
'best_population': 'best_element',
'worst_population': 'worst_element',
"get_best_population": "get_best_element",
"get_best_populations": "get_best_elements"}
"get_best_populations": "get_best_elements"
}

def __str__(self):
return '\n\n'.join(map(str, self))

def migrate(self, migrate_prob=None):
def migrate(self, migrate_prob=None, copy=True):
migrate_prob = migrate_prob or self.migrate_prob
for population, other in zip(self[:-1], self[1:]):
if random() < (migrate_prob or self.migrate_prob):
other.append(population.get_best_individual(copy=True))
population.append(other.get_best_individual(copy=True))
if random() < migrate_prob:
other.append(population.get_best_individual(copy=copy))
population.append(other.get_best_individual(copy=copy))

def transition(self, *args, **kwargs):
super().transition(*args, **kwargs)
for p in self:
p.transition(*args, **kwargs)
self.migrate()

def max_fitness(self):
Expand Down
4 changes: 2 additions & 2 deletions pyrimidine/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ def __new__(cls, name, bases, attrs):
if 'element_class' in attrs:
element_class = attrs['element_class']
if (not isinstance(element_class, MetaContainer)
and isinstance(element_class, tuple) and not isinstance(element_class[0], MetaContainer)
and isinstance(element_class, tuple) and not any(isinstance(ec, MetaContainer) for ec in element_class)
and not isinstance(element_class, ParamType)):
raise TypeError('`element_class` should be an instance of MetaContainer, or a list of such instances.')
raise TypeError('`element_class` should be an instance of `MetaContainer`, or a tuple where one element is an instance of `MetaContainer`.')

def _flatten(self, type_):
elms = []
Expand Down

0 comments on commit 8d67a00

Please sign in to comment.