Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Freakwill committed Aug 28, 2024
1 parent 5c1190c commit 97b198d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
16 changes: 10 additions & 6 deletions pyrimidine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ class BaseChromosome(FitnessMixin, metaclass=MetaArray):
"n_chromosomes": "n_elements"
}

@property
def class_name(self):
if hasattr(self.__class__, '_name'):
return self.__class__._name
else:
return self.__class__.__name__

def __repr__(self):
return f'{self.__class__.__name__}: {":".join(map(repr, self))}'
return f'{self.class_name}: {":".join(map(repr, self))}'

def __str__(self):
return "|".join(map(str, self))
Expand Down Expand Up @@ -462,7 +469,7 @@ def dual(self):
return self.__class__([c.dual() for c in self])


class BaseMultiPopulation(PopulationMixin, metaclass=MetaHighContainer):
class BaseMultiPopulation(MultiPopulationMixin, metaclass=MetaHighContainer):
"""Base class of BaseMultiPopulation
Attributes:
Expand All @@ -477,7 +484,7 @@ class BaseMultiPopulation(PopulationMixin, metaclass=MetaHighContainer):

alias = {'populations': 'elements',
'n_populations': 'n_elements',
'best_population': 'best_element',
'get_best_populationspopulation': 'best_element',
'worst_population': 'worst_element',
'get_best_population': 'get_best_element',
'get_best_populations': 'get_best_elements'
Expand Down Expand Up @@ -511,9 +518,6 @@ def mutate(self):
def mate(self):
raise NotImplementedError

# def max_fitness(self):
# return max(map(attrgetter('max_fitness'), self))

def get_best_individual(self, copy=True):
bests = map(methodcaller('get_best_individual'), self)
k = max(b.fitness for b in bests)
Expand Down
4 changes: 2 additions & 2 deletions pyrimidine/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ def __call__(self, *args, **kwargs):
for k, v in kwargs.items():
setattr(o, k, v)

o.element_type = o.element_class

return o

def __getitem__(self, class_):
Expand Down Expand Up @@ -359,6 +357,7 @@ def __ifloordiv__(self, n):
def __floordiv__(self, n):
class cls(self):
default_size = n
cls._name = self.__name__
return cls

def random(self, n_elements=None, *args, **kwargs):
Expand Down Expand Up @@ -554,5 +553,6 @@ def __ifloordiv__(self, n):
def __floordiv__(self, n):
class cls(self):
default_size = n
cls._name = self.__name__
return cls

26 changes: 18 additions & 8 deletions pyrimidine/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""


from itertools import chain
from operator import methodcaller, attrgetter

import numpy as np
Expand Down Expand Up @@ -62,10 +63,10 @@ def local_search(self, *args, **kwargs):
"""
raise NotImplementedError('If you apply a local search algorithm, you must define the `local_search` method.')

def ezolve(self, max_iter=None, init=True):
def ezolve(self, max_iter=None, initialize=True):
# Extreamly eazy evolution method for lazybones
max_iter = max_iter or self.max_iter
if init:
if initialize:
self.init()
for k in range(1, max_iter+1):
self.transition(k)
Expand Down Expand Up @@ -108,12 +109,11 @@ def evolve(self, initialize:bool=True, max_iter:int=100, period:int=1, verbose:b

if verbose:
def _row(t, attrs, res, sep=" & "):
return f'{sep.join(map(str, concat((("[%d]"%t,), (getattr(self, attr) for attr in attrs), res.values()))))}'
from toolz.itertoolz import concat
return sep.join(map(str, chain(("[%d]"%t,), (getattr(self, attr) for attr in attrs), res.values())))
if not history_flag:
res = stat(self)
print(f""" ** History **
{" & ".join(concat((("iteration",), attrs, res.keys())))}
{" & ".join(chain(("iteration",), attrs, res.keys()))}
-------------------------------------------------------------""")
print(_row(0, attrs, res))

Expand All @@ -122,11 +122,13 @@ def _row(t, attrs, res, sep=" & "):

if history_flag and (period == 1 or t % period ==0):
res = stat(self)
history = pd.concat([history,
history = pd.chain([history,
pd.Series(res.values(), index=res.keys()).to_frame().T],
ignore_index=True)

if verbose and (period == 1 or t % period ==0):
if not history_flag:
res = stat(self)
print(_row(t, attrs, res))

if control:
Expand Down Expand Up @@ -375,7 +377,7 @@ def evolve(self, stat=None, *args, **kwargs):
"""

if stat is None:
stat = {'Best Fitness': 'max_fitness', 'Mean Fitness': 'mean_fitness',
stat = {'Max Fitness': 'max_fitness', 'Mean Fitness': 'mean_fitness',
'STD Fitness': 'std_fitness'}
return super().evolve(stat=stat, *args, **kwargs)

Expand Down Expand Up @@ -547,4 +549,12 @@ def drop(self, n=1):
elif not isinstance(n, int):
n = int(n)
ks = self.argsort()
self.elements = [self[k] for k in ks[n:]]
self.elements = [self[k] for k in ks[n:]]


class MultiPopulationMixin(PopulationMixin):

@property
def solution(self):
return self.best_element.solution()

0 comments on commit 97b198d

Please sign in to comment.