-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_em.py
139 lines (122 loc) · 4.79 KB
/
example_em.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from matplotlib import pyplot as plt
import numpy as np
from Pend2dBallThrowDMP import *
class EM():
"""
Implements policy search using Expectation Maximization to update parameters of upper-level policy
"""
def __init__(self):
"""
numDim: dimension of state space
numSamples: number of episodic rollouts per iteration
maxIter: number of parameter updates
numTrials: number of independent learning trials
"""
self.env = Pend2dBallThrowDMP()
self.lambd = 7
self.numDim = 10
self.numSamples = 25
self.maxIter = 100
self.numTrials = 10
self.saveFigures = True
# Do your learning
def calculate_R_and_theta(self, Mu_w, Sigma_w):
# initialize theta vector (2D array: state dimension x number of samples)
# and R (reward) vector (1D array: reward per episode)
# and w (weights) vector (1D array: weight of current sample)
numDim = self.numDim
numSamples = self.numSamples
env = self.env
theta = np.zeros((numDim, numSamples))
R = np.zeros(numSamples)
for i in range(0, numSamples):
# ... then draw a sample and simulate an episode
sample = np.random.multivariate_normal(Mu_w, Sigma_w)
reward = env.getReward(sample)
theta[:,i] = sample
R[i] = reward
return R, theta
def calculate_w(self, R, theta):
# calculate the weights
lambd = self.lambd
numSamples = self.numSamples
w = np.zeros(numSamples)
beta = lambd / (np.max(R) - np.min(R))
for i in range(0, numSamples):
w[i] = np.exp(beta * (R[i] - np.max(R)))
return w
# update omega
def update_omega(self, w, theta):
# calculate mean
Mu_w = 0
for l in range(0, self.numSamples):
Mu_w += w[l]*theta[:,l]
Mu_w /= np.sum(w)
# calculate covariance
Sigma_w = 0
for l in range(0, self.numSamples):
Sigma_w += w[l]*(np.outer((theta[:,l] - Mu_w),(theta[:,l] - Mu_w)))
Sigma_w /= np.sum(w)
return Mu_w, Sigma_w
def run_trials(self):
maxIter = self.maxIter
numSamples = self.numSamples
numDim = self.numDim
numTrials = self.numTrials
env = self.env
cnt = 0
lambd_list = [7, 3, 25]
# Run Trials for different temperature values lambda
for lambd in lambd_list:
self.lambd = lambd
R_mean_storage = np.zeros((maxIter,numTrials))
R_mean = np.zeros(maxIter)
R_std = np.zeros(maxIter)
for t in range(0, numTrials):
R_old = np.zeros(numSamples)
Mu_w = np.zeros(numDim)
Sigma_w = np.eye(numDim) * 1e6
for k in range(0, maxIter):
# sample the R and theta vector (25 values each) for the current iteration
R, theta = self.calculate_R_and_theta(Mu_w, Sigma_w)
if np.linalg.norm(np.mean(R_old) - np.mean(R)) < 1e-3:
break
# get the weights
w = self.calculate_w(R, theta)
# update the parameters
Mu_w, Sigma_w = self.update_omega(w, theta)
# Regularization
Sigma_w += np.eye(numDim)
mR = np.mean(R)
# store the average return of current run
R_mean_storage[k,t] = mR
R_old = R
if k == maxIter and t == numTrials:
print(np.mean(R))
R_mean = np.mean(R_mean_storage, axis=1)
R_std = np.sqrt(np.diag(np.cov(R_mean_storage)))
print("Average return of final policy: ")
print(R_mean[-1])
print("\n")
#### plotting for different lambdas ####
if cnt == 0:
plt.errorbar(np.arange(1, maxIter + 1), R_mean, 1.96 * R_std, marker='^',color='blue', label='lambda = 7')
elif cnt == 1:
plt.errorbar(np.arange(1, maxIter + 1), R_mean, 1.96 * R_std, marker='^',color='green', label='lambda = 3')
elif cnt == 2:
plt.errorbar(np.arange(1, maxIter + 1), R_mean, 1.96 * R_std, marker='^', color='red',label='lambda = 25')
plt.yscale("symlog")
if self.saveFigures:
plt.savefig('1lambda3725.pdf')
# Save animation
env.animate_fig ( np.random.multivariate_normal(Mu_w,Sigma_w) )
cnt += 1
########################################
plt.legend(loc='best')
if __name__ == '__main__':
plt.figure()
plt.xlabel("Number of Runs")
plt.ylabel("Average return")
plt.hold('on')
test = EM()
test.run_trials()