2022-10-16 18:58:32 +02:00
|
|
|
import torch as th
|
2022-06-19 15:01:30 +02:00
|
|
|
from time import sleep, time
|
|
|
|
import numpy as np
|
|
|
|
import pygame
|
2022-09-20 21:56:50 +02:00
|
|
|
import yaml
|
2022-06-19 15:01:30 +02:00
|
|
|
|
2022-06-22 16:05:14 +02:00
|
|
|
from columbus import env
|
|
|
|
from columbus.observables import Observable, CnnObservable
|
2022-06-19 15:01:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-09-20 21:56:50 +02:00
|
|
|
env = chooseEnv()
|
|
|
|
while True:
|
|
|
|
playEnv(env)
|
|
|
|
input('<again?>')
|
2022-06-19 15:01:30 +02:00
|
|
|
env.close()
|
|
|
|
|
|
|
|
|
2022-06-22 13:08:44 +02:00
|
|
|
def getAvaibleEnvs():
|
|
|
|
# kinda hacky... idk
|
|
|
|
strs = dir(env)
|
|
|
|
for s in strs:
|
|
|
|
if s.startswith('Columbus') and s != 'ColumbusEnv':
|
|
|
|
yield getattr(env, s)
|
|
|
|
|
|
|
|
|
2022-09-20 21:56:50 +02:00
|
|
|
def loadConfigDefinedEnv(EnvClass):
|
|
|
|
p = input('[Path to config> ')
|
|
|
|
with open(p, 'r') as f:
|
|
|
|
docs = list([d for d in yaml.safe_load_all(
|
|
|
|
f) if 'name' in d and d['name'] not in ['SLURM']])
|
|
|
|
for i, doc in enumerate(docs):
|
|
|
|
name = doc['name']
|
|
|
|
print('['+str(i)+'] '+name)
|
|
|
|
ds = int(input('[0]> ') or '0')
|
|
|
|
doc = docs[ds]
|
|
|
|
cur = doc
|
|
|
|
path = 'params.task.env_args'
|
|
|
|
p = path.split('.')
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if len(p) == 0:
|
|
|
|
break
|
|
|
|
key = p.pop(0)
|
|
|
|
print(key)
|
|
|
|
cur = cur[key]
|
|
|
|
except Exception as e:
|
|
|
|
print('Unable to find key "'+key+'"')
|
|
|
|
path = input('[Path> ')
|
|
|
|
print(cur)
|
|
|
|
return EnvClass(fps=30, **cur)
|
|
|
|
|
|
|
|
|
2022-06-22 13:08:44 +02:00
|
|
|
def chooseEnv():
|
|
|
|
envs = list(getAvaibleEnvs())
|
|
|
|
for i, Env in enumerate(envs):
|
|
|
|
print('['+str(i)+'] '+Env.__name__)
|
|
|
|
while True:
|
|
|
|
inp = input('[#> ')
|
|
|
|
try:
|
|
|
|
i = int(inp)
|
|
|
|
except:
|
|
|
|
print('[!] You have to enter the number...')
|
|
|
|
if i < 0 or i >= len(envs):
|
|
|
|
print(
|
|
|
|
'[!] That is a number, but not one that makes sense in this context...')
|
2022-09-20 21:56:50 +02:00
|
|
|
if envs[i] in [env.ColumbusConfigDefined]:
|
|
|
|
return loadConfigDefinedEnv(envs[i])
|
|
|
|
Env = envs[i]
|
|
|
|
return Env(fps=30)
|
2022-06-22 13:08:44 +02:00
|
|
|
|
|
|
|
|
2022-10-16 18:58:32 +02:00
|
|
|
def value_func(obs):
|
|
|
|
return th.rand(obs.shape[0])-0.5
|
|
|
|
|
|
|
|
|
2022-06-19 15:01:30 +02:00
|
|
|
def playEnv(env):
|
|
|
|
done = False
|
2022-08-25 13:38:59 +02:00
|
|
|
env.reset()
|
2022-06-19 15:01:30 +02:00
|
|
|
while not done:
|
|
|
|
t1 = time()
|
2022-10-16 18:58:32 +02:00
|
|
|
# env.render(value_func=value_func)
|
2022-06-19 15:01:30 +02:00
|
|
|
env.render()
|
|
|
|
pos = (0.5, 0.5)
|
|
|
|
pos = pygame.mouse.get_pos()
|
|
|
|
pos = (min(max((pos[0]-env.joystick_offset[0]-20)/60, 0), 1),
|
|
|
|
min(max((pos[1]-env.joystick_offset[1]-20)/60, 0), 1))
|
2022-06-29 12:42:20 +02:00
|
|
|
pos = pos[0]*2-1, pos[1]*2-1
|
2022-06-19 15:01:30 +02:00
|
|
|
obs, rew, done, info = env.step(np.array(pos, dtype=np.float32))
|
|
|
|
print('Reward: '+str(rew))
|
|
|
|
print('Score: '+str(info))
|
|
|
|
t2 = time()
|
|
|
|
dt = t2 - t1
|
|
|
|
delay = (1/env.fps - dt)
|
|
|
|
if delay < 0:
|
|
|
|
print("[!] Can't keep framerate!")
|
|
|
|
else:
|
|
|
|
sleep(delay)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|