Skip to content
Snippets Groups Projects
Select Git revision
  • edd2e89498f0c8324bf4481dad083170d41bf5fa
  • master default protected
2 results

Gym_new.py

Blame
  • Gym_new.py 2.76 KiB
    import asyncio
    import random
    import json
    
    import websockets
    import logging
    
    
    class Gym:
        host = None
        port = None
        player = None
        logger = None
    
    
        def __init__(self, host ='localhost', port =9080, player ="player1"):
            self.host = host
            self.port = port
            self.player = player
            random.seed()
    
            self.logger = logging.getLogger('websockets')
            self.logger.setLevel(logging.INFO)
            self.logger.addHandler(logging.StreamHandler())
            print('New gym instance IP:', host, 'Port:', port, 'Playername:', player)
            
        # Call reset function of environment and returns observation, reward, done, info
        # Send data to reset enviroment
        async def reset(self, delta):
            message = json.dumps([['reset'], delta], sort_keys=True, indent=4)
            data = []
            await self.send_websocket(message, data)
            all_information = json.loads(data[0])
            reward = all_information[3]
            observation = all_information[0:3]
            done = all_information[4]
            info = all_information[5]
            return observation, reward, done, info
    
        # send action to env returns observation, reward, done, info
        # Obeservation is an array with the following information: 
        # [speed, steering-angle, [length of all sensors (starting straight and then clockwise around the car)]]
        # creates JSON object
        async def step(self, action, delta):
            global maxaction
            message = []
            if action.find("accelerate")!=-1:
                message.append( "accelerate")
            if action.find("left")!= -1:
                message.append( "steer_left")
            if action.find("right")!= -1:
                message.append("steer_right")
            if action.find("brake")!= -1:
                message.append("brake")
            if action.find("reverse")!= -1:
                message.append("reverse")
            if action.find("screen_on")!= -1:
                message.append("screen_on")
            if action.find("screen_off")!= -1:
                message.append("screen_off")
            
            message = json.dumps([message,delta], sort_keys=True, indent=4)
            data = []
            
            await self.send_websocket(message, data)
            all_information = json.loads(data[0])
            reward = all_information[3]
            observation = all_information[0:3]
            done = all_information[4]
            info = all_information[5]
            return observation, reward, done, info
    
        async def connect_websocket(self):
            print("creating Websocket ")
            uri = "ws://localhost:9080"
            self.websocket = await websockets.connect(uri)
    
        
        async def send_websocket(self, message, data):
                await self.websocket.send(message)
                data.append(await self.websocket.recv())
    
        async def close_websocket(self):
            await self.websocket.close()