forked from donghun2018/seqdecisionlib-release
-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathDriverScriptAdaptive.py
135 lines (79 loc) · 4.51 KB
/
DriverScriptAdaptive.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
import numpy as np
import pandas as pd
from copy import copy
from collections import (namedtuple, defaultdict)
import matplotlib.pyplot as plt
from StaticModelAdaptive import StaticModel
from PolicyAdaptive import Policy
if __name__ == "__main__":
seed = 89720123
#reading parameter file and initializing variables
file = 'Parameters.xlsx'
parDf = pd.read_excel(file, sheet_name = 'parameters')
parDict=parDf.set_index('Index').T.to_dict('list')
print("Starting adaptive stochastic shortest path with parameters")
params = {key:v for key, value in parDict.items() for v in value}
params.update({'seed':seed})
print(params)
state_names = ['CurrentNode', 'CurrentNodeLinksCost']
decision_names = ['NextNode']
# create the model, given the above policy
M = StaticModel(state_names, decision_names, params)
policy_names = ['PureExploitation']
P = Policy(M, policy_names)
theta_list = M.init_args['theta_set'].split()
obj_along_iterations = {theta:[] for theta in theta_list}
vbar_along_iterations = {theta:[] for theta in theta_list}
for theta in theta_list:
model = copy(M)
model.theta_step = float(theta)
model.prng = np.random.RandomState(model.init_args['seed'])
print("************Starting iterations for theta {}".format(model.theta_step))
for ite in list(range(model.init_args['nIterations'])):
model.obj = 0
model.state = model.build_state(model.init_state)
print("\tTheta {} - Iteration {} - Stepsize {:.2f} - InitState {} - Vbar {:.2f}".format(model.theta_step,model.n,model.alpha(),model.state.CurrentNode,model.V_t[model.state.CurrentNode]))
step = 1
while model.state.CurrentNode != model.init_args['target_node']:
# calling policy and choosing a decision
decision,vhat = P.make_decision(model)
x = model.build_decision({'NextNode': decision})
print("\t\t Theta {} - Iteration {} - Step {} - Current State {} - vbar = {:.2f}".format(model.theta_step,model.n,step, model.state.CurrentNode,model.V_t[model.state.CurrentNode]))
# update vfa
vbar = model.update_VFA(vhat)
# print current state, decision , vhat and new vbar
#model.print_State()
print("\t\tDecision={}, vhat {:.2f} and new vbar for current state {:.2f}".format(x[0],vhat,model.V_t[model.state.CurrentNode]))
# transition to the next state w/ the given decision
model.transition_fn(x)
step += 1
print("Finishing Theta {} and Iteration {} with {} steps. Total cost: {} \n".format(model.theta_step,model.n,step-1,model.obj))
model.n+=1
obj_along_iterations[theta].append(model.obj)
vbar_along_iterations[theta].append(model.V_t[model.origin_node])
#Ploting the results
fig1, axsubs = plt.subplots(1,2)
fig1.suptitle('Comparison of theta^step - stepsize type {} - origin {}, target {}, dist {} '.format(M.init_args['stepsize_rule'],M.origin_node,M.target_node,M.dist) )
color_list = ['b','g','r','m','c']
nThetas = list(range(len(theta_list)))
Iterations = list(range(model.init_args['nIterations']))
totals = [np.array(obj_along_iterations[theta]).sum() for theta in theta_list]
print("Totals ",totals)
for theta,t in zip(theta_list,nThetas):
axsubs[0].plot(Iterations, np.array(obj_along_iterations[theta]).cumsum(), "{}-".format(color_list[t]),label = "{}".format(theta))
#axsubs[0].plot(Iterations, obj_along_iterations[theta], "{}-".format(color_list[t]),label = "{}".format(theta))
axsubs[0].set_title('Objective function')
axsubs[0].legend()
axsubs[0].set_xlabel('Iterations')
axsubs[0].set_ylabel('Cost')
axsubs[1].plot(Iterations, vbar_along_iterations[theta], "{}o-".format(color_list[t]),label = "{}".format(theta))
axsubs[1].set_title('Vbar')
axsubs[1].legend()
axsubs[1].set_xlabel('Iterations')
axsubs[1].set_ylabel('Cost')
#axsubs.plot(Iterations, obj_along_iterations[theta], "{}o-".format(color_list[t]),label = "{}".format(theta))
#axsubs[0].set_title('Cost')
#axsubs.legend()
#axsubs.set_xlabel('Iterations')
#axsubs.set_ylabel('Cost')
plt.show()