使用Mondial的陆地边界最长的谓词-Prolog

问题描述

我正在尝试使用Prolog,finding largest value from a setOf list的一些帮助来编写具有最长陆地边界的谓词。我在这里面临的问题是生成输出不是我所期望的。

我的代码的当前输出按所有国家和大洲的“周长”生成降序。但是我只需要每个大陆的一个条目(具有最高周长),其次就不能将位于多个大陆上的国家/地区包括在内。

我的代码

lb_country(Continent,Country):-
    setof(L-R-C,X^Y^Z^( encompasses(C,R,X),\+ geo_sea(Y,C,Z),circumference(L,C) ),Cs),reverse(Cs,HighToLowAreas),member(_-Continent-Country,HighToLowAreas).

包含的谓词周长定义为


borders_sym(X,Y,L) :- borders(X,L);borders(Y,X,L). 
bord(Country,L) :-     borders_sym(_,L),Country = C.

circumference(C,Country) :-
    setof(P,bord(Country,P),List),sum_list(List,C).

example of encompasses and geo_sea

% encompasses(C,Fr),country C is encompassed by region R to fraction Fr (percent)
encompasses(austria,'Europe',100).
encompasses(austria,100).
encompasses(afghanistan,'Asia',100).
encompasses(antigua_and_barbuda,'America',100).
encompasses(albania,100).
encompasses(american_samoa,'Australia/Oceania',100).
encompasses(andorra,100).
encompasses(angola,'Africa',100).
encompasses(armenia,100).
encompasses(aruba,100).
encompasses(australia,100).
encompasses(anguilla,100).
encompasses(azerbaijan,100).
encompasses(bangladesh,100).
encompasses(barbados,100).
encompasses(benin,100).
encompasses(bermuda,100).
encompasses(belgium,100).
encompasses(burkina_faso,100).
encompasses(pitcairn_islands,100).
encompasses(pakistan,100).
encompasses(poland,100).
encompasses(papua_new_guinea,100).
encompasses(puerto_rico,100).
encompasses(paraguay,100).
encompasses(qatar,100).
encompasses(argentina,100).
encompasses(russia,75).
encompasses(botswana,100).
encompasses(central_african_republic,100).
encompasses(taiwan,100).
encompasses(congo,100).
encompasses(chile,100).
encompasses(reunion,25).



% geo_sea(N,the sea N is in country C in province P
geo_sea('Andaman Sea',india,'Andaman and Nicobar Is.').
% geo_sea(N,'Andaman and Nicobar Is.').
geo_sea('Andaman Sea',myanmar,'Ayeyarwady').
geo_sea('Andaman Sea','Bago').
geo_sea('Andaman Sea','Mon').
geo_sea('Andaman Sea','Yangon').
geo_sea('Andaman Sea',indonesia,'Aceh').
geo_sea('Andaman Sea',thailand,'Thailand').
geo_sea('arabian Sea','Goa').
geo_sea('arabian Sea','Gujarat').
geo_sea('arabian Sea','Karnataka').
geo_sea('arabian Sea','Kerala').
geo_sea('arabian Sea','Lakshadweep Is.').
geo_sea('arabian Sea','Maharashtra').
geo_sea('arabian Sea',oman,'Oman').
geo_sea('arabian Sea',pakistan,'Balochistan').
geo_sea('arabian Sea','Sindh').
geo_sea('Arctic Ocean',canada,'northwest Territories').

borders predicate

% borders(X,country X borders country Y,the border is L kilometers
borders(austria,switzerland,164).
borders(austria,czech_republic,362).
borders(austria,germany,784).
borders(afghanistan,china,76).
borders(afghanistan,iran,936).
borders(afghanistan,2430).
borders(afghanistan,tajikistan,1206).
borders(afghanistan,turkmenistan,744).
borders(afghanistan,uzbekistan,137).
borders(austria,liechtenstein,37).
borders(austria,hungary,366).
borders(austria,italy,430).
borders(albania,greece,282).
borders(albania,kosovo,112).
borders(albania,macedonia,151).
borders(albania,montenegro,172).
borders(andorra,spain,65).
borders(andorra,france,60).
borders(angola,namibia,1376).



解决方法

对于圆周2,您可以编写:

circumference(Country,C) :-
    findall(Km,( border(Country,_,Km) ; border(_,Country,Km) ),Kms),sum_list(Kms,C).

该表包含/ 3个国家的完整列表?然后,您可以将“完全在一个大陆上的国家”定义为:encompasses(Country,100)

要按大洲分组,并且只列出整个大洲上的竞争,您可以执行以下操作:

?- bagof(Country,encompasses(Country,Continent,100),Countries).
Continent = 'Africa',Countries = [angola,benin,burkina_faso] ;
Continent = 'America',Countries = [antigua_and_barbuda,aruba,anguilla,barbados,bermuda] ;
Continent = 'Asia',Countries = [afghanistan,armenia,azerbaijan,bangladesh] ;
Continent = 'Australia/Oceania',Countries = [american_samoa,australia] ;
Continent = 'Europe',Countries = [austria,albania,andorra,belgium].

您现在可以按照您的问题进行操作:添加周长,按其排序,反向,取第一个元素:

longest_border(Continent,Length) :-
    bagof(N-C,(   encompasses(C,circumference(C,N)
          ),Cs),keysort(Countries,S),reverse(S,[X-Y|_]).

但是您丢失了很多数据。我得到:

?- longest_border(Continent,Length).
Continent = 'Africa',Country = angola,Length = 1376 ;
Continent = 'America',Country = bermuda,Length = 0 ;
Continent = 'Asia',Country = afghanistan,Length = 5529 ;
Continent = 'Australia/Oceania',Country = australia,Length = 0 ;
Continent = 'Europe',Country = austria,Length = 2143.