Guest User

Learning ordered/3

a guest
Nov 10th, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 7.07 KB | None | 0 0
  1. :-module(ordered, [background_knowledge/2
  2.                   ,metarules/2
  3.                   ,positive_example/2
  4.                   ,negative_example/2
  5.                   ]).
  6.  
  7.  
  8. :-use_module(configuration).
  9.  
  10. /** <module> A longer example for Veedrac on HN.
  11.  
  12. Initiate a multi-predicate learning session to learn all the targets
  13. jointly. Use dynamic learning to learn each target incrementally, by
  14. reusing each clause learned in an earlier iteration:
  15.  
  16. ==
  17. ?- time(learn_dynamic([llength/2,shorter/2,ordered/3])).
  18. llength([],0).
  19. llength(A,B):-tail(A,C),p(B,D),llength(C,D).
  20. shorter(A,B):-llength(A,C),llength(B,D),s(C,D).
  21. ordered(A,B,C):-shorter(A,B),shorter(B,C).
  22. % 19,682 inferences, 0.016 CPU in 0.009 seconds (172% CPU, 1259648 Lips)
  23. true.
  24. ==
  25.  
  26. If something fails, check that the following configuration options are
  27. set:
  28.  
  29. ==
  30. ?- list_config.
  31. example_clauses(call)
  32. experiment_file(data/drafts/functions/ordered.pl,ordered)
  33. learner(louise)
  34. max_invented(0)
  35. minimal_program_size(2,inf)
  36. recursion_depth_limit(dynamic_learning,50000)
  37. recursive_reduction(false)
  38. reduction(plotkins)
  39. resolutions(5000)
  40. symbol_range(predicate,[P,Q,R,S,T])
  41. symbol_range(variable,[X,Y,Z,U,V,W])
  42. theorem_prover(resolution)
  43. unfold_invented(false)
  44. true.
  45. ==
  46.  
  47. */
  48.  
  49. % ================================================================================
  50. % Metarule constraints. This is actually a misnomer- those are _clause_
  51. % constraints, used to eliminate unwanted clauses. We use them here
  52. % mainly for aesthetical reasons.
  53. % ================================================================================
  54.  
  55. % This constraint cuts left-recursive shorter/2 clauses that make it
  56. % fiddly to run the learned hypothesis in Prolog.
  57. configuration:metarule_constraints(M,fail):-
  58.     M =.. [m,Id,P|Ps]
  59.     ,Id \= projection
  60.     ,left_recursive(P,Ps).
  61.  
  62. left_recursive(T,[T|_Ps]):-
  63.     !.
  64. left_recursive(T,[T,T|_Ps]):-
  65.     !.
  66.  
  67. % Cuts clause re-definitng llength/2 by shorter/2:
  68. % llength(A,B):-shorter(A,C),s(B,D),llength(C,D).
  69. % This would be fine in an exploratory session, say.
  70. configuration:metarule_constraints(M,fail):-
  71.     M =.. [m,_Id,llength,shorter|_].
  72.  
  73.  
  74. % Avoids definition of shorter/2 by tail/2:
  75. % shorter(A,B):-tail(A,C),tail(B,D),shorter(C,D).
  76. % This compares the tails of two lists, which is the same as comparing
  77. % the full lists, so it's correct, but again it's more useful in an
  78. % exploratory session.
  79. configuration:metarule_constraints(M,fail):-
  80.     M =.. [m,_Id,shorter,tail|_].
  81.  
  82.  
  83. % ================================================================================
  84. % Various configs
  85. % ================================================================================
  86.  
  87. % Sets the maximum number of predicates allowed to be invented.
  88. % It's set to 0 because we don't need to invent any. We still use
  89. % dynamic learning for for the ability to learn incrementally by reusing
  90. % predicates learned (or invented) in earlier learning steps.
  91. :- auxiliaries:set_configuration_option(max_invented, [0]).
  92.  
  93.  
  94. % ================================================================================
  95. % Declarations of Meta-Interpretive Learning problem elements
  96. % Elements of a MIL problem are: metarules, background knowledge (BK)
  97. % and examples.
  98. % ================================================================================
  99.  
  100. % Metarules for the experiment are derived from Prolog program
  101. % skeletons. They are essentially "program recipes" that can be reused
  102. % to construct different programs.
  103. configuration: list_rec_func metarule 'P(x,y):- Q(x,z),R(y,u),P(z,u)'.
  104. configuration: triadic_chain metarule 'P(x,z,y):- Q(x,z), R(z,y)'.
  105. configuration: m1 metarule 'P(x,y):- Q(x,z), R(y,u), S(z,u)'.
  106.  
  107. % Metarule declarations - though specific metarules are declared for
  108. % each learning target, multi-predicate learning allows Louise to use
  109. % any of the declared metarules for any target.
  110. metarules(llength/2,[abduce,list_rec_func]).
  111. metarules(shorter/2,[m1]).
  112. metarules(ordered/3,[triadic_chain]).
  113.  
  114. % Background knowledge (BK) declarations - again, multi-predicate
  115. % learning means any BK predicate can be used for any target. For
  116. % example, we could leave the two latter lists empty and add s/2 to the
  117. % first.
  118. background_knowledge(llength/2, [tail/2,p/2]).
  119. background_knowledge(shorter/2, [s/2]).
  120. background_knowledge(ordered/3, [s/2
  121.                                 ]).
  122.  
  123. % Positive examples are declared separately for each target. They are
  124. % added to the BK during learning, so any learning target can be defined
  125. % in terms of these partial, extensional definitions of other targets.
  126. positive_example(llength/2,E):-
  127.         member(E, [llength([],0)
  128.                   ,llength([a],s(0))
  129.                   %,llength([a,b],s(s(0)))
  130.                   ]
  131.               ).
  132. positive_example(shorter/2,shorter(Xs,Ys)):-
  133.         member(Xs-Ys,[[a]-[b,c]
  134.              ,[1,2]-[3,4,5]
  135.                      ]
  136.               ).
  137. positive_example(ordered/3,ordered(S1,S2,S3)):-
  138.         member([S1,S2,S3],[[[a],[b,c],[d,e,f]]
  139.                           ]
  140.               ).
  141.  
  142. % Negative examples
  143. negative_example(shorter/2,_):-
  144.         fail.
  145. negative_example(llength/2,_):-
  146.         fail.
  147. negative_example(ordered/3,ordered(S1,S2,S3)):-
  148. % These are not strictly needed - but I add them to address Veedrac's
  149. % concern that Louise is not learning a sound program for ordered/2.
  150.         member([S1,S2,S3],[[[a],[b],[c]]
  151.               ,[[a,b,c],[a,b],[c]]
  152.               ]
  153.               ).
  154.  
  155.  
  156. % ================================================================================
  157. % Background knowledge predicate definitions.
  158. %
  159. % BK in Louise consists of arbitrary Prolog programs. Strictly speaking,
  160. % they must have definite datalog heads, but this is not enforced in
  161. % practice. I like to see BK as a kind of programming library,
  162. % containing sub-routines, sub-programs from which the target programs
  163. % can be composed.
  164. %
  165. % Metarules on the other hand must be definite datalog and Louise _does_
  166. % enforce this restriction. The predicates below are used to allow
  167. % Louise to process non-datalog examples, i.e. examples with function
  168. % symbols in their head or body literals. In a sense, the function
  169. % symbols are "hidden away" in the BK. The technique is similar to
  170. % "flattening": Rouveirol, C, Machine Learning, 1994:
  171. %
  172. % https://paperity.org/p/7456046/flattening-and-saturation-two-representation-changes-for-generalization
  173. %
  174. % The predicates below have universal applicability for problems
  175. % involving lists and numbers. head/2 and tail/2 split a list, s/2 and
  176. % p/2 are used to "dereference" Peano number functions.
  177. %
  178. % Note hat head/2 is not actually used above.
  179. % ================================================================================
  180.  
  181. %!      head(?List,?Head) is det.
  182. %
  183. %       True when Head is the head of list List.
  184. %
  185. head([H|_T],H).
  186.  
  187. %!      tail(?List,?Tail) is det.
  188. %
  189. %       True when Tail is the tail of list List.
  190. %
  191. tail([_H|T],T).
  192.  
  193.  
  194. %!      p(?Number, ?Predecessor) is nondet.
  195. %
  196. %       A Peano Number and its Predecessor.
  197. %
  198. p(s(N),N).
  199.  
  200.  
  201. %!      s(?Number, ?Successor) is nondet.
  202. %
  203. %       A Peano Number and its Successor.
  204. %
  205. s(N,s(N)).
  206.  
Advertisement
Add Comment
Please, Sign In to add comment