-
Notifications
You must be signed in to change notification settings - Fork 0
/
creation.py
73 lines (54 loc) · 1.96 KB
/
creation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Functions for creation of primary population.
All these expose the following parameters:
pop_size -- size of the population to create
"""
import random
import itertools
from functools import partial
import mutator
from unit import Unit
from utils import named_functions_interface_decorator
@named_functions_interface_decorator
def named_functions() -> dict:
"""Return creation functions"""
return {
'MOD': memory_oriented_diversity,
'INP': stdin_printer,
'AME': all_methods_equally,
}
return funcs
def default_functions() -> tuple:
"""Return default creation functions"""
return named_functions.as_tuple() + anonymous_functions()
def anonymous_functions() -> tuple:
"""Return creation functions that have no name"""
return ()
def stdin_printer(pop_size:int) -> tuple:
"""Return pop_size new Unit instance. Source code is oriented toward
printing of stdin with workarounds.
"""
source = '+++.>,[.,]>+++.'
chrom_size = 10
return tuple(Unit(source, chrom_size) for _ in range(pop_size))
def memory_oriented_diversity(pop_size:int) -> tuple:
"""Return pop_size new Unit instance. Source code is oriented toward
movement and memory management, with less brackets and IO
"""
specs = range(30, 40), range(3, 20), '<<<>>>++--[],..'
return tuple(Unit.from_spec(*specs) for _ in range(pop_size))
def all_methods_equally(pop_size:int) -> tuple:
"""Return pop_size new Unit instance, using all the others
creation methods.
"""
methods = tuple(
method
for method in set(named_functions().values()) | set(anonymous_functions())
if method is not all_methods_equally
)
unit_per_method = pop_size // len(methods)
remaining = pop_size % len(methods)
for method in methods:
yield from method(unit_per_method)
# choose one for each remaining unit to produce
for _ in range(remaining):
yield from random.choice(methods)(1)