Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
Open_Ai_Gym_Autobahn_Auffahren
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Midras Lappe
Open_Ai_Gym_Autobahn_Auffahren
Commits
b368afc9
Commit
b368afc9
authored
4 years ago
by
Theo Brockmann
Browse files
Options
Downloads
Patches
Plain Diff
Update Gym_new.py
parent
bc6a0e77
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Gym_new.py
+82
-155
82 additions, 155 deletions
Gym_new.py
with
82 additions
and
155 deletions
Gym_new.py
+
82
−
155
View file @
b368afc9
import
asyncio
import
random
import
json
from
socket
import
socket
import
websockets
#from ActionSpace import ActionSpace
import
logging
...
...
@@ -13,28 +10,15 @@ class Gym:
host
=
None
port
=
None
player
=
None
families
=
None
types
=
None
protocols
=
None
logger
=
None
# Create a TCP/IP socket
sock
=
None
last_recieved_JSON
=
None
loop
=
None
maxaction
=
5
#websocket = None
def
__init__
(
self
,
host
=
'
localhost
'
,
port
=
9080
,
player
=
"
player1
"
):
self
.
host
=
host
self
.
port
=
port
self
.
player
=
player
#self.loop = loop
random
.
seed
()
# self.families = self.get_constants('AF_')
# self.types = self.get_constants('SOCK_')
# self.protocols = self.get_constants('IPPROTO_')
self
.
logger
=
logging
.
getLogger
(
'
websockets
'
)
self
.
logger
.
setLevel
(
logging
.
INFO
)
self
.
logger
.
addHandler
(
logging
.
StreamHandler
())
...
...
@@ -45,38 +29,21 @@ class Gym:
async
def
reset
(
self
,
delta
):
message
=
json
.
dumps
([[
'
reset
'
],
delta
],
sort_keys
=
True
,
indent
=
4
)
data
=
[]
#print('send_action:',message)
await
self
.
send_websocket
(
message
,
data
)
test
=
json
.
loads
(
data
[
0
])
# print(test)
# print(test[0])
# print(test[1])
reward
=
test
[
3
]
observation
=
test
[
0
:
3
]
done
=
test
[
4
]
info
=
0
# print(data)
# print(data[0][0])
return
observation
,
reward
,
done
,
info
# send action to env returns observation, reward, done, info
# creates JSON object
async
def
step
(
self
,
action
,
delta
):
global
maxaction
# Test ob die Action im richtigen Bereich ist!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
test_message
=
[]
if
action
.
find
(
"
accelerate
"
)
!=-
1
:
#(action>=0) and (action < maxaction):
if
action
.
find
(
"
accelerate
"
)
!=-
1
:
test_message
.
append
(
"
accelerate
"
)
#message = json.dumps({'action': action, 'delta': delta}, sort_keys=True, indent=4)
#message = json.dumps([action], sort_keys=True, indent=4)
#message2 = str(action)
#hier data aufteilen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#test = json.loads(data)
#print(test)
#x = data.split(',')
#print(x)
if
action
.
find
(
"
left
"
)
!=
-
1
:
test_message
.
append
(
"
steer_left
"
)
if
action
.
find
(
"
right
"
)
!=
-
1
:
...
...
@@ -90,66 +57,26 @@ class Gym:
if
action
.
find
(
"
screen_off
"
)
!=
-
1
:
test_message
.
append
(
"
screen_off
"
)
else
:
pass
#print("Action is not a valid Step")
message
=
json
.
dumps
([
test_message
,
delta
],
sort_keys
=
True
,
indent
=
4
)
#message = json.dumps([action], sort_keys=True, indent=4)
data
=
[]
#print('send_action:', message)
await
self
.
send_websocket
(
message
,
data
)
#print("awaited send_action")
test
=
json
.
loads
(
data
[
0
])
# print(test)
# print(test[0])
# print(test[1])
reward
=
test
[
3
]
observation
=
test
[
0
:
3
]
done
=
test
[
4
]
info
=
test
[
5
]
# print(data)
# print(data[0][0])
return
observation
,
reward
,
done
,
info
"""
Returns a random action of action space
"""
# Gibt jetzt eine Action zurück als Zahlenwert
def
get_sample_action
(
self
):
global
maxaction
action
=
random
.
randrange
(
0
,
maxaction
)
return
action
# socket_echo_client_easy.py
# Wird jetzt nicht mehr benutzt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# async def send_action(self, next_action):
# message = '["' + next_action.name + '"]'
# data = []
# print('send_action:',message)
# await self.send_websocket(message, data)
# print("awaited send_action")
# print(data)
# return data
async
def
connect_websocket
(
self
):
print
(
"
creating Websocket
"
)
uri
=
"
ws://localhost:9080
"
self
.
websocket
=
await
websockets
.
connect
(
uri
)
# async with websockets.connect(uri) as websocket:
# print("Websocket created ")
# self.websocket = websocket
async
def
send_websocket
(
self
,
message
,
data
):
async
def
send_websocket
(
self
,
message
,
data
):
await
self
.
websocket
.
send
(
message
)
# print(f"> {name}")
data
.
append
(
await
self
.
websocket
.
recv
())
#print(f"< {data}")
#print(data)
async
def
close_websocket
(
self
):
await
self
.
websocket
.
close
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment