From 73cdcc17da3696c8c6a3dfe541c06da75f2efe67 Mon Sep 17 00:00:00 2001 From: Garfield Date: Sat, 27 Jul 2019 09:38:02 +0800 Subject: [PATCH 1/2] modify range for python 3.6 --- agent/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/agent.py b/agent/agent.py index 07e163b..b8c6364 100644 --- a/agent/agent.py +++ b/agent/agent.py @@ -44,7 +44,7 @@ def act(self, state): def expReplay(self, batch_size): mini_batch = [] l = len(self.memory) - for i in xrange(l - batch_size + 1, l): + for i in range(l - batch_size + 1, l): mini_batch.append(self.memory[i]) for state, action, reward, next_state, done in mini_batch: From 0424b025e278700d73c49b846964403e48325a3b Mon Sep 17 00:00:00 2001 From: Garfield Date: Sat, 27 Jul 2019 09:55:52 +0800 Subject: [PATCH 2/2] modify for python 3.6 --- evaluate.py | 14 +++++++------- functions.py | 2 +- train.py | 18 +++++++++--------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/evaluate.py b/evaluate.py index 0055e9d..6df0e76 100644 --- a/evaluate.py +++ b/evaluate.py @@ -6,7 +6,7 @@ import sys if len(sys.argv) != 3: - print "Usage: python evaluate.py [stock] [model]" + print("Usage: python evaluate.py [stock] [model]") exit() stock_name, model_name = sys.argv[1], sys.argv[2] @@ -22,7 +22,7 @@ total_profit = 0 agent.inventory = [] -for t in xrange(l): +for t in range(l): action = agent.act(state) # sit @@ -31,19 +31,19 @@ if action == 1: # buy agent.inventory.append(data[t]) - print "Buy: " + formatPrice(data[t]) + print("Buy: " + formatPrice(data[t])) elif action == 2 and len(agent.inventory) > 0: # sell bought_price = agent.inventory.pop(0) reward = max(data[t] - bought_price, 0) total_profit += data[t] - bought_price - print "Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price) + print("Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price)) done = True if t == l - 1 else False agent.memory.append((state, action, reward, next_state, done)) state = next_state if done: - print "--------------------------------" - print stock_name + " Total Profit: " + formatPrice(total_profit) - print "--------------------------------" + print("--------------------------------") + print(stock_name + " Total Profit: " + formatPrice(total_profit)) + print("--------------------------------") diff --git a/functions.py b/functions.py index 5129e0c..f8d216e 100644 --- a/functions.py +++ b/functions.py @@ -24,7 +24,7 @@ def getState(data, t, n): d = t - n + 1 block = data[d:t + 1] if d >= 0 else -d * [data[0]] + data[0:t + 1] # pad with t0 res = [] - for i in xrange(n - 1): + for i in range(n - 1): res.append(sigmoid(block[i + 1] - block[i])) return np.array([res]) diff --git a/train.py b/train.py index c4ba8ed..59e6541 100644 --- a/train.py +++ b/train.py @@ -3,7 +3,7 @@ import sys if len(sys.argv) != 4: - print "Usage: python train.py [stock] [window] [episodes]" + print("Usage: python train.py [stock] [window] [episodes]") exit() stock_name, window_size, episode_count = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) @@ -13,14 +13,14 @@ l = len(data) - 1 batch_size = 32 -for e in xrange(episode_count + 1): - print "Episode " + str(e) + "/" + str(episode_count) +for e in range(episode_count + 1): + print("Episode " + str(e) + "/" + str(episode_count)) state = getState(data, 0, window_size + 1) total_profit = 0 agent.inventory = [] - for t in xrange(l): + for t in range(l): action = agent.act(state) # sit @@ -29,22 +29,22 @@ if action == 1: # buy agent.inventory.append(data[t]) - print "Buy: " + formatPrice(data[t]) + print("Buy: " + formatPrice(data[t])) elif action == 2 and len(agent.inventory) > 0: # sell bought_price = agent.inventory.pop(0) reward = max(data[t] - bought_price, 0) total_profit += data[t] - bought_price - print "Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price) + print("Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price)) done = True if t == l - 1 else False agent.memory.append((state, action, reward, next_state, done)) state = next_state if done: - print "--------------------------------" - print "Total Profit: " + formatPrice(total_profit) - print "--------------------------------" + print("--------------------------------") + print("Total Profit: " + formatPrice(total_profit)) + print("--------------------------------") if len(agent.memory) > batch_size: agent.expReplay(batch_size)