fondamenti di intelligenza artificiale 1 part (6 cfu) 16

12
FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 st PART (6 CFU) 16 June 2011 Available time: 2 h Mark: 32/32 pts Exercise 1 (6 pts) Rewrite the following sentences as First Order Logic predicates: every person works as nurse or teacher (xor) all the nurses are male all the persons are female or male (xor) Steve is male and Roberta is female, both are persons. Then convert them in clauses and prove through resolution that Roberta teaches. Exercise 2 (5 pts) Consider the following game tree where the evaluation of the terminal nodes is given from the point of view of the first player (that is a Max player). Show how the min-max and alpha-beta algorithms do solve the problem. Exercise 3 (6 pts) The following Prolog program computes the connected component of a given graph: comp([e,d],[e,d]) :- !. comp(L,C) :- member(N,L), edge(N,X), not(membchk(X,L)), !, comp([X|L],C). comp(L,L). edge(a,b). edge(b,c). edge(d,e). member(X,[X|T]). member(X,[H|T]) :- member(X,T). membchk(X,[X|T]) :- !. membchk(X,[H|T]) :- membchk(X,T). Provide the SLD derivation tree for the query ?- comp([d],C).

Upload: others

Post on 03-Jun-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

FONDAMENTI DI INTELLIGENZA ARTIFICIALE – 1st PART (6 CFU) 16 June 2011 – Available time: 2 h – Mark: 32/32 pts

Exercise 1 (6 pts) Rewrite the following sentences as First Order Logic predicates:

• every person works as nurse or teacher (xor) • all the nurses are male • all the persons are female or male (xor) • Steve is male and Roberta is female, both are persons.

Then convert them in clauses and prove through resolution that Roberta teaches.  Exercise 2 (5 pts) Consider the following game tree where the evaluation of the terminal nodes is given from the point of view of the first player (that is a Max player). Show how the min-max and alpha-beta algorithms do solve the problem.

Exercise 3 (6 pts) The following Prolog program computes the connected component of a given graph:

comp([e,d],[e,d]) :- !. comp(L,C) :- member(N,L), edge(N,X), not(membchk(X,L)), !, comp([X|L],C). comp(L,L). edge(a,b). edge(b,c). edge(d,e). member(X,[X|T]). member(X,[H|T]) :- member(X,T). membchk(X,[X|T]) :- !. membchk(X,[H|T]) :- membchk(X,T).

Provide the SLD derivation tree for the query ?- comp([d],C).

Page 2: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

Exercise 4 (4 pts) Write a Prolog predicate adjacent(X,Y,Z) that is true whether X and Y are adjacent in the list Z:

?- adjacent(3,4,[1,2,3,4,5,6]). yes ?- adjacent(3,4,[1,2,3,5,4]). no ?- adjacent(3,4,[]). no

Exercise 5 (7 pts) Consider the following graph, depicting a states space. S is the initial state and G the goal. The edges are tagged with their costs.

The nodes that are already on the path from the starting node to the current one can be avoided. When needed, use the following heuristic function h(n) which returns an esteem of the distance of the node n from the goal. If, at some point, the heuristic gives the same value for several nodes, comply with the alphabetical order.

List the nodes in the same order as they are expanded and the solution path found for each of the following algorithms:

a. Uniform cost search b. Best-first greedy search c. A* search

Exercise 6 (4 pts) Describe the concept of arc-consistency for a constraint graph, possibly providing at least an example and the algorithm to obtain it (AC-3). HOW THE MARK IS COMPUTED: • 6 CFU exam: the mark is determined by this 1st part • 9 CFU exam: the mark is determined by the weighted average between the 1st part (whose weight is 2/3)

and the 2nd (whose weight is 1/3); hence the final mark is given by !×!!"  !"!"  !"#$!!!"!"#$  !"#$

! and

ranges from 0 to a maximum of 32 (equivalent to praise).

n S A B E F G h(n) 10 9 4 6 7 0

A

S

B

E F

G

5

6

6

