• Non ci sono risultati.

Sistemi Intelligenti Reinforcement Learning: Temporal Difference

N/A
N/A
Protected

Academic year: 2022

Condividi "Sistemi Intelligenti Reinforcement Learning: Temporal Difference"

Copied!
33
0
0

Testo completo

(1)

Sistemi Intelligenti

Reinforcement Learning:

Temporal Difference

Alberto Borghese

Università degli Studi di Milano

Laboratorio di Sistemi Intelligenti Applicati (AIS-Lab) Dipartimento di Informatica

[email protected]

(2)

A.A. 2015-2016 2/28

Sommario

Temporal differences SARSA

(3)

Schematic diagram of an agent

E nvi ronm ent

Sensors

Actuators Agent

What the world is like now

(internal

representation)?

STATE

What action I should do now?

Condition- action rule (vocabulary)

s(t+1) = f[s(t),a(t)]

a(t) = g[s(t)]

s, stato; a = uscita

s(t+1)

a(t) s(t)

s(t+1)

s, stato;

a, ingresso r(t+1)

s(t)

(4)

A.A. 2015-2016 4/28

Tecnica full-backup

t

t+1 π(s,a) fissata Back-up

Conosciamo Vk(st) ∀st, anche per s’t+1 quindi:

Analizziamo la transizione da st,at -> (s’t+1)

Calcoliamo un nuovo valore di V per s: Vk+1(st) congruente con:

Vk(st+1) ed rt+1

Full backup se esaminiamo tutti gli s’, e tutte le a (cf. DP).

Da s’ mi guardo indietro ed aggiorno V(s).

(5)

Schema di

Apprendimento

Policy iteration Value iteration Generalized Policy iteration

{

Competizione e cooperazione -> V corretta e policy ottimale.

(6)

A.A. 2015-2016 6/28 http:\\homes.dsi.unimi.it\borghese\

World RL competition

Started in NIP2006.

It became very popular and started a workshop on its own. Visit:

http://rl-competition.org

(7)

How About Learning the Value Function?

Facciamo imparare all’agente la value function, per una certa politica: Vπ:

Una volta imparata la value function, V, l’agente seleziona la policy ottima passo per passo, “one step lookahead”:

π*(s)

[

' ( ')

]

'

max

'

arg P R

as s V s

s a

s s a

γ π

+

=

> >

( )

,

[

( ')

]

)

