forked from ellisk42/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.py
166 lines (144 loc) · 6.56 KB
/
circuit.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from ec import explorationCompression, commandlineArguments
from grammar import Grammar
from utilities import eprint, sampleDistribution
from circuitPrimitives import primitives
from task import Task
from type import arrow, tbool
from recognition import *
import itertools
import random
NUMBEROFTASKS = 10**3 / 4
inputDistribution = [#(1,1),
#(2,2),
(3,3),
(4,4),
(4,5),
(4,6),
# (4,5),
# (4,6),
# (4,7)
]
MAXIMUMINPUTS = max(i for p,i in inputDistribution)
gateDistribution = [(1,1),
(2,2),
(2,3),
# (4,4),
#(5,5),
#(6,5),
]
operationDistribution = [(1,'NOT'),
(2,'AND'),
(2,'OR'),
(4,'m2'),
(0,'m4')]
INPUTSPERGATE = {'NOT': 1,
'AND': 2,
'OR': 2,
'm2': 3,
'm4': 6}
GATEFUN = {'NOT': lambda x: not x,
'AND': lambda x,y: x and y ,
'OR': lambda x,y: x or y,
'm2': lambda x,y,c: [x,y][int(c)],
'm4': lambda a,b,c,d,x,y: [ [a,b][int(x)], [c,d][int(x)] ][int(y)]}
class Circuit(object):
def __init__(self, _ = None, numberOfInputs = None, numberOfGates = None):
assert numberOfInputs != None
assert numberOfGates != None
self.numberOfInputs = numberOfInputs
self.operations = []
while not self.isConnected():
self.operations = []
while len(self.operations) < numberOfGates:
validInputs = range(-numberOfInputs, len(self.operations))
gate = sampleDistribution(operationDistribution)
if 'm' in gate:
if self.numberOfInputs < INPUTSPERGATE[gate]: continue
arguments = list(np.random.choice(validInputs,
size = INPUTSPERGATE[gate],
replace = False))
else:
arguments = list(np.random.choice(validInputs,
size = INPUTSPERGATE[gate],
replace = False))
self.operations.append(tuple([gate] + arguments))
self.name = "%d inputs ; "%self.numberOfInputs + \
" ; ".join("%s(%s)"%(o[0],",".join(map(str,o[1:])))
for o in self.operations )
xs = list(itertools.product(*[ [False,True] for _ in range(numberOfInputs) ]))
ys = [ self.evaluate(x) for x in xs ]
self.examples = zip(xs,ys)
# the signature is invariant to the construction of the circuit and only depends on its semantics
self.signature = tuple(ys)
def __eq__(self,o): return self.name == o.name
def __ne__(self,o): return not (self == o)
def task(self):
request = arrow(*[tbool for _ in range(self.numberOfInputs + 1) ])
features = Circuit.extractFeatures(list(self.signature))
return Task(self.name, request, self.examples, features = features, cache = True)
@staticmethod
def extractFeatures(ys):
features = [ float(int(y)) for y in ys ]
maximumFeatures = 2**MAXIMUMINPUTS
return features + [-1.]*(maximumFeatures - len(features))
def evaluate(self,x):
x = list(reversed(x))
outputs = []
for z in self.operations:
f = GATEFUN[z[0]]
arguments = [ (outputs + x)[a] for a in z[1:] ]
outputs.append(f(*arguments))
return outputs[-1]
def isConnected(self):
def used(o):
arguments = { j for j in o[1:] if j >= 0 }
return arguments | { k for j in arguments for k in used(self.operations[j]) }
if self.operations == []: return False
usedIndices = used(self.operations[-1])
return len(usedIndices) == len(self.operations) - 1
class FeatureExtractor(HandCodedFeatureExtractor):
def _featuresOfProgram(program, t):
numberOfInputs = len(t.functionArguments())
xs = list(itertools.product(*[ [False,True] for _ in range(numberOfInputs) ]))
f = program.evaluate([])
ys = [ program.runWithArguments(x) for x in xs ]
return Circuit.extractFeatures(ys)
class DeepFeatureExtractor(MLPFeatureExtractor):
def __init__(self, tasks):
super(DeepFeatureExtractor, self).__init__(tasks, H = 16)
def _featuresOfProgram(self, program, tp):
numberOfInputs = len(tp.functionArguments())
xs = list(itertools.product(*[ [False,True] for _ in range(numberOfInputs) ]))
ys = [ program.runWithArguments(x) for x in xs ]
return Circuit.extractFeatures(ys)
if __name__ == "__main__":
circuits = []
import random
random.seed(0)
while len(circuits) < NUMBEROFTASKS*2:
inputs = sampleDistribution(inputDistribution)
gates = sampleDistribution(gateDistribution)
newTask = Circuit(numberOfInputs = inputs,
numberOfGates = gates)
if newTask not in circuits:
circuits.append(newTask)
eprint("Sampled %d circuits with %d unique functions"%(len(circuits),
len({t.signature for t in circuits })))
tasks = [t.task() for t in circuits[:NUMBEROFTASKS] ]
testing = [t.task() for t in circuits[NUMBEROFTASKS:] ]
baseGrammar = Grammar.uniform(primitives)
explorationCompression(baseGrammar, tasks,
testingTasks = testing,
outputPrefix = "experimentOutputs/circuit",
evaluationTimeout = None,
**commandlineArguments(iterations = 10,
aic = 1.,
structurePenalty = 1,
CPUs = numberOfCPUs(),
featureExtractor = DeepFeatureExtractor,
topK = 2,
maximumFrontier = 100,
helmholtzRatio = 0.5,
a = 2,
activation = "relu",
pseudoCounts = 5.))