Skip to content

Instantly share code, notes, and snippets.

@alexander-gabriel
Created January 10, 2020 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-gabriel/8e4593e76120752cbccb9a5c73cec691 to your computer and use it in GitHub Desktop.
Save alexander-gabriel/8e4593e76120752cbccb9a5c73cec691 to your computer and use it in GitHub Desktop.
trying to understand inference in opencog part 1
import cProfile
import pstats
from opencog.type_constructors import *
from opencog.bindlink import execute_atom, evaluate_atom
from opencog.ure import BackwardChainer, ForwardChainer
from opencog import atomspace
from opencog.utilities import initialize_opencog
from opencog.atomspace import AtomSpace, types
from opencog.scheme_wrapper import scheme_eval
TRUE = TruthValue(1.0, 1.0)
# including predefined deduction rules
def build_deduction_rulebase():
deduction_rbs = ConceptNode("deduction-rule-base")
InheritanceLink(
deduction_rbs,
ConceptNode("URE"))
execute_code = \
'''
(use-modules (opencog rule-engine))
;;(load-from-path "/home/rasberry/git/ure/tests/ure/meta-rules/conditional-full-instantiation-meta-rule.scm")
(load-from-path "/home/rasberry/git/ure/tests/ure/rules/bc-deduction-rule.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/meta-rules/predicate/conditional-full-instantiation.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/meta-rules/predicate/conditional-partial-instantiation.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/meta-rules/predicate/universal-full-instantiation.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/term/deduction.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/propositional/modus-ponens.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/propositional/contraposition.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/wip/implication-scope-to-implication.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/wip/implication-instantiation.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/wip/negation-introduction.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/wip/not-simplification.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/wip/not-elimination.scm")
(load-from-path "/home/rasberry/git/opencog/opencog/pln/rules/wip/evaluation-to-member.scm")
(define rbs (Concept "deduction-rule-base"))
(ure-add-rule rbs bc-deduction-rule-name)
(ure-add-rule rbs conditional-full-instantiation-implication-meta-rule-name)
(ure-add-rule rbs conditional-full-instantiation-inheritance-meta-rule-name)
(ure-add-rule rbs conditional-partial-instantiation-meta-rule-name)
(ure-add-rule rbs universal-full-instantiation-forall-1ary-meta-rule-name)
(ure-add-rule rbs deduction-inheritance-rule-name)
(ure-add-rule rbs deduction-implication-rule-name)
(ure-add-rule rbs deduction-subset-rule-name)
(ure-add-rule rbs modus-ponens-inheritance-rule-name)
(ure-add-rule rbs modus-ponens-implication-rule-name)
(ure-add-rule rbs modus-ponens-subset-rule-name)
(ure-add-rule rbs crisp-contraposition-implication-scope-rule-name)
(ure-add-rule rbs contraposition-implication-rule-name)
(ure-add-rule rbs contraposition-inheritance-rule-name)
(ure-add-rule rbs implication-scope-to-implication-rule-name)
(ure-add-rule rbs implication-full-instantiation-rule-name)
(ure-add-rule rbs implication-partial-instantiation-rule-name)
(ure-add-rule rbs negation-introduction-rule-name)
(ure-add-rule rbs not-simplification-rule-name)
(ure-add-rule rbs not-elimination-rule-name)
;;(ure-add-rule rbs )
(ure-set-maximum-iterations rbs 30)
(ure-set-complexity-penalty rbs 0.1)
(EvaluationLink (stv 0 1)
(PredicateNode "URE:attention-allocation")
(ConceptNode "deduction-rule-base")
)
'''
scheme_eval(atomspace, execute_code)
return deduction_rbs
# truth value calculation for the following deduction rule
def deduction_formula(AC, AB, BC):
tv1 = AB.tv
tv2 = BC.tv
if tv1.mean > 0.5 and tv2.mean > 0.5 and tv1.confidence > 0.5 and tv2.confidence > 0.5:
AC.tv = TruthValue(1, 1)
else:
AC.tv = TruthValue(0, 0)
return AC
# custom deduction rule for the linked predicate
def build_linked_deduction(deduction_rbs):
deduction_rule = BindLink(
VariableList(
TypedVariableLink(
VariableNode('$A'),
TypeNode('ConceptNode')),
TypedVariableLink(
VariableNode('$B'),
TypeNode('ConceptNode')),
TypedVariableLink(
VariableNode('$C'),
TypeNode('ConceptNode'))),
AndLink(
EvaluationLink(
PredicateNode("linked"),
ListLink(
VariableNode('$A'),
VariableNode('$B'))),
EvaluationLink(
PredicateNode("linked"),
ListLink(
VariableNode('$B'),
VariableNode('$C'))),
NotLink(
IdenticalLink(
VariableNode('$A'),
VariableNode('$C')))),
ExecutionOutputLink(
GroundedSchemaNode('py: deduction_formula'),
ListLink(
EvaluationLink(
PredicateNode("linked"),
ListLink(
VariableNode('$A'),
VariableNode('$C'))),
EvaluationLink(
PredicateNode("linked"),
ListLink(
VariableNode('$A'),
VariableNode('$B'))),
EvaluationLink(
PredicateNode("linked"),
ListLink(
VariableNode('$B'),
VariableNode('$C'))))))
deduction_rule_name = DefinedSchemaNode("linked-deduction-rule")
DefineLink(
deduction_rule_name,
deduction_rule)
MemberLink(deduction_rule_name, deduction_rbs)
# basic structure of classes
def build_ontology():
thing = ConceptNode("thing").truth_value(1.0, 1.0)
concept = ConceptNode("concept").truth_value(1.0, 1.0)
InheritanceLink(concept, thing).truth_value(1.0, 1.0)
place = ConceptNode("place").truth_value(1.0, 1.0)
InheritanceLink(place, concept).truth_value(1.0, 1.0)
entity = ConceptNode("entity").truth_value(1.0, 1.0)
InheritanceLink(entity, thing).truth_value(1.0, 1.0)
obj = ConceptNode("object").truth_value(1.0, 1.0)
organism = ConceptNode("organism").truth_value(1.0, 1.0)
InheritanceLink(organism, entity).truth_value(1.0, 1.0)
InheritanceLink(obj, entity).truth_value(1.0, 1.0)
plant = ConceptNode("plant").truth_value(1.0, 1.0)
InheritanceLink(plant, organism).truth_value(1.0, 1.0)
strawberryplant = ConceptNode("strawberryplant").truth_value(1.0, 1.0)
InheritanceLink(strawberryplant, plant).truth_value(1.0, 1.0)
creature = ConceptNode("creature").truth_value(1.0, 1.0)
robot = ConceptNode("robot").truth_value(1.0, 1.0)
human = ConceptNode("human").truth_value(1.0, 1.0)
InheritanceLink(creature, organism).truth_value(1.0, 1.0)
InheritanceLink(robot, creature).truth_value(1.0, 1.0)
InheritanceLink(human, creature).truth_value(1.0, 1.0)
crate = ConceptNode("crate").truth_value(1.0, 1.0)
InheritanceLink(crate, obj).truth_value(1.0, 1.0)
def has_crate(picker):
link = StateLink(ListLink(picker, PredicateNode("has_crate")), ConceptNode("TRUE"))
link.tv = TRUE
return link
def not_has_crate(picker):
link = StateLink(ListLink(picker, PredicateNode("has_crate")), ConceptNode("FALSE"))
link.tv = TRUE
return link
def seen_picking(picker):
link = StateLink(ListLink(picker, PredicateNode("seen_picking")), ConceptNode("TRUE"))
link.tv = TRUE
return link
def not_seen_picking(picker):
link = StateLink(ListLink(picker, PredicateNode("seen_picking")), ConceptNode("FALSE"))
link.tv = TRUE
return link
# add a place conceptnode to the KB
def add_place(name, truth_value=TRUE):
node1 = ConceptNode(name)
node1.tv = truth_value
link = InheritanceLink(node1, ConceptNode("place"))
link.tv = truth_value
# add two 'linked' places
def add_place_link(place1, place2, truth_value=TRUE):
p1 = ConceptNode(place1)
p1.tv = truth_value
link = InheritanceLink(p1, ConceptNode("place"))
link.tv = truth_value
p2 = ConceptNode(place2)
p2.tv = truth_value
link = InheritanceLink(p2, ConceptNode("place"))
link.tv = truth_value
link = EvaluationLink(
PredicateNode("linked"),
ListLink(p1, p2))
link.tv = truth_value
# add arbitrary typed things to the KB
def add_thing(name, klasse, truth_value=TRUE):
node1 = ConceptNode(name)
node1.tv = truth_value
node2 = ConceptNode(klasse)
node2.tv = truth_value
link = InheritanceLink(node1, node2)
link.tv = truth_value
return node1
class Config(object):
picker_name = "Picker01"
robot_name = "Robot01"
class Goal(Config):
goal = None
def set_facts(self):
pass
class Deliver(Goal):
log_name_prefix = "Deliver"
def set_facts(self):
not_has_crate(ConceptNode(self.picker_name))
not_seen_picking(ConceptNode(self.picker_name))
class Size(Config):
size = 4
log_name_postfix = "Size.log"
class Small(Size):
size = 10
log_name_postfix = "Small.log"
class Range(Config):
picker_pos = 2
robot_pos = 1
log_name = "Range"
class Short(Range):
picker_pos = 2
log_name = "Short"
class Middle(Range):
picker_pos = 4
log_name = "Middle"
class Long(Range):
picker_pos = 8
log_name = "Long"
class DeliverShortSmall(Deliver, Short, Small): pass
class DeliverMiddleSmall(Deliver, Middle, Small): pass
class DeliverLongSmall(Deliver, Long, Small): pass
if __name__ == '__main__':
configs = [DeliverLongSmall(), DeliverMiddleSmall(), DeliverShortSmall()]
# iterate through configs
for config in configs:
print("--------------------------------------------------\n")
print("Robot position: {:d}".format(config.robot_pos))
print("Picker position: {:d}\n".format(config.picker_pos))
atomspace = AtomSpace()
initialize_opencog(atomspace)
rbs = build_deduction_rulebase()
build_linked_deduction(rbs)
build_ontology()
p = add_thing(config.picker_name, "human")
r = add_thing(config.robot_name, "robot")
c = add_thing("crate01", "crate")
s = add_thing("strawberry01", "strawberryplant")
# build waypoints as defined in config
for index in range(1,config.size-1):
# add waypoint concepts and 'linked' predicate
add_place_link("WayPoint{:03d}".format(index), "WayPoint{:03d}".format(index+1))
# add statelinks to set picker and robot position according to config
if config.picker_pos == index:
link = StateLink(p, ConceptNode("WayPoint{:03d}".format(index)))
link.tv = TRUE
if config.robot_pos == index:
link = StateLink(r, ConceptNode("WayPoint{:03d}".format(index)))
link.tv = TRUE
# add statelinks to set picker and robot position if configured for last WayPoint
if config.picker_pos == config.size:
link = StateLink(p, ConceptNode("WayPoint{:03d}".format(config.size)))
link.tv = TRUE
if config.robot_pos == config.size:
link = StateLink(r, ConceptNode("WayPoint{:03d}".format(config.size)))
link.tv = TRUE
# add statelinks to set crate and plant position for waypoint 1
link = StateLink(s, ConceptNode("WayPoint{:03d}".format(1)))
link.tv = TRUE
link = StateLink(c, ConceptNode("WayPoint{:03d}".format(1)))
link.tv = TRUE
# add config-defined facts to KB
config.set_facts()
# define test queries
queries = []
# WARN: if I add this to the queries it adds the links to the atomspace (as it should I guess)
# and proceeds to return the results for the just added links, truth values differ over distances
# queries.append(
# EvaluationLink(
# PredicateNode("linked"),
# ListLink(
# ConceptNode("WayPoint{:03d}".format(config.robot_pos)),
# ConceptNode("WayPoint{:03d}".format(config.picker_pos)))))
# this returns results only for predefined links, but even there the truth value has zero confidence
queries.append(
GetLink(
VariableList(
VariableNode("origin"),
VariableNode("destination")),
AndLink(
StateLink(
ConceptNode(config.robot_name),
VariableNode("origin")),
StateLink(
ConceptNode(config.picker_name),
VariableNode("destination")),
StateLink(
ListLink(VariableNode("picker"), PredicateNode("seen_picking")),
ConceptNode("FALSE")),
StateLink(
ListLink(VariableNode("picker"), PredicateNode("has_crate")),
ConceptNode("FALSE")),
EvaluationLink(
PredicateNode("linked"),
ListLink(
VariableNode("origin"),
VariableNode("destination"))))))
for query in queries:
print("Query: {:}\n".format(query))
chainer = BackwardChainer(atomspace,
rbs,
query)
chainer.do_chain()
results = chainer.get_results()
print("Result {:}".format(results))
try:
print("Truth: {:}".format(results.get_out()[0].tv))
except:
pass
print("\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment