-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
70 lines (53 loc) · 1.65 KB
/
environment.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
import argparse
import gym
from collections import deque
from agent import agent,memory
from preprocess import preProcess
import numpy as np
EPISODES = 1000
SKIP_INTIAL_FRAMES = 50
FRAMES_PER_ACTION = 25
BATCH_SIZE = memory.size
env = gym.make("CarRacing-v2",render_mode="human")
agent.model.summary()
agent.model.load_weights(r"C:\Users\Dell\Downloads\DQN700.h5")
print("S0a")
for episode in range(EPISODES):
init_state = env.reset()[0]
total_reward = 0
negative_reward_counter = 0
time_step = 1
current_state = init_state
current_state = preProcess.process(current_state)
for i in range(SKIP_INTIAL_FRAMES):
env.step([0,0,0])
while True:
env.render()
action = agent.act(current_state)
reward = 0
for i in range(FRAMES_PER_ACTION):
next_state, r, done, info,d = env.step(action)
reward +=r
total_reward += reward
print(reward)
if reward<0:
negative_reward_counter+=1
else:
negative_reward_counter = 0
if total_reward<0:
print("TOTAL_REWARD")
break
if negative_reward_counter>5:
print("NEG REWARD",negative_reward_counter)
break
next_state = preProcess.process(next_state)
memory.add([current_state,action,reward,next_state])
current_state = next_state
if memory.size>=BATCH_SIZE:
print("LEARNING",total_reward)
agent.learn()
agent.update_model()
print(episode)
if episode%20==0:
print("Saved")
agent.save_model("DQN",episode)