您好,我是answer-set-programming 的新手。我过去做了一点prolog!我正在尝试解决这个问题,我相信它可以用hamiltonian-cycle解决,让我知道你的意见。如果您不熟悉 ASP,您可以访问此站点 [clingo 和 gringo][1]。您可以使用此命令在终端中运行文件,clingo name_of_the_file.lp
或者clingo name_of_the_file.lp4
我在 Ubuntu 中对其进行了测试。
(这些是 .lp 或 .lp4 文件)我阅读并理解的第一个代码是具有 3 个结果的代码
% Generating part
% ---------------
% Cardinality constraint:
% For any ground fact cycle(X,Y) in the answer set:
% - there must be a corresponding edge(X,Y)
% - there must be exactly 1 of cycle(X,Y) for any X
% - there must be exactly 1 of cycle(X,Y) for any Y
{ cycle(X,Y) : edge(X,Y) } = 1 :- node(X).
{ cycle(X,Y) : edge(X,Y) } = 1 :- node(Y).
% Define
reached(Y) :- cycle(1,Y).
reached(Y) :- cycle(X,Y), reached(X).
% Testing part
% ------------
% It is a contradiction to that have a "node" that is not "reached"
:- node(Y), not reached(Y).
% Defining part
% -------------
% Nodes
node(1..6).
% (Directed) Edges
edge(1,(2;3;4)). edge(2,(4;5;6)). edge(3,(1;4;5)).
edge(4,(1;2)). edge(5,(3;4;6)). edge(6,(2;3;5)).
% Edge Costs cost(X,Y,Cost)
cost(1,2,2). cost(1,3,3). cost(1,4,1).
cost(2,4,2). cost(2,5,2). cost(2,6,4).
cost(3,1,3). cost(3,4,2). cost(3,5,2).
cost(4,1,1). cost(4,2,2).
cost(5,3,2). cost(5,4,2). cost(5,6,1).
cost(6,2,4). cost(6,3,3). cost(6,5,1).
% Optimize minimum cost and cycle
#minimize { C,X,Y : cycle(X,Y), cost(X,Y,C) }.
% Displaying part
% ---------------
#show cycle/2.
我得到这个结果:
clingo version 5.4.0
Reading from cycle_hamilt.lp4
Solving...
Answer: 1
cycle(1,4) cycle(4,2) cycle(3,1) cycle(2,6) cycle(6,5) cycle(5,3)
Optimization: 13
Answer: 2
cycle(1,4) cycle(4,2) cycle(3,1) cycle(2,5) cycle(6,3) cycle(5,6)
Optimization: 12
Answer: 3
cycle(1,2) cycle(4,1) cycle(3,4) cycle(2,5) cycle(6,3) cycle(5,6)
Optimization: 11
OPTIMUM FOUND
Models : 3
Optimum : yes
Optimization : 11
Calls : 1
Time : 0.003s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.003s
我试图将此代码转换为:
% Generate
{ cycle(X,Y) : edge(X,Y) } = street1 :- node(X).
{ cycle(X,Y) : edge(X,Y) } = street1 :- node(Y).
% Define
reached(Y) :- cycle(street1,Y).
reached(Y) :- cycle(X,Y), reached(X).
% Test
:- node(Y), not reached(Y).
% Nodes
%node(1..6).
node(street1..street6).
%node(street1).
%node(street2).
%node(street3).
%node(street4).
%node(street5).
%node(street6).
%node(street1;street2;street3;street4;street5;street6).
% (Directed) Edges
edge(street1,(street2;street3;street4)).
edge(street2,(street4;street5;street6)).
edge(street3,(street1;street4;street5)).
edge(street4,(street1;street2)).
edge(street5,(street3;street4;street6)).
edge(street6,(street2;street3;street5)).
% Edge Costs
cost(street1,street2,2). cost(street1,street3,3). cost(street1,street4,1).
cost(street2,street4,2). cost(street2,street5,2). cost(street2,street6,4).
cost(street3,street1,3). cost(street3,street4,2). cost(street3,street5,2).
cost(street4,street1,1). cost(street4,street2,2).
cost(street5,street3,2). cost(street5,street4,2). cost(street5,street6,1).
cost(street6,street2,4). cost(street6,street3,3). cost(street6,street5,1).
% Optimize minimum cost and cycle
#minimize { C,X,Y : cycle(X,Y), cost(X,Y,C) }.
% Display
#show cycle/2.
这似乎有点尴尬我得到了这个结果:
clingo version 5.4.0
Reading from cleaning_street_names.lp4
Solving...
UNSATISFIABLE
Models : 0
Calls : 1
Time : 0.003s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.003s
我试图纠正它,就像你在评论中告诉我的那样:
% Generate
{ cycle(X,Y) : edge(X,Y) } = 1 :- node(X).
{ cycle(X,Y) : edge(X,Y) } = 1 :- node(Y).
% Define
reached(Y) :- cycle(1,Y).
reached(Y) :- cycle(X,Y), reached(X).
% Test
:- node(Y), not reached(Y).
% Nodes
%node(1..6).
node(street1..street6).
%node(street1).
%node(street2).
%node(street3).
%node(street4).
%node(street5).
%node(street6).
%node(street1;street2;street3;street4;street5;street6).
% (Directed) Edges
edge(street1,(street2;street3;street4)).
edge(street2,(street4;street5;street6)).
edge(street3,(street1;street4;street5)).
edge(street4,(street1;street2)).
edge(street5,(street3;street4;street6)).
edge(street6,(street2;street3;street5)).
% Edge Costs
cost(street1,street2,2). cost(street1,street3,3). cost(street1,street4,1).
cost(street2,street4,2). cost(street2,street5,2). cost(street2,street6,4).
cost(street3,street1,3). cost(street3,street4,2). cost(street3,street5,2).
cost(street4,street1,1). cost(street4,street2,2).
cost(street5,street3,2). cost(street5,street4,2). cost(street5,street6,1).
cost(street6,street2,4). cost(street6,street3,3). cost(street6,street5,1).
% Optimize minimum cost and cycle
#minimize { C,X,Y : cycle(X,Y), cost(X,Y,C) }.
% Display
#show cycle/2.
我得到了这个结果:
clingo version 5.4.0
Reading from cleaning_street_names.lp4
cleaning_street_names.lp4:30:6-22: info: interval undefined:
street1..street6
Solving...
Answer: 1
SATISFIABLE
Models : 1
Calls : 1
Time : 0.002s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.002s
(如果我把node(1..6)
. 结果是UNSATISFIABLE
)