-
Notifications
You must be signed in to change notification settings - Fork 0
/
mut.py
86 lines (76 loc) · 3.23 KB
/
mut.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
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""
Created on Mar 23 2016
@author: Vasken Dermardiros
ID: 26049516
Final project for COEN6321: Evolutionary Systems
Professor Nawwaf Kharma
mutations
---------
1. Rule bit flip: {'100': '0'} --> {'101': '0'}
2. Rule bit to wildcard: {'100': '0'} --> {'1.0': '0'}, the "." is a wildcard
3. Rule order shuffle: {'100': '0'} --> {'010': '0'}
4. Rule add bits: {'100': '0'} --> {'01010': '0'}
5. Rule flip output bit: {'100': '0'} --> {'100': '1'}
6. Rulebook add rule: {'100': '0'} --> {'100': '0', '11': '1'}
"""
# Import dependencies
import numpy as np
import random
# Pick a random rule from the rulebook and flip a bit
def bitFlip(rulebook):
rd = random.randint(0, len(rulebook)-1) # '-1' due to indexing
rule, output = rulebook.items()[rd]
rulebook.pop(rule) # remove rule from rulebook
ruleAsList = list(rule)
rdb = random.randint(0, len(ruleAsList)-1)
# If the chosen bit is a wildcard, stop this mutation
if ruleAsList[rdb] == '.': return rulebook.update({rule: output})
ruleAsList[rdb] = str((int(ruleAsList[rdb]) + 1) % 2)
modifiedRule = ''.join(ruleAsList)
return rulebook.update({modifiedRule: output})
# Pick a random rule from the rulebook and add a wildcard
def bitWild(rulebook):
rd = random.randint(0, len(rulebook)-1) # '-1' due to indexing
rule, output = rulebook.items()[rd]
rulebook.pop(rule) # remove rule from rulebook
ruleAsList = list(rule)
rdb = random.randint(0, len(ruleAsList)-1)
ruleAsList[rdb] = '.'
modifiedRule = ''.join(ruleAsList)
return rulebook.update({modifiedRule: output})
# Pick a random rule from the rulebook and shuffle the bits
def shuffle(rulebook):
rd = random.randint(0, len(rulebook)-1) # '-1' due to indexing
rule, output = rulebook.items()[rd]
rulebook.pop(rule) # remove rule from rulebook
ruleAsList = list(rule)
random.shuffle(ruleAsList) # shuffle
modifiedRule = ''.join(ruleAsList)
return rulebook.update({modifiedRule: output})
# Pick a random rule from the rulebook, add a bit
def addBit(rulebook):
rd = random.randint(0, len(rulebook)-1) # '-1' due to indexing
rule, output = rulebook.items()[rd]
rulebook.pop(rule) # remove rule from rulebook
rule = str(random.randint(0, 1)) + rule
return rulebook.update({rule: output})
# Pick a random rule from the rulebook and flip its output
def flipOutput(rulebook):
rd = random.randint(0, len(rulebook)-1) # '-1' due to indexing
rulebook[rulebook.keys()[rd]] = str((int(rulebook.values()[rd]) + 1) % 2)
return rulebook.update()
# Add a random rule to the rulebook
def addRule(rulebook):
newRule = createRule(len(max(rulebook.keys())))
return rulebook.update(newRule)
# same function from "functions.py" file
# copied here to avoid circular referencing...
def createRule(ruleLength):
# based on a Poisson distribution for actual length
actualLength = np.random.poisson(ruleLength-1, 1) + 1 # can't be 0
rule = np.random.randint(2, size=actualLength) # this is a list
rule = ''.join(map(str, rule)) # this is a string
output = str(np.random.randint(2, size=1)[0]) # this is also a string
return {rule: output}