-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerOfHanoi.py
executable file
·125 lines (111 loc) · 4.39 KB
/
TowerOfHanoi.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
"""
Tower of Hanoi expert system.
Usage new instance pass in number of discs. Run a simulation.
"""
from rbs.nealCoverClass import NealCoverFunctions
from rbs.stateMachineClass import FSAHelperFunctions
from rbs.narcBuilder import NeuralCognitiveArchitectureBuilder
class TowerOfHanoi:
def __init__(self, sim, simulator, discNum):
self.neal = NealCoverFunctions(simulator, sim)
self.fsa = FSAHelperFunctions(simulator, sim, self.neal)
self.narc = NeuralCognitiveArchitectureBuilder(simulator, sim, self.fsa, self.neal).build()
self.narc.addRule(
"ToH",
[
(True, "ToH", ("?d",), "r1"),
],
[
("assert", ("tower", ("A",))),
("assert", ("tower", ("B",))),
("assert", ("tower", ("C",))),
("assert", ("stackTop", (0,))),
("assert", ("stack", (0, "goal", 1, "?d", "A", "C"))),
("assert", ("addDisk", ("?d", "A"))),
("retract", "r1")
]
)
self.narc.addRule(
"addDisk",
[
(True, "addDisk", ("?d","?from"), "a"),
("test", ">", "?d", 1)
],
[
("retract", "a"),
("assert", ("diskAt", ("?d", "?from"))),
("assert", ("addDisk", (("-", "?d", 1),"?from")))
]
)
self.narc.addRule(
"addFinalDisk",
[
(True, "addDisk", ("?d","?from"), "a"),
("test", "=", "?d", 1)
],
[
("retract", "a"),
("assert", ("diskAt", ("?d", "?from")))
]
)
self.narc.addRule(
"GoalToGoals",
[
(True, "stack", ("?t", "goal", "?topDisc", "?bottomDisc", "?from", "?to"), "g"),
(True, "stackTop", ("?t",), "st"),
(True, "tower", ("?from",), "towerFrom"),
(True, "tower", ("?to",), "towerTo"),
(True, "tower", ("?other",), "towerOther"),
("test", "<>", "?from", "?other"),
("test", "<>", "?to", "?other"),
("test", "<", ("+", "?topDisc", 1), "?bottomDisc"),
],
[
("retract", "g"),
("retract", "st"),
("assert", ("stackTop",(("+","?t",2),))),
("assert", ("stack", ("?t", "goal", "?topDisc", ("-", "?bottomDisc", 1), "?other", "?to"))),
("assert", ("stack", (("+", "?t", 1), "move", "?bottomDisc", "?from", "?to"))),
("assert", ("stack", (("+", "?t", 2), "goal", "?topDisc", ("-", "?bottomDisc", 1), "?from", "?other")))
]
)
self.narc.addRule(
"GoalToMoves",
[
(True, "stack", ("?t", "goal", "?topDisc", "?bottomDisc", "?from", "?to"), "g"),
(True, "stackTop", ("?t",), "st"),
(True, "tower", ("?from",), "towerFrom"),
(True, "tower", ("?to",), "towerTo"),
(True, "tower", ("?other",), "towerOther"),
("test", "<>", "?from", "?other"),
("test", "<>", "?to", "?other"),
("test", "=", ("+", "?topDisc", 1), "?bottomDisc"),
],
[
("retract", "g"),
("retract", "st"),
("assert", ("stackTop",(("+", "?t", 2),))),
("assert", ("stack", ("?t", "move", "?topDisc", "?other", "?to"))),
("assert", ("stack", (("+", "?t", 1), "move", "?bottomDisc", "?from", "?to"))),
("assert", ("stack", (("+", "?t", 2), "move", "?topDisc", "?from", "?other")))
]
)
self.narc.addRule(
"MakeMove",
[
(True, "stack", ("?t","move","?disc","?from","?to"), "g"),
(True, "stackTop", ("?t",), "st"),
(True, "diskAt", ("?disc","?from"), "d")
],
[
("retract", "g"),
("retract", "d"),
("retract", "st"),
("assert", ("diskAt",("?disc","?to"))),
("assert", ("stackTop",(("-","?t",1),)))
]
)
self.narc.addFact("ToH", (discNum,))
self.narc.apply()
def printSpikes(self, name):
self.narc.printSpikes()