I have this code that evaluates a list of numbers and returns lists matching the pattern [G,A],[A,B],[B,C],[C,D],[D,E],[E,F],[F,G]
. However, I want the numbers to only to be unique numbers. EG: ([0,2],[2,4],[4,19],[19,3],[3,5],[5,7],[7,0]
). The different method returns true or false based on the numbers inputted. However, the code still returns numbers that have more than one similar numbers.
r5(L,R):-
R = [[G,A],[A,B],[B,C],[C,D],[D,E],[E,F],[F,G]],
[A,B,C,D,E,F,G] ins 0 .. 27,
different([A,B,C,D,E,F,G]),
member([G,A],L),
member([A,B],L),
member([B,C],L),
member([C,D],L),
member([D,E],L),
member([E,F],L),
member([F,G],L),
label([A,B,C,D,E,F,G]).
This is an example of a portion what it returns:
R = [[0, 2], [2, 0], [0, 2], [2, 5], [5, 9], [9, 2], [2, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 5], [5, 23], [23, 17], [17, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 5], [5, 24], [24, 19], [19, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 5], [5, 26], [26, 12], [12, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 9], [9, 5], [5, 2], [2, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 9], [9, 15], [15, 2], [2, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 9], [9, 15], [15, 17], [17, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 9], [9, 15], [15, 19], [19, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 15], [15, 9], [9, 2], [2, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 21], [21, 1], [1, 12], [12, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 21], [21, 1], [1, 20], [20, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 21], [21, 11], [11, 12], [12, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 21], [21, 11], [11, 17], [17, 0]] ;
R = [[0, 2], [2, 0], [0, 2], [2, 21], [21, 24], [24, 19], [19, 0]] ;
R = [[0, 2], [2, 0], [0, 12], [12, 1], [1, 18], [18, 12], [12, 0]] ;
None of those are valid because they contain repeated numbers. Following are two outputs from further down in the output that are valid:
R = [[0, 2], [2, 9], [9, 4], [4, 8], [8, 23], [23, 17], [17, 0]] ;
R = [[0, 2], [2, 9], [9, 4], [4, 8], [8, 24], [24, 19], [19, 0]] ;
This is the different method:
different(X) :-
sort(X, Sorted),
length(X, OriginalLength),
length(Sorted, SortedLength),
OriginalLength == SortedLength.
Here is my original call:
r5_3([[0, 2],[2,0],[0, 12],[12,0],[0, 17],[17,0],[0, 19],[19,0],[0, 20],[20,0],[1, 4],[4,1],[1, 12],[12,1],[1, 18],[18,1],[1, 20],[20,1],[1, 21],[21,1],[2, 5],[5,2],[9, 2],[2,9],[2, 15],[15,2],[2, 21],[21,2],[8, 3],[3,8],[10, 3],[3,10],[16, 3],[3,16],[3, 22],[22,3],[25, 3],[3,25],[8, 4],[4,8],[9, 4],[4,9],[4, 23],[23,4],[26, 4],[4,26],[9, 5],[5,9],[5, 23],[23,5],[24, 5],[5,24],[26, 5],[5,26],[14, 6],[6,14],[17, 6],[6,17],[18, 6],[6,18],[24, 6],[6,24],[25, 6],[6,25],[18, 7],[7,18],[19, 7],[7,19],[22, 7],[7,22],[23, 7],[7,23],[26, 7],[7,26],[8, 14],[14,8],[8, 23],[23,8],[8, 24],[24,8],[9, 15],[15,9],[9, 13],[13,9],[16, 10],[10,16],[10, 20],[20,10],[10, 13],[13,10],[10, 27],[27,10],[11, 12],[12,11],[17, 11],[11,17],[11, 21],[21,11],[25, 11],[11,25],[11, 27],[27,11],[18, 12],[12,18],[26, 12],[12,26],[14, 15],[15,14],[16, 14],[14,16],[26, 14],[14,26],[17, 15],[15,17],[19, 15],[15,19],[16, 20],[20,16],[16, 22],[22,16],[17, 23],[23,17],[18, 27],[27,18],[24, 19],[19,24],[19, 27],[27,19],[25, 20],[20,25],[24, 21],[21,24],[13, 21],[21,13],[25, 22],[22,25],[13, 22],[22,13],[27, 13],[13,27]],R).
all_different/1
is part of the CLP(FD) library. Your different/1
will only work if you do your label/1
before calling different/1
.
So you could either use all_different/1
(preferred) with your current implementation, or you could re-arrange your code as follows:
r5(L,R):-
R = [[G,A],[A,B],[B,C],[C,D],[D,E],[E,F],[F,G]],
[A,B,C,D,E,F,G] ins 0 .. 27,
% all_different([A,B,C,D,E,F,G]), % preferred in place of 'different/1' below
label([A,B,C,D,E,F,G]),
different([A,B,C,D,E,F,G]),
member([G,A],L),
member([A,B],L),
member([B,C],L),
member([C,D],L),
member([D,E],L),
member([E,F],L),
member([F,G],L).
L
in the original code is a little unclear. A more general solution might look something like this:
:- use_module(library(clpfd)).
% Define consecutive intervals consisting of Length intervals
% with elements from 0 to MaxNumber
consecutive_intervals(MaxNumber, Length, Intervals):-
length(Elements, Length), % Establish the number of elements
Elements ins 0 .. Max, % Establish the range of each element
all_different(Elements), % Each element is different
list_intervals(Elements, Intervals), % Define consecutive intervals
label(Elements).
% list_intervals(List, Intervals)
% Intervals is a complete list of consecutive intervals with elements from List
list_intervals([X1,X2|Xs], [[X1,X2]|T]) :-
list_intervals([X2|Xs], X1, T).
list_intervals([X2], X1, [[X2,X1]]).
list_intervals([X2,X3|Xs], X1, [[X2,X3]|T]) :-
list_intervals([X3|Xs], X1, T).
The number of solutions is large, but here's an small, abbreviated example:
?- consecutive_intervals(4, 3, R).
R = [[0, 1], [1, 2], [2, 0]] ;
R = [[0, 1], [1, 3], [3, 0]] ;
R = [[0, 1], [1, 4], [4, 0]] ;
R = [[0, 2], [2, 1], [1, 0]] ;
R = [[0, 2], [2, 3], [3, 0]] ;
R = [[0, 2], [2, 4], [4, 0]] ;
...
R = [[4, 3], [3, 0], [0, 4]] ;
R = [[4, 3], [3, 1], [1, 4]] ;
R = [[4, 3], [3, 2], [2, 4]] ;
false.