1 / 27

Factorial

Factorial. factorial_of(0,1). factorial_of(N,Ans) :-N>0, N1 is N-1, % “ is ” eval numerical expressions like N-1. factorial_of(N1,PrevAns), Ans is PrevAns*N. ?- factorial_of(5,Ans).  ?- factorial_of(3,6). ?-factorial_of(X,120). Integer Operation.

rollo
Télécharger la présentation

Factorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Factorial • factorial_of(0,1). • factorial_of(N,Ans) :-N>0, • N1 is N-1, % “is” eval numerical expressions like N-1. • factorial_of(N1,PrevAns), • Ans is PrevAns*N. • ?- factorial_of(5,Ans).  • ?- factorial_of(3,6). • ?-factorial_of(X,120). Artificial Intelligence a modern approach

  2. Integer Operation • Increment • incr(x, f(X)). • Sum • sum(0,Y,Y). • sum(f(X),Y,f(Z)):- sum(X,Y,Z). • Mul • mul(0, Y,0). • mul(f(X),Y,Z):- mul(X,Y,H),sum(H,Y,Z). Artificial Intelligence a modern approach

  3. Factorial • factorial_of(0,f(0)). • factorial_of(f(X),A) :- factorial_of(X,P), mul(P,f(X),A). • ?-factorial_of(X, f(f(f(f(f(f(0))))))). Artificial Intelligence a modern approach

  4. From Number to Function representation • n2f(0, 0). • n2f(X,f(Y)):- X>0,X1 is X-1,n2f(X1,Y). • f2n(0,0). • f2n(f(Y),X):- f2n(Y,X1),X1 is X+1. Artificial Intelligence a modern approach

  5. Monkey And Bananas • In the M&B problem we have: • objects: a monkey, a box, the bananas, and a floor. • locations: we’ll call them a, b, and c. • relationsof objects to locations. For example: • the monkey is at location a; • the monkey is on the floor; • the bananas are hanging; • the box is in the same location as the bananas. • To represent these relations we need to choose appropriate predicates and arguments: • at(monkey,a). • on(monkey,floor). • status(bananas,hanging). • at(box,X), at(bananas,X). a

  6. Monkey And Bananas • A hungry monkey is in a room. • Suspended from the roof, just out of his reach, is a bunch of bananas. • In the corner of the room is a box. • The monkey desperately wants the bananas but he can’t reach them. • What shall he do?

  7. After several unsuccessful attempts to reach the bananas, the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them. The hungry monkey is now a happy monkey.

  8. Situation Calculus • In situation Calculus • An action is represented by a function that takes a state (or situation) of the world s and return an other state of the world s’ • We have an initial state s0 and by using different actions e.g. push we obtain different state. (situation) • push(Mo,Bl,c, push(Mo,Bl,a,s0)) • Status is different from situation Artificial Intelligence a modern approach

  9. Situation Calculus • In situation Calculus • An action is represented by a function that takes a state (or situation) of the world s and return an other state of the world s’ • We have an initial state s0 and by using different actions e.g. push we obtain different state. (situation) • push(Mo,Bl,c, push(Mo,Bl,a,s0)) • Status is different from situation ? • Different situations represent the same status. Artificial Intelligence a modern approach

  10. Situation Calculus • In situation Calculus • An action is represented by a function that takes a state (or situation) of the world s and return an other state of the world s’ • We have an initial state s0 and by using different actions e.g. push we obtain different state. (situation) • push(Mo,Bl,c, push(Mo,Bl,a,s0)) • Status is different from situation ? • Different situations represent the same state. Artificial Intelligence a modern approach

  11. Situation Calculus (Action, Situation) • In situation Calculus • An action is represented by a function that takes a state (or situation) of the world s and return an other state of the world s’ • We have an initial state s0 and by using different actions e.g. push we obtain different state. (situation) • push(Mo,Bl,c, push(Mo,Bl,a,s0)) • push, goto, grasp,climb • Status is different from situation ? • Different situations represent the same state. • A situation is represented by complex term • (nested function) Artificial Intelligence a modern approach

  12. Situation Calculus (Action, Situation) • In situation Calculus • a state is represented by using predicates • on, has, at • Crucial point the status may changes but FOL use monotonic reasoning Artificial Intelligence a modern approach

  13. Situation Calculus (State Predicates) • In situation Calculus • a state is represented by using predicates • on, has, at • Crucial point the status may changes but FOL use monotonic reasoning • Solution this property of states depends by the specific situation: the last arguments is a situation element • at(x,y,s) says that the element x is is in the same position of y in the situation s • E.g. suppose that at the first time block and monkey are in the same position. • at(Mo,Bl, s0) is true • at(Mo,Bl, push(Mo,Bl,a,s0)) not necessary true. Artificial Intelligence a modern approach

  14. Situation Calculus (State Predicates) • In situation Calculus • a state is represented by using predicates (FLUENT) • on, has, at • Crucial point the status may changes but FOL use monotonic reasoning • Solution this property of states depends by the specific situation: the last arguments is a situation element • at(x,y,s) says that the element x is is in the same position of y in the situation s • E.g. suppose that at the first time block and monkey are in the same position. • at(Mo,Bl, s0) is true • at(Mo,Bl, push(Mo,Bl,a,s0)) not necessary true. Artificial Intelligence a modern approach

  15. Situation Calculus (Change and Frame Axiom) • Frame Axion • At(x,y,s) -> At(x,y,push(x,y,loc,s)) • Change Axiom • At(x,y,s) -> At(y,loc,push(x,y,loc,s)) • FOR EACH ACTION WE KNOW WHAT CHANGE A WHAT REMAIN THE SAME • Why not just assume that a fluent does not change unless we know itwill change? Artificial Intelligence a modern approach

  16. SITUATION CALCULUS EXAMPLE • INIT: -On(M,B,s_0) • Change axioms: • 1. -On(M,B,s) -> At(M,B,goto(M,B,s)) • 2. On(M,B,s) & At(B,L,s) -> Has(M,F,grasp(M,F,s)) • 3. At(M,B,s) -> At(B,loc,push(M,B,loc,s) • 4. At(M,B,s) -> On(M,B,climb(M,B,s)) • Frame axioms: • 5. At(M,B,s) -> At(M,B,push(M,B,loc,s)) • 6. At(B,loc,s) -> At(B,loc,climb(M,B,s))  • NG (negated goal): • (Forall s) - Has(M,F,s) • (EXIST s) Has(M,F,s) Artificial Intelligence a modern approach

  17. DNF • DNF • 1. On(M,B,s) v At(M,B,goto(M,B,s)) • 2. -On(M,B,s) v -At(B,L,s) v Has(M,F,grasp(M,F,s)) • 3. -At(M,B,s) v At(B,loc,push(M,B,loc,s)) • 4. -At(M,B,s) v On(M,B,climb(M,B,s)) • 5. -At(M,B,s) v At(M,B,push(M,B,loc,s)) • 6. -At(B,loc,s) v At(B,loc,climb(M,B,s)) • -On(M,B,s_0) • -Has(M,F,s) • This is not so efficient and no effective for prolog recursive negation non-safe and constraint Artificial Intelligence a modern approach

  18. Strips • Once we have decided on appropriate state predicates we need to represent the Initial and Goal states. • Initial State: on(monkey, floor), on(box, floor), at(monkey, a), at(box, b), at(bananas, c), status(bananas, hanging). • Goal State: on(monkey, box), on(box, floor), at(monkey, c), at(box, c), at(bananas, c), status(bananas, grabbed). • Only this last state can be known without knowing the details of the Plan (i.e. how we’re going to get there).

  19. Initial and Goal State (Strips) • Once we have decided on appropriate state predicates we need to represent the Initial and Goal states. • Initial State: on(monkey, floor), on(box, floor), at(monkey, a), at(box, b), at(bananas, c), status(bananas, hanging). • Goal State: on(monkey, box), on(box, floor), at(monkey, c), at(box, c), at(bananas, c), status(bananas, grabbed). • Only this last state can be known without knowing the details of the Plan (i.e. how we’re going to get there).

  20. Representing Operators (Strips) • STRIPS operators are defined as: • NAME: How we refer to the operator e.g. go(Agent, From, To). • PRECONDITIONS: What states need to hold for the operator to be applied. e.g. [at(Agent, From)]. • ADDLIST: What new states are added to the world as a result of applying the operator e.g. [at(Agent, To)]. • DELETE LIST: What old states are removed from the world as a result of applying the operator. e.g. [at(Agent, From)]. • We will specify operators within a Prolog predicate opn/4: opn( go(Agent,From,To), [at(Agent, From)], [at(Agent, To)], [at(Agent, From)] ) :-agent(Agent),cell(From),cell(To). Name Preconditions Add List Delete List

  21. The Frame Problem • When representing operators we make the assumption that the only effects our operator has on the world are those specified by the add and delete lists. • In real-world planning this is a hard assumption to make as we can never be absolutely certain of the extent of the effects of an action. • This is known in AI as the Frame Problem. • Real-World systems, such as Shakey, are notoriously difficult to plan for because of this problem. • Plans must constantly adapt based on incoming sensory information about the new state of the world otherwise the operator preconditions will no longer apply. • The planning domains we will be working in our Toy-Worlds so we can assume that our framing assumptions are accurate.

  22. All Operators

  23. Finding a solution (Strips) • Look at the state of the world: • Is it the goal state? If so, the list of operators so far is the plan to be applied. • If not, go to Step 2. • Pick an operator: • Check that the preconditions are satisfied. If the check fails, backtrack to get another operator. • Apply the operator: • Make changes to the world: delete from and add to the world state. • Add operator to the list of operators already applied. • Check that the state is not already obtained(to stop looping). If the check fails, backtrack to get another operator. • Go to Step 1.

  24. Finding a solution in Prolog • The main work of generating a plan is done by the solve/4 predicate. % First check if the Goal states are a subset of the current state. solve(State, StateHistory, Goal, Plan, Plan):- is_subset(Goal, State) solve(State, StateHistory, Goal, Sofar, Plan):- opn(Op, Precons, Delete, Add), % get first operator is_subset(Precons, State), % check preconditions hold delete_list(Delete, State, Remainder), % delete old states append(Add, Remainder, NewState), % add new states \+ member(NewState, StateHistory), % check for looping solve(NewState,[NewState|StateHistory] Goal, [Op|Sofar], Plan).% recurse • GOAL: ?solve(InitialState,[Initialstate],Finalstate,[],Plan). • On first glance this seems very similar to a normal depth-first search algorithm (unifies with first possible move and pursues it)

  25. List Operator • member(X,[X|T]). • member(X,[Y|T]):- \+ X=Y, member(X,T). • remove(X,[],[]). • remove(X,[X|L1],L2):-remove(X,L1,L2). • remove(X,[Y|L1],[Y|L2]):- \+ X=Y, remove(X,L1,L2). • is_subset([],L1). • is_subset([X|T],L1):-member(X,L1),is_subset(T,L1). • delete_list([],L1,L1). • delete_list([X|T],L2,L1):- remove(X,L1,L3),delete_list(T,L3,L2). Artificial Intelligence a modern approach

  26. List Operator • append([],Y,Y). • append([X|T],Y,[X|Y1]):-append(T,Y,Y1). Artificial Intelligence a modern approach

More Related