forked from opencog/opencog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocksworld.py
More file actions
176 lines (141 loc) · 5.28 KB
/
Copy pathblocksworld.py
File metadata and controls
176 lines (141 loc) · 5.28 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
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
167
168
169
170
171
172
173
174
175
176
try:
from opencog.atomspace import AtomSpace, types, Atom, TruthValue, get_type_name
import opencog.cogserver
except ImportError:
from atomspace_remote import AtomSpace, types, Atom, TruthValue, get_type_name
from rules import evaluation_link_template, execution_link_template, actionDone_template
from tree import *
t = types
def action_template(action,arguments):
return execution_link_template(
action,
arguments)
def not_template(link):
return T('NotLink', link)
def parse_params(vars, relation_l):
return [vars[v_str] for v_str in relation_l[1:]]
def parse_conditions_list(vars, conditions_str):
conditions = []
for c_str in conditions_str.split('|'):
conditions_l = c_str.split()
pred_str = conditions_l[0]
pred_true = True
if pred_str[0] == '~':
pred_str = pred_str[1:]
pred_true = False
pred = predicates[pred_str]
params = parse_params(vars, conditions_l)
link = evaluation_link_template(pred,params)
if not pred_true:
link = not_template(link)
conditions.append(
link
)
return conditions
def new_rule(atomspace, action_str, pre_str, post_str):
action_l = action_str.split()
action = actions[action_l[0]]
vars = {}
for v_str in action_l[1:]:
vars[v_str] = atomspace.add(t.VariableNode, v_str)
preconditions = parse_conditions_list(vars, pre_str)
preconditions.append(actionDone_template(atomspace, action_template(action, parse_params(vars, action_l))))
postconditions = parse_conditions_list(vars, post_str)
pil = T('PredictiveImplicationLink',
T('SimultaneousAndLink',
preconditions
),
T('SimultaneousAndLink',
postconditions
)
)
atom_from_tree(pil, atomspace).tv = TruthValue(1, TruthValue().confidence_to_count(1))
print pil
return pil
blocks = {}
actions = {}
predicates = {}
def blocksworld_rules(atomspace):
for letter in 'ABC':
blocks[letter] = atomspace.add(t.ObjectNode, 'movable_block_'+letter)
for action_name in 'unstack stack pickup putdown start end'.split(' '):
actions[action_name] = atomspace.add(t.SchemaNode, action_name)
for predicate_name in 'clear on ontable handempty holding'.split(' '):
predicates[predicate_name] = atomspace.add(t.PredicateNode,predicate_name)
# top = new_var()
# bottom = new_var()
#
# preconditions = [
# evaluation_link_template(predicates['clear'],[top]),
# evaluation_link_template(predicates['on'],[top, bottom]),
# evaluation_link_template(predicates['handempty'],[])
# ]
# postconditions = [
# evaluation_link_template(predicates['holding'],[top]),
# evaluation_link_template(predicates['clear'],[bottom]),
# # what?
# not_template(evaluation_link_template(predicates['clear'],[top])),
# not_template(evaluation_link_template(predicates['on'],[top, bottom])),
# not_template(evaluation_link_template(predicates['handempty'],[]))
# ]
#
# pil = T('PredictiveImplicationLink',
# T('SimultaneousAndLink',
# preconditions,
# actionDone_template(atomspace, action_template(actions['unstack'], [top, bottom]))
# ),
# T('SimultaneousAndLink',
# postconditions
# )
# )
# atom_from_tree(pil, atomspace)
# new_rule([('clear',top), ('on',top, bottom), ('handempty')],
# [('holding', top), ('clear', bottom), ('~clear', top), ('~on', top, bottom), ('~handempty')],
# ('unstack', top, bottom)
# )
#
# new_rule([('holding',top), ('clear', bottom)],
# [('on',top,bottom),('clear',top),('handempty'),('~holding',top),('~clear, bottom')],
# ('stack',top, bottom)
# )
new_rule(atomspace,
'unstack top bottom',
'clear top | on top bottom | handempty',
'holding top | clear bottom | ~clear top | ~on top bottom | ~handempty'
)
new_rule(atomspace,
'stack top bottom',
'holding top | clear bottom',
'on top bottom | clear top | handempty | ~holding top | ~clear bottom'
)
new_rule(atomspace,
'pickup block',
'ontable block | clear block | handempty',
'holding block | ~ontable block | ~clear block | ~handempty'
)
new_rule(atomspace,
'putdown block',
'holding block',
'ontable block | clear block | handempty | ~holding block'
)
initial_conditions_and = T('SimultaneousAndLink',
#parse_conditions_list(blocks, 'on C A | handempty | ontable A | ontable B | clear B | clear C')
parse_conditions_list(blocks, 'ontable C | handempty | ontable A | ontable B | clear B | clear C')
)
atom = atom_from_tree(initial_conditions_and, atomspace)
atom.tv = TruthValue(1, TruthValue().confidence_to_count(1))
def blocksworld_test(atomspace):
atomspace.clear()
blocksworld.blocksworld_rules(atomspace)
import logic
target_tr = T('SimultaneousAndLink',
#parse_conditions_list(blocks, 'on A B | on B C')
parse_conditions_list(blocks, 'on A B')
)
c = logic.Chainer(atomspace)
res = logic.do_planning(atomspace, target_tr,c)
f = '<blocksworld_test>'
if len(res):
print 'PASSED'
else:
print 'FAILED'