Prolog在比较和数据过滤方面

时间:2011-10-29 23:57:28

标签: prolog artificial-intelligence

以下事实代表这种格式的票房前10部电影

%% movie(title , star, genre, gross) 
movie(battle_los_angeles, aaron_eckhart, action, 13500000). 
movie(rango, johnny_depp, animation, 51100000). 
movie(red_riding_hood, amanda_seyfried, fantasy, 5300000). 
movie(the_adjustment_bureau, matt_damon, sci_fi, 30400000). 
movie(mars_needs_moms, seth_green, animation, 1700000). 
movie(beastly, alex_pettyfer, romance, 13600000). 
movie(hall_Pass, owen_wilson, comdey, 31300000). 
movie(just_go_with_it, adam_sandler, comedy, 91200000). 
movie(unknown, liam_neeson, thriller, 56000000). 
movie(the_king_speech, colin_Firth, history, 126400000). 
  • 定义仅返回电影所有标题的规则。
  • 定义一个只返回电影中所有明星的规则。
  • 定义返回具有相同流派的电影标题的规则。
  • 定义一个基于的两个电影片名之间进行比较的规则 毛。

我的解决方案是:

domains
s=symbol
predicates
nondeterm movie(s,s,s,integer)
nondeterm title(s)
nondeterm star(s)
clauses 
%% movie(title , star, genre, gross) 
movie(battle_los_angeles, aaron_eckhart, action,13500000). 
movie(rango, johnny_depp, animation, 51100000). 
movie(red_riding_hood, amanda_seyfried, fantasy,5300000). 
movie(the_adjustment_bureau, matt_damon, sci_fi,30400000). 
movie(mars_needs_moms, seth_green, animation,1700000). 
movie(beastly, alex_pettyfer, romance,13600000). 
movie(hall_Pass, owen_wilson, comdey,31300000). 
movie(just_go_with_it, adam_sandler, comedy, 91200000). 
movie(unknown, liam_neeson, thriller, 56000000). 
movie(the_king_speech, colin_Firth, history, 126400000). 
title(X):-movie(X,_,_,_).
star(X):-movie(_,X,_,_).

goal
star(X).

我试图解决最后两点,但我不知道如何......

  • 定义返回具有相同流派的电影标题的规则。
  • 定义一个基于的两个电影片名之间进行比较的规则 毛。

1 个答案:

答案 0 :(得分:3)

  

定义仅返回电影所有标题的规则

     

标题(X): - 电影(X,,_)

实际上,该谓词会返回一个电影的标题,但作为一个生成器会多次执行。返回所有这些元素或生成它们之间存在很大差异。

  

定义一个规则,根据总计比较两个电影片名。

你是什么意思“比较规则”?它应该初始化排序类型,如(<)或(>)或(==) - compare_gross?或者它应该初始化元素max_gross的最大值?

最后,我的解决方案是:

%% movie(title , star, genre, gross)
movie(battle_los_angeles, aaron_eckhart, action, 13500000). 
movie(rango, johnny_depp, animation, 51100000). 
movie(red_riding_hood, amanda_seyfried, fantasy, 5300000). 
movie(the_adjustment_bureau, matt_damon, sci_fi, 30400000). 
movie(mars_needs_moms, seth_green, animation, 1700000). 
movie(beastly, alex_pettyfer, romance, 13600000). 
movie(hall_Pass, owen_wilson, comedy, 31300000). 
movie(just_go_with_it, adam_sandler, comedy, 91200000). 
movie(unknown, liam_neeson, thriller, 56000000). 
movie(the_king_speech, colin_Firth, history, 126400000). 

% Define a rule that only returns all the titles of movies.
titles( Titles ) :- 
    findall( X, movie( X, _, _, _), Titles ).

% Define a rule that only returns all the stars of movies.
stars( Stars ) :- 
    findall( X, movie( _, X, _, _), Stars ).

get_by_genre( Genre, Titles ) :-
    findall( X, movie( X, _, Genre, _ ), Titles ).

% Define a rule that compares between two movie titles based on gross
compare_gross( Title1, Title2, Ans ) :-
    movie( Title1, _, _, Gross1 ),
    movie( Title2, _, _, Gross2 ),
    compare( Ans, Gross1, Gross2 ).

max_gross( Title1, Title2, TitleMax ) :-
    movie( Title1, _, _, Gross1 ),
    movie( Title2, _, _, Gross2 ),
    (
        ( Gross1 > Gross2
        , TitleMax = Title1
        )
        ;   
        ( Gross1 =< Gross2
        , TitleMax = Title2
        )
    ),
    !.

你有这个

?- titles(X).
X = [battle_los_angeles, rango, red_riding_hood, the_adjustment_bureau, mars_needs_moms, beastly, hall_Pass, just_go_with_it, unknown|...].

?- stars(X).
X = [aaron_eckhart, johnny_depp, amanda_seyfried, matt_damon, seth_green, alex_pettyfer, owen_wilson, adam_sandler, liam_neeson|...].

?- get_by_genre( animation, X ).
X = [rango, mars_needs_moms].

?- compare_gross( rango, hall_Pass, Ans ).
Ans = (>).

?- max_gross( rango, hall_Pass, Ans ).
Ans = rango.