Factorial
Download
1 / 27

Factorial - PowerPoint PPT Presentation


  • 100 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Factorial' - rollo


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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).

Artificial Intelligence a modern approach


Integer operation
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


Factorial1
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


From number to function representation
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


Monkey and bananas
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


Monkey and bananas1
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?


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.


Situation calculus
Situation Calculus the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus1
Situation Calculus the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus2
Situation Calculus the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus action situation
Situation Calculus (Action, Situation) the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus action situation1
Situation Calculus (Action, Situation) the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus state predicates
Situation Calculus (State Predicates) the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus state predicates1
Situation Calculus (State Predicates) the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


Situation calculus change and frame axiom
Situation Calculus (Change and Frame Axiom) the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

  • 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


    Situation calculus example
    SITUATION CALCULUS EXAMPLE the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • 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


    DNF the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • 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


    Strips
    Strips the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • 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).


    Initial and goal state strips
    Initial and Goal State (Strips) the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • 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).


    Representing operators strips
    Representing Operators the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.(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


    The frame problem
    The Frame Problem the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • 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.


    All operators
    All Operators the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.


    Finding a solution strips
    Finding a the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.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.


    Finding a solution in prolog
    Finding a solution in the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.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)


    List operator
    List Operator the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • 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


    List operator1
    List Operator the monkey walks to the box, pushes it under the bananas, climbs on the box, picks the bananas and eats them.

    • append([],Y,Y).

    • append([X|T],Y,[X|Y1]):-append(T,Y,Y1).

    Artificial Intelligence a modern approach


    ad