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
from compoundterm import CPTerm from compoundrule import CPRule from compoundcpsystem import CPSystem from cpparser import ParseTerm from cpparser import ParseRule import sys 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')
from cpparser import ParseTerm from cpparser import ParseRule ''' x = ParseTerm('p(u() n(M) s())') print(x) print(x.ToString()) x = ParseTerm('p(u() X Y 166 n(M) s())') print(x) print(x.ToString()) print(x.value) x = ParseTerm('t(38)') print(x.ToString()) ''' rule4 = ParseRule('s1 ->+ s1 p(u(Xm(Y))n(Z)s(SY)) | p(u(X)n(Zm(Y))s(S))') print(rule4.ToString())
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('s1 v(v(R)Y) ->1 s2 s(r(R)u(Y)p(h(R)p()))') rule2 = ParseRule( 's2 s(r(R)u()p(h(F)p(P))) ->+ s3 z(p(h(R)p(h(F)p(P)))) | e(f(F)t(R))') rule3 = ParseRule( 's2 ->+ s2 s(r(R)u(Z)p(h(T)p(h(F)p(P)))) | s(r(R)u(v(T)Z)p(h(F)p(P))) e(f(F)t(T))' ) rule4 = ParseRule('s2 s(A) ->+ s2') rule5 = ParseRule('s3 ->1 s4 q(P) | z(p(P))') sys1 = CPSystem('s1') sys1.AddRule(rule1) sys1.AddRule(rule2) sys1.AddRule(rule3) sys1.AddRule(rule4) sys1.AddRule(rule5) 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)
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()