Skip to content

Commit

Permalink
modify diff function and extended boolean operators
Browse files Browse the repository at this point in the history
  • Loading branch information
maxeeem committed Mar 12, 2024
1 parent 7fd58e7 commit a5ecf18
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Tests/test_NAL/test_NAL2.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_conversions_between_inheritance_and_similarity(self):
20
)
self.assertTrue(
output_contains(tasks_derived, '<bird <-> swan>. %0.10;0.081%')
output_contains(tasks_derived, '<bird <-> swan>. %0.10;0.81%')
)
pass

Expand Down
8 changes: 4 additions & 4 deletions Tests/test_NAL/test_NAL5.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_conversions_between_implication_and_equivalence(self):
tasks_derived = process_two_premises(
'<<robin --> [flying]> ==> <robin --> bird>>. %0.90;0.90%',
'<<robin --> bird> ==> <robin --> [flying]>>. %0.90;0.90%',
7
20
)
self.assertTrue(
output_contains(tasks_derived, '<<robin --> [flying]> <=> <robin --> bird>>. %0.81;0.81%')
Expand Down Expand Up @@ -982,11 +982,11 @@ def test_question_1(self):
)

def test_0(self):
rules, task, belief, concept, task_link, term_link, result1, result2 = rule_map_two_premises(
tasks_derived = process_two_premises(
'A.',
'<(&&, A, B)==>C>.',
'A.')
tasks_derived = [rule(task, belief, task_link, term_link) for rule in rules]
10
)
self.assertTrue(
output_contains(tasks_derived, '<B==>C>. %1.00;0.81%')
)
Expand Down
9 changes: 5 additions & 4 deletions pynars/NAL/Functions/ExtendedBooleanFunctions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import numpy as np
from functools import reduce
from statistics import mean

Not = lambda x: (1-x)

And = lambda *x: np.prod(x)
Or = lambda *x: 1 - np.prod(1-np.array(x))
Average = lambda *x: np.mean(x)
And = lambda *x: reduce(lambda a,b: a*b, x, 1)
Or = lambda *x: 1 - reduce(lambda a,b: a*(1-b), x, 1)
Average = lambda *x: mean(x)

def Scalar(x):
x = 0.5 + 4*(x-0.5)**3
Expand Down
3 changes: 2 additions & 1 deletion pynars/NARS/InferenceEngine/KanrenEngine/KanrenEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def __init__(self):
nal5_rules.append(rule)

for rule in nal3_rules:
# replace --> with ==> in NAL3 (except difference)
# replace --> with ==> and <-> with <=> in NAL3 (except difference)
if '(-,' not in rule and '(~,' not in rule:
rule = rule.replace('-->', '==>')
rule = rule.replace('<->', '<=>')

# replace | with || in NAL3 (except difference)
if '||' not in rule:
Expand Down
10 changes: 10 additions & 0 deletions pynars/NARS/InferenceEngine/KanrenEngine/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from .KanrenEngine import KanrenEngine
from .util import *

engine = KanrenEngine()
print('--')

x = parse('<A-->B>. %0.90;0.90%')
y = parse('<B-->A>. %0.90;0.90%')
rule = convert("{<S --> P>. <P --> S>} |- <S <-> P> .int")
conclusion = engine.apply(rule, logic(x.term), logic(y.term))[0]
print(truth_functions['int'](x.truth, y.truth))
print('')

exit()

engine = KanrenEngine()
print('--')
Expand Down
6 changes: 2 additions & 4 deletions pynars/NARS/InferenceEngine/KanrenEngine/nal-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ rules:
{<P --> M>. <M --> S>} |- <S --> P> .exe
{<M --> P>. <S --> M>} |- <P --> S> .exe'
# TODO: change to .res
{<S --> P>. <P --> S>} |- <S <-> P> .ded
{<S --> P>. <P --> S>} |- <P <-> S> .ded
nal2: |
{<M <-> P>. <S <-> M>} |- <S <-> P> .res
{<M <-> P>. <M <-> S>} |- <S <-> P> .res
Expand All @@ -32,6 +28,8 @@ rules:
{<P <-> M>. <M --> S>} |- <P --> S> .ana'
nal3: |
{<S --> P>. <P --> S>} |- <S <-> P> .int
{<S --> P>. <P --> S>} |- <P <-> S> .int
# 'composition
{<M --> T1>. <M --> T2>} |- <M --> (&, T1, T2)> .int
{<T1 --> M>. <T2 --> M>} |- <(|, T1, T2) --> M> .int
Expand Down
10 changes: 7 additions & 3 deletions pynars/NARS/InferenceEngine/KanrenEngine/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def diff(c):
difference = -1 # result of applying diff

def calculate_difference(l: Term, r: Term):
return (l - r) if l.contains(r) and not l.equal(r) else None
return (l - r) if not l.sub_terms.isdisjoint(r.sub_terms) and not l.equal(r) else None

def do_diff(t: Term):
nonlocal difference
Expand All @@ -343,7 +343,9 @@ def do_diff(t: Term):
return calculate_difference(*c.terms.terms)

# STATEMENT
elif type(c) is Statement and c.copula is Copula.Implication:
elif type(c) is Statement \
and c.copula is Copula.Implication \
or c.copula is Copula.Inheritance:
# check subject
subject = c.subject
if subject.is_compound:
Expand All @@ -366,7 +368,9 @@ def do_diff(t: Term):

# check predicate
predicate = c.predicate
if predicate.is_compound and difference is not None and difference != -1: # already failed one check
if predicate.is_compound and \
(c.copula is Copula.Inheritance or \
(difference is not None and difference != -1)): # already failed one check
if predicate.connector is Connector.ExtensionalDifference:
do_diff(predicate)
if difference is not None:
Expand Down
2 changes: 1 addition & 1 deletion pynars/Narsese/_py/Term.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, word, do_hashing=False, word_sorted=None, is_input=False) ->

@property
def sub_terms(self) -> Set[Type['Term']]:
return (self, *self._components) if self._components is not None else set((self, ))
return set((self, *self._components)) if self._components is not None else set((self, ))

@property
def components(self) ->Set[Type['Term']]:
Expand Down

0 comments on commit a5ecf18

Please sign in to comment.