如何使用clpfd中的tuple_in?

问题描述 投票:1回答:2

我在这里遵循clpfd练习:http://www.pathwayslms.com/swipltuts/clpfd/clpfd.html

我有以下解决方案,例如5号1

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).


allpaths(From,To,Route):-
  trains(Ts),
  length(Ts,Max_Trains),
  between(2,Max_Trains,Length_Of_Route),
  Route=[_,_|_],length(Route,Length_Of_Route),
  maplist(same_length([_,_,_,_]),Route),
  create_chain(Route,TimeChain,1),
  train_chain(Route),
  chain(TimeChain, #<),
  tuples_in(Route,Ts),
  nth1(1,Route,[From|_]),
  nth1(Length_Of_Route,Route,[_,To|_]).

create_chain([],[],_).
create_chain(List_of_Lists,[First,Second|Chain],1):-
  List_of_Lists =[Item|T],
  Item =[_T1,_T2,First,Second],
  create_chain(T,Chain,2).
create_chain(List_of_Lists,[First,Second|Chain],2):-
  List_of_Lists =[Item|T],
  Item =[_T1,_T2,First,Second],
  create_chain(T,Chain,1).

train_chain([_]).
train_chain(List_Of_Lists):-
  List_Of_Lists =[First,Second|T],
  First = [_One,Two,_,_],
  Second =[Two,_Three,_,_],
  train_chain([Second|T]).

我不确定我是否以“预期”方式完成此操作,但它可以正常工作,看起来还可以。我基本上会创建跨火车的变量列表,然后将约束应用于该列表。我不确定是否以“预期”方式完成此操作,因为我只在火车时间使用clpfd而不是火车时间。

现在,与员工进行下一次练习时,与火车练习不同,约束是针对单个员工,而不是整个员工。我不了解在这种情况下如何使用tuples_in,用findall完成练习似乎很简单。如何使用constraints和tuples_in来实现它?有什么好处?

employees([
 [1, 75, 0, 30, 25],
 [2, 83, 0, 45, 25],
 [3, 90, 1, 45, 50],
 [4, 45, 3, 75, 25],
 [5, 89, 0, 52, 50]
 ]).

promotions(Es,Promotions):-
  employees(Es),
  findall([Id,Score,V,T,RT],
    (member([Id,Score,V,T,RT],Es),Score>80,V=<1,T>RT),
  Promotions).
prolog swi-prolog clpfd
2个回答
1
投票

我认为应该很简单


0
投票

很高兴我终于可以在StackOverflow上回答一些问题。这是我对第一个问题的解决方案。我使用的是Accumulator模式,您可以在chaper 6 of LearnPrologNow上找到它们,这里也有友好的幻灯片,请直接转到第6章:slides

© www.soinside.com 2019 - 2024. All rights reserved.