-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDQN_LEFT_ANIMATION.py
84 lines (59 loc) · 2.1 KB
/
DQN_LEFT_ANIMATION.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
import PingPongGame as PONG
import numpy as np
import math
import random
import tensorflow as tf
import pygame
import matplotlib.pyplot as plt
import MyFunctions
clock = pygame.time.Clock()
fstring = input("enter a name for the reward data file:")
results_file = open("./"+fstring,'w')
BATCH_SIZE = 32
ALPHA = 1e-4
GAMMA = .99
EPSILON = .97
rewards_array = list()
MODEL_L_STR = input ("enter the name of a model (left side player)")
#MODEL_R_STR = input ("enter the name of a model (right side player)")
LOAD_MODEL = False
session = tf.Session()
State_InL = tf.placeholder(tf.float32, shape = [None, 1,64,64])
with tf.variable_scope("paddleL"):
Q_L = MyFunctions.DQN(State_InL, reuse = False)
#saver1 = tf.train.Saver(var_list=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='paddleL'))
#saver1.restore(session, MODEL_L_STR)
session.run(tf.global_variables_initializer())
Game = PONG.PongGame(320, "pixels")
AVL=AVR = [1,0,0]
L,R, STATE = Game.Run4Frames(AVL,AVR)
AVL=AVR=5
NUM_ROUNDS_PLAYED = 0
time_step = 0
LRewSUM=0
RRewSUM=0
training_data = list()
RANDOM_FACTOR=0
temp = 0
oldREWSUM = 0
while (1):
clock.tick(10)
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:#for exiting the game
Game.QUITGAME()
#here I model the built in player:------------------------------------------------------------------------
if time_step%25 ==0:
RANDOM_FACTOR=random.randint(-25,25)
#print(RANDOM_FACTOR)
if Game.BALL_Y+Game.BALL_DIM/2 > Game.PADDLE_RIGHT_Y+25+RANDOM_FACTOR:
AVR=[0,0,1]
else:
AVR=[1,0,0]
#----------------------------------------------------------------------------------------------------------
AVL = session.run(Q_L, feed_dict = {State_InL: [STATE]})
AVL = MyFunctions.ONE_HOT_ACTIONS(AVL)
L,R, STATE = Game.Run4Frames(AVL,AVR)
LRewSUM = L+LRewSUM
RRewSUM = R+RRewSUM
print(LRewSUM,RRewSUM)