-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThe Computer as Master Mind Vis.py
More file actions
83 lines (65 loc) · 2.61 KB
/
The Computer as Master Mind Vis.py
File metadata and controls
83 lines (65 loc) · 2.61 KB
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
from itertools import permutations, product
import sys
from timeit import default_timer as timer
#~ pip install anytree
from anytree import AnyNode, Node, RenderTree, AsciiStyle
from anytree.importer import JsonImporter, DictImporter
from anytree.exporter import DotExporter
from anytree.search import findall
def nodenamefunc(node):
if len(node.ancestors) == 0:
return node.name
Name = ""
for i, anc in enumerate(node.ancestors):
if i == 0:
Name += anc.name
else:
Name += f":{anc.AnswerIdx}/{anc.name}"
return Name + f":{node.AnswerIdx}/{node.name}"
def edgeattrfunc(node, child):
return f'label="{GetBWStringLinear(child.AnswerIdx)}"'
def filterfunc(node):
return (len(node.children) > 0)
def Export(Tree, FileName):
DotExporter(Tree,
#~ options=["ratio=0.0125;"],
#~ options=["ratio=0.03;"],
#~ options=["ratio=0.70710678118654752440084436210485;", 'fontsize="40";'],
#~ options=["ratio=0.70710678118654752440084436210485;"],
#~ options=["ratio=0.5;"],
nodenamefunc = nodenamefunc,
nodeattrfunc = lambda node: 'label="%s"' % node.Pattern,
edgeattrfunc = edgeattrfunc,
filter_=filterfunc,
).to_dotfile(FileName)
def SplitTree(Root, NumChildren = [-1]):
AllNewRoots = []
AllChildren = Root.children
PrevIdx = 0
for Num in NumChildren:
TheseChildren = AllChildren[PrevIdx:PrevIdx+Num]
PrevIdx += Num
NewRoot = Node(Root.name, Pattern = Root.Pattern, Codes = [], AnswerIdx = Root.AnswerIdx)
for Child in TheseChildren:
Child.parent = NewRoot
AllNewRoots.append(NewRoot)
return AllNewRoots
################### Main ###################
#~ Some helpers for indexing
filename = "The Computer as Master Mind Helpers.py"
with open(filename, "rb") as source_file:
code = compile(source_file.read(), filename, "exec")
exec(code)
#~ Import it from file
Imp = JsonImporter(DictImporter(nodecls=Node))
with open("ClassicMastermindTree.json", "r") as f:
#~ with open("FamilyMastermindTree.json", "r") as f:
Root = Imp.read(f)
#~ Export it as one file
Export(Root, "ClassicMastermindTree.dot")
#~ Export(Root, "FamilyMastermindTree.dot")
#~ Split it, then export as separate files
#~ Forest = SplitTree(Root, [3, 1, 1, 1, 1, 1, 2, 100])
#~ for i, Tree in enumerate(Forest):
#~ Export(Tree, f"ClassicMastermindTree_{i}.dot")
print("Use GraphViz dot to visualize the tree: 'dot.exe ClassicMastermindTree.dot -T svg -o ClassicMastermindTree.svg'")