first commit

This commit is contained in:
vandechat96
2023-02-28 22:29:48 +01:00
commit 94a5a22062
9 changed files with 153 additions and 0 deletions

28
pip1/3DMPsolver.lp Normal file
View File

@@ -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).

7
pip1/3DMinstance.db Normal file
View File

@@ -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).

23
pip1/3DMsolver.lp Normal file
View File

@@ -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.

10
pip1/3DMsolver_short.lp Normal file
View File

@@ -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).

49
pip2/dbms.lp Normal file
View File

@@ -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).

8
pip2/instance.db Normal file
View File

@@ -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).

5
pip2/instance2.db Normal file
View File

@@ -0,0 +1,5 @@
d2(r,1,2).
d2(r,1,3).
d2(r,1,4).
q2(r,x,y).
q2(r,y,z).

11
pip2/instance3.db Normal file
View File

@@ -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).

12
queen.lp Normal file
View File

@@ -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) }.