7

4

7

5

6

Page 3: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

FONDAMENTI DI INTELLIGENZA ARTIFICIALE – 2nd PART (3 CFU) 16 June 2011 – Available time: 45 min – Mark: 32/32 pts

Exercise 7 (8 pts) Briefly show with an example the advantages of having semantic annotations for some piece of information available on the Web. Exercise 8 (10 pts) Consider the problem of the blocks world and its STRIPS-like description in terms of on, handempty, clear, ontable predicates and putdown, pickup, stack, unstack actions. Given the following initial state and goal:

initial state goal

describe the behaviour of a linear planner (STRIPS) starting from this configuration, also detailing the final state, the goal in the STRIPS-like language and the two plans that are generated as a solution by considering the two possible orderings of the goals. Which is supposed to be the optimal plan? Explain why it does not get generated and say how this problem is known in literature. Which different planning methodology can be used to get the optimal plan? Exercise 9 (14 pts) Write a meta-interpreter that, given a propositional logic program (a program without variables) and a goal, tries to solve the goal by accepting an empty list as input and returning the list of distinct predicates that are invoked and resolved successfully as output. For example, given the following program:

p :- q,r. q :- r. r.

the call ?- solve(p,[],L)

returns the following answer (three distinct calls; p, q and r are resolved successfully; r is invoked twice –in p and in q– but it is included in the list only once):

Yes L=[p,q,r] HOW THE MARK IS COMPUTED: • 3 CFU exam: the mark is determined by this 2nd part • 9 CFU exam: the mark is determined by the weighted average between the 1st part (whose weight is 2/3)

and the 2nd (whose weight is 1/3); hence the final mark is given by !×!!"  !"#$  !"#$!!!"!"#$  !"#$

! and

ranges from 0 to a maximum of 32 (equivalent to praise).

c a b

a

b c

Page 4: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16
Page 5: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

FONDAMENTI DI INTELLIGENZA ARTIFICIALE – PRIMA PARTE (6 CFU) 16 Giugno 2011 – Tempo a disposizione: 2 h – Risultato: 32/32 punti

Esercizio 1 (punti 6) Scrivere le seguenti frasi in logica dei predicati del primo ordine:

• Ogni persona lavora come infermiere oppure come insegnante (xor) • Tutti gli infermieri sono maschi • Tutte le persone sono maschi o femmine (xor) • Steve è un maschio e Roberta è una femmina, entrambi sono persone.

Trasformarle poi in clausole e dimostrare tramite risoluzione che Roberta insegna.  Esercizio 2 (punti 5) Si consideri il seguente albero di gioco in cui la valutazione dei nodi terminali è dal punto di vista del primo giocatore (che è Max). Si mostri come gli algoritmi min-max e alfa-beta risolvono il problema.

Esercizio 3 (punti 6) Il seguente programma Prolog calcola la componente connessa di un grafo:

comp([e,d],[e,d]) :- !. comp(L,C) :- member(N,L), edge(N,X), not(membchk(X,L)), !, comp([X|L],C). comp(L,L). edge(a,b). edge(b,c). edge(d,e). member(X,[X|T]). member(X,[H|T]) :- member(X,T). membchk(X,[X|T]) :- !. membchk(X,[H|T]) :- membchk(X,T).

Si mostri l’albero di derivazione SLD relativo alla query ?- comp([d],C).

Page 6: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

Esercizio 4 (punti 4) Si scriva un predicato Prolog adiacenti(X,Y,Z)che è vero se X e Y sono adiacenti nella lista Z:

?-adiacenti(3,4,[1,2,3,4,5,6]). yes ?-adiacenti(3,4,[1,2,3,5,4]). no ?-adiacenti(3,4,[]). no

Esercizio 5 (punti 7) Si consideri il seguente grafo che rappresenta uno spazio di stati. S è lo stato iniziale, G lo stato goal. Gli archi sono etichettati con i costi.

