Skip to content
Snippets Groups Projects
Select Git revision
  • e1f2867c7d021ebd8864ec5e043f0ccc43ce841f
  • master default protected
  • v3-modify-mail
  • snyk-fix-207483a1e839c807f95a55077e86527d
  • translations_3b5aa4f3c755059914cfa23d7d2edcde_ru
  • translations_6e4a5e377a3e50f17e6402264fdbfcc6_ru
  • translations_3b5aa4f3c755059914cfa23d7d2edcde_fa_IR
  • translations_en-yml--master_fa_IR
  • snyk-fix-7d634f2eb65555f41bf06d6af930e812
  • translations_en-yml--master_ar
  • translations_3b5aa4f3c755059914cfa23d7d2edcde_el
  • jfederico-patch-1
  • v2
  • v3
  • v1
  • release-3.1.0.2
  • release-3.1.0.1
  • release-3.1.0
  • release-2.14.8.4
  • release-3.0.9.1
  • release-3.0.9
  • release-3.0.8.1
  • release-2.14.8.3
  • release-3.0.8
  • release-3.0.7.1
  • release-2.14.8.2
  • release-3.0.7
  • release-3.0.6.1
  • release-3.0.6
  • release-3.0.5.4
  • release-3.0.5.3
  • release-2.14.8.1
  • release-3.0.5.2
  • release-3.0.5.1
  • release-3.0.5
35 results

.ruby-version

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()