-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHAPED_BLE.py
211 lines (154 loc) · 5.98 KB
/
SHAPED_BLE.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import numpy as np
from random import randint
import random
import tensorflow
from collections import deque
from keras import losses
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout
from keras.optimizers import Adam, Adamax
from keras.initializers import Zeros, Ones
from ENV_TRAIN import Retail_Environment
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import pandas as pd
from pandas import DataFrame
class DQN_Agent():
def __init__(self, state_size, action_size, gamma, epsilon_decay, epsilon_min, learning_rate, epochs, env, batch_size, update, iteration, S1, S2, b, factor, x):
self.state_size = state_size
self.action_size = action_size
self.memory = deque(maxlen = 20000)
self.gamma = gamma
self.epsilon_decay = epsilon_decay
self.epsilon_min = epsilon_min
self.learning_rate = learning_rate
self.epochs = epochs
self.env = env
self.batch_size = batch_size
self.update = update
self.epoch_counter = 0
self.epsilon = 1.0
self.iteration = iteration
self.S1 = S1
self.S2 = S2
self.b = b
self.alpha = (1-((S2 - S1)/b))
self.model = self.build_model()
self.target_model = self.build_model()
#self.trained_model = self.train()
self.factor = factor
self.x = x
def build_model(self):
model = Sequential()
model.add(Dense(32, input_dim = self.state_size, activation = 'relu'))
model.add(Dense(32, activation = 'relu'))
model.add(Dense(self.action_size, activation = 'linear'))
model.compile(loss = losses.mean_squared_error, optimizer = Adam(lr = self.learning_rate))
return model
def act(self, state):
if np.random.rand() <= self.epsilon:
return random.randrange(self.action_size)
act_values = self.model.predict(state)
return np.argmax(act_values[0])
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def replay(self):
#experience replay from replay memory
minibatch = random.sample(self.memory, self.batch_size)
current_states = np.array([experience[0] for experience in minibatch])
current_qs_list = np.zeros((self.batch_size, 1, self.env.max_order + 1))
for k in range(self.batch_size):
current_qs_list[k] = self.model.predict(current_states[k])
new_states = np.array([experience[3] for experience in minibatch])
future_qs_list = np.zeros((self.batch_size, 1, self.env.max_order + 1))
for k in range(self.batch_size):
future_qs_list[k] = self.target_model.predict(new_states[k])
x = []
y = []
for i, (current_state, action, reward, next_state, done) in enumerate(minibatch):
if not done:
max_fut_q = np.max(future_qs_list[i])
new_q = reward + self.gamma*max_fut_q
else:
new_q = reward
current_qs = current_qs_list[i]
current_qs[0][action] = new_q
x.append(current_state[0])
y.append(current_qs[0])
self.model.fit(np.array(x), np.array(y), batch_size = self.batch_size, verbose = 0, shuffle = False)
#decay epsilon
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
#update target network
if self.epoch_counter % self.update == 0:
self.update_target_model()
def update_target_model(self):
weights = self.model.get_weights()
target_weights = self.target_model.get_weights()
for i in range(len(target_weights)):
target_weights[i] = weights[i]
self.target_model.set_weights(target_weights)
print('***** Target network updated *****')
def train(self):
scores = []
for e in range(self.epochs):
done = False
score = 0
state, _ = self.env.reset()
prev_val = 0
while not done:
state = np.reshape(state, [1, self.state_size])
action = self.act(state)
next_state, reward, done, _ = self.env.step(action)
score += reward
next_state = np.reshape(next_state, [1, self.state_size])
EW = 0
demand_left = 0
adj_state = []
for i in range(self.env.leadtime):
if FIFO == True:
EW += max(0, self.env.state[-1 - i] - self.env.mean_demand - demand_left)
demand_left = max(0, (demand_left + self.env.mean_demand) - self.env.state[-1 - i])
elif LIFO == True:
dem = self.env.mean_demand
k = self.env.leadtime - 1
j = 0
while dem > 0 and j <= self.env.lifetime - 1:
in_store = adj_state[k + j]
adj_state[k + j] = max(0, adj_state[k + j] - dem)
dem = max(0, dem - in_store)
j += 1
EW += max(0, adj_state[-1])
for p in range(self.env.leadtime + self.env.lifetime - 2):
adj_state[-1 - p] = adj_state[-2 - p]
adj_state[0] = 0
in_inv = 0
for i in range(self.env.leadtime + self.env.lifetime - 1):
in_inv += state[0][i]
if in_inv < self.b:
order = max(0, round(self.S1 - (self.alpha * in_inv) + EW))
else:
order = max(0, self.S2 - in_inv + EW)
cur_val = -self.factor * abs(order - action)
F = cur_val - ((1/self.gamma)*prev_val)
prev_val = cur_val
total = reward + F
self.remember(state, action, total, next_state, done)
state = next_state
avg_score = score / self.env.time
self.epoch_counter += 1
print('Epoch ' + str(self.epoch_counter) + ' | Avg score per period: ' + str(-avg_score))
if len(self.memory) > self.batch_size:
self.replay()
scores.append(-avg_score)
df = DataFrame({'Reward': scores})
path = PATH
df.to_excel(str(path) + str(self.x) + '/EVAL' + str(self.x) + '/Lifetime ' + str(self.env.lifetime) + ' - iteration ' + str(self.iteration) + '.xlsx')
self.model.save(str(path) + '/EVAL' + str(self.x) + '/Lifetime ' + str(self.env.lifetime) + ' - iteration ' + str(self.iteration) + '.h5')
return scores
def save(self, name):
self.model.save_weights(name)
def load(self, name):
self.model.load_weights(name)
def get_qs(self, state):
return self.model.predict(np.array(state).reshape(-1, *self.state_size))[0]