Skip to content
Snippets Groups Projects
Commit b5ea406f authored by Theo Brockmann's avatar Theo Brockmann
Browse files

Upload New File

parent b1939723
Branches
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 5 08:57:41 2021
@author: theob
"""
import asyncio
import Gym_new
import time
import nest_asyncio
import numpy as np
async def main():
env = Gym_new.Gym()
await env.connect_websocket()
win = 0
loss = 0
for i in range(100):
delta = 0.01
kurve = True
not_on = True
show_angle = 0
observation, reward, done, info = await env.reset("",0.01)
total_reward = 0
actions = "accelerate"
observation, reward, done, info = await env.step(actions, 0.5)
old_observation = observation
while not done and total_reward > -80 and reward[1] < 50:
speed = observation[0]
angle = observation[1]
try:
if kurve:
soll_winkel = (observation[2][1] - observation[2][3]) - 1.5 * (observation[2][6] - observation[2][2])
soll_winkel = min(5, soll_winkel)
if soll_winkel > angle:
actions = 'steer_right'
elif soll_winkel < angle:
actions = "steer_left"
if speed < 50:
actions += "accelerate"
else:
if observation[2][2] < 6 and not_on:
soll_winkel = (observation[2][1]-observation[2][3]) - min(2, 0.25 * (6.5 - observation[2][2])) + 0.5 * (6 - min(6, observation[2][5]) + (4 - min(4, observation[2][6])) + 0.25 * (4 - min(4, observation[2][7])) - (4 - min(4, observation[2][2])))
elif observation[2][1] > 8:
not_on = False
soll_winkel = min(4, 0.5 * (observation[2][1]-observation[2][3]) - 0.5 * (6.5 - observation[2][2]))# - 0.5 * (6.5 - min(6.5, observation[2][2]))
else:
not_on = False
soll_winkel = 0
if show_angle > 10:
# print(soll_winkel)
show_angle = 0
show_angle += 1
soll_winkel = min(5, soll_winkel)
if soll_winkel > angle:
actions = 'steer_right'
elif soll_winkel < angle:
actions = "steer_left"
if speed < 80 and observation[2][0] > 5:
actions += "accelerate"
elif observation[2][0] < 4:
actions += "brake"
except:
pass
det_kurve = observation[2][5:8]
old_observation = observation
observation, reward, done, info = await env.step(actions,delta)
det_kurve2 = observation[2][5:8]
try:
if det_kurve2[0]/det_kurve[0] > 1.5 or det_kurve2[1]/det_kurve[1] > 1.5 or det_kurve2[2]/det_kurve[2] > 1.5:
kurve = False
except:
pass
total_reward -= reward[1]
actions = ""
#print(old_observation)
print(total_reward)
if total_reward > 50:
win += 1
else:
loss += 1
print(win)
print(loss)
print(i + 1)
f=open('ClientV3TestDone_spped50_10_03_map3.csv','ab')
np.savetxt(f,[np.append(done,reward[0])])
f.close()
print('\n')
# Indent
print(win)
print(loss)
print(win/(win+loss))
await env.close_websocket()
if __name__ == "__main__":
nest_asyncio.apply()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment