Hello I am new to answer-set-programming. I have done a little prolog in the past!
I am trying to solve this problem I believe it can be solved with hamiltonian-cycle, let me know your opinion. If you are not familiar with ASP you can visit this site [clingo and gringo][1]. You can run the files in terminal with this command clingo name_of_the_file.lp
or clingo name_of_the_file.lp4
I tested it in Ubuntu.
(these are .lp or .lp4 files) The first code I read and understood was that which has 3 results
% 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.
I get this result:
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
I tried to convert this code in this:
% 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.
and this one seems a bit awkward I get this result:
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
I tried to correct it like you told me in the comments :
% 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.
and I got this result:
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
(if I put node(1..6)
. the result it's UNSATISFIABLE
)