Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MDP_David_class_first_example.py #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions MDP/MDP_David_class_first_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 10 10:51:18 2018

@author: olmer.garciab
"""

# implementing class example

P=[[0,0.5,0,0,0.,0.5,0],
[0,0,0.8,0,0.0,0,0.2],
[0,0.,0,0.6,0.4,0,0],
[0,0.,0,0,0.,0,1],
[0.2,0.4,0.4,0,0,0,0],
[0.1,0.,0,0,0.,0.9,0],
[0,0.,0,0,0.,0,1]]
R=[-2,-2,-2,10,1,-1,0]


# total dicount reward
def G_t(S,R,gamma):
g=0
for s,i in zip(S,range(len(S))):
g=g+R[s]*gamma**i
return g

#g = lambda y: sum( f(y) for f in (lambda x: x**i for i in range(n)) )
# for example for the chain of state S_1
gamma=0.5
S_1=[0,1,2,3,6]
print(G_t(S_1,R,gamma))




#dynamic programming
#based in #https://harderchoices.com/2018/02/26/dynamic-programming-in-python-reinforcement-learning/
def iterative_value_function(N, theta=0.0001, gamma=0.9):
V_s =R.copy() # 1.
probablitiy_map = P # 2.
delta = 100 # 3.
while not delta < theta: # 4.
delta = 0 # 5.
for state in range(0,N): # 6.
v = V_s[state] # 7.

total =R[state] # 8.
for state_prime in range(0,N):
total += probablitiy_map[state][state_prime] * (gamma * V_s[state_prime])
#print(total)

V_s[state] =total # 9.
delta = max(delta, abs(v - V_s[state])) # 10.
#print(delta)
V_s=[round(v,2) for v in V_s]
return V_s # 11.


N=len(R)




print('gamma',0.9,iterative_value_function(N,gamma=0.9))
print('gamma',1,iterative_value_function(N,gamma=1))
print('gamma',0,iterative_value_function(N,gamma=0))


#vectorial way
import numpy as np
from numpy.linalg import inv
gamma=0.9
P=np.array(P)
R=np.array(R).reshape((-1,1))
v=np.matmul(inv(np.eye(N)-gamma*P),R)
print(v)