å§ããåã«ãhabrachitateliããã®è©±é¡ã®è³ªåã«ç°¡åã«çããããšæããŸãã
-ããããŒã°ã¯å®éã«ã©ãã§äœ¿çšãããŠããŸããïŒ
-ãã®ãããªãããžã§ã¯ããååšããäžéšã¯ç¬¬1èšäºã®ã³ã¡ã³ãã§åŒçšãããŸããã ã»ãšãã©ã®ããã°ã©ããŒã絶æã®ããã§ã¯ãªããPrologã奜ãã®ã§ãPrologã«æžãããšãéèŠã§ãã çµå±ã®ãšãããPrologã¯ãUIã®äœæããã¡ã€ã«ã®æäœãªã©ã®ã¿ã¹ã¯ã«ã¯äœ¿çšã§ããŸããã
-ãªããã®ãããªãããžã§ã¯ããå°ãªãã®ã§ããïŒ
-PrologãææããŠããããã°ã©ããŒãéåžžã«å°ãªãã®ã¯ã人ã ããããç 究ããŠããªãã ãã§ãªããå®å šãªããã°ã©ã ãæžãã®ã«ååãªç 究ãããŠããªãããã§ãã äž»ãªçç±ã¯ãã©ã®ãããªç¶æ³ã§ããã䜿çšããã®ãæåãã人ã ãæ確ã«ç解ããŠããªãããã§ãã å€ãã®å ŽåãPrologã®ç±å¿ãªãµããŒã¿ãŒãããŒããŒããããŠã¹ãã³ãã©ãŒãå«ããã¹ãŠãæžã蟌ã¿ãCã®å Žåãããã³ãŒããããã«æªåãããããšãããããŸãã
-ãªãPrologã³ãã¥ããã£ããªãã®ã§ããïŒ
-ããã§ãã ã¢ã«ãããã¯ãªç°å¢ã§éåžžã«å¥œãŸããŠããèšèªã®ç¹ç°æ§ã§ãïŒã»ãšãã©ã®Prologã·ã¹ãã ã¯ããŸããŸãªå€§åŠã§æžãããŠãããã»ãšãã©ãã¹ãŠã®å€§åŠãç¬èªã®PrologãäœæããŠããŸãïŒããã®ãããèšèªã®é©çšæ§ãæãªãããŠãããšèšããŸãã ã³ãã¥ããã£ã¯å°ãããéåžžã«å¿ å®ã§ããããšã¯æ³šç®ã«å€ããŸããã»ãšãã©ã®æ¢ç¥ã®èšèªã¯çŸä»£èšèªã«åæ ãããŸãïŒLispãML-> FïŒãScala; Smalltalk-> JavaãScalaïŒagentsïŒãscript-> RubyïŒããããŒã°ã
ç§ã¯ããã§ååãªå²åŠçæšè«ã§ãããå®éã®äŸãé²ããããšãã§ãããšæããŸã:)
æåŸã«ããã€ãã®ããã«ãè³åã®ã¿ã¹ã¯ãæåŸ ããŠããŸãã
äŸ1-å®å šãªæ°åãæ€çŽ¢ãã
ãã®äŸã§ã¯ãè¿°éšã/ 2ã§ããå¿ èŠããããŸãã Xã¯3 + 1 * 2-å³åŽã®åŒãèšç®ããå·ŠåŽã®å€æ°ã«ä»£å ¥ããŸããããã¯ä»£å ¥ïŒïŒïŒã§ã¯ãããŸããããX = 7ãšããã¹ããŒãã¡ã³ãã§ããåçŽã«èšãã°ãX = 7ãX = 3ãšãããã¬ãŒãºã¯Xåæã«7ãš3ã«ãªãã
åã®ãããã¯ã®åé¡ã®è§£æ±ºçãå¿ èŠã§ãã ã¿ã¹ã¯ã¯ããã¹ãŠã®èªç¶æ°ãé£ç¶ããŠçæããè¿°èªãæžãããšã§ããã解決çã¯æ¬¡ã®ãšããã§ãã
ints(0). ints(X) :- ints(Y), X is Y + 1.
ããã¯å®éã«ã¯ãåŒæ°ãæŽæ°ã§ããããšããã§ãã¯ããæšæºæŽæ°/ 1è¿°èªã®å®£èšããŒãžã§ã³ã§ãã æšæºè¿°èªã®åé¡ã¯ããªã¯ãšã¹ãã«å¯ŸããŠæ£ããæ©èœããããšã§ãïŒ-æŽæ°ïŒ1ïŒããã³ãªã¯ãšã¹ãæŽæ°ïŒXïŒã«å¯ŸããŠã¯æ©èœããŸããã
ã¿ã¹ã¯ ïŒãã¹ãŠã®å®å šãªæ°ãèŠã€ããããã°ã©ã ãäœæããŸãã
解決çã¯æããã§ãããã¹ãŠã®æŽæ°ã調ã¹ãŠãããããå®å šã§ãããã©ããã確èªããŸãããã®æŠç¥ã¯åœä»€åèšèªã«éåžžã«ããé©çšã§ããŸãã解決çæ€çŽ¢ã¢ã«ãŽãªãºã ãããã«æ¢ãæ¹æ³ã«æ°ä»ããŠããŸããããåé¡ãåæããŸããã ããããŒã°ã§ã¯ãåé¡ã®è§£æ±ºçã®æ€çŽ¢ã説æããã®ã§ã¯ãªããåé¡ã®ã¹ããŒãã¡ã³ãã説æããããšããå¿ èŠããããŸãããããè¡ãã«ã¯ãã«ãŒã«ã«åŸããŸãã
解決çãèŠã€ããããã®æé ã説æããããšããªãã§ãã ããããã§ã«è§£æ±ºçãèŠã€ããŠãããšä»®å®ãã解決çãèŠã€ãã£ãããšã確èªããã ãã§ãã
å¥åŠãªããšã§ããããã®æŠç¥ã¯ããŸãæ©èœããŸãã
%% ints(0). ints(X) :- ints(Y), X is Y + 1. %% - 1) 2) perfect_number(X) :- ints(X), Y is X - 1, calculatesum_divisors_till(Sum, X, Y), Sum = X. %% 1- , 2- - , %% 3- - calculatesum_divisors_till(0, _NumberToDivide, 0). calculatesum_divisors_till(Sum, NumberToDivide, Till) :- Till > 0, Rem is NumberToDivide mod Till, Rem = 0, Ts is Till - 1, calculatesum_divisors_till(SumPrev, NumberToDivide, Ts), Sum is SumPrev + Till. calculatesum_divisors_till(Sum, NumberToDivide, Till) :- Till > 0, Rem is NumberToDivide mod Till, Rem > 0, Ts is Till - 1, calculatesum_divisors_till(Sum, NumberToDivide, Ts).
ãœãŒã¹ããã¹ãããã¡ã€ã«ã«è²Œãä»ããã€ã³ã¿ãŒããªã¿ãŒãå®è¡ããŠã³ã³ãã€ã«ããŸãïŒãªã¯ãšã¹ãïŒ-compileïŒ 'path_to_file / perfect_numbers.pl'ã䜿çšïŒããªã¯ãšã¹ããèšè¿°ããŸãïŒ-perfect_numberïŒXïŒ ã泚æããªã¯ãšã¹ãã¯æ¬¡ã®ããã«ãªããŸãã-perfect_numberïŒXïŒãX> 6.ãã®åŸããã¹ãŠã®çãã¯6以äžã«ãªããŸãããã¡ãããããã°ã©ã ã¯æé©ã«åäœããŸããããã§ãã¯èªäœã¯åçŽãªä»åãã䜿çšããŠåçŽåã§ããŸãã
äŸ2-é åã®çæã
ãã®åé¡ãå®åŒåããŠè§£æ±ºããã«ã¯ããªã¹ãã®æŠå¿µãå¿ èŠã§ãã ãªã¹ãã¯èšèªã®åºæ¬çãªæŠå¿µã§ã¯ãããŸããããªã¹ãéã§ã¯ãCã®ãªã³ã¯ãªã¹ããšã®çŽæ¥çãªé¡äŒŒæ§ãåŒãåºãããšãã§ããŸããååž°çãªããŒã¿æ§é ãšããŠã®çšèªã®å®çŸ©ã«æ»ããŸãããã
%% nil list(nil). %% 1 list(t(1, nil)). %% 1, 2, 3 list(t(1, t(2, t(3, nil) ) ) ). %% %% 1. (1- ) %% _ - member(X, t(Y, _)) :- X = Y. %% 2. , member(X, t(_, Tail)) :- member(X, Tail).
å€ãã®äººãéåžžã®ååž°ãèšãã®ã§ããªã¹ããPrologã§ç¹ã«ç¹å¥ã«èŠããªãããã«ããããã®ããã®æ§æç³è¡£ããããŸãïŒnilã¯[]ãtïŒ1ãnilïŒ-[1]ãtïŒ1ãtïŒ2ãnilïŒãšæžãããšãã§ããŸãïŒ-[1ã2]ãtïŒ1ããµããªã¹ãïŒ-[1 | ãµããªã¹ã]ãtïŒ1ãtïŒ2ããµããªã¹ãïŒïŒ-[1ã2 | ãµããªã¹ã]ã çšèªã®å éšåãç°ãªãå Žåãããããããªã¹ãã«ã¯æ§æç³è¡£ã䜿çšããããšããå§ãããŸãïŒã»ãšãã©ã®å Žåãçšèªã¯ããããšåŒã°ããŸãïŒã
%% 1. (1- ) member(X, [X|_]). %% 2. , member(X, [_| Tail]) :- member(X, Tail).
é åãçæãããšããå ã®åé¡ã«æ»ããŸãã é åã®æ°ãnïŒã§ããããšã誰ããå®å šã«èŠããŠããŸãããã»ãšãã©ã®ããã°ã©ãã«ãã®ã¿ã¹ã¯ãäžãããšã誰ããåŠæ ¡ã§ãããæžããŠæ€çŽ¢ã®æ¹æ³ãå¿ããããšãå¿ æ»ã«èŠããŠèšãã§ãããã å¹³åããŠãçŽ20ååŸã«è©ŠããŠèŠãããåŸãã¢ã«ãŽãªãºã ã衚瀺ãããŸããPrologãç¥ã£ãŠããå Žåããã®ã¢ã«ãŽãªãºã ã¯2åã§æžã蟌ãŸãããããŸã£ããæžã蟌ãŸããŸãã:)
ããããŒã°ã®æ±ºå®æ¹æ³ ãã®ã«ãŒã«ã¯ã解決çãèŠã€ããããã§ã¯ãªãã解決çãèŠã€ãã£ãããšã確èªããããã«äœ¿çšããŸãã Permè¿°èªïŒSourceãPermutationïŒ -Sourceã¯ãœãŒã¹ãªã¹ããPermutationã¯çœ®æã§ãã
%% perm([], []). %% 1- , %% , %% %% perm(Source, [Element|Tail]) :- member_list_exclude(Element, Source, SourceExcluded), perm(SourceExcluded, Tail). %% , , 2- %% member_list_exclude %% 1- - , 2- - , 3- - member_list_exclude(X, [X|L], L). member_list_exclude(X, [Y|L], [Y|Ls]) :- member_list_exclude(X, L, Ls).
ãªã¯ãšã¹ãïŒ-permïŒ[1ã2ã3]ãXïŒã¯ãã¹ãŠã®é åãçæããŸãã èå³æ·±ãããšã«ããªã¯ãšã¹ãã¯åŒæ°ã«å¯ŸããŠå¯Ÿç§°ã§ãïŒ-permïŒXã[1ã2ã3]ïŒããã®ãªã¯ãšã¹ãã¯ããªãŒãºããæ©èœããããã«ã¯ãpermã§member_list_excludeãšpermã亀æããå¿ èŠããããŸãã
äŸ3-çµã¿åããã®çæã
å®è£ ãç°¡åãªçµã¿åããã®çæã¯ãé åã®çæã«äŒŒãŠããŸãã è¿°èªã¡ã³ããŒ/ 2ãå¿ èŠã§ã-èŠçŽ ã¯ãªã¹ãã«å±ããŸãã 2ã€ã®ãªã¹ãããããšä»®å®ããŸãïŒ1çªç®ã®ãœãŒã¹ãªã¹ãã2çªç®-ææ¡ãããçµã¿åãããçµã¿åããã®æ£ç¢ºæ§ã確èªããå¿ èŠããããŸãã çµã¿åããé ç®ã¯ããœãŒã¹ãªã¹ãã®é åºã§é 眮ãããŸãã
member(X, [X|_]). member(X, [_|L]) :- member(X, L). comb([], []). %% 1 : 1- comb([X|List], [X|Tail]) :- comb(List, Tail). %% 2 : , %% 1- comb([_|List], Tail) :- comb(List, Tail).
äŸ4-ãœãŒãã
ãã®äŸãååã«è©³çŽ°ã«æ€èšããäž»èŠãªãœãªã¥ãŒã·ã§ã³ã®æé©åãè©Šã¿ãŸãã Prologã«æžã蟌ãããã»ã¹ã¯æ¬¡ã®ãšããã§ãïŒ1ïŒåé¡ã®åæã®èª¬æãšåŸ¹åºçãªè§£æ±ºçã®ååŸ2ïŒå³åŽã®è¿°èªã®åé 眮ã«ããè«ççãªæé©å3ïŒåçŽåããããã§ãã¯ã®å°å ¥ãŸãã¯äžèŠãªæ¡ä»¶ã®åé€ã®è«ççãªæé©å4ïŒã«ãããªãã«ãããã¥ãŒãªã¹ãã£ãã¯ã®å°å ¥ãšåã ã®ã±ãŒã¹ã®æé©å
ãªãã·ã§ã³1ã䞊ã¹æ¿ãã¯åçŽã§ãã䞊ã¹æ¿ããããé åã®æåã®èŠçŽ ã¯æå°éã«æããæ®ãã®èŠçŽ ã¯äžŠã¹æ¿ããå¿ èŠããããŸãã æåã®é åã¯ãœãŒã¹ã§ã2çªç®ã®é åã¯ãœãŒãããããœãŒã¹ã§ãã
sort([], []). sort(List, [Min|SortRest]) :- min_list_exclude(Min, List, Exclude), sort(Exclude, SortRest). %% , min_list_exclude(M, [M], []). min_list_exclude(Min, [M|L], ExcludeRes) :- min_list_exclude(Ms, L, Exclude), find_result(result(M, L), result(Ms, [M|Exclude]), result(Min, ExcludeRes)). %% find_result(result(M, L), result(Ms, _), result(M, L)):- M < Ms. find_result(result(M, _), result(Ms, Exclude), result(Ms, Exclude)):- Ms =< M.
ãã®ã¢ã«ãŽãªãºã ã®è€éãã¯äºæ¬¡é¢æ°ã§ãããäž»ãªåé¡ã¯ãæçšãªæ å ±ãä¿åããã«æå°èŠçŽ ãæ¢ããã³ã«çºçããããšã«æ°ä»ããããããŸããã
ãŸãããœãŒããããé åã®æåã®èŠçŽ ãäœã§ãããã決å®ããããšããŠããããšã«æ³šæããŠãã ããã
ãªãã·ã§ã³2.ã¯ã€ãã¯ãœãŒãã åé¡ã2çªç®ã®åŽããèŠãŠããœãŒããããé åå ã®ãªã¹ãã®æåã®èŠçŽ ã®äœçœ®ã決å®ããŠã¿ãŸãããïŒå ã®é åã«ååž°ãé©çšããŸãïŒã
sort_b([], []). sort_b([T|R], List) :- split(T, R, Less, Great), sort_b(Less, LessSort), sort_b(Great, GreatSort), append(LessSort, [T|GreatSort], List). %% 2 split(_, [],[], []). split(T, [M|R],[M|Less], Great) :- M < T, split(T,R, Less,Great). split(T, [M|R],Less, [M|Great]) :- M >= T, split(T,R, Less,Great). %% 2 append([], M, M). append([L|Left], Right, [L|Res]) :- append(Left, Right, Res).
ã¯ã€ãã¯ãœãŒãã¯ããã«ãœãŒãããã確å®ã«é«éã§ããããããœãŒãçµæãæ¹åãããŠããããšã«æ°ä»ãã§ãããã çµæãããã«æ¹åããããã«ãããŒãžãœãŒããæãåºãããšãã§ããŸããããŒãžãœãŒãã¯ãããã®å ŽåãOïŒn lg nïŒãè¿ããŸãããæ®å¿µãªãããã®ãœãŒãã¯é åã®ã¿ã«é©çšãããäœæ¥ãããªã³ã¯ãªã¹ãã«ã¯é©çšãããŸããã ã¹ãã¬ãŒãžã«è¿œå ã®ããŒã¿æ§é ã䜿çšããå¯äžã®ãªãã·ã§ã³ã¯ããªãŒã§ãã
ãªãã·ã§ã³3.ãã€ããªããªãŒã䜿çšãã䞊ã¹æ¿ãã
ãã®çš®ã®ãœãŒãã§ã¯ãå ã®ãªã¹ãããã€ããªããªãŒã«å€æããå·ŠåŽã®ããªãŒãã©ããŒãµã«ã䜿çšããŠããœãŒããããé åãååŸããŸãã ããªãŒã¯ãååž°çšèªããªãŒïŒObjectãLeftSubTreeãRightSubTreeïŒã§è¡šãããŸãã
sort_tree([], nil). sort_tree([X|L], Tree) :- sort_tree(L, LTree), plus(X, LTree, Tree). %% plus(X, nil, tree(X, nil, nil)). plus(X, tree(O, L, R), tree(O, ResL, R)) :- O >= X, plus(X, L, ResL). plus(X, tree(O, L, R), tree(O, L, ResR)) :- O < X, plus(X, R, ResR). sort_t(X, Y) :- sort_tree(X, Tree), tree_list(Tree, Y). append_list([], L, L). append_list([X|L], R, [X|T]) :- append_list(L, R, T). tree_list(nil, []). tree_list(tree(X, L, R), List) :- tree_list(L, ListL), tree_list(R, ListR), append_list(ListL, [X|ListR], List).
ãªãã·ã§ã³4.平衡äºåæšã䜿çšãããœãŒãã
ãã€ããªããªãŒã䜿çšããåé¡ã¯ãã¯ã€ãã¯ãœãŒãã䜿çšããå Žåãšåãã§ãã ãã®æ¹æ³ã¯ãæé©ãªããã©ãŒãã³ã¹ãä¿èšŒãããã®ã§ã¯ãããŸããã äºåæšã®å Žåãæšã¯äžåè¡¡ã§ããå¯èœæ§ããããèŠçŽ ãè¿œå ããæé ã¯å¯Ÿæ°ã§ã¯ãªãç·åœ¢ã«ããããšãã§ããŸãã ç¹ã«ãã®ããã«ãããªãŒãã©ã³ã·ã³ã°æé ãå®è¡ãããŸãã以äžã§ã¯ãæ £ããããã«ã AVLããªãŒã䜿çšããã¢ã«ãŽãªãºã ã瀺ããŸãã
sort_btree(X, Y) :- sort_tree(X, Tree), tree_list(Tree, Y). tree_list(nil, []). tree_list(tree(X, L, R, _), List) :- tree_list(L, ListL), tree_list(R, ListR), append(ListL, [X|ListR], List). sort_tree([], nil). sort_tree([X|L], Tree) :- sort_tree(L, LTree), plus_tree(X, LTree, Tree). construct_tree(A, AL, AR, tree(A, AL, AR, ADepth)) :- diff(AL, AR, _, ADepth). diff(AL, AR, ADiff, ADepth) :- depth_tree(ALs, AL), depth_tree(ARs, AR), ADiff is ALs - ARs, max_int(ALs, ARs, AD), ADepth is AD + 1. max_int(A, B, A) :- A > B. max_int(A, B, B) :- A =< B. append([], L, L). append([X|L], R, [X|T]) :- append(L, R, T). depth_tree(0, nil). depth_tree(X, tree(_, _, _, X)). plus_tree(X, nil, tree(X, nil, nil, 1)). plus_tree(X, tree(O, L, R, _), Res) :- O >= X, plus_tree(X, L, ResL), diff(ResL, R, Diff, Dep), balance_tree(tree(O, ResL, R, Dep), Diff, Res). plus_tree(X, tree(O, L, R, _), Res) :- O < X, plus_tree(X, R, ResR), diff(L, ResR, Diff, Dep), balance_tree(tree(O, L, ResR, Dep), Diff, Res). %% No rotations balance_tree(Tree, ADiff, Tree) :- ADiff < 2, ADiff > -2. %% Small right rotation balance_tree(tree(A, tree(B, BL, BR, _), AR, _), ADiff, Result) :- ADiff > 1, diff(BL, BR, BDiff, _), BDiff >= 0, construct_tree(A, BR, AR, ASubTree), construct_tree(B, BL, ASubTree, Result). %% Big right rotation balance_tree(tree(A, tree(B, BL, BR, _), AR, _), ADiff, Result) :- ADiff > 1, diff(BL, BR, BDiff, _), BDiff < 0, BR = tree(C, CL, CR, _), construct_tree(B, BL, CL, BSubTree), construct_tree(A, CR, AR, ASubTree), construct_tree(C, BSubTree, ASubTree, Result). %% Small left rotation balance_tree(tree(A, AL, tree(B, BL, BR, _), _), ADiff, Result) :- ADiff < -1, diff(BL, BR, BDiff, _), BDiff =< 0, construct_tree(A, AL, BL, ASubTree), construct_tree(B, ASubTree, BR, Result). %% Big left rotation balance_tree(tree(A, AL, tree(B, BL, BR, _), _), ADiff, Result) :- ADiff < -1, diff(BL, BR, BDiff, _), BDiff > 0, BL = tree(C, CL, CR, _), construct_tree(B, CR, BR, BSubTree), construct_tree(A, AL, CL, ASubTree), construct_tree(C, ASubTree, BSubTree, Result).
ãã®äŸã¯ãPrologã§ã®å®è£ ã«ã¯ååãªè¡šçŸåããããŸããããäžèŠæš¡ã®ããã°ã©ã ã®ã¢ã€ãã¢ãæäŸããŸãã ãã¬ãŒãã³ã°ã®ããã«ãããã«ãœãŒããŸãã¯æ¿å ¥ã«ãããœãŒããå®è£ ã§ããŸããããã¯èªè ã®è£éã«ä»»ãããŠããŸãã
äŸ5-茞è¡ã®åé¡ã
次ã®ã¿ã¹ã¯ãšããŠãå€å žçãªç¶æ ã®åé¡ãæ€èšããŠãã ãã;ãã®ã¿ã¹ã¯ã¯ãPrologã䜿çšããå©ç¹ãã¯ããã«ããåæ ããŠããŸãã åé¡ã®äžè¬çãªå£°æïŒæ°Žã®å ¥ã£ãããã€ãã®å®¹åšãããå Žåã茞è¡ã«ãã£ãŠç¹å®ã®å®¹åšã«äžå®éã®æ°ŽãåŸãããšãå¿ èŠã§ãã ããšãã°ã容éã12ãªããã«ã8ãªããã«ã5ãªããã«ã®æ°Žå·®ãã3ã€çšæããæåã®æ°Žå·®ããã€ãŸã12ãªããã«ãå®å šã«æºãããã¿ã¹ã¯ã6ãªããã«ã«èšå®ããŸãã ãŸãããã³ãšçŽã§ãã®åŠæ ¡ã®åé¡ã解決ããŠã¿ãŠãã ãã :)
ããŸããŸãªã¢ã«ãŽãªãºã ãçæããŠã¿ã¹ã¯ã«é©çšããåã«ããŸãPrologã®èŠ³ç¹ããæ¡ä»¶ãæžãçŽããŸãããã 容éãsosudïŒIdãMaximumCapacityãCurrentCapacityïŒãšããçšèªãšããŠèª¬æããã·ã¹ãã ã®ç¶æ ã容éã®ãªã¹ããšããŠèª¬æããŸãã äŸ[sosudïŒ1ã12ã12ïŒãsosudïŒ2ã8ã0ïŒãsosudïŒ3ã5ã0ïŒ] ã 次ã«ããªã¯ãšã¹ãã«ã€ããŠèª¬æããŸãã
%% solve_pr_wo(InitialState, Goal, Steps). :- solve_pr_wo([sosud(1, 12, 12), sosud(2, 8, 0), sosud(3, 5, 0)], sosud(X, _, 6), Steps).
ç®æš= sosudïŒ_ã_ã6ïŒã§ããããšã«æ³šæããŠãã ãããã€ãŸããæ£ç¢ºã«6ãªããã«ãå«ãããã«ãè¹ã®å®¹éã¯éèŠã§ã¯ãããŸããã
ããã§ãã¹ãŠãããã£ãã®ã§ãã¹ããããStepså€æ°ã§æå®ãããŠãããšä»®å®ããŠããœãªã¥ãŒã·ã§ã³ãæ€èšŒããæ¹æ³ã説æããŸãã
%% , %% solve_pr_wo(State, Goal, []) :- member(Goal, State). %% Sosud Sosud2 %% ResSosud, ResSosud2. %% : %% mv(sosud(1, 12, 12) -> sosud(2, 8, 0), sosud(1, 12, 4) -> sosud(2, 8, 8)). solve_pr_wo(State, Goal, [mv(Sosud -> Sosud2 , ResSosud -> ResSosud2)| Steps]) :- %% , %% member(Sosud, State), member(Sosud2, State), not(Sosud = Sosud2), %% , %% 4 mv(Sosud, Sosud2, ResSosud, ResSosud2), %% replace(Sosud, State, ResSosud, State2), replace(Sosud2, State2, ResSosud2, StateX), %% solve_pr_wo(StateX, Goal, Steps). %% %% replace(ElementToReplace, InList, ElementToReplaceWith, OutList). replace(S, [S|L], X, [X|L]). replace(S, [T|L], X, [T|Ls]) :- replace(S, L, X, Ls). %% - 2 %% %% mv(sosud(Id1, Max1, Current), sosud(Id2, Max2, Current2), sosud(Id1, Max1, 0), sosud(Id2, Max2, Current3)) :- Current > 0, Current3 is Current2 + Current, Current3 =< Max2. %% mv(sosud(Id1, Max1, Current), sosud(Id2, Max2, Current2), sosud(Id1, Max1, Current3), sosud(Id2, Max2, Max2)) :- Current > 0, Current3 is Current2 + Current - Max2, Current3 >= 0.
ããã«ã茞è¡ã®æé ãæ£ããå Žåã¯ããããã説æããå 容ã確èªã§ããªãããããã¡ã€ã³ã®æ€èšŒã¯å¿ èŠãªãããã«æããããããããŸããã å®éãæ€èšŒã®å®å šæ§ã¯ãããã°ã©ã ãæ£ããåçãäžããå¯èœæ§ãå€§å¹ ã«åäžãããŸãã ããã¯èšã£ãŠããããã°ã©ã ãéå°ã«ãã§ãã¯ãããšæ©èœããå Žåã«ãã£ãŠã¯æé©åããªãå Žåãããããã«æé©åãããŸãããããã°ã©ã ã®ãã§ãã¯ãäžååãªå Žåãå®å šã«èª€ã£ãçµæãçæãããããäžéšã®å ¥åããŒã¿ã§ããªãŒãºããŸãã
ããŠãããã°ã©ã ã®èª¬æãæžãããŠããŸã-ããªãã¯ãããå®è¡ããããšãã§ããŸãã ããã°ã©ã ãæ©èœããªãããšã«é©ããªãã§ãã ãããåã«ãã³ã°ããŸã:)ããã¯èŠãç®ã»ã©æªããããŸãããããã°ã©ã ããã³ã°ããªãã£ãå Žåãããã¯æ£ããçããäžããããã§ãã ãªããã³ã°ããã®ããç解ããå¿ èŠããããŸããããã§ãPrologãã«ãŒã«ãå±éããŠè§£æ±ºçãèŠã€ããæ¹æ³ãç解ããã®ã«åœ¹ç«ã¡ãŸãã å®éãsolve_pr_wo-> solve_pr_woãååž°çã«åŒã³åºããã³ã«ã2ã€ã®ã¡ã³ããŒ/ 2ã€ã®è¿°èªãåŒã³åºããåžžã«åã1çªç®ãš2çªç®ãçæããããšãç解ããããã«ãæ倧10åã®æ»ãç¹ãèšæ¶ã§ããé ãæã€å¿ èŠã¯ãããŸãã2çªç®ã®è¹ïŒè¿°éšã¯ããã¯ãã©ããã³ã°ãåŒãèµ·ããããã¡ã³ããŒã1çªç®ãš1çªç®ã®è¹ãéžæããããšãèš±å¯ããŸããïŒã ã€ãŸããã¢ã«ãŽãªãºã ã¯åžžã«ç¬¬1ãã第2ã«ãããã³ãã®éã«æµããŸãã
ãã®äžæ¡çã解決ããããã«ãåãã¢ã¯ã·ã§ã³ã2åå®è¡ããããšãçŠæ¢ããããšãã€ãŸããç¶æ ã®å±¥æŽãä¿æããæ¡ä»¶ããã§ã«æºããããŠããå Žåã¯åå ¥åãçŠæ¢ããããšãããã«æãæµ®ãã³ãŸãã ç¹°ãè¿ããé€ããå€ãã®èš±å®¹å¯èœãªèŒžè¡æŠç¥ãçµã蟌ãããšãããããŸããã å®éãæŠç¥ã®ã»ãããçµã蟌ãã§ããã·ã¹ãã ã®èš±å®¹å¯èœãªç¶æ ã®ã»ãããã€ãŸã解æ³ã¯çµã蟌ã¿ãŸããããããã¯èšŒæããããšã¯é£ãããããŸããã
ç¶æ ã®ãªã¹ããšãœãªã¥ãŒã·ã§ã³ãåŒã³åºãåäžã®è¿°èªãå«ãããã°ã©ã ã®ãã«ããŒãžã§ã³ïŒ
write_list([]). write_list([X|L]) :- writeln(X), write_list(L). solution :- solve_pr([sosud(1, 12, 12), sosud(2, 8, 0), sosud(3, 5, 0)], sosud(_, _, 6), [], Steps), write_list(Steps). replace(S, [S|L], X, [X|L]). replace(S, [T|L], X, [T|Ls]) :- replace(S, L, X, Ls). %% , %% , , solve_pr(State, Goal, _, [State]) :- member(Goal, State). solve_pr(State, Goal, History, [State|Steps]) :- member(Sosud, State), member(Sosud2, State), not(Sosud = Sosud2), mv(Sosud, Sosud2, ResSosud, ResSosud2), replace(Sosud, State, ResSosud, State2), replace(Sosud2, State2, ResSosud2, StateX), %%% not(member(StateX, [State|History])), solve_pr(StateX, Goal, [State|History], Steps). %% mv(sosud(_Id, Max, Current), sosud(_Id2, Max2, Current2), ...,...). %% mv(sosud(Id1, Max1, Current), sosud(Id2, Max2, Current2), sosud(Id1, Max1, 0), sosud(Id2, Max2, Current3)) :- Current > 0, Current3 is Current2 + Current, Current3 =< Max2. %% mv(sosud(Id1, Max1, Current), sosud(Id2, Max2, Current2), sosud(Id1, Max1, Current3), sosud(Id2, Max2, Max2)) :- Current > 0, Current3 is Current2 + Current - Max2, Current3 >= 0.
ãã¹ãŠãçŸåšæ©èœããŠããŸãïŒ æŒç¿ãšããŠãæé©ãªã¹ãããæ°ã§èŒžè¡ãèŠã€ããããã«ããã°ã©ã ãå€æŽã§ããŸãã ãããã®ã¿ã¹ã¯ãè©Šãããšãã§ããŸãã
泚 ïŒåœä»€åããã°ã©ãã³ã°ã®ç±å¿ãªãµããŒã¿ãŒã¯ããã¹ãŠã®ç¶æ ãæ»ãå€ïŒæ·±ãã®ãã¹ïŒã§å埩ããã ãã§ããããã¥ãŒãªã¹ãã£ãã¯ã䜿çšããã«å®å šã«æ£ããããšã«æ°ä»ãã§ãããã äºå®ãPrologã§ã®æèã¯ãç·åœããã§è¡ãã®ã§ã¯ãªããåé¡ã®èª¬æãšãœãªã¥ãŒã·ã§ã³ã®æ€èšŒã®èª¬æã§è¡ãå¿ èŠããããå¿ èŠãªå Žåã¯åžžã«æé©åã®ããã«èšç®ã®å¿ é æ§ã«æ»ãå¿ èŠããããŸãïŒ èªç¶ã®äºéæ§ã¯ãã€ãã¹ã§ã¯ãªãããã©ã¹ã§ãã ãŸãã倧èŠæš¡ãªPrologã·ã¹ãã ã¯ããªã¿ãŒã³ã®ããç¶æ ãæ€çŽ¢ããã®ã«éåžžã«ããé©åããŠããããšã«ã泚æããŠãã ããã
ãããã«
ãã®èšäºã§èª¬æããã¿ã¹ã¯ã¯ãPrologã§ã®ããã°ã©ãã³ã°ã®ç 究ã§ãã ãããã®ã»ãšãã©ã¯çŽ10ã15è¡ããããããPrologã®ããã°ã©ãã¯ååãªé »åºŠã§ããããã¡ã¢ãªããåçŸã§ããŸãã ãããŠãããªãã¯ééããªããããã«æ»ããªããã°ãªããŸãããããã¯ããã°ã©ãã³ã°ã®æè¡ãæãèµ·ããããããã§ãïŒCã§ã®ã¯ã€ãã¯ãœãŒãã®ããã«ïŒã æ¯æ¥ã®äœ¿çšã®ããã®ããè€éã§é©çšãããã¿ã¹ã¯ã«ã€ããŠã¯ãåŸã§èª¬æããŸãã
æåŸã«ãè³åã«ã¯ãã§ã«2ã€ã®ã¿ã¹ã¯ããããŸã ã
- ãåãã®ããã«ãæ©èœçããã³è«ççãªæ¹æ³ã§ãããããæ¹æ³ã§å¯äœçšã®ããããã°ã©ã ãé¿ããã¢ããã«ã©ããããç¹å¥ãªæŠå¿µãèãåºããŸãã æšæºçãªåé¡ã¯ãå€çã®åé¡ã§ããããšãã°ããã¡ã€ã«ãžã®ããŒã¿ã®æžã蟌ã¿ããã¡ã€ã«ãžã®èšé²ã®ããŒã«ããã¯ããœã±ããã«ããæ°ãã€ãã®éä¿¡ã®ãã£ã³ã»ã«ãäžå¯èœã§ãããããããã¯ãã©ããã³ã°ã¯æ£ããæ©èœããŸããã 1ã€ã®ã¢ããã€ã¹-ãããã®ç®çã«Prologã䜿çšããªãã§ãã ããã ããããPrologã«éåžžã«é©ããç¹å®ã®è¿°èªããããŸãããå¯äœçšããããŸãã assertïŒassertaãassertzïŒã®äŸïŒã«ãŒã«ïŒãã¡ã¯ãïŒããŒã¿ããŒã¹ã«åçŽãªã«ãŒã«ïŒãã¡ã¯ãïŒãè¿œå ããŸãã assertïŒprimeïŒ3ïŒïŒã®äŸ ïŒ3ãçŽ æ°ã§ãããšããäºå®ãšã¯ãšãªïŒ-primeïŒXïŒã¯ããœãŒã¹ããã°ã©ã ã空ã§ãã£ãŠã3ãè¿ãããã«ãªããŸããã
ç®ç ïŒ assertã®å®£èšããŒãžã§ã³ãèšè¿°ããŸããã€ãŸããããã¯ãã©ããã³ã°ããã°ã©ã ãè¿ããšããäºå®ã¯ã¡ã¢ãªã«æ®ããŠããã¹ãã§ã¯ãªããè«ççãªä»®å®ãšããŠæ©èœããå¿ èŠããããŸãã
æäœäŸ ïŒã¯ãšãªcïŒXïŒã¯ã次ã®ããã°ã©ã ã«å¯ŸããŠ1ã€ã®æ°å€4ãè¿ãå¿ èŠããããŸãïŒ
a(X) :- b(Y), X is Y + 1 . c(X) :- my_assert(b(3)), a(X). c(X) :- b(X).
- 2 , â . , . , â , â . , â . , , â .
: a/1 ( , ), subset_a/1, , a.
: subset_a(X) X = [], X = [1], X = [2], X = [1, 2] ( ):
a(1). a(2). subset_a(X) :- ....?
ãæž èŽããããšãããããŸããã