Si può evitare di visitare nodi già visitati nel cammino dal nodo iniziale al nodo attuale. Quando serve, si usi la funzione di valutazione euristica h(n) che segue, che fornisce una stima della distanza del nodo n dal goal. A parità di euristica si utilizzi l’ordine alfabetico.

Per ognuno dei seguenti algoritmi si listino i nodi nell’ordine in cui sono espansi e i cammini soluzione trovati:

a. Ricerca di costo uniforme b. Ricerca greedy best-first c. Ricerca A*

Esercizio 6 (punti 4) Si descriva il concetto di arc-consistenza per un constraint graph possibilmente con almeno un esempio e l’algoritmo per ottenerla (AC-3). VOTO: • Esame da 6 CFU, il voto è determinato da questa I parte • Esame da 9 CFU, è la media pesata della I parte (che vale 2/3) e della II (che vale 1/3) ovvero il voto

finale è dato da: !×!"#"  !  !"#$%!!"#"  !!  !"#$%!

e varia quindi da 0 ad un massimo di 32 (equivalente alla lode).

N S A B E F G h(n) 10 9 4 6 7 0

A

S

B

E F

G

5

6

6

7

4

7

5

6

Page 7: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

FONDAMENTI DI INTELLIGENZA ARTIFICIALE – SECONDA PARTE (3 CFU) 16 Giugno 2011 – Tempo a disposizione: 45 min – Risultato: 32/32 punti

Esercizio 7 (punti 8) Si illustri brevemente con un esempio il vantaggio di avere annotazioni semantiche all’interno di informazioni disponibili sul Web. Esercizio 8 (punti 10) Si consideri il problema del mondo a blocchi e la sua descrizione in termini STRIPS-like, con predicati on, handempty, clear, ontable ed azioni putdown, pickup, stack ed unstack. Dato lo stato iniziale ed il goal:

stato iniziale goal

si indichi come si comporta un pianificatore lineare (STRIPS) in questa situazione, descrivendo lo stato finale, il goal nel linguaggio STRIPS-like ed i due piani generati per la soluzione considerando i due possibili ordinamenti dei goals. Quale sarebbe il piano ottimo? Perché non lo genera e come si chiama questo problema in letteratura? Come si potrebbe ottenere il piano ottimo utilizzando una diversa metodologia di planning? Esercizio 9 (punti 14) Si scriva un meta-interprete che dato un programma logico proposizionale (senza variabili) ed un goal, a partire da una lista vuota in ingresso, cerchi di risolvere il goal restituendo in uscita la lista dei predicati distinti invocati e risolti con successo. Esempio: dato il programma

p :- q,r. q :- r. r.

la chiamata: ?- solve(p,[],L)

restituisce il seguente risultato (tre chiamate distinte; p, q, r, risolte con successo; r è chiamato due volte –in p e in q– ma compare nella lista una sola volta):

Yes L=[p,q,r] VOTO: • Esame da 3 CFU, il voto è determinato da questa II parte • Esame da 9 CFU, è la media pesata della I parte (che vale 2/3) e della II (che vale 1/3) ovvero il voto

finale è dato da: !×!"#"  !  !"#$%!!"#"  !!  !"#$%!

e varia quindi da 0 ad un massimo di 32 (equivalente alla lode).

c a b

a

b c

Page 8: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16
Page 9: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

SOLUZIONE PARTE I Esercizio 1 Traduzione in predicati in logica del primo ordine:

• Ogni persona lavora o come infermiere oppure come insegnante (xor). ∀X person(X) → works(X,teacher) xor works(X,nurse)

• Tutti gli infermieri sono maschi. ∀X works(X,nurse) → male(X)

• Tutte le persone sono o maschi o femmine (xor). ∀X person(X) → male(X) xor female(X)

• Steve è un maschio e Roberta è una femmina, entrambi sono persone.

male(steve), female(roberta), person(steve). person(roberta).

• Negazione del Goal: Roberta non è un’insegnante.

¬works(roberta,teacher) Traduzione in clausole:

