2013-02-28 96 views
0

我有以下的知识基础在序言比较

team_name(1, conneticut). 
team_name(2, duke). 
team_name(3, memphis). 
team_name(4, villanova). 
team_name(5,gonzaga). 

win_lose(1,22,2). 
win_lose(2,24,1). 
win_lose(3,23,2). 
win_lose(4,20,2). 
win_lose(5,21,3). 

zone(west,5). 
zone(south,3). 
zone(east,1). 
zone(east,2). 
zone(east,4). 

我想编写一个查询,允许团队对团队用最少的胜利,这两者在同一区域最多的边路进攻,而主队是一个与最胜

我有以下

canPlay(X,Y).     Y can play X 
canPlay(X,Y):-zone(zone(X)=:=Y).  Y can play X, if Y zone == X 

这是行不通的。

回答

0

我想我明白你想要做什么。您正尝试将最好的得分队伍与最差的得分队伍配对,但只能在同一个区域内进行配对。这是我会怎么处理它:

canPlay(X, Y) :- 
    team_name(XId, X), team_name(YId, Y), % let's get team X and team Y 
    X \= Y,         % make sure they aren't the same 
    zone(Zone, XId), zone(Zone, YId).  % but make sure they are in the same zone 

这是一个良好的刺,但它会产生所有的组合,而不仅仅是其中的两支球队都适当地匹配获胜的那些。

?- setof(X-Y, canPlay(X, Y), Pairings). 
Pairings = [conneticut-duke, conneticut-villanova, duke-conneticut, 
      duke-villanova, villanova-conneticut, villanova-duke]. 

我不完全了解您的其他要求,但希望这足以让您走上正确的轨道。

+0

我知道你做了什么,这让我更有意义。只有一个问题,如何检查获胜分数最高的球队是否与同一区域中得分最低的球队打比赛。 康涅狄格呼喊能够发挥villanova,但不应该能够发挥公爵,因为v​​illanova拥有较少的胜利,然后公爵。 – codeeeeeNOT 2013-02-28 18:09:52

+0

你读过答案了吗? “这是一个不错的第一杆,但它会产生所有的组合,而不仅仅是两支球队胜利相匹配的组合。” – 2013-02-28 19:16:13

0

我觉得这个应该符合你的要求

canPlay(Home, Guest) :- 
    % get different Zones 
    setof(Zone, Team^zone(Zone, Team), Zones), 

    % in a Zone 
    member(Zone, Zones), 

    % get ordered list 
    setof(Win-Team, Lost^(zone(Zone, Team), win_lose(Team, Win, Lost)), Standings), 

    % take first and last 
    append([[_-HomeId],_,[_-GuestId]], Standings), 

    team_name(HomeId, Home), 
    team_name(GuestId, Guest).