adding options for regularization and relative state masking via interaction graphs during data processing and experiment running. found 0.002 regularization on actions gives up to 3m of deviation with no collisions. added shell script to run ray experiments overnight

This commit is contained in:
Arec
2021-08-02 14:38:06 -07:00
parent fb91ee1a62
commit 6e524cf4b5
4 changed files with 66 additions and 16 deletions

View File

@@ -11,6 +11,9 @@ from ray.tune.schedulers import ASHAScheduler
from hyperopt import hp
from ray.tune.suggest.hyperopt import HyperOptSearch
# get graphs
import intersim
from intersim.graphs import ConeVisibilityGraph
from src.main import basestr, main
@@ -45,6 +48,10 @@ def parse_args():
help='seed')
parser.add_argument('--nframes', default=500, type=int,
help='frames for test animation')
parser.add_argument('--graph', action='store_true',
help='whether to mask the relative states based on a ConeVisibilityGraph')
parser.add_argument('-d', default='./expert_data', type=str,
help='data directory')
args = parser.parse_args()
kwargs = {
'train':args.train,
@@ -55,7 +62,11 @@ def parse_args():
'seed':args.seed,
'ray':args.ray,
'nframes':args.nframes,
'datadir':os.path.abspath(args.d),
'graph':None
}
if args.graph:
kwargs['graph'] = ConeVisibilityGraph(r=20, half_angle=120)
return kwargs
def get_full_config(ray_config:dict, method:str)->dict:
@@ -124,23 +135,21 @@ if __name__ == '__main__':
def ray_train(config, datadir=None):
full_config = get_full_config(config, kwargs['method'])
main(full_config, filestr='exp', datadir=datadir, **kwargs)
datadir = os.path.abspath('./expert_data')
main(full_config, filestr='exp', **kwargs)
ray_config = get_ray_config(kwargs['method'])
search = HyperOptSearch(ray_config, max_concurrent=8, metric='cv_loss',mode="min",)
custom_scheduler = ASHAScheduler(metric='cv_loss', mode="min", grace_period=15)
analysis = tune.run(
partial(ray_train, datadir=datadir),
ray_train,
#config=ray_config,
search_alg=search,
scheduler=custom_scheduler,
local_dir=outdir,
#resources_per_trial={"cpu": 2},
time_budget_s=120*60,
num_samples=100,
num_samples=200,
)
elif kwargs['ray'] and kwargs['test']:
analysis = Analysis(outdir, default_metric="cv_loss", default_mode="min")

9
experiments/experiments.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/sh
python experiments/experiment.py --ray --train -d ./expert_data/base
python experiments/experiment.py --ray --test -d ./expert_data/base --nframes 1000
python experiments/experiment.py --ray --train -d ./expert_data/reg
python experiments/experiment.py --ray --test -d ./expert_data/reg --nframes 1000
python experiments/experiment.py --ray --train -d ./expert_data/reg_graph --graph
python experiments/experiment.py --ray --test -d ./expert_data/reg_graph --graph --nframes 1000

View File

@@ -7,21 +7,28 @@ import numpy as np
import intersim
from intersim.utils import get_map_path, get_svt, SVT_to_stateactions
from intersim import collisions
from intersim.graphs import ConeVisibilityGraph
import os
opj = os.path.join
def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, **kwargs):
def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0,
mask_relstate: bool = False, regularize_actions: bool = False,
**kwargs):
"""
Function to save (joint) states and observations from simulated frame
Args:
path (str): directory to save data
loc (int): location index
track (int): track index
mask_relstate (bool): whether to mask the relative states from the cone visibility graph
regularize_actions (bool): whether to regularize the action selection
kwargs: arguments for environment instantiation
"""
action_reg = 0.002 if regularize_actions else 0
if not os.path.isdir(path):
os.mkdir(path)
os.makedirs(path)
filestr = opj(path,intersim.LOCATIONS[loc]+'_track%03i'%(track))
svt, svt_path = get_svt(base='InteractionSimulator', loc=loc, track=track)
@@ -31,8 +38,13 @@ def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, *
states, actions = SVT_to_stateactions(svt)
# animate from environment
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm, **kwargs,
min_acc=-np.inf, max_acc=np.inf)
if mask_relstate:
cvg = ConeVisibilityGraph(r=20, half_angle=120)
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm,
min_acc=-np.inf, max_acc=np.inf, graph=cvg, mask_relstate=True, **kwargs)
else:
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm, **kwargs,
min_acc=-np.inf, max_acc=np.inf)
env.reset()
done = False
@@ -47,7 +59,7 @@ def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, *
max_devs.append(norms.max())
# propagate environment
ob, r, done, info = env.step(env.target_state(svt.simstate[i+1]))
ob, r, done, info = env.step(env.target_state(svt.simstate[i+1], mu=action_reg))
obs.append(ob)
actions_taken.append(info['action_taken'])
i += 1
@@ -165,9 +177,25 @@ if __name__ == '__main__':
help='track number (default 0)')
parser.add_argument('--all-tracks', action='store_true',
help='whether to process all tracks at location')
parser.add_argument('--graph', action='store_true',
help='whether to mask the relative states based on a ConeVisibilityGraph')
parser.add_argument('--reg', action='store_true',
help='whether to regularize actions in the action targeter')
parser.add_argument('-o', default='./expert_data', type=str,
help='output folder')
args = parser.parse_args()
kwargs = {
'loc':args.loc,
'track': args.track,
'path':args.o,
'mask_relstate':args.graph,
'regularize_actions': args.reg
}
if args.all_tracks:
for i in range(intersim.MAX_TRACKS):
generate_expert_data(loc=args.loc, track=i)
kwargs['track'] = i
generate_expert_data(**kwargs)
else:
generate_expert_data(loc=args.loc,track=args.track)
generate_expert_data(**kwargs)

View File

@@ -59,14 +59,14 @@ def main(config, method='bc', train=False, test=False, loc=0, datadir='./expert_
# simulate policy
track = 4
simulate_policy(policy, loc=loc, track=track, filestr=filestr, nframes=kwargs['nframes'])
simulate_policy(policy, loc=loc, track=track, filestr=filestr, nframes=kwargs['nframes'], graph=kwargs['graph'])
# run test metrics
test_dataset = InteractionDatasetSingleAgent(output_dir=datadir, loc=loc, tracks=[track])
metrics(filestr, test_dataset, policy)
def simulate_policy(policy, loc=0, track=0, filestr='', nframes=float('inf')):
def simulate_policy(policy, loc=0, track=0, filestr='', nframes=float('inf'), graph=None):
"""
Simulate a trained policy
Args:
@@ -79,8 +79,12 @@ def simulate_policy(policy, loc=0, track=0, filestr='', nframes=float('inf')):
basepath = os.path.abspath('./InteractionSimulator')
svt, svt_path = get_svt(base=basepath, loc=loc, track=track)
osm = get_map_path(base=basepath, loc=loc)
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm,
min_acc=-np.inf, max_acc=np.inf)
if graph:
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm,
min_acc=-np.inf, max_acc=np.inf, graph=graph, mask_relstate=True)
else:
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm,
min_acc=-np.inf, max_acc=np.inf)
# env = gym.make('intersim:intersim-v0', loc=loc, track=track,
# min_acc=-np.inf, max_acc=np.inf)