-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclustering.py
63 lines (56 loc) · 1.64 KB
/
clustering.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
from framework import *
from grammar import *
from substitution import *
from myGlobals import *
class Cluster:
def __init__(self, inputLGG, outputLGG, inputList, outputList):
self.inputLGG = inputLGG
self.outputLGG = outputLGG
self.inputList = inputList
self.outputList = outputList
def combine(input, output, cluster):
try:
return InferProgram(cluster.inputList + [input], cluster.outputList + [output])
# return InferProgram([cluster.inputLGG, input], [cluster.outputLGG, output])
except:
return None
def isJustList(tree):
if tree._type != LIST:
return False
count = 0
for x in tree.list:
if x._type != EMPTY:
count = count + 1
if count > 1:
return True
return False
def tooGeneral(program):
if isJustList(program.input) or isJustList(program.output):
return True
return False
def FormClusters(inputList, outputList):
clusters = []
print len(inputList), len(outputList)
for i in range(len(inputList)):
input, output = inputList[i], outputList[i]
best = -1
for j in range(len(clusters)):
newProgram = combine(input, output, clusters[j])
if newProgram is None:
continue
if tooGeneral(newProgram):
continue
clusters[j].inputList = clusters[j].inputList + [input]
clusters[j].outputList = clusters[j].outputList + [output]
clusters[j].inputLGG, clusters[j].outputLGG = newProgram.input, newProgram.output
best = j
break
if best >= 0:
continue
# print "New cluster", i
# input.printTree()
# output.printTree()
clusters.append(Cluster(input, output, [input], [output]))
return clusters
def execute(cluster, input):
return RunProgram(Program(cluster.inputLGG, cluster.outputLGG), input)