Пример #1
0
def test3():
    parent = Relation()
    male = Relation()

    def son(father, boy):
        return conde((parent(father, boy), male(boy)))

    fact(parent, "Abraham", "Isaac")
    fact(male, "Abraham")
    fact(male, "Isaac")

    x = var()
    assert ("Isaac",) == run(0, x, son("Abraham", x))
Пример #2
0
from logpy import run, var, fact
from logpy.assoccomm import eq_assoccomm as eq
from logpy.assoccomm import commutative, associative

# Define some dummy Operationss
add = 'add'
mul = 'mul'
# Declare that these ops are commutative using the facts system
fact(commutative, mul)
fact(commutative, add)
fact(associative, mul)
fact(associative, add)

# Define some wild variables
x, y = var('x'), var('y')

# Two expressions to match
pattern = (mul, (add, 1, x), y)                # (1 + x) * y
expr    = (mul, 2, (add, 3, 1))                # 2 * (3 + 1)
print run(0, (x,y), eq(pattern, expr))         # prints ((3, 2),) meaning
                                               #   x matches to 3
                                               #   y matches to 2
Пример #3
0
from logpy import run, var, fact
import logpy.assoccomm as la

# Define mathematical operations
add = 'addition'
mul = 'multiplication'

# Declare that these operations are commutative
# using the facts system
fact(la.commutative, mul)
fact(la.commutative, add)
fact(la.associative, mul)
fact(la.associative, add)

# Define some variables
a, b, c = var('a'), var('b'), var('c')

# Generate expressions
expression_orig = (add, (mul, 3, -2), (mul, (add, 1, (mul, 2, 3)), -1))
expression1 = (add, (mul, (add, 1, (mul, 2, a)), b), (mul, 3, c))
expression2 = (add, (mul, c, 3), (mul, b, (add, (mul, 2, a), 1)))
expression3 = (add, (add, (mul, (mul, 2, a), b), b), (mul, 3, c))

# Compare expressions
print(run(0, (a, b, c), la.eq_assoccomm(expression1, expression_orig)))
print(run(0, (a, b, c), la.eq_assoccomm(expression2, expression_orig)))
print(run(0, (a, b, c), la.eq_assoccomm(expression3, expression_orig)))
Пример #4
0
from logpy import run, fact, eq, Relation, var

adjacent = Relation()
coastal = Relation()

file_coastal = 'coastal_states.txt'
file_adjacent = 'adjacent_states.txt'

# Read the file containing the coastal states
with open(file_coastal, 'r') as f:
    line = f.read()
    coastal_states = line.split(',')

# Add the info to the fact base
for state in coastal_states:
    fact(coastal, state)

# Read the file containing the coastal states
with open(file_adjacent, 'r') as f:
    adjlist = [line.strip().split(',') for line in f if line and line[0].isalpha()]

# Add the info to the fact base
for L in adjlist:
    head, tail = L[0], L[1:]
    for state in tail:
        fact(adjacent, head, state)

# Initialize the variables
x = var()
y = var()
Пример #5
0
    ('william', 'soni'),
    ('david', 'ben'),
    ('david', 'nisha'),
    ('david', 'julie'),
    ('david', 'neil'),
    ('david', 'peter'),
    ('adam', 'richa'),
)

facts(Mother, ('megan', 'william'), ('megan', 'david'), ('megan', 'adam'),
      ('emma', 'chris'), ('emma', 'krish'), ('emma', 'stephaine'),
      ('olivia', 'wayne'), ('olivia', 'nisha'), ('olivia', 'julie'),
      ('olivia', 'neil'), ('olivia', 'peter'), ('lily', 'sophia'),
      ('lily', 'richa'))

fact(Male, 'john')
fact(Male, 'william')
fact(Male, 'david')
fact(Male, 'adam')
fact(Male, 'rahul')
fact(Male, 'ben')
fact(Male, 'neil')
fact(Male, 'peter')

fact(Female, 'megan')
fact(Female, 'emma')
fact(Female, 'nisha')
fact(Female, 'julie')
fact(Female, 'olivia')
fact(Female, 'richa')
fact(Female, 'lily')
Пример #6
0
This example builds a small database of the US states.

The `adjacency` relation expresses which states border each other
The `coastal` relation expresses which states border the ocean
"""
from logpy import run, fact, eq, Relation, var

adjacent = Relation()
coastal = Relation()

coastal_states = 'WA,OR,CA,TX,LA,MS,AL,GA,FL,SC,NC,VA,MD,DE,NJ,NY,CT,RI,MA,ME,NH,AK,HI'.split(
    ',')

for state in coastal_states:  # ['NY', 'NJ', 'CT', ...]
    fact(coastal, state)  # e.g. 'NY' is coastal

with open(
        'examples/data/adjacent-states.txt') as f:  # lines like 'CA,OR,NV,AZ'
    adjlist = [
        line.strip().split(',') for line in f if line and line[0].isalpha()
    ]

for L in adjlist:  # ['CA', 'OR', 'NV', 'AZ']
    head, tail = L[0], L[1:]  # 'CA', ['OR', 'NV', 'AZ']
    for state in tail:
        fact(adjacent, head, state)  # e.g. 'CA' is adjacent to 'OR',
        #      'CA' is adjacent to 'NV', etc...

x = var()
y = var()
Пример #7
0
from computations.core import Computation, CompositeComputation, Identity
from term import termify
termify(Computation)
termify(Identity)
termify(CompositeComputation)

CompositeComputation._term_args = lambda self: self.computations
CompositeComputation._term_new = staticmethod(
        lambda args: CompositeComputation(*args))

from logpy import fact
from logpy.assoccomm import commutative, associative

fact(commutative, CompositeComputation)
fact(associative, CompositeComputation)
Пример #8
0
def start_main():
    def parents(x, y):
        return conde([Father(x, y)], [Mother(x, y)])

    def grandparents(x, y):
        z = var()
        return conde((parents(x, z), parents(z, y)))

    def grandfather(x, y):
        z = var()
        return conde((grandparents(x, y), Male(x)))

    def grandmother(x, y):
        z = var()
        return conde((grandparents(x, y), Female(x)))

    def uncle(m, y):
        z = var()
        x = var()
        return conde((Father(x, z), Father(x, m), Father(z, y), Male(m)))

    def sibling(x, y):
        z = var()
        return conde((parents(z, x), parents(z, y)))

    def brother(x, y):
        z = var()
        return conde((parents(z, x), parents(z, y), Male(x)))

    def sister(x, y):
        z = var()
        return conde((parents(z, x), parents(z, y), Female(x)))

    def aunty(x, y):
        z = var()
        return conde((uncle(z, y), Couple(z, x), Female(x)))

    def children(x, y):
        return ((parents(x, y)))

    def son(x, y):
        return ((parents(x, y), Male(y)))

    def daughter(x, y):
        return ((parents(x, y), Female(y)))

    global choice

    Father = Relation()

    Mother = Relation()

    Male = Relation()

    Female = Relation()

    Couple = Relation()

    facts(Couple, ('john', 'megan'),
          ('william', 'emma'),
          ('david', 'olivia'),
          ('adam', 'lily'))
    facts(Father, ('john', 'william'),
          ('john', 'david'),
          ('john', 'adam'),
          ('william', 'chris'),
          ('william', 'stephaine'),
          ('david', 'wayne'),
          ('david', 'tiffany'),
          ('david', 'julie'),
          ('david', 'neil'),
          ('david', 'peter'),
          ('adam', 'sophia'))

    facts(Mother, ('megan', 'william'),
          ('megan', 'david'),
          ('megan', 'adam'),
          ('emma', 'chris'),
          ('emma', 'stephaine'),
          ('olivia', 'wayne'),
          ('olivia', 'tiffany'),
          ('olivia', 'julie'),
          ('olivia', 'neil'),
          ('olivia', 'peter'),
          ('lily', 'sophia'))

    fact(Male, 'john')
    fact(Male, 'william')
    fact(Male, 'david')
    fact(Male, 'adam')
    fact(Male, 'chris')
    fact(Male, 'wayne')
    fact(Male, 'neil')
    fact(Male, 'peter')

    fact(Female, 'megan')
    fact(Female, 'emma')
    fact(Female, 'tiffany')
    fact(Female, 'julie')
    fact(Female, 'olivia')
    fact(Female, 'sophia')
    fact(Female, 'lily')
    fact(Female, 'stephaine')

    x = var()
    y = var()
    z = var()
    #
    # print(
    #     color.Bold + "---------------------------------Welcome to Relation Predicting Program------------------------------" + color.End + "\n\n")
    # print(
    #     color.Bold + "-----------------------------------------------------------------------------Created by:" + color.End,
    #     color.Darkcyan + color.Bold + "RUPESH YADAV" + color.End, "\n\n")
    # print("-----------------------------------------------------------------------------------------------------")
    # print(color.Bold + color.Blue + "Enter The Name Below From Above To Know There Realtives" + color.End)
    # print("-----------------------------------------------------------------------------------------------------")
    # print(color.Red + "1)JOHN\n2)WILLIAM\n3)DAVID\n4)ADAM\n5)CHRIS\n6)WAYNE\n7)NEIL\n8)PETER\n" + color.End)
    # print(
    #     color.Green + "9)MEGAN\n10)EMMA\n11)TIFFANY\n12)JULIE\n13)OLIVIA\n14)SOPHIA\n15)LILY\n16)STEPHAINE" + color.End)

    NameList = ['john', 'megan',
                'william', 'emma',
                'david', 'olivia',
                'adam', 'lily',
                'peter', 'neil',
                'sophia', 'julie',
                'tiffany', 'stephaine',
                'wayne', 'chris']

    name = input()
    FakeName = name.upper()
    name = name.lower()
    if name in NameList:
        print(color.Red + "Which Realtion Of" + color.End,
              color.Cyan + color.Bold + color.Underline + FakeName + color.End,
              color.Red + "You want to know?" + color.End)

        print(
            color.Blue + "1)PARENT\n2)SIBLING\n3)GRANDPARENT\n)4)UNCLE\n5)AUNTY\n6)CHILDREN\n7)COUPLE\n\n" + color.End)

        Relat = input()
        Relat = Relat.lower()
        if Relat == 'parent':
            print(
                color.Yellow + "Press 1 To Know The Name of Both Parent\nPress 2 To Know The Name Of Mother\nPress 3 To Know The Name Of Father" + color.End)
            option = int(input())
            if option == 1:
                out = (run(0, x, parents(x, name)))
            elif option == 2:
                out = (run(0, x, Mother(x, name)))
            else:
                out = (run(1, x, Father(x, name)))
            if len(out) == 0:
                print(color.Cyan + color.Bold + color.Underline + FakeName + color.End,
                      color.Green + "Parent Details Not In DataBase---SORRY" + color.End)
            else:
                print(out)
        elif Relat == 'sibling':
            print(
                color.Yellow + "Press 1 To Know The Name of Both Gender Sibling\nPress 2 To Know The Name Of Sisters\nPress 3 To Know The Name Of Brother" + color.End)
            option = int(input())
            if option == 1:
                out = (run(0, x, sibling(x, name)))
            elif option == 2:
                out = (run(1, x, sister(x, name)))
            else:
                out = (run(1, x, brother(x, name)))
            list1 = list(out)

            if name in list1:
                list1.remove(name)

            if len(list1) == 0:
                print(color.Cyan + color.Bold + color.Underline + FakeName + color.End,
                      color.Green + "Does not have Entered type sibling ---SORRY" + color.End)
            else:
                print(list1)
        elif Relat == 'grandparent':
            print(
                color.Yellow + "Press 1 To Know The Name of Both Grandmaa and Grandpaa\nPress 2 To Know The Name Of GrandFather\nPress 3 To Know The Name Of GrandMother" + color.End)
            option = int(input())
            if option == 1:
                out = (run(0, x, grandparents(x, name)))
            elif option == 2:
                out = (run(1, x, grandfather(x, name)))
            else:
                out = (run(1, x, grandmother(x, name)))
            list1 = list(out)

            if name in list1:
                list1.remove(name)

            if len(list1) == 0:
                print(color.Cyan + color.Bold + color.Underline + FakeName + color.End,
                      color.Green + "Does not have GrandParent data ---SORRY" + color.End)
            else:
                print(list1)
        elif Relat == 'uncle':
            out1 = run(1, x, Father(x, name))
            out = run(0, x, uncle(x, name))
            a = out1[0]
            list1 = list(out)
            if a in list1:
                list1.remove(a)
            print(list1)
        elif Relat == 'aunty':
            out = (run(0, x, aunty(x, name)))
            out1 = run(0, x, Mother(x, name))
            list1 = list(out)
            a = out1[0]
            if a in list1:
                list1.remove(a)
            print(list1)
        elif Relat == 'children':
            print(run(0, x, children(name, x)))
        elif Relat == 'couple':
            print(run(1, x, Couple(x, name)))
        else:
            print(color.Red + "The Relation You Is Wrong Or May Not Be In Database" + color.End)

    else:
        print(color.Blue + "The Name" + color.End, color.Red + color.Underline + FakeName + color.End,
              color.Blue + "You Have Entered Is Not In The DataBase" + color.End)
Пример #9
0
                # TODO
                return
            wtf = [v.shape[0]] + node.inputs[1:]
            return [v.reshape(wtf, ndim=len(wtf)).__getitem__(vidx[0])]


if 0:

    from logpy import fact, Relation

    x = tensor.vector('x')
    from theano.tensor import exp, log
    rules = [
            (x + x, 2*x),
            (x * x, x**2),
            (exp(log(x)), x),
            (log(exp(x)), x),
            ]
    vars = [x]
    reduces = Relation('reduces')
    for source, target in rules:
        fact(reduces, source, target)

    def simplify(expr):
        source, target = var(), var()
        with variables(*vars):
            result = run(0, target, (reduces, source, target),
                                    (eq, expr, source))
        return result

Пример #10
0
This example builds a small database of the US states.

The `adjacency` relation expresses which states border each other
The `coastal` relation expresses which states border the ocean
"""
from logpy import run, fact, eq, Relation, var

adjacent = Relation()
coastal  = Relation()


coastal_states = 'WA,OR,CA,TX,LA,MS,AL,GA,FL,SC,NC,VA,MD,DE,NJ,NY,CT,RI,MA,ME,NH,AK,HI'.split(',')

for state in coastal_states:        # ['NY', 'NJ', 'CT', ...]
    fact(coastal, state)            # e.g. 'NY' is coastal

with open('examples/data/adjacent-states.txt') as f: # lines like 'CA,OR,NV,AZ'
    adjlist = [line.strip().split(',') for line in f
                                       if line and line[0].isalpha()]

for L in adjlist:                   # ['CA', 'OR', 'NV', 'AZ']
    head, tail = L[0], L[1:]        # 'CA', ['OR', 'NV', 'AZ']
    for state in tail:
        fact(adjacent, head, state) # e.g. 'CA' is adjacent to 'OR',
                                    #      'CA' is adjacent to 'NV', etc...

x = var()
y = var()

print run(0, x, adjacent('CA', 'NY')) # is California adjacent to New York?