From 94a5a220625c6a0a0adff8b21bd0cebc4c6d026f Mon Sep 17 00:00:00 2001 From: vandechat96 Date: Tue, 28 Feb 2023 22:29:48 +0100 Subject: [PATCH] first commit --- pip1/3DMPsolver.lp | 28 +++++++++++++++++++++++ pip1/3DMinstance.db | 7 ++++++ pip1/3DMsolver.lp | 23 +++++++++++++++++++ pip1/3DMsolver_short.lp | 10 +++++++++ pip2/dbms.lp | 49 +++++++++++++++++++++++++++++++++++++++++ pip2/instance.db | 8 +++++++ pip2/instance2.db | 5 +++++ pip2/instance3.db | 11 +++++++++ queen.lp | 12 ++++++++++ 9 files changed, 153 insertions(+) create mode 100644 pip1/3DMPsolver.lp create mode 100644 pip1/3DMinstance.db create mode 100644 pip1/3DMsolver.lp create mode 100644 pip1/3DMsolver_short.lp create mode 100644 pip2/dbms.lp create mode 100644 pip2/instance.db create mode 100644 pip2/instance2.db create mode 100644 pip2/instance3.db create mode 100644 queen.lp diff --git a/pip1/3DMPsolver.lp b/pip1/3DMPsolver.lp new file mode 100644 index 0000000..bc00490 --- /dev/null +++ b/pip1/3DMPsolver.lp @@ -0,0 +1,28 @@ +%#show path/7. +#show satisfiable/0. +#show unsatisfiable/0. +dom(a). dom(b). dom(c). +%tri(a,a,a). tri(a,b,c). +%tri(b,b,b). tri(b,c,a). +%tri(c,c,c). tri(c,a,b). +tri(a,b,c). tri(a,c,b). +tri(b,a,a). tri(b,b,b). +tri(c,a,c). tri(c,c,a). + +same(X,Y,Z, X,Y,Z) :- tri(X,Y,Z). + +% There is an edge if two triplet have a common letter +edge(X,Y,Z, X1,Y1,Z1) :- tri(X,Y,Z), tri(X1,Y1,Z1), not same(X,Y,Z, X1,Y1,Z1), X = X1. +edge(X,Y,Z, X1,Y1,Z1) :- tri(X,Y,Z), tri(X1,Y1,Z1), not same(X,Y,Z, X1,Y1,Z1), Y = Y1. +edge(X,Y,Z, X1,Y1,Z1) :- tri(X,Y,Z), tri(X1,Y1,Z1), not same(X,Y,Z, X1,Y1,Z1), Z = Z1. + +%path(X,Y,Z, XF,YF,ZF) :- edge(X,Y,Z, X1,Y1,Z1), edge(X1,Y1,Z1, X2,Y2,Z2), edge(X2,Y2,Z2, XF,YF,ZF), not same(X1,Y1,Z1, XF,YF,ZF), not same(X,Y,Z, X2,Y2,Z2). + +% The 1 and 0 are used to diferentiate the two type of vertice +% If it is 0 the path stoped in the first type and must then go to the other type and vice versa +path(X,Y,Z, XF,YF,ZF, 1) :- edge(X,Y,Z, XF,YF,ZF). +path(X,Y,Z, XF,YF,ZF, 0) :- path(X,Y,Z, X1,Y1,Z1, 1), edge(X1,Y1,Z1, XF,YF,ZF). +path(X,Y,Z, XF,YF,ZF, 1) :- path(X,Y,Z, X1,Y1,Z1, 0), edge(X1,Y1,Z1, XF,YF,ZF). + +satisfiable() :- tri(X,Y,Z), not path(X,Y,Z, X,Y,Z, 1). +unsatisfiable() :- tri(X,Y,Z), path(X,Y,Z, X,Y,Z, 1). diff --git a/pip1/3DMinstance.db b/pip1/3DMinstance.db new file mode 100644 index 0000000..60a1c14 --- /dev/null +++ b/pip1/3DMinstance.db @@ -0,0 +1,7 @@ +dom(a). dom(b). dom(c). %dom(d). +%tri(X,Y,Z) :- dom(X), dom(Y), dom(Z). +tri(a,a,a). tri(a,b,c). tri(a,c,b). tri(b,b,b). +tri(b,c,a). tri(c,a,b). tri(c,b,a). tri(c,c,c). +%dom(0). dom(1). +%tri(0,0,0). tri(0,1,0). tri(0,1,1). tri(1,1,0). + diff --git a/pip1/3DMsolver.lp b/pip1/3DMsolver.lp new file mode 100644 index 0000000..b212598 --- /dev/null +++ b/pip1/3DMsolver.lp @@ -0,0 +1,23 @@ +#show match/3. + +% Let the choice to have a triplet in the match or not +unmatch(X,Y,Z) :- tri(X,Y,Z), not match(X,Y,Z). +match(X,Y,Z) :- tri(X,Y,Z), not unmatch(X,Y,Z). + +% every letter in domain must be used +:- dom(X), not match(X,_,_). + +% cannot have different matching with same first letter +:- match(X,Y,Z), match(X,Y1,Z1), Z != Z1. +:- match(X,Y,Z), match(X,Y1,Z1), Y1 != Y. + +% cannot have different matching with same second letter +:- match(X,Y,Z), match(X1,Y,Z1), X1 != X. +:- match(X,Y,Z), match(X1,Y,Z1), Z != Z1. + +% cannot have different matching with same third letter +:- match(X,Y,Z), match(X1,Y1,Z), X != X1. +:- match(X,Y,Z), match(X1,Y1,Z), Y1 != Y. + + + diff --git a/pip1/3DMsolver_short.lp b/pip1/3DMsolver_short.lp new file mode 100644 index 0000000..7897c67 --- /dev/null +++ b/pip1/3DMsolver_short.lp @@ -0,0 +1,10 @@ +#show match/3. + +% chose exactly S match in the possble triplets +% where S is the number of different letters in the domain +{ match(X,Y,Z) : tri(X,Y,Z)} = S :- S = #count {E : dom(E)}. + +% each letter in the domain must be in each column of the matching +:- dom(X), not match(X,_,_). +:- dom(X), not match(_,X,_). +:- dom(X), not match(_,_,X). diff --git a/pip2/dbms.lp b/pip2/dbms.lp new file mode 100644 index 0000000..2d675d8 --- /dev/null +++ b/pip2/dbms.lp @@ -0,0 +1,49 @@ +#show val/2. + +% var(X) represent the different variable defined in the query +var(X) :- q1(_,X). + +var(X) :- q2(_,X,_). +var(X) :- q2(_,_,X). + +var(X) :- q3(_,X,_,_). +var(X) :- q3(_,_,X,_). +var(X) :- q3(_,_,_,X). + +var(X) :- q4(_,X,_,_,_). +var(X) :- q4(_,_,X,_,_). +var(X) :- q4(_,_,_,X,_). +var(X) :- q4(_,_,_,_,X). + +% const(X) are the constants present in the facts +const(X) :- d1(_,X). + +const(X) :- d2(_,X,_). +const(X) :- d2(_,_,X). + +const(X) :- d3(_,X,_,_). +const(X) :- d3(_,_,X,_). +const(X) :- d3(_,_,_,X). + +const(X) :- d4(_,X,_,_,_). +const(X) :- d4(_,_,X,_,_). +const(X) :- d4(_,_,_,X,_). +const(X) :- d4(_,_,_,_,X). + +% choose som valuation for a variable X to a constant Y +{val(X,Y) : var(X), const(Y)}. + +% there can only be one valuation for each varibale +:- val(X,Y), val(X,Y1), Y != Y1. +% each variable must be used +:- var(X), not val(X,_). + +% if there are some valuations and a querry using the variable of those valuations +% then ther must be a fact in the database that prove it by using the constant of the valuations +:- val(Var,Val), q1(Rel,Var), not d1(Rel, Val). + +:- val(Var1,Val1), val(Var2, Val2), q2(Rel,Var1, Var2), not d2(Rel, Val1, Val2). + +:- val(Var1, Val1), val(Var2, Val2), val(Var3, Val3), q3(Rel,Var1,Var2,Var3), not d3(Rel,Val1,Val2,Val3). + +:- val(Var1, Val1), val(Var2, Val2), val(Var3, Val3), val(Var4, Val4), q4(Rel,Var1,Var2,Var3, Var4), not d4(Rel,Val1,Val2,Val3, Val4). diff --git a/pip2/instance.db b/pip2/instance.db new file mode 100644 index 0000000..b941ead --- /dev/null +++ b/pip2/instance.db @@ -0,0 +1,8 @@ +d2(r,1,2). +d2(r,2,1). +d2(s,1,1). +d3(t,2,1,2). +q2(r,x,y). +q2(r,y,x). +q2(s,x,x). +q3(t,y,z,y). diff --git a/pip2/instance2.db b/pip2/instance2.db new file mode 100644 index 0000000..dae66d5 --- /dev/null +++ b/pip2/instance2.db @@ -0,0 +1,5 @@ +d2(r,1,2). +d2(r,1,3). +d2(r,1,4). +q2(r,x,y). +q2(r,y,z). diff --git a/pip2/instance3.db b/pip2/instance3.db new file mode 100644 index 0000000..8597ee7 --- /dev/null +++ b/pip2/instance3.db @@ -0,0 +1,11 @@ +q2(e,1,2). q2(e,1,3). q2(e,2,3). +q2(e,3,4). +q2(e,4,2). + +d2(e,red,blue). +d2(e,red,green). +d2(e,green,blue). +d2(e,green,red). +d2(e,blue,green). +d2(e,blue,red). + diff --git a/queen.lp b/queen.lp new file mode 100644 index 0000000..292c5f5 --- /dev/null +++ b/queen.lp @@ -0,0 +1,12 @@ +% place queens on the chess board +{ q(1..n,1..n) }. +% exactly 1 queen per row/column +:- X = 1..n, not #count{ Y : q(X,Y) } = 1. +:- Y = 1..n, not #count{ X : q(X,Y) } = 1. +% pre-calculate the diagonals +d1(X,Y,X-Y+n) :- X = 1..n, Y = 1..n. +d2(X,Y,X+Y-1) :- X = 1..n, Y = 1..n. +% at most one queen per diagonal +:- D = 1..n*2-1, 2 { q(X,Y) : d1(X,Y,D) }. +:- D = 1..n*2-1, 2 { q(X,Y) : d2(X,Y,D) }. +