Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minifying pynars requirements to allow running in constrained environ… #103

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Tests/test_RuleMap/test_sparse_lut.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from networkx.generators.random_graphs import fast_gnp_random_graph
from pynars.NARS.DataStructures._py.Link import LinkType
from pynars.NARS.RuleMap import Interface_SyllogisticRules, RuleMap
from pynars.Narsese import Budget
Expand Down
10 changes: 6 additions & 4 deletions pynars/NAL/Functions/ExtendedBooleanFunctions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import numpy as np
from functools import reduce
from statistics import mean
from operator import mul

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(mul, x, 1)
Or = lambda *x: 1 - reduce(mul, (1 - xi for xi in x), 1)
Average = lambda *x: mean(x)

def Scalar(x):
x = 0.5 + 4*(x-0.5)**3
Expand Down
2 changes: 0 additions & 2 deletions pynars/NARS/RuleMap/add_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from typing_extensions import Protocol
from collections import OrderedDict

from numpy import product

from pynars.Config import Enable
from pynars.NARS.RuleMap.Interface import Interface_CompositionalRules, Interface_SyllogisticRules, Interface_DecompositionalRules, Interface_TransformRules, Interface_ConditionalRules, Interface_TemporalRules, Interface_VariableRules
from pynars.Narsese import Copula, Task
Expand Down
11 changes: 7 additions & 4 deletions pynars/Narsese/_py/Compound.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty complicated. Is it not enough to check equality of 1.) term connector and 2.) constituent terms?

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ordered_set import OrderedSet
from typing import Set
from pynars.utils.tools import list_contains
import numpy as np
from pynars.Global import States


Expand Down Expand Up @@ -358,9 +357,13 @@ def equal(self, o: Type['Compound']) -> bool:
set2: Iterable[Term] = o.terms - self.terms
if len(set1) == len(set2) == 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to put the comment on the equality function. Couldnt we end on this line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that’s a question for @bowen-xu
I’m not sure what the original rationale for this code was.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for handling variable things.
(&&, <#x-->bird>, <#x-->animal>) and (&&, <robin-->bird>, <robin-->animal>) are "equal". There is a similar function identical() that compars the hash values of two terms.
The two compounds above has different hash values, and the difference is not empty, though they are "equal".

return True
eq_array = np.array([[term1.equal(term2)
for term2 in set2] for term1 in set1])
if np.prod(eq_array.sum(axis=0)) > 0 and np.prod(eq_array.sum(axis=1)) > 0:
# ChatGPT: directly returns the result of the logical AND condition,
# checking if all column sums and all row sums are greater than zero.
# This uses the built-in all() function to ensure every sum in each direction (column and row)
# is greater than zero. The zip(*eq_array) unpacks each row of eq_array into columns.
eq_array = [[term1.equal(term2)
for term2 in set2] for term1 in set1]
if all(sum(col) > 0 for col in zip(*eq_array)) and all(sum(row) > 0 for row in eq_array):
Copy link
Collaborator

@ccrock4t ccrock4t May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of numpy equivalence, I think in this case they are equivalent, but not in all cases. I can see a difference in the behaviors of the previous code vs this code if the "sum()" values could possibly result in negative integers/floats.

I think that will not be an issue here, since it seems like we are summing Boolean values here, which are interpreted as 1 and 0. Though summing Booleans in the first place seems iffy.

return True
else:
return False
Expand Down
1 change: 0 additions & 1 deletion pynars/Narsese/_py/Term.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Iterable, List, Set, Type
from enum import Enum
from pynars.utils.IndexVar import IndexVar
from numpy import prod
from ordered_set import OrderedSet
# from pynars.utils.tools import find_pos_with_pos, find_var_with_pos
from copy import copy, deepcopy
Expand Down
1 change: 0 additions & 1 deletion pynars/Narsese/_py/Truth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pynars.Config import Config
from typing import Type
import numpy as np

class Truth:
# analytic: Type['Truth']
Expand Down
2 changes: 0 additions & 2 deletions pynars/utils/IndexVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from ordered_set import OrderedSet
from bidict import bidict

from numpy import prod

class IntVar:
def __init__(self, num: int) -> None:
self.num = int(num)
Expand Down
4 changes: 2 additions & 2 deletions pynars/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def rand_seed(x: int):
import random
random.seed(x)

import numpy as np
np.random.seed(x)
# import numpy as np
# np.random.seed(x)

# if using pytorch, set its seed!
# # import torch
Expand Down
15 changes: 15 additions & 0 deletions requirements-min.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ale-py>=0.7.3
# git+git://github.com/mila-iqia/atari-representation-learning.git
# AutoROM>=0.4.2
# AutoROM.accept-rom-license>=0.4.2
bidict>=0.21.2
depq>=1.5.5
jstyleson>=0.0.2
lark==0.12.0
ordered-set>=4.0.2
sty>=1.0.0rc2
tqdm<=3.1.4
typing>=3.7.4.3
typing_extensions>=4.0.1
sparse-lut>=1.0.0
miniKanren>=1.0.3
Loading