Clingo ASP - 将序列定义为给定集合的排列

问题描述 投票:0回答:0

我从这里继续我的旅程。

我正在尝试学习 ASP 并从事这样的项目:
找到具有以下属性的序列 s = (s1, s2, ..., sn):

  • s 是集合 {0, 1, ..., n-1} 的排列;
  • 序列 v = (|s2-s1|, |s3-s2|, ..., |sn-sn-1|) 是集合 {1, 2, ..., n-1} 的排列;

解决 n = 11 的问题。

我觉得我有进步,但我现在卡住了。

这是我的代码:

% Define a sequence s = (s1, s2, ..., sn), n = 11, values between 0 and 10.
{s(N) : N=0..10}.

% Each number 0-10 has to occur exactly once.
1 { in(N) : s(N) } 1 :- N=0..10.

% I want s to be a permutation of {0,1,2,...,10} - how?
% ???

% Define a sequence v = (...), values between 0 and 10.
{v(M) : M=1..10}.

% Define the calculation of absolute differences between two adjacent elements of s.
diff(X, Y, D) :- v(X), v(Y), D = |X-Y|.

% Define a set of such differences.
abs_diff(D) :- diff(_, _, D).

% Each number 1-10 has to occur exactly once in that sequence.
1 { in_v(A) : v(A) } 1 :- A=1..10.

% v is a set of those differences.
v(X) :- abs_diff(X).

% Display results.
#show s/1.
#show v/1.

程序运行,但返回错误输出: v(1) v(2) v(3) v(4) v(5) v(6) v(7) v(8) v(9) v(10) v(0) s(0) s(1) s(2) s(3) s(4) s(5) s(6) s(7) s(8) s(9) s(10)

我觉得我缺少的是将“s”定义为集合 {0,1,2,...,10} 的排列,而不是始终是该集合。但我该怎么做呢?

我尝试了以下解决方案:

  1. 运行不正常。
% Define the input set
set(0..10).

% Define the permutation predicate
perm([]).
perm([X|T]) :-
  set(X),
  select(X, Set, Rest),
  perm(T),
  Set = [X|Rest].
  
% Define the select predicate to choose an element from the set
select(X, [X|T], T).
select(X, [H|T], [H|T1]) :-
  select(X, T, T1).
-:5:6-7: error: syntax error, unexpected [, expecting ) or ;

-:6:6-7: error: syntax error, unexpected [, expecting ) or ;

-:13:11-12: error: syntax error, unexpected [

-:14:11-12: error: syntax error, unexpected [
  1. 输出仍然错误。
1 { permute(I,D) : I=1..10, D=1..11 } 1 :-
  dif(1,1), dif(10,11),
  { permute(I,D) : I=1..10, D=1..11 } = 11,
  permutation(D, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).
  1. 输出仍然错误。
#const n=11.

1 { s(X,Y) : Y=0..n-1 } 1 :- X=1..n.

:- s(X,Y), s(Z,Y), X!=Z.
:- s(X,Y), s(X,Z), Y!=Z.

任何帮助表示赞赏。

permutation answer-set-programming clingo
© www.soinside.com 2019 - 2024. All rights reserved.