I am trying to find all the paths in a graph with minimum distance using DLV. Say I have the following graph:
I am expecting to obtain the predicates (I hope I don't skip any):
path(a, b, 1), path(a, d, 1), path(a, e, 1), path(a, c, 2)
path(b, a, 1), path(b, c, 1), path(d, d, 2), path(b, e, 2)
path(c, b, 1), path(c, e, 1), path(c, a, 2), path(c, d, 3)
path(d, a, 1), path(d, b, 2), path(d, e, 2), path(d, c, 3)
path(e, a, 1), path(e, c, 1), path(e, d, 2), path(e, b, 2)
I assume that you can travel an arch both left or right. So, I tried the following:
path(X, Y, 1) :- arc(X, Y).
path(Y, X, 1) :- arc(X, Y).
path(X, Z, L) :- path(X, Y, M), path(Y, Z, N),
X!=Z,
L = M + N,
not path(X, Z, V), V < L, #int(V)
The idea of the third rule was to add 2 existing paths if they are not going back (X!=Z) and there is not already a path connecting the same edges with a shorter distance (not path(X, Z, V), V < L, #int(V)). I had to add #int(V) because otherwise the rule was not safe. I don't know if there is a better way of resolving this safety issue with an integer value.
When I run this code (with the flag -N=5 to set #maxint=5) I get paths that should not be there, for example, path(d,a,5). I don't know if the problem is with the #int(V) or something else but I wouldn't expect these paths to appear since I already have a path(d,a,1). Probably it is because of #int(V) but I can't figure out how to do this right.
Can anyone help me solve this? Thanks in advance.
Solution to the problem using lists to keep track of the path:
path(X, Y, [X, Y], 1) :- arc(X, Y).
path(Y, X, [Y, X], 1) :- arc(X, Y).
path(X, Z, P, D) :- path(X, Y, P1, D1),
path(Y, Z, P2, 1),
#insLast(P1, Z, P),
D = D1 + 1,
not #member(Z, P1).
shortest_path(X, Y, D) :- node(X), node(Y),
#min{L: path(X, Y, P, L)} = D.
Solution without the need of lists (with the help of CapelliC)
path(X, Y, 1) :- arc(X,Y).
path(Y, X, 1) :- arc(X,Y).
path(X, Y, D) :- path(X,Z,D0), arc(Z,Y),
#count{A: node(A)} = Max,
D0<Max, X != Y,
D = D0+1.
shorter_paths(X, Y, D) :- node(X), node(Y),
#min{L: path(X, Y, L)} = D.
Note that we need to define all nodes with a predicate node() and that the predicate arc() assumes that the edge of the graph is bidirectional.
examples/spaths.dl from DES distribution. See the commented code below... -
%
% Shortest Paths in a Graph
%
% Datalog Formulation
%
% Program: Shortest paths in a graph
% Author : Fernando Sáenz-Pérez
% Date : September, 2009
edge(a,b).
edge(a,c).
edge(b,a).
edge(b,d).
path(X,Y,1) :-
edge(X,Y).
path(X,Y,L) :-
path(X,Z,L0),
edge(Z,Y),
count(edge(A,B),Max),
L0<Max,
L is L0+1.
spaths(X,Y,L) :-
min(path(X,Y,Z),Z,L).
% Note that the following is not stratifiable in DES
%sp(X,Y,1) :-
% edge(X,Y).
%sp(X,Y,L) :-
% sp(X,Z,L0),
% not(shorter(X,Z,L0)),
% edge(Z,Y),
% L is L0+1.
%shorter(X,Y,L) :-
% sp(X,Y,L0),
% L0<L.
I have a system of coupled partial differential equations as shown in 1.
system of equations
If one writes the equations componentwise, one gets nine equations.
For simplicity, one can set $\rho$ and $h$ constant.
I put the equations into a mathematica notebook and used 'NDSolve'.
I counted a number of 268 derivatives in the system. However, I didnt find more than 87 conditions that made sence.
When I run the notebook, I get the message
NDSolve::pdord: Some of the functions have zero differential order, so the equations will be solved as a system of differential-algebraic equations. >>
LinearSolve::sing: Matrix SparseArray[Automatic,<<3>>] is singular. >>
NDSolve::mconly: For the method IDA, only machine real code is available. Unable to continue with complex values or beyond floating-point exceptions. >>
I understand the first error: There is no time derivative in $\phi$, so the system is strictly speaking a differential-algebraic-system.
After about 40 minutes, mathematica says:
No more memory available.
Mathematica kernel has shut down.
Try quitting other applications and then retry.
What can I change to reduce the memory cost, or -- better -- what can I change to solve the system more elegant?
Here is the mathematica code:
NDSolve[{
eqn1 == 0,
eqn2 == 0,
eqn3 == 0,
eqn4 == 0,
eqn5 == 0,
eqn6 == 0,
eqn7 == 0,
eqn8 == 0,
eqn9 == 0,
(*Initial Conditions*)
T[0, x, y, z] == 300,
p[0, x, y, z] == 101300,
phi[t, x, y, z] == 75 x /. t -> 0,
u1[0, x, y, z] == 0,
(* ect... *)
(*BOUNDARY CONDITIONS*)
phi[t, x, y, z] == 0 /. x -> 0,
phi[t, x, y, z] == 750 /. x -> 10,
phi[t, x, y, z] == 75 x /. y -> 0,
phi[t, x, y, z] == 75 x /. y -> 10,
phi[t, x, y, z] == 75 x /. z -> 0,
phi[t, x, y, z] == 75 x /. z -> 10,
\!\(
\*SubscriptBox[\(\[PartialD]\), \(y\)]\(phi[t, x, y, z]\)\) == 0 /.
y -> 0,
\!\(
\*SubscriptBox[\(\[PartialD]\), \(y\)]\(phi[t, x, y, z]\)\) == 0 /.
y -> 10,
(* ect..... *)
},
{p, u1, u2, u3, T, phi, a1, a2, a3},
{t, 0, 10}, {x, 0, 10}, {y, 0, 10}, {z, 0, 10}
]
And here is the Latex-code for the equations:
\begin{align}
%%% first
\frac{\partial \rho}{\partial t} + \vec{u} \cdot \nabla \rho + \rho \nabla \cdot \vec{u} &=0 \\
%%% second
\rho \frac{\partial \vec{u}}{\partial t}
+ \rho \vec{u} \cdot \nabla \vec{u} \pm \nabla p
+ \nabla \cdot \left[- \mu \left( \nabla \vec{u} + \nabla \vec{u}^t \right) \right] \nonumber
\\ -
\left[ \sigma
\left( - \nabla \phi - \frac{\partial \vec{A}}{\partial t} \right)
+ \vec{u} \times (\nabla \times \vec{A}) \right]
%
\times \left[ \nabla \times \vec{A} \right]
&= \vec{0} \\
%%% third
\rho \frac{\partial h}{\partial t}
+ \rho \vec{u} \cdot \nabla h
+ \nabla \cdot \nabla T
- \frac{\partial p}{\partial t}
- \overset{.}{Q_j} + \overset{.}{Q_r}
&= 0 \\
%%% fourth
\nabla \cdot \left[ \sigma
\left( - \nabla \phi - \frac{\partial \vec{A}}{\partial t} \right)
+ \vec{u} \times (\nabla \times \vec{A}) \right] &= 0 \\
%%% fifth
\frac{\partial \vec{A}}{\partial t} + \nabla \phi - \vec{u} \times \nabla \times \vec{A} - \eta \nabla ^2 \vec{A} &= \vec{0}
\end{align}
I add the plain mathematica code here:
Needs["NDSolve`FEM`"]
kB = N[ 8.6*10^-5 ];
AG = N[1.2*10^6 ];
mu0 = 1.257*10^(-6);
sigma = N[20.];
eta = 1/(mu0 * sigma);
rho = N[1.];
h = N[1672000.];
r = 1/ 287.058 ;
mu = N[1.];
kappah = N[5.];
cp = N[2.4];
lbb = NDSolve[{
0 == D[rho, t] + u1[t, x, y, z]*D[rho, x] +
u2[t, x, y, z]*D[rho, y] + u3[t, x, y, z]*D[rho, z] +
rho*(D[u1[t, x, y, z], x] + D[u2[t, x, y, z], y] +
D[u3[t, x, y, z], z]),
0 ==
rho*D[u1[t, x, y, z], t] +
rho*u1[t, x, y, z]*D[u1[t, x, y, z], x] - D[p[t, x, y, z], x] -
mu*D[D[u1[t, x, y, z], x], x] -
mu*D[D[u1[t, x, y, z], y], y] -
mu*D[D[u1[t, x, y, z], z], z] -
(((-sigma)*D[phi[t, x, y, z], y] -
sigma*D[a2[t, x, y, z], t] +
u3[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]))*(D[
a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
((-sigma)*D[phi[t, x, y, z], z] -
sigma*D[a3[t, x, y, z], t] +
u1[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]))*(D[
a1[t, x, y, z], z] - D[a3[t, x, y, z], x])),
0 ==
rho*D[u2[t, x, y, z], t] +
rho*u2[t, x, y, z]*D[u2[t, x, y, z], y] - D[p[t, x, y, z], y] -
mu*D[D[u2[t, x, y, z], x], x] -
mu*D[D[u2[t, x, y, z], y], y] -
mu*D[D[u2[t, x, y, z], z], z] -
(((-sigma)*D[phi[t, x, y, z], z] -
sigma*D[a3[t, x, y, z], t] +
u1[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]))*(D[
a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
(D[a2[t, x, y, z], x] -
D[a1[t, x, y, z], y])*((-sigma)*D[phi[t, x, y, z], x] -
sigma*D[a1[t, x, y, z], t] +
u2[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]))),
0 ==
rho*D[u3[t, x, y, z], t] +
rho*u3[t, x, y, z]*D[u3[t, x, y, z], z] - D[p[t, x, y, z], z] -
mu*D[D[u3[t, x, y, z], x], x] -
mu*D[D[u3[t, x, y, z], y], y] -
mu*D[D[u3[t, x, y, z], z], z] -
(((-sigma)*D[phi[t, x, y, z], x] -
sigma*D[a1[t, x, y, z], t] +
u2[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]))*(D[
a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
((-sigma)*D[phi[t, x, y, z], y] -
sigma*D[a2[t, x, y, z], t] +
u3[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]))*(D[
a3[t, x, y, z], y] - D[a2[t, x, y, z], z])),
0 == rho*D[h, t] +
rho*(u1[t, x, y, z]*D[h, x] + u2[t, x, y, z]*D[h, y] +
u3[t, x, y, z]*D[h, z]) +
kappah*(D[T[t, x, y, z], x, x] + D[D[T[t, x, y, z], y], y] +
D[D[T[t, x, y, z], z], z]) - D[p[t, x, y, z], t],
0 == D[
sigma*(-D[phi[t, x, y, z], x] - D[a1[t, x, y, z], t]) +
u2[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]),
x] + D[sigma*(-D[phi[t, x, y, z], y] - D[a2[t, x, y, z], t]) +
u3[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]),
y] +
D[sigma*(-D[phi[t, x, y, z], z] - D[a3[t, x, y, z], t]) +
u1[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]),
z],
0 ==
D[a1[t, x, y, z], t] + D[phi[t, x, y, z], x] -
u2[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
eta*D[D[a1[t, x, y, z], x], x],
0 ==
D[a2[t, x, y, z], t] + D[phi[t, x, y, z], y] -
u3[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
eta*D[D[a2[t, x, y, z], y], y],
0 ==
D[a3[t, x, y, z], t] + D[phi[t, x, y, z], z] -
u1[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
eta*D[D[a3[t, x, y, z], z], z], T[0, x, y, z] == 300,
p[0, x, y, z] == 101300,
phi[t, x, y, z] == 75*x /. t -> 0, u1[0, x, y, z] == 0,
u2[0, x, y, z] == 0, u3[0, x, y, z] == 0,
a1[0, x, y, z] == 0, a2[0, x, y, z] == 0,
a3[0, x, y, z] == 0, D[T[t, x, y, z], x] == 0 /. x -> 0,
D[T[t, x, y, z], x] == 0 /. x -> 10,
D[T[t, x, y, z], y] == 0 /. y -> 0,
D[T[t, x, y, z], y] == 0 /. y -> 10,
D[T[t, x, y, z], z] == 0 /. z -> 0,
D[T[t, x, y, z], z] == 0 /. z -> 10,
phi[t, x, y, z] == 0 /. x -> 0,
phi[t, x, y, z] == 750 /. x -> 10,
phi[t, x, y, z] == 75*x /. y -> 0,
phi[t, x, y, z] == 75*x /. y -> 10,
phi[t, x, y, z] == 75*x /. z -> 0,
phi[t, x, y, z] == 75*x /. z -> 10,
D[phi[t, 0, y, z], x] == (-sigma^(-1))*0.5*AG*T[t, 0, y, z]^2*
Exp[-4/(kB*T[t, 0, y, z])],
D[phi[t, 10, y, z], x] == (-sigma^(-1))*0.5*AG*T[t, 10, y, z]^2*
Exp[-4/(kB*T[t, 10, y, z])],
D[phi[t, x, y, z], y] == 0 /. y -> 0,
D[phi[t, x, y, z], y] == 0 /. y -> 10,
D[phi[t, x, y, z], z] == 0 /. z -> 0,
D[phi[t, x, y, z], z] == 0 /. z -> 10,
p[t, x, y, z] == 101300 /. x -> 0,
p[t, x, y, z] == 101300 /. x -> 10,
p[t, x, y, z] == 101300 /. y -> 0,
p[t, x, y, z] == 101300 /. y -> 10,
p[t, x, y, z] == 101300 /. z -> 0,
p[t, x, y, z] == 101300 /. z -> 10, u1[t, x, y, z] == 0 /. x -> 0,
u1[t, x, y, z] == 0 /. x -> 10,
D[u1[t, x, y, z], y] == 0 /. y -> 0,
D[u1[t, x, y, z], y] == 0 /. y -> 10,
D[u1[t, x, y, z], z] == 0 /. z -> 0,
D[u1[t, x, y, z], z] == 0 /. z -> 10,
u2[t, x, y, z] == 0 /. x -> 0,
u2[t, x, y, z] == 0 /. x -> 10,
D[u2[t, x, y, z], y] == 0 /. y -> 0,
D[u2[t, x, y, z], y] == 0 /. y -> 10,
D[u2[t, x, y, z], z] == 0 /. z -> 0,
D[u2[t, x, y, z], z] == 0 /. z -> 10,
u3[t, x, y, z] == 0 /. x -> 0, u3[t, x, y, z] == 0 /. x -> 10,
D[u3[t, x, y, z], y] == 0 /. y -> 0,
D[u3[t, x, y, z], y] == 0 /. y -> 10,
D[u3[t, x, y, z], z] == 0 /. z -> 0,
D[u3[t, x, y, z], z] == 0 /. z -> 10,
a1[t, x, y, z] == 0 /. x -> 0,
a1[t, x, y, z] == 0 /. x -> 10,
a1[t, x, y, z] == 0 /. y -> 0,
a1[t, x, y, z] == 0 /. y -> 10,
a1[t, x, y, z] == 0 /. z -> 0,
a1[t, x, y, z] == 0 /. z -> 10,
D[a1[t, x, y, z], x] == 0 /. x -> 0,
D[a1[t, x, y, z], x] == 0 /. x -> 10,
D[a1[t, x, y, z], y] == 0 /. y -> 0,
D[a1[t, x, y, z], y] == 0 /. y -> 10,
D[a1[t, x, y, z], z] == 0 /. z -> 0,
D[a1[t, x, y, z], z] == 0 /. z -> 10,
a2[t, x, y, z] == 0 /. x -> 0,
a2[t, x, y, z] == 0 /. x -> 10,
a2[t, x, y, z] == 0 /. y -> 0,
a2[t, x, y, z] == 0 /. y -> 10,
a2[t, x, y, z] == 0 /. z -> 0,
a2[t, x, y, z] == 0 /. z -> 10,
D[a2[t, x, y, z], x] == 0 /. x -> 0,
D[a2[t, x, y, z], x] == 0 /. x -> 10,
D[a2[t, x, y, z], y] == 0 /. y -> 0,
D[a2[t, x, y, z], y] == 0 /. y -> 10,
D[a2[t, x, y, z], z] == 0 /. z -> 0,
D[a2[t, x, y, z], z] == 0 /. z -> 10,
a3[t, x, y, z] == 0 /. x -> 0,
a3[t, x, y, z] == 0 /. x -> 10,
a3[t, x, y, z] == 0 /. y -> 0,
a3[t, x, y, z] == 0 /. y -> 10,
a3[t, x, y, z] == 0 /. z -> 0,
a3[t, x, y, z] == 0 /. z -> 10,
D[a3[t, x, y, z], x] == 0 /. x -> 0,
D[a3[t, x, y, z], x] == 0 /. x -> 10,
D[a3[t, x, y, z], y] == 0 /. y -> 0,
D[a3[t, x, y, z], y] == 0 /. y -> 10,
D[a3[t, x, y, z], z] == 0 /. z -> 0,
D[a3[t, x, y, z], z] == 0 /. z -> 10
},
{p, u1, u2, u3, T, phi, a1, a2, a3},
{t, 0, 10}, {x, 0, 10}, {y, 0, 10}, {z, 0, 10}]
Thanks a lot!