Skip to content

Commit

Permalink
Merge pull request singnet#40 from ngeiswei/merge-opencog-to-singnet
Browse files Browse the repository at this point in the history
Merge opencog -> singnet
  • Loading branch information
ngeiswei authored Oct 14, 2021
2 parents a804991 + 7933f0e commit a737b49
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
18 changes: 9 additions & 9 deletions examples/cartpole.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def plan(self, goal, expiry) -> List:
The format for a cognitive schematic is as follows
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
And (or SimultaneousAnd?)
Expand Down Expand Up @@ -152,7 +152,7 @@ def plan(self, goal, expiry) -> List:
hTV = TruthValue(0.9, 0.1) # High TV
lTV = TruthValue(0.1, 0.1) # Low TV

# PredictiveImplicationScope <high TV>
# BackPredictiveImplicationScope <high TV>
# TypedVariable
# Variable "$angle"
# Type "NumberNode"
Expand All @@ -169,7 +169,7 @@ def plan(self, goal, expiry) -> List:
# Evaluation
# Predicate "Reward"
# Number "1"
cs_rr = PredictiveImplicationScopeLink(
cs_rr = BackPredictiveImplicationScopeLink(
TypedVariableLink(angle, numt),
time_offset,
AndLink(
Expand All @@ -185,7 +185,7 @@ def plan(self, goal, expiry) -> List:
tv=hTV,
)

# PredictiveImplicationScope <high TV>
# BackPredictiveImplicationScope <high TV>
# TypedVariable
# Variable "$angle"
# Type "NumberNode"
Expand All @@ -202,7 +202,7 @@ def plan(self, goal, expiry) -> List:
# Evaluation
# Predicate "Reward"
# Number "1"
cs_ll = PredictiveImplicationScopeLink(
cs_ll = BackPredictiveImplicationScopeLink(
TypedVariableLink(angle, numt),
time_offset,
AndLink(
Expand All @@ -222,7 +222,7 @@ def plan(self, goal, expiry) -> List:
# actions, i.e. going left when the pole is falling to the right
# and such, which should make the situation worse.

# PredictiveImplicationScope <low TV>
# BackPredictiveImplicationScope <low TV>
# TypedVariable
# Variable "$angle"
# Type "NumberNode"
Expand All @@ -239,7 +239,7 @@ def plan(self, goal, expiry) -> List:
# Evaluation
# Predicate "Reward"
# Number "1"
cs_rl = PredictiveImplicationScopeLink(
cs_rl = BackPredictiveImplicationScopeLink(
TypedVariableLink(angle, numt),
time_offset,
AndLink(
Expand All @@ -255,7 +255,7 @@ def plan(self, goal, expiry) -> List:
tv=lTV,
)

# PredictiveImplicationScope <low TV>
# BackPredictiveImplicationScope <low TV>
# TypedVariable
# Variable "$angle"
# Type "NumberNode"
Expand All @@ -272,7 +272,7 @@ def plan(self, goal, expiry) -> List:
# Evaluation
# Predicate "Reward"
# Number "1"
cs_lr = PredictiveImplicationScopeLink(
cs_lr = BackPredictiveImplicationScopeLink(
TypedVariableLink(angle, numt),
time_offset,
AndLink(
Expand Down
52 changes: 32 additions & 20 deletions rocca/agents/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,9 @@ def load_opencog_modules(self):
# Load PLN. All rules must be pre-loaded here
scheme_eval(self.atomspace, "(use-modules (opencog pln))")
scheme_eval(self.atomspace, "(use-modules (opencog spacetime))")
scheme_eval(
self.atomspace,
"(pln-load-rule 'predictive-implication-scope-direct-evaluation)",
)
scheme_eval(
self.atomspace, "(pln-load-rule 'predictive-implication-scope-deduction)"
)
rules = ["back-predictive-implication-scope-direct-evaluation",
"back-predictive-implication-scope-deduction-cogscm"]
self.pln_load_rules(rules)
# scheme_eval(self.atomspace, "(pln-log-atomspace)")

def reset_action_counter(self):
Expand Down Expand Up @@ -138,6 +134,20 @@ def make_goal(self):

return EvaluationLink(PredicateNode("Reward"), NumberNode(str(1)))

def pln_load_rules(self, rules=[]):
"""Load PLN rules.
Take a list of rule scheme symbols (but without the single
quote for the symbol), such as
["back-predictive-implication-scope-direct-evaluation",
"back-predictive-implication-scope-deduction-cogscm"]
"""

for rule in rules:
scheme_eval(self.atomspace, "(pln-load-rule '" + rule + ")")

def pln_bc(self, query, vardecl=None, maxiter=10, rules=[]):
"""Call PLN backward chainer with the given query and parameters.
Expand All @@ -152,11 +162,13 @@ def pln_bc(self, query, vardecl=None, maxiter=10, rules=[]):

agent_log.fine("pln_bc(query={}, maxiter={})".format(query, maxiter))

# Load rules
# Add rules (should be previously loaded)
if rules:
scheme_eval(self.atomspace, "(pln-rm-all-rules)")
for rule in rules:
scheme_eval(self.atomspace, "(pln-add-rule '" + rule + ")")
er = scheme_eval(self.atomspace, "(pln-add-rule '" + rule + ")")
agent_log.info("(pln-add-rule '" + rule + ")")
agent_log.info("er = " + str(er))

# Generate and run query
command = "(pln-bc "
Expand Down Expand Up @@ -247,9 +259,9 @@ def infer_cogscms(self):
T = VariableNode("$T")
P = VariableNode("$P")
Q = VariableNode("$Q")
query = PredictiveImplicationScopeLink(V, T, P, Q)
query = BackPredictiveImplicationScopeLink(V, T, P, Q)
mi = 10
rules = ["predictive-implication-scope-deduction"]
rules = ["back-predictive-implication-scope-deduction-cogscm"]
cogscms = self.pln_bc(query, maxiter=mi, rules=rules)

agent_log.fine("Inferred cognitive schematics = {}".format(cogscms))
Expand Down Expand Up @@ -369,7 +381,7 @@ def to_sequential_and(self, timed_clauses):
early_events = get_events(early_clauses)
latest_clauses = get_latest_clauses(timed_clauses)
latest_events = get_events(latest_clauses)
return AltSequentialAndLink(
return BackSequentialAndLink(
to_nat(1), maybe_and(early_events), maybe_and(latest_events)
)
else:
Expand Down Expand Up @@ -417,7 +429,7 @@ def to_predictive_implication_scope(self, pattern):
then the resulting predictive implication scope is
PredictiveImplicationScope
BackPredictiveImplicationScope
VariableList
S Z
Execution
Expand Down Expand Up @@ -453,7 +465,7 @@ def to_predictive_implication_scope(self, pattern):
then the resulting predictive implication scope is
PredictiveImplicationScope
BackPredictiveImplicationScope
Variable "$X"
S Z
And
Expand All @@ -474,7 +486,7 @@ def to_predictive_implication_scope(self, pattern):
then the resulting predictive implication (scope) is
PredictiveImplication <1 - s, c>
BackPredictiveImplication <1 - s, c>
<antecedent>
Evaluation
Predicate "Reward"
Expand Down Expand Up @@ -503,11 +515,11 @@ def to_predictive_implication_scope(self, pattern):
lag = SLink(ZLink())

ntvardecl = self.get_nt_vardecl(pattern)
# TODO: fix python PredictiveImplicationScopeLink binding!
# preimp = PredictiveImplicationScopeLink(ntvardecl, lag, pt, pd)
# TODO: fix python BackPredictiveImplicationScopeLink binding!
# preimp = BackPredictiveImplicationScopeLink(ntvardecl, lag, pt, pd)
preimp = scheme_eval_h(
self.atomspace,
"(PredictiveImplicationScopeLink "
"(BackPredictiveImplicationScopeLink "
+ str(ntvardecl)
+ str(lag)
+ str(pt)
Expand All @@ -525,7 +537,7 @@ def to_predictive_implication_scope(self, pattern):
agent_log.fine("preimp = {}".format(preimp))
# Calculate the truth value of the predictive implication
mi = 2
rules = ["predictive-implication-scope-direct-evaluation"]
rules = ["back-predictive-implication-scope-direct-evaluation"]
return self.pln_bc(preimp, maxiter=mi, rules=rules)[0]

def is_desirable(self, cogscm):
Expand Down Expand Up @@ -724,7 +736,7 @@ def plan(self, goal, expiry):
information. The supported format for cognitive schematics
are as follows
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<lag-n>
SequentialAnd [optional]
Expand Down
32 changes: 16 additions & 16 deletions rocca/agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def get_vardecl(cogscm):
Given a cognitive schematic of that format
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
And (or SimultaneousAnd?)
Expand All @@ -272,7 +272,7 @@ def get_vardecl(cogscm):
return <vardecl>.
If the cognitive schematic is an PredictiveImplicationLink then
If the cognitive schematic is an BackPredictiveImplicationLink then
return an empty VariableSet.
"""
Expand Down Expand Up @@ -301,7 +301,7 @@ def get_context(cogscm):
For instance given a cognitive schematic of that format
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
And
Expand All @@ -313,7 +313,7 @@ def get_context(cogscm):
Another example, given a cognitive schematic of that format
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
SequentialAnd
Expand Down Expand Up @@ -359,13 +359,13 @@ def is_scope(atom):
def is_predictive_implication(atom):
"""Return True iff the atom is a predictive implication link."""

return is_a(atom.type, get_type("PredictiveImplicationLink"))
return is_a(atom.type, get_type("BackPredictiveImplicationLink"))


def is_predictive_implication_scope(atom):
"""Return True iff the atom is a predictive implication scope link."""

return is_a(atom.type, get_type("PredictiveImplicationScopeLink"))
return is_a(atom.type, get_type("BackPredictiveImplicationScopeLink"))


def is_and(atom):
Expand All @@ -377,11 +377,11 @@ def is_and(atom):
def is_sequential_and(atom):
"""Return True iff atom is a sequential and.
Also for now we use AltSequentialAndLink.
Also for now we use BackSequentialAndLink.
"""

return is_a(atom.type, get_type("AltSequentialAndLink"))
return is_a(atom.type, get_type("BackSequentialAndLink"))


def is_execution(atom):
Expand Down Expand Up @@ -413,7 +413,7 @@ def get_antecedent(atom):
For instance is the cognitive schematics is represented by
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
<antecedent>
Expand All @@ -437,7 +437,7 @@ def get_succedent(atom):
For instance is the cognitive schematics is represented by
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
<antecedent>
Expand All @@ -459,9 +459,9 @@ def get_succedent(atom):
def get_lag(atom):
"""Given an temporal atom, return its lag component.
For instance if it is a PredictiveImplicationScope
For instance if it is a BackPredictiveImplicationScope
PredictiveImplicationScope
BackPredictiveImplicationScope
<vardecl>
<lag>
<antecedent>
Expand Down Expand Up @@ -494,7 +494,7 @@ def get_t0_clauses(antecedent):
For instance if the cognitive schematics has the following format
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<lag>
SequentialAnd
Expand Down Expand Up @@ -599,7 +599,7 @@ def get_total_lag(atom):
For instance if the atom is
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<lag-1>
SequentialAnd
Expand Down Expand Up @@ -628,7 +628,7 @@ def get_t0_execution(cogscm):
Given a cognitive schematic of that formats
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
And (or SimultaneousAnd?)
Expand Down Expand Up @@ -657,7 +657,7 @@ def get_context_actual_truth(atomspace, cogscm, i):
Given a cognitive schematic of that format
PredictiveImplicationScope <tv>
BackPredictiveImplicationScope <tv>
<vardecl>
<expiry>
And (or SimultaneousAnd?)
Expand Down

0 comments on commit a737b49

Please sign in to comment.