1. ¬person(X) or works(X,teacher) or works(X,nurse). 2. ¬person(X) or ¬works(X,teacher) or ¬works(X,nurse). 3. ¬works(X,nurse) or male(X). 4. ¬person(X) or male(X) or female(X). 5. ¬person(X) or ¬male(X) or ¬female(X). 6. male(steve). 7. female(roberta). 8. person(steve). 9. person(roberta).

10. ¬works(roberta,teacher)

Risoluzione:

10 + 1 = 11 ¬person(roberta) or works(roberta,nurse). 11 + 9 = 12 works(roberta,nurse). 12 + 3 = 13 male(roberta). 13 + 5 = 14 ¬person(roberta) or ¬female(roberta). 14 + 9 = 15 ¬female(roberta). 15 + 7 = Contraddizione

Page 10: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

Esercizio 2 min-max:

Alfa-beta:

I nodi non colorati sono quelli che vengono tagliati nell’algoritmo alfa-beta.

Page 11: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

Esercizio 3

Esercizio 4 %% adjacent(X,Y,Zs) is true if the elements X and Y are adjacent in the list Zs. adjacent(X,Y,[X,Y|Zs]). adjacent(X,Y,[Z|Zs]):-adjacent(X,Y,Zs).

Esercizio 5

• Ricerca di costo uniforme o Ordine di espansione: S, A, F, E, B, G (o, visitando i nodi già visitati: S, A, F, E, F, A, B, E, G) o Cammino soluzione: S, F, E, G

• Ricerca greedy best-first

o Ordine di espansione: S, F, E, G o Cammino soluzione: S, F, E, G

• Ricerca A*

o Ordine di espansione: S, F, A, B, E, G o Cammino soluzione: S, F, E, G

Page 12: FONDAMENTI DI INTELLIGENZA ARTIFICIALE 1 PART (6 CFU) 16

SOLUZIONE PARTE II Esercizio 7 - Description Logics Soluzione... vedi slides Esercizio 8

• Stato iniziale: on(c, a), ontable(a), ontable(b), clear(b), clear(c), handempty

• Stato finale: on(a, b), on(b, c), ontable(c), clear(a), handempty

• Goal: on(a, b), on(b, c)

• Piani generabili con diverso ordinamento dei goals: o [unstack(c, a), putdown(c), pickup(a), stack(a, b), unstack(a, b), putdown(a), pickup(b),

stack(b, c), pickup(a), stack(a ,b)] o [pickup(b), stack(b, c), unstack(b, c), putdown(b), unstack(c, a), putdown(c), pickup(a),

stack(a, b), unstack(a, b), putdown(a), pickup(b), stack(b, c), pickup(a), stack(a, b)] • Il piano ideale non ottenibile con pianificazione “lineare”:

o [unstack(c, a), putdown(c), pickup(b), stack(b, c), pickup(a), stack(a, b)] • È l’anomalia di Sussmann; soluzione: pianificazione nello spazio dei piani POP (partial order

planning). Esercizio 9 - Metainterprete solve(GOAL,Lin,Lout) %% Lout sono i predicati distinti risolti per arrivare al successo di GOAL solve(true,L,L). solve((A,B), Lin, Lout) :- solve(A,Lin,L1), solve(B,L1,Lout). solve(A,L,L) :- system(A), !, call(A). solve(A,Lin,Lout) :- clause(A,B), member(A,Lin), !, solve(B,Lin,Lout). solve(A,Lin,Lout) :- clause(A,B), solve(B,[A|Lin],Lout). Nota: versione con 2 soli parametri (invocabile con ?- solve(p,L)) solve(GOAL,L) %% L sono i predicati distinti risolti per arrivare al successo di GOAL %% predicato “union” x l’unione di due insiemi: vedi slides solve(true,[]). solve((A,B), L) :- solve(A, L1), solve(B, L2), union(L1,L2,L). solve(A,[]):- system(A), !, call(A). solve(A,L) :- clause(A,B), solve(B,L2), union([A],L2,L).