- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有这个知识库:
free(ann,slot(time(8,0),time(9,0))).
free(ann,slot(time(10,0),time(11,0))).
free(bob,slot(time(7,0),time(8,30))).
free(bob,slot(time(10,0),time(11,0))).
free(carla,slot(time(8,0),time(9,0))).
free(carla,slot(time(10,0),time(10,15))).
因此,经过大量努力,我设法编写了一些内容,使用以下代码打印在特定时段内第一个有空的人:
meetone(Person, slot(time(BeginHour, BeginMinute), time(EndHour, EndMinute)))
:- free(Person, slot(time(BH, BM), time(EH, EM))),
BH*60 + BM =< EndHour*60 + EndMinute,
EH*60 + EM >= BeginHour*60 + BeginMinute.
main :- (meetone(Person,slot(time(7,15),time(7,20))); halt),
write(Person),
nl,
halt.
:- initialization(main).
这将打印 bob,即预期结果。
这就是事情变得复杂的地方(至少对我来说)。假设我想找出知识库中每个人共有的所有时间段。以下代码演示了我最终想要如何调用它:
people([ann,bob,carla]).
meet :- ???
main :- (setof(Slot,meet(Slot),Slots); halt),
write(Slots),
nl,
halt.
:- initialization(main).
这里有一些模糊的伪代码,我认为它们可能会实现我正在寻找的目标,但我没有足够的经验来让它工作。
最终输出将显示 8:00 - 8:30 和 10:00 - 10:15 的时段。任何帮助实现这一目标的帮助将不胜感激。
最佳答案
Prolog 有一些语法特征,恕我直言,确实有助于编写易于理解的代码。那么这是你的问题,利用运算符来获得可读性:
free(ann, 08:00 > 09:00).
free(ann, 10:00 > 11:00).
free(bob, 07:00 > 08:30).
free(bob, 10:00 > 11:00).
free(carla, 08:00 > 09:00).
free(carla, 10:00 > 10:15).
meetone(Person, Slot) :- free(Person, SlotP), contains(SlotP, Slot).
contains(Slot1, Slot2) :-
timepoints(Slot1, B1, E1),
timepoints(Slot2, B2, E2),
B1 =< E2, E1 >= B2.
timepoints(BH:BM > EH:EM, B, E) :-
B is BH*60 + BM,
E is EH*60 + EM.
main :- setof(Person, meetone(Person, 7:15 > 7:20), Available),
maplist(writeln, Available).
我试图引入两个实用程序,一种可重用的代码:contains/2 和 timepoints/3。拥有这样的片段可以帮助编写更复杂的逻辑......
运行这段代码,我明白了
?- main.
bob
true.
现在回答你的主要问题:
Let's say I want to find out all the time slots everyone in the Knowledge Base has in common
我将开始编写一个 common_timeslot/3,解释(计算)预期的内容:
common_timeslot(S1, S2, Common) :-
timepoints(S1, B1, E1),
timepoints(S2, B2, E2),
% do you mean intersection by common ?
...
否则,仅考虑身份
common_timeslot(S, S, S).
定义了这个之后,所有的公共(public)资源都可以通过
找到main :-
setof(Sc/P/Q, Sp^Sq^(
free(P, Sp), free(Q, Sq), Q \= P,
common_timeslot(Sp, Sq, Sc)
), Commons),
maplist(writeln, Commons).
结果
?- main.
(8:0>9:0)/ann/carla
(8:0>9:0)/carla/ann
(10:0>11:0)/ann/bob
(10:0>11:0)/bob/ann
true.
编辑记录垫子评论,现在我发布整个程序
free(ann, 08:00 < 09:00).
free(ann, 10:00 < 11:00).
free(bob, 07:00 < 08:30).
free(bob, 10:00 < 11:00).
free(carla, 08:00 < 09:00).
free(carla, 10:00 < 10:15).
meetone(Person, Slot) :- free(Person, SlotP), contains(SlotP, Slot).
contains(Slot1, Slot2) :-
timepoints(Slot1, B1, E1),
timepoints(Slot2, B2, E2),
B1 =< E2, E1 >= B2.
timepoints(BH:BM < EH:EM, B, E) :-
( ( var(B), var(E) )
-> B is BH * 60 + BM,
E is EH * 60 + EM
; BH is B // 60,
BM is floor(B mod 60),
EH is E // 60,
EM is floor(E mod 60)
).
% common_timeslot(S, S, S).
common_timeslot(S1,S2,S) :-
timepoints(S1,B1,E1),
timepoints(S2,B2,E2),
B is max(B1,B2),
E is min(E1,E2),
B < E,
timepoints(S,B,E).
% base case: C passed all commonality test
check_common(C, [], C).
% S is the current common, to be checked for availability on person P
check_common(S, [P|Ps], C) :-
free(P, Sp),
common_timeslot(S, Sp, Ct),
check_common(Ct, Ps, C).
main :- setof(P, S^free(P,S), [FirstP|Others]),
forall(free(FirstP, S), (
check_common(S, Others, C),
writeln(FirstP:C)
)).
结果
?- main.
ann: (8:0<8:30)
ann: (10:0<10:15)
true.
主要变化是 timepoints/3 现在是“双向”的。然后我按照您在评论中的解释介绍了 common_timeslot/3 。
我认为您会欣赏这些小的句法抽象有助于拥有清晰的“应用”逻辑。当然,forall/2,或setof/3 是您需要了解的内置函数,以便更熟练地使用 Prolog。
关于Prolog 时间重叠问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475643/
我对具有 2 个轴的数据有交叉识别问题,例如 A = array([['x0', 'y0', 'data0', 'data0'], ['x0', 'y0', 'data0', '
我知道这是代码有点傻,但有人可以解释为什么 isList [42]返回 True而isList2 [42]打印 False ,以及如何防止这种情况?我想更好地理解一些更晦涩的 GHC 类型扩展,我认为
我正在使用memmove(),但目标似乎正在覆盖源,或者也许我不明白覆盖是什么。我有一个 char 数组(目标),然后是一个指向目标的指针,该指针位于 vector 内部。 char destinat
以下AS3代码有时会导致音频多次播放,就像疯狂的回声一样,几乎同时播放。通常使用该URL都可以,但是当我使用https://soundcloud.com url时,它总是会发疯。在极少数情况下,我认为
我正在尝试在 android 2.2 中实现类似操作栏的东西。这是我的 main.xml
如何避免第一个值的重叠问题 而且,我怎样才能看到最后一个被剪裁的值? 最佳答案 我认为您在修改轴上的样式和调整视口(viewport)之间有几种选择。 我会尝试: 禁用左轴,启用右轴 chart.le
我正在构建一个简单的应用程序,您可以在其中使用纸娃娃之类的工具来描述您的外观。 Check out this image.计划是有 4 个水平 ScrollView :第一个用于发型,第二个用于面部毛
我有一个问题...我在绝对布局中有两个 ScrollView 。换句话说,它们是全屏的并且相互重叠 上面的scrollview是水平滚动的,下面的是垂直滚动的scrollview。 当我水平滚动时,我
我看了一些类似的问题,但我不太明白在我的层次结构中我应该做什么? 我有 用于屏幕底部的标签菜单 和 对于其他将创建的 fragment 。 我有 9 个标签菜单,每个都是 fragment 。 一
在我的 Android 应用程序中,我有一个编辑文本和一个按钮,单击该按钮会向我的主要 Activity 添加一个 fragment ,其中包含在我的编辑文本中写入的消息。问题是,当我更改消息并单击按
在我的分段控件中,有时标题比其段宽。我怎样才能让它截断? 假设第 1 段的标题是 Text overlaps,第 2 段的名称是 ok。 我希望它看起来如何: [Text ov...| ok
我想创建一个带有重叠单元格的 uitableview,如下图所示。问题是,即使我为单元格的内容 View 设置 clipsToBounds = NO,单元格假标题(例如,将与前一个单元格重叠的西类牙语
有了这个CSS .addProblemClass{ width:300px; height:300px; /*width:25%; height:40%;*/
我有跨窗口移动的图像(2 行),当我离开页面选项卡时,然后返回它,所有图像都相互堆叠。 JS代码(记入jfriend00) function startMoving(img) { va
这是我的一段代码。图像在 23 毫秒后正常可见,但永远不会像第二行所示那样返回隐藏状态。如果我将其从 17 毫秒更改为大于 23 毫秒的值,它就会起作用。反之亦然,如果我将第一行更改为 16 毫秒,它
我正在可汗学院为学校项目编写一款太空入侵者游戏,但我不知道如何在子弹和外星人之间进行碰撞,然后摆脱子弹所碰撞的外星人。这是非常基本的 JS,尽管我尝试过,但我不太明白如何将有关该主题的其他答案放入我的
当我尝试重新加载 tableView 的数据时出现奇怪的重叠,导致单元格的高度发生变化(使用 UITableViewAutomaticDimension),然后内容与上面的单元格重叠,无法弄清楚怎么做
我是一个新手,如果这是一个愚蠢的问题,请原谅我。我想有一个部分与标题分开,但发生了两种情况: (1) 当我把 在 下面,它们相互重叠,如下所示: Section overlapping header
我正在尝试创建两个 那是重叠的。唯一的问题是第二个 在第一个的前面它必须是相反的。我尝试设置第一个 的 z-index至 1但它仍然不起作用。 这是我的代码: #content{ backgrou
是否有重叠 2 个 div 的有效方法。 我有以下内容,但无法让它们重叠。 #top-border{width:100%; height:60px; background:url(image.jpg)
我是一名优秀的程序员,十分优秀!