简体   繁体   English

Prolog CLPFD尝试为列表列表定义域

[英]Prolog CLPFD trying to define domain for lists of list

I am working on a constraints programming problem in Prolog and I am having problems trying to define a domain for lists of list. 我正在研究Prolog中的约束编程问题,我在尝试为列表列表定义域时遇到问题。 The initial challenge of the problem is as follows: 该问题的最初挑战如下:

   trains([[1,2,0,1], %from station, to station, departs at, arrives at
           [2,3,4,5],
           [2,3,0,1],
           [3,4,5,6],
           [3,4,2,3],
           [3,4,8,9]]).

   threepath(A,D,Ps):-
        Ps = [[A,B, _T0, T1], [B,C, T2, T3], [C,D, T4, _T5]],
        T2 #> T1,
        T4 #> T3,
        trains(Ts),
        tuples_in(Ps, Ts).

After that, I am expected to expand on this to accommodate for any number of trains instead of only 3. Here is my attempt at doing that: 在那之后,我希望扩展这个以适应任何数量的列车,而不仅仅是3.这是我尝试这样做的:

  anypath(A,D,Ps,N):-
        length(Ps,N),
        Ps ins Xs,
        Xs = [A,B,C,D],
        Xs ins 1..9. %How to define the domain for a list of length 4 inside a list of variable length.

However, I am not very sure how to define a domain for lists of list. 但是,我不太确定如何为列表列表定义域。 So far, I have defined length(Ps, N) so that Ps can have any length. 到目前为止,我已经定义了长度(Ps,N),因此Ps可以有任何长度。 Then, I tried to define the variables inside of Ps so that they will be list of length 4 but failed horribly. 然后,我尝试在Ps中定义变量,以便它们将是长度为4的列表但是失败可怕。

Additionally, I am also not sure how to define the constraints for a variable length of Ps like the 3 scenario case where T2 #> T1 and T3 #> T4. 另外,我也不确定如何为可变长度的Ps定义约束,如3#场景情况,其中T2#> T1和T3#> T4。 The pattern that I am seeing is the last element of the next list should be greater than the third element of the list before it but I am stuck on the syntax to represent this constraint as well. 我看到的模式是下一个列表的最后一个元素应该大于它之前的列表的第三个元素,但我仍然坚持语法来表示这个约束。

Right now, I am trying to use recursion to somehow set the Head of Ps to a list of length 4 and to recurse through to do the same with the tails since I won't be able to know how long the list Ps will be. 现在,我正在尝试使用递归以某种方式将P的头部设置为长度为4的列表并递归以对尾部执行相同操作,因为我将无法知道列表Ps将持续多长时间。

I would be grateful if someone can shed some light on this. 如果有人能对此有所了解,我将不胜感激。

Update on Progress 25/3/2015 2015年5月25日进展更新

I read on an example of another problem that a maplist was used to produce inner lists. 我读了另一个问题的例子,即使用maplist来生成内部列表。 An excerpt of the code is: 代码的摘录是:

length_(Length, List) :- length(List, Length).

child_row(X) :- X ins 1..16 .

ww(X) :-
        write(X),
        write('/').

print_row(Row) :-
        maplist(ww, Row),
        nl.

children(Class) :-
        length(Class, 4),
        maplist(length_(4), Class),
        maplist(child_row , Class),

From what I understand, maplist(length_(4), Class) applies the length_(4) to every element inside Class and creates inner lists of length 4 as a result. 根据我的理解,maplist(length_(4),Class)将length_(4)应用于Class内的每个元素,并创建长度为4的内部列表。 So, I tried to apply this to my problem and here is my attempt: 所以,我试图将此应用于我的问题,这是我的尝试:

length_(Length, List) :- length(List, Length).
anypath(A,D,Ps,N):-
    length(Ps,N),
    maplist(length_(4), Ps),
    %constraint(Ps),
    trains(Ts),
    tuples_in(Ps, Ts).

However, I get an error message saying "length/2: Type error: list' expected, found 4'" regardless of whether N is set to 3 or 4 and I don't quite understand this as well since it should work the same way as the example above and gtrace is a bit messy to detect what's wrong for me. 但是,我得到一条错误消息“长度/ 2:类型错误: list' expected, found 4'”,无论N是否设置为3或4,我也不太明白这一点,因为它应该工作相同作为上面的例子的方式和gtrace有点混乱,以检测我的错误。

I am currently stuck at the moment and I will update if I figure anything out. 我目前陷入困境,如果我发现任何问题,我会更新。

So, I have another question that I hope can be answered is "What is the normal practice of creating inner lists and how do you normally do it yourself?". 所以,我有另一个问题,我希望可以回答的是“创建内部列表的常规做法是什么,你通常如何自己做?”。

Thanks! 谢谢!

You can flatten the list with 您可以使用以下内容展平列表

append(Trains, FlatTrains)

and then constrain the domain of FlatTrains 然后约束FlatTrains的域

FlatTrains ins 1..9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM