def CPSubsetSum(original_set, target_number): #{1,2,3,4}, 10 rule1 = ParseRule('s0 ->1 s1 p(u()n(M)s()) | m(M)') rule2 = ParseRule('s1 ->1 s2 o(X) | p(u(X)s(T)A)) t(T)') rule3 = ParseRule('s1 ->1 s3 o() | p(n()A)') rule4 = ParseRule('s1 ->+ s1 p(u(Xm(Y))n(Z)s(SY)) | p(u(X)n(Zm(Y))s(S))') rule5 = ParseRule('s1 p(A) ->+ s1') #system #t = ParseTerm('t(10)'), when target_number = 10 t = ParseTerm('t(' + str(target_number) + ')') string_m = 'm(' for i in original_set: string_m += 'm(' + str(i) + ')' string_m += ')' #m = ParseTerm('m(m(1)m(2)m(3)m(4))'), when original_set = {1,2,3,4} m = ParseTerm(string_m) sys = CPSystem('s0') sys.AddRule(rule1) sys.AddRule(rule2) sys.AddRule(rule3) sys.AddRule(rule4) sys.AddRule(rule5) sys.AddTerm(t) sys.AddTerm(m) #Initial state of the cP system sys.SystemSnapshot() #sys.Run() sys.RunProfile( ) #if use this, need to run: python -m memory_profiler subsetsum_fullparser.py
rule1 = ParseRule('s1 v(v(R)Y) ->1 s2 s(u(Y)p(h(R)p()))') rule2 = ParseRule('s2 ->1 s3 q(P) | s(u()p(P))') rule3 = ParseRule( 's2 ->+ s2 s(u(Z)p(h(T)p(h(F)p(P)))) | s(u(v(T)Z)p(h(F)p(P)))') rule4 = ParseRule('s2 s(A) ->+ s2') sys1 = CPSystem('s1') sys1.AddRule(rule1) sys1.AddRule(rule2) sys1.AddRule(rule3) sys1.AddRule(rule4) size = 5 #size = 6 #size = 7 for i in range(1, size): for j in range(1, size): temp_term = ParseTerm('e(f(' + str(i) + ')t(' + str(j) + '))') sys1.AddTerm(temp_term) #sys.stdout = open('4.txt', 'w') #sys.stdout = open('5.txt', 'w') #sys.stdout = open('6.txt', 'w') sys1.AddTerm(ParseTerm('v(v(1)v(2)v(3)v(4))')) #sys1.AddTerm(ParseTerm('v(v(1)v(2)v(3)v(4)v(5)')) #sys1.AddTerm(ParseTerm('v(v(1)v(2)v(3)v(4)v(5)v(6))')) sys1.SystemSnapshot() sys1.Run() #sys1.RunProfile()
from cpparser import ParseTerm from toolbox import UnifyCompound from toolbox import PrintUnifyCompound import sys import time #pattern = ParseTerm('p(ABBC)') #term = ParseTerm('p(s(1)s(1)s(1)2d(2))') #sys.stdout = open('unification.txt', 'w') #pattern = ParseTerm('t( a(X) b(XY) c(W) d(2Ze(f()g()h())) ) ') #term = ParseTerm('t( a(4) d(6e(f()g()h())) b(4a()b()c()) c(f()) ) ') #pattern = ParseTerm('t(aXg(Y))') #term = ParseTerm('t(af(b())g(c())) ') pattern = ParseTerm('t(a(XYZ)b(WAB))') term = ParseTerm('t(a(4)b(2abc)) ') #pattern = ParseTerm('t(a(ABCD)b(WXYZ))') #term = ParseTerm('t(a(11)b(7)) ') print('Pattern:', pattern.ToString()) print('Term:', term.ToString()) t1 = int(round(time.time() * 1000)) x = UnifyCompound(pattern, term) t2 = int(round(time.time() * 1000)) PrintUnifyCompound(x) print('Number of unifiers: ', len(x)) print('Unification time: ', t2 - t1, 'milliseconds')
from compoundterm import CPTerm from compoundrule import CPRule from compoundcpsystem import CPSystem from cpparser import ParseTerm from cpparser import ParseRule rule1 = ParseRule('s1 ->+ s2 b(X) | a(X)') rule2 = ParseRule('s2 b(XY1) ->+ s3 | a(X)') sys1 = CPSystem('s1') sys1.AddRule(rule1) sys1.AddRule(rule2) sys1.AddTerm(ParseTerm('a(7)')) sys1.AddTerm(ParseTerm('a(19)')) sys1.AddTerm(ParseTerm('a(11)')) sys1.AddTerm(ParseTerm('a(87)')) sys1.AddTerm(ParseTerm('a(7)')) sys1.SystemSnapshot() sys1.Run()
from compoundterm import CPTerm from compoundrule import CPRule from compoundcpsystem import CPSystem from cpparser import ParseTerm from cpparser import ParseRule import sys rule1 = ParseRule('s0 a(XYc(Z)) b(Xd(ZW)) ->+ s1 x(X) y(Y) z(Z) w(W)') sys1 = CPSystem('s0') sys1.AddRule(rule1) sys1.AddTerm(ParseTerm('a(g() h() c(h()) )')) sys1.AddTerm(ParseTerm('b( f() d(j() k()) )')) sys1.AddTerm(ParseTerm('a( g() j() c(j()) )')) sys1.AddTerm(ParseTerm('b( g() d(j() k()) )')) sys1.Run() rule2 = ParseRule('s0 a(XYc(ZW)) b(Xd(Z)) ->+ s1 x(X) y(Y) z(Z) w(W)') sys2 = CPSystem('s0') sys2.AddRule(rule2) sys2.AddTerm(ParseTerm('a(g() h() c(h()k()) )')) sys2.AddTerm(ParseTerm('b( f() d(h()) )')) sys2.AddTerm(ParseTerm('a( g() j() c(j()k()) )')) sys2.AddTerm(ParseTerm('b( g() d(j()) )')) sys2.Run()
from compoundterm import CPTerm from compoundrule import CPRule from compoundcpsystem import CPSystem from cpparser import ParseTerm from cpparser import ParseRule rule1 = ParseRule('s1 a(XY1) ->+ s1 a(Y1) | a(X)') rule2 = ParseRule('s1 a(X) a(X) ->1 s2 b(X)') sys1 = CPSystem('s1') sys1.AddRule(rule1) sys1.AddRule(rule2) sys1.AddTerm(ParseTerm('a(144)')) sys1.AddTerm(ParseTerm('a(88)')) sys1.SystemSnapshot() sys1.Run()