( '|

'

'| R V s

P a

s s

V j j

j

a s s s

a s s a

j

π

π =

π

È una funzione dello stato.

Full backup, for all states

(8)

A.A. 2015-2016 8/28 http:\\homes.dsi.unimi.it\borghese\

[ ( ' ) ]

)

(

'|

'

'|

1

s max P R V s

V

s s a k

s

a s s a

k j j

j

γ

+

=

+

Value iteration

( ) , [ ( ' ) ]

)

(

'|

'

'|

1

s a s P R V s

V

k

a j s s s

a s s a

j

k j

j

γ

π +

=

+

∑ ∑

Invece di considerare una policy stocastica, consideriamo l’azione migliore:

s

Facciamo imparare all’agente la value function, per una certa politica: Vπ, analizzando quello che succede in uno step temporale:

L’apprendimento della policy si può inglobare nella value iteration:

(9)

Problema legato alla conoscenza della risposta dell’ambiente

[ ( ' ) ]

)

(

'|

'

'|

1

s max P R V s

V

s s a k

s

a s s a

k j j

j

γ

+

+

Full backup, single state, s, all future states s’

Fino a questo punto, è noto un modello dell’ambiente:

•R(.)

•P(.)

Environment modeling -> Value function computation ->

Policy optimization.

(10)

10/28

A.A. 2015-2016

Osservazioni

Iterazione tra:

• Calcolo della Value function

•Miglioramento della policy

( ) [ [ ( ' ) ] ]

)

(

'|

'

'|

1

s s P R V s

V

s s a k

s

a s s a

k j j

j

γ

π +

=

+

∑ ∑

[

'| ( ')

]

'

max

'|

arg P

j

R

j V s

j

a s s s

a s s a

γ π

+

=

> >

Non sono noti

(11)

Background su Temporal Difference (TD) Learning

Al tempo t abbiamo a disposizione:

rt+1 = r’

st+1 = s’

aj

s

R

s '|

aj

s

P

s '|

Reward certo

Transizione certa

vengono misurati dall’ambiente

Come si possono utilizzare per apprendere?

(12)

12/28

A.A. 2015-2016

Confronto con il setting associativo

Occupazione di memoria minima: Solo Qk e k.

NB k è il numero di volte in cui è stata scelta aj.

Questa forma è la base del RL. La sua forma generale è:

NewEstimate = OldEstimate + StepSize [Target – OldEstimate]

NewEstimate = OldEstimate + StepSize * Error.

StepSize =

α

= 1/k a = cost

Rewards weight w = 1 Weight of i-th reward at time k: w = (1-a)k-i Qual è la differenza introdotta dall’approccio DP?

[

k k

]

k k

k k

k k

k Q r Q

N r N

Q Q

Q = + = + +

+ + +

+ 1

1 1 1

1 α

(13)

Un possibile aggiornamento

Ad ogni istante di tempo di ogni trial aggiorno la Value function:

[ ( ' ) ]

) ,

( )

(

'|

'

'|

1

s s a P R V s

V

s s a k

s

a s s a

j k

j

γ

π +

=

+

∑ ∑

In iterative policy evaluation ottengo questo aggiornamento:

Qual’è il problema?

[ ' ( ) ]

)

1

( s r V s

V

k+

= + γ

k

s

s’

a

s’

(14)

14/28

A.A. 2015-2016

Un possibile aggiornamento di Q(s,a)

) ( )

( )

( s V s V s

V

k

=

k

+ ∆

k

[

k k

]

k k

k k

k k

k k

k Q r Q Q Q

N r N

Q Q

Q = + = + + = +

+ + +

+ 1

1 1 1

1 α

Come calcolo ∆Vk(s)?

Quanto vale α?

(15)

TD(0) update

Ad ogni istante di tempo di ogni trial aggiorno la Value function:

( ) ( )

[

t k t k t

]

t k t

k

s V s r V s V s

V

+1

( ) = ( ) + α

+1

+ γ

+1

( ) , [ ( ' ) ]

)

(

'|

'

'|

1

s s a P R V s

V

s s a k

s

a s s a

j

k j j

j

γ

π +

=

+

∑ ∑

Da confrontare con la iterative policy evaluation:

{ R s s } E { r V s s s }

E s

V

π

( ) =

π t

|

t

= =

π t+1

+ γ

π

( ' ) |

t

=

E con il valore di uno stato sotto la policy π(s,a):

Quanto vale α?

Sample backup

(16)

16/28

A.A. 2015-2016

Confronto con il setting associativo

Occupazione di memoria minima: Solo Qk e k.

NB k è il numero di volte in cui è stata scelta aj.

Questa forma è la base del RL. La sua forma generale è:

NewEstimate = OldEstimate + StepSize [Target – OldEstimate]

NewEstimate = OldEstimate + StepSize * Error.

StepSize =

α

= 1/k a = cost

[

k k

]

k k

k k

k k

k Q r Q

N r N

Q Q

Q = + = + +

+ + +

+ 1

1 1 1

1 α

(17)

Setting α value

α(st, at, st+1) = 1/k(st, at, st+1), where k represents the number of occurrences of st, at, st+1. With this setting the estimated Q tends to the expected value of Q(s,a).

Per semplicità si assume solitamente α < 1 costante. In questo caso, Q(s,a) assume il valore di una media pesata dei reward a lungo termine collezionati da (s,a), con peso: (1-α)k: exponential recency-weighted average.

(18)

18/28

A.A. 2015-2016

Sommario

Temporal differences SARSA

(19)

Sample backup

Full backup Single sample is

evaluated a’

a’

(20)

20/28

A.A. 2015-2016

Sample backup

a’

SARSA algorithm State –

Action – Reward –

State (next) – Action (next)

Campionamento di st+1 e di rt+1

(21)

TD(0)

Inizializziamo V(s) = 0.

Inizializziamo la policy: π(s,a).

Repeat { s = s0;

Repeat // For each state until terminal state, analyze an episode { a = π(s);

s_next = NextState(s, a);

reward = Reward(s, s_next, a);

V(s) = V(s) + α [reward + γ V(s next) – V(s)];

s = s_next; }

until TerminalState }

until convergence of V(s) for policy π(s,a)

(22)

22/28

A.A. 2015-2016

Esempio: valutazione della policy TD

Stato Tempo

percorrenz a stimato

del segmento

Tempo percorrenza

attuale del segmento

Tempo totale previsto in precedenza

Qk(s,a)

Tempo totale previsto aggiornato

Qk+1(s,a)

Increase or decrease

Q(s,a)

Esco dall’ufficio 0 0 30 30 + (10 + 25 – 30) >

Salgo in auto 5 10 25 25 + (15 + 10 - 25) =

Esco dall’autostrada 15 15 10 10+ (10 + 5 - 10) >

Esco su strada

secondaria 5 10 5 5+ (2 + 3 - 5)

=

Entro Strada di casa 2 2 3 3 + (1 + 0 - 3) <

Parcheggio 3 1 0 0

Q(s,a) è l’expected “Time-to-Go” - γ = 1 α = 1

(23)

Alcuni passi di apprendimento di TD(0)

Alcuni passi di iterazione per TD(0)

V(0) = V(0) + α (r1 + γV(1) - V(0)) = 30 + α (5 + 35 − 30) = 30 + α * ∆ Stima iniziale del tempo di percorrenza totale: 30m

Tempo di percorrenza fino all’auto: 5m

Stima del tempo di percorrenza dal parcheggio: 35m

V(1) = V(1) + α (r1 + γV(2) - V(1)) = 35 + α (20 + 15 − 35) = 35 + α * ∆ Stima iniziale del tempo di percorrenza dal parcheggio: 35m

Tempo di percorrenza fino ad uscita autostrada: 20m Tempo di percorrenza fino ad uscita autostrada: 20m

Stima del tempo di percorrenza dall’uscita autostrada: 15m

(24)

24/28

A.A. 2015-2016

Ruolo di α

Q(1,a1) = Q(1,a1) + α (r1 + γQ(2,a2) - Q(1,a1)) = 30 + α (10+25 − 30) = 30 − α*5 Stima iniziale del tempo di percorrenza dal parcheggio: 30m

Tempo per raggiungere l’auto: 10m

Stima del tempo di percorrenza dall’uscita Dal parcheggio: 25m

α < 1.

If α << 1 aggiorno molto lentamente la value function.

If α = 1/k aggiorno la value function in modo da tendere al valore atteso. Devo memorizzare le occorrenze dello stato s.

If α = cost. Aggiorno la value function, pesando maggiormente i risultati collezionati dalle visite dello stato più recenti.

(25)

Proprietà del metodo TD

Non richiede conoscenze a priori dell’ambiente.

L’agente stima dalle sue stesse stime precedenti (bootstrap).

Si dimostra che il metodo converge asintoticamente.

Batch vs trial learning.

Converge!!

Rimpiazza iterative Policy evaluation.

Rimane il passo di Policy iteration (improvement).

( ) ( )

[

t t t

]

t

t

V s r V s V s

s

V

π

( ) =

π

( ) + α

+1

+ γ

π +1

π

Single backup, single state, st, single future state st+1

(26)

26/28

A.A. 2015-2016 http:\\homes.dsi.unimi.it\borghese\

Le value function

=

=

=

=

=

= r+ + s s E

s s R E s

V t

k

k t k t

t

0

} 1

| { )

( π π γ

π





 = =

=

=

=

=

= r+ + s s a a

E a

a s s

R E

a s

Q t t

k

k t k t

t

t | , } ,

{ )

, (

0

γ 1 π

π π

( )

,

[

'| ( ')

]

'

'| R V s

P s

a j j

j

a s s s

a s s a

j

γ π

π +

[

'| ( ')

]

'

'| R V s

P j s s aj

s

a s s

γ π

+

=

(27)

Serve davvero la Value Function?

La Value Function deriva dalla visione della Programmazione Dinamica.

Ma è proprio necessario conoscere la Value function (che riguarda gli stati)? In fondo a noi interessa determinare la Policy.

(28)

28/28

A.A. 2015-2016 http:\\homes.dsi.unimi.it\borghese\

.

.

.

al

Calcolo ricorsivo della value function Q





 = =

=

=

=

=

= r+ + s s a a

E a

a s s

R E

a s

Q t t

k

k t k t

t

t | , } ,

{ )

, (

0

γ 1 π

π π

+

=

l

l l

a s s s

a s

s R s a Q s a

P '| ( ', ) ( ', )

'

'|

π π

γ

(29)

.

.

.

al

Come apprendere Q: SARSA

( ) ( )

[

t t t t t

]

t t

t

t

a Q s a r Q s a Q s a

s

Q ( , ) = ( , ) + α

+1

+ γ

+1

,

+1

− ,

1) Apprendiamo il valore di Q per una policy data (on-policy).

2) Dopo avere appreso la funzione Q, possiamo modificare la

policy in modo da migliorarla (policy improvement)

s = state, a = action, r = reward, s = state, a = action è di tipo TD(0)

(30)

30/28

A.A. 2015-2016

SARSA Algorithm (progetto)

1) Apprendiamo il valore di Q per una policy data (on-policy).

2) Dopo avere appreso la funzione Q, possiamo modificare la policy in modo da migliorarla.

Come integrare i due passi?

Q(s,a) = rand(); // s, a, eventualmente Q(s,a) = 0

Repeat // for each episode

{ s = s0;

Repeat // for each step of the single episode {

a = Policy(s); // ε-greedy??

s_next = NextState(s,a);

reward = Reward(s,s_next,a);

a_next = Policy(s_next); // ε-greedy?

Q(s,a) = Q(s,a) + α [reward + γ Q(s_next, a_next) – Q(s,a)];

s = s_next;

} // until last state

} // until the end of learning

(31)

Esempio

A B

C

Q(s,a) iniziale = 0.

r = 0 se s’ = G; altrimenti r = -1.

π(s,a) data.

From Start to Goal.

Upwards wind

(32)

32/28

A.A. 2015-2016

Esempio - risultato

Correzione di Q ad un passo:

Q(S, east) = 0 + 0.5 [-1 + 0 – 0] = -0.5 Q(A, east) = 0 + 0.5 [-1 + 0 –0] = -0.5

Q(C, west) = 0 + 0.5 [0 + 0 – 0] = 0; (NB c’è il vento verso l’alto di 1) ε = 0.1

α= 0.5 γ = 1

( ) ( )

[

t t t t t

]

t t t

t

a Q s a r Q s a Q s a

s

Q ( , ) = ( , ) + α

+1

+ γ

+1

,

+1

− ,

Policy π, greedy or ε-greedy

A C

Per trial or per epoch Al termine, policy

improvement.

(33)

Sommario

Temporal differences SARSA

Riferimenti

Documenti correlati

In study 1, the effect of intraperitoneal (i.p.) injection of the TRPA1 antagonist HC-030031 (100 mg/kg), or its vehicle (4% dimethyl sulfoxide, DMSO, and 4% Tween 20 in 0.9% NaCl)

Il datore di lavoro si troverebbe cosi a scegliere tra una delle due strade: o instaurare un rapporto di lavoro subordinato a tempo indeterminato (quello che già esiste da sempre)

Welfare - life-time consumption-equivalent welfare increase; U Rate - Unem- ployment Rate; UD - unemployment duration, NS - results for a model with non-separable utility assumption,

Consistent with our prediction, we find that CEO and CFO inside debt holdings negatively affect the three-year GAAP and cash effective tax rate volatility,

In order to better understand the value creation in architecture the research started focussing on the economic features of design, the research started ana- lysing

Table 17 - Quantiles (Q.) of the predictive distribution of annual and seasonal rainfall at San Benedetto dei Marsi site.. Probability of rainfall (at least one rain event) and

• value function and rollout to propagate information in the tree search, as well as to update the

Ecco perché, indipendentemente dalla sua natura (pub- blico, privato, misto) e dalla quota di PIL destinata alla sanità, la sostenibilità di un sistema sanitario non può