Skip to content

Commit

Permalink
wip experimenting with structural inference taking one theorem at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
maxeeem committed Feb 14, 2024
1 parent c0db1c7 commit ff761d0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
23 changes: 23 additions & 0 deletions Tests/test_NAL/test_NAL4.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,29 @@ def test_structural_transformation_10(self):
)
pass

def test_other_stuff(self):
# tasks_derived = []
# # tasks_derived = process_two_premises(
# # '<<(*, $a, $b) --> is> <=> <$a --> $b>>.',
# # None
# # )
# tasks_derived.extend(process_two_premises(
# '<(*,cat,animal)-->is>.',
# '<cat-->animal>.'
# ))
# tasks_derived.extend(process_two_premises(
# '<(*,dog,animal)-->is>.',
# '<dog-->animal>?',
# 600
# ))
# # for t in tasks_derived: print(t)
# self.assertTrue(
# output_contains(tasks_derived, '<dog --> animal>. %1.00;0.29%')
# or
# output_contains(tasks_derived, '<dog --> animal>. %1.00;0.40%')
# )
pass

if __name__ == '__main__':

test_classes_to_run = [
Expand Down
4 changes: 2 additions & 2 deletions Tests/utils_for_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
nars = Reasoner(100, 100)
engine: GeneralEngine = nars.inference

NUM_CYCLES_MULTIPLIER = 4
def process_two_premises(premise1: str, premise2: str, n_cycle: int) -> List[Task]:
NUM_CYCLES_MULTIPLIER = 10
def process_two_premises(premise1: str, premise2: str, n_cycle: int = 0) -> List[Task]:
''''''
tasks_all_cycles = []

Expand Down
28 changes: 20 additions & 8 deletions pynars/NARS/Control/Reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
from ..InferenceEngine.KanrenEngine import util

class Reasoner:
# avg_inference = 0
# num_runs = 0
avg_inference = 0
num_runs = 0

all_theorems = Bag(100, 100, take_in_order=False)

def __init__(self, n_memory, capacity, config='./config.json', nal_rules={1, 2, 3, 4, 5, 6, 7, 8, 9}) -> None:
Expand Down Expand Up @@ -147,7 +148,7 @@ def consider(self, tasks_derived: List[Task]):
# t0 = time()
tasks_inference_derived = self.inference_step(concept)
tasks_derived.extend(tasks_inference_derived)
# t1 = time() - t0
# t1 = time() - t0 + 1e-6 # add epsilon to avoid division by 0
# self.avg_inference += (t1 - self.avg_inference) / self.num_runs
# print("inference:", 1 // self.avg_inference, "per second", f"({1//t1})")

Expand Down Expand Up @@ -329,7 +330,10 @@ def inference_step(self, concept: Concept):

results = []

results.extend(self.inference.inference_immediate(task.sentence))
res, cached = self.inference.inference_immediate(task.sentence)

if not cached:
results.extend(res)

for term, truth in results:
# TODO: how to properly handle stamp for immediate rules?
Expand Down Expand Up @@ -363,7 +367,7 @@ def inference_step(self, concept: Concept):

# t0 = time()
theorems = []
for _ in range(min(5, len(self.all_theorems))):
for _ in range(min(1, len(self.all_theorems))):
theorem = self.all_theorems.take(remove=True)
theorems.append(theorem)

Expand Down Expand Up @@ -467,8 +471,13 @@ def inference_step(self, concept: Concept):
# beleif_eternalized = belief # TODO: should it be added into the `tasks_derived`?

# t0 = time()

results, cached = self.inference.inference(task.sentence, belief.sentence)

results = []

res, cached = self.inference.inference(task.sentence, belief.sentence)

if not cached:
results.extend(res)

# t1 = time() - t0

Expand All @@ -478,7 +487,10 @@ def inference_step(self, concept: Concept):

# print("avg:", 1 // self._inference_time_avg, "per second")

results.extend(self.inference.inference_compositional(task.sentence, belief.sentence))
res, cached = self.inference.inference_compositional(task.sentence, belief.sentence)

if not cached:
results.extend(res)

# print(">>>", results)

Expand Down
8 changes: 7 additions & 1 deletion pynars/NARS/InferenceEngine/KanrenEngine/KanrenEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(self):
# INFERENCE (SYLLOGISTIC)
@cache_notify
def inference(self, t1: Sentence, t2: Sentence) -> list:
# print(f'Inference syllogistic\n{t1}\n{t2}')
results = []

t1e = variable_elimination(t2.term, t1.term)
Expand Down Expand Up @@ -142,8 +143,10 @@ def apply(self, rule, l1, l2):
#############
# IMMEDIATE #
#############


@cache_notify
def inference_immediate(self, t: Sentence):
# print(f'Inference immediate\n{t}')
results = []

l = logic(t.term)
Expand All @@ -165,6 +168,7 @@ def inference_immediate(self, t: Sentence):

@cache_notify
def inference_structural(self, t: Sentence, theorems = None):
# print(f'Inference structural\n{t}')
results = []

if not theorems:
Expand All @@ -191,7 +195,9 @@ def inference_structural(self, t: Sentence, theorems = None):
# COMPOSITIONAL #
#################

@cache_notify
def inference_compositional(self, t1: Sentence, t2: Sentence):
# print(f'Inference compositional\n{t1}\n{t2}')
results = []

common = set(t1.term.sub_terms).intersection(t2.term.sub_terms)
Expand Down

0 comments on commit ff761d0

Please sign in to comment.