godatadriven / evol

a python grammar for evolutionary algorithms and heuristics
https://evol.rtfd.io
MIT License
187 stars 12 forks source link

added slots on individual #147

Closed koaning closed 4 years ago

koaning commented 4 years ago

It's a really minor change and the effect is not super huge, but still noticeable.

If you consider this example.py file.

import random
from evol import Population

def create_candidate():
    return random.random() - 0.5

def func_to_optimise(x):
    return x*2

random.seed(42)

@profile
def foo():
    return Population(chromosomes=[create_candidate() for _ in range(0000)],
                      eval_function=func_to_optimise, maximize=True)

if __name__ == "__main__":
    foo()

Here's the results without __slots__

    (venv) ➜  evol git:(e212796) ✗ python -m memory_profiler examples/population_demo.py
Filename: examples/population_demo.py

Line #    Mem usage    Increment   Line Contents
================================================
    19   53.949 MiB   53.949 MiB   @profile
    20                             def foo():
    21   54.176 MiB    0.012 MiB       return Population(chromosomes=[create_candidate() for _ in range(10000)],
    22   56.332 MiB    2.156 MiB                         eval_function=func_to_optimise, maximize=True)

Here it is with slots

(venv) ➜  evol git:(e212796) ✗ python -m memory_profiler examples/population_demo.py
Filename: examples/population_demo.py

Line #    Mem usage    Increment   Line Contents
================================================
    19   54.051 MiB   54.051 MiB   @profile
    20                             def foo():
    21   54.262 MiB    0.004 MiB       return Population(chromosomes=[create_candidate() for _ in range(10000)],
    22   55.516 MiB    1.254 MiB                         eval_function=func_to_optimise, maximize=True)

Can't hurt to have tho, so I added it.

koaning commented 4 years ago

ah nevermind this.

we use assert all(x.__dict__ == y.__dict__ for x, y in zip(simple_population, pop)) in a few places. the .__dict__ stuff is kind of blocked by slots.