通过递延扩展Prolog目标? [英] Extending a Prolog goal through recursion?

查看:55
本文介绍了通过递延扩展Prolog目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经(最终)实现了一些目标,这些目标将根据 startBy startAfter duration 安排一系列任务.但是,计划目标仅接受规定数量的任务.我想扩展计划目标的功能,以接受单个列表并在计划时迭代该列表.

I've implemented (finally) a few goals that will schedule a series of tasks according to a startBy startAfter and duration. The schedule goal however, accepts only the prescribed number of tasks. I'd like to extend the functionality of the schedule goal to accept a single list and iterate through that list while scheduling.

不幸的是,我认为这将需要与以下所示的可以运行冲突目标完全不同的目标.

unfortunately I think this is going to require rather different goals than the can run and conflicts goals shown below.

更新:现在要关闭...如果RT,TT对中的任何一个不冲突,can_run/3将返回true ...这绝对不是我想要的,但是我很接近...如果有人可以告诉我我如何防止这种情况发生(也许以某种方式使用!运算符?)那真是太棒了.

UPDATE: Coming close now ... can_run/3 returns true if any one of the RT,TT pairs does not conflict ... which is decidedly not what I want, but I am close ... if anyone can tell me how to keep that from happening (perhaps using the ! operator somehow?) That would be marvelous.

更新:Eet werkz!现在用于清理多余的不完善解决方案(不安排任务)和解决方案排列(a,b,c和a,c,b和b,a,c和b,c,a等...)

UPDATE: Eet werkz! Now for cleanup of superfluous imperfect solutions (not scheduling a task) and the solution permutations (a,b,c and a,c,b and b,a,c and b,c,a etc...)

更新:巴哈..好吧,所以这实际上是行不通的...持续超过1的时间... 杂音杂音另外...似乎...这可能需要大量处理

UPDATE: Bah .. okay so this isn't actually working ... for anything over a duration of 1 ... mutter mutter Also ... seems like it may be rather processing intensive

更新(最后):使它工作并实施了最小窗口优先"的启发式方法,以缩短处理时间...对于无法解决的计划,仍然存在快速返回false的问题,但是找到解决方案很快.

UPDATE (final): Got it working and implemented a "smallest window first" heuristic to improve processing time ... still have problems returning false quickly for schedules that cannot be resolved, but finding a solution comes rather quickly.

这是我到目前为止所拥有的:

Here is what I have so far:

can_run(R,T) :- startAfter(R,TA),startBy(R,TB),between(TA,TB,T).

conflicts(R,T,RTs) :- duration(R,D),member([RT,TT],RTs),R=\=RT,duration(RT,DT),T<DT+TT,T+D>TT.

schedule :- findall(R,request(R),Rs),predsort(windowCompare,Rs,Rtn),schedule(Rtn,[]).

windowCompare(D,R1,R2) :- startAfter(R1,SA1),startBy(R1,SB1),W1=SB1-SA1,
                          startAfter(R2,SA2),startBy(R2,SB2),W2=SB2-SA2,
                          W1>W2->D='>';D='<'.

schedule(Rs,RTs) :- Rs==[];
                    (
                    member(R,Rs),select(R,Rs,Rst),
                    can_run(R,T),\+conflicts(R,T,RTs),
                    append(RTs,[[R,T]],RTN),schedule(Rst,RTN),
                    writef('%t @ %t\n',[R,T])
                    ).

request(1).
request(2).
request(3).
request(4).
request(5).
request(6).
request(7).
request(8).
request(9).

startAfter(1,0).
startAfter(2,0).
startAfter(3,0).
startAfter(4,0).
startAfter(5,0).
startAfter(6,0).
startAfter(7,0).
startAfter(8,0).
startAfter(9,0).

startBy(1,20).
startBy(2,40).
startBy(3,15).
startBy(4,5).
startBy(5,0).
startBy(6,35).
startBy(7,30).
startBy(8,10).
startBy(9,25).

duration(1,5).
duration(2,5).
duration(3,5).
duration(4,5).
duration(5,5).
duration(6,5).
duration(7,5).
duration(8,5).
duration(9,5).

我想我可能需要维护一个持久结构,每次迭代都会更新...

I'm thinking i may need to maintain a persistent structure that each iteration updates...

推荐答案

如果列表中的任何对冲突,如果您想要的是can_run(R,T,Rts)失败,那么谓词中的final子句应该是

If what you want is for can_run(R,T,Rts) to fail if ANY of the pairs in the list conflicts, then the final clause in your predicate should be

\+ (member([RT,TT], RTs), conflicts(T, RT,TT))

我不熟悉between/3谓词,但重要的是,between(TA,TB,T)的解决方案的作用是在调用+(..之前将T绑定到底值. )

I'm not familiar with the between/3 predicate, but it is important that the effect of the solution of between(TA,TB,T) is to bind T to a ground value before the call to + (...)

这篇关于通过递延扩展Prolog目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