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:
@@ -11,6 +11,9 @@ from ray.tune.schedulers import ASHAScheduler
|
|||||||
from hyperopt import hp
|
from hyperopt import hp
|
||||||
from ray.tune.suggest.hyperopt import HyperOptSearch
|
from ray.tune.suggest.hyperopt import HyperOptSearch
|
||||||
|
|
||||||
|
# get graphs
|
||||||
|
import intersim
|
||||||
|
from intersim.graphs import ConeVisibilityGraph
|
||||||
|
|
||||||
|
|
||||||
from src.main import basestr, main
|
from src.main import basestr, main
|
||||||
@@ -45,6 +48,10 @@ def parse_args():
|
|||||||
help='seed')
|
help='seed')
|
||||||
parser.add_argument('--nframes', default=500, type=int,
|
parser.add_argument('--nframes', default=500, type=int,
|
||||||
help='frames for test animation')
|
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()
|
args = parser.parse_args()
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'train':args.train,
|
'train':args.train,
|
||||||
@@ -55,7 +62,11 @@ def parse_args():
|
|||||||
'seed':args.seed,
|
'seed':args.seed,
|
||||||
'ray':args.ray,
|
'ray':args.ray,
|
||||||
'nframes':args.nframes,
|
'nframes':args.nframes,
|
||||||
|
'datadir':os.path.abspath(args.d),
|
||||||
|
'graph':None
|
||||||
}
|
}
|
||||||
|
if args.graph:
|
||||||
|
kwargs['graph'] = ConeVisibilityGraph(r=20, half_angle=120)
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def get_full_config(ray_config:dict, method:str)->dict:
|
def get_full_config(ray_config:dict, method:str)->dict:
|
||||||
@@ -124,23 +135,21 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
def ray_train(config, datadir=None):
|
def ray_train(config, datadir=None):
|
||||||
full_config = get_full_config(config, kwargs['method'])
|
full_config = get_full_config(config, kwargs['method'])
|
||||||
main(full_config, filestr='exp', datadir=datadir, **kwargs)
|
main(full_config, filestr='exp', **kwargs)
|
||||||
|
|
||||||
datadir = os.path.abspath('./expert_data')
|
|
||||||
|
|
||||||
ray_config = get_ray_config(kwargs['method'])
|
ray_config = get_ray_config(kwargs['method'])
|
||||||
search = HyperOptSearch(ray_config, max_concurrent=8, metric='cv_loss',mode="min",)
|
search = HyperOptSearch(ray_config, max_concurrent=8, metric='cv_loss',mode="min",)
|
||||||
custom_scheduler = ASHAScheduler(metric='cv_loss', mode="min", grace_period=15)
|
custom_scheduler = ASHAScheduler(metric='cv_loss', mode="min", grace_period=15)
|
||||||
|
|
||||||
analysis = tune.run(
|
analysis = tune.run(
|
||||||
partial(ray_train, datadir=datadir),
|
ray_train,
|
||||||
#config=ray_config,
|
#config=ray_config,
|
||||||
search_alg=search,
|
search_alg=search,
|
||||||
scheduler=custom_scheduler,
|
scheduler=custom_scheduler,
|
||||||
local_dir=outdir,
|
local_dir=outdir,
|
||||||
#resources_per_trial={"cpu": 2},
|
#resources_per_trial={"cpu": 2},
|
||||||
time_budget_s=120*60,
|
time_budget_s=120*60,
|
||||||
num_samples=100,
|
num_samples=200,
|
||||||
)
|
)
|
||||||
elif kwargs['ray'] and kwargs['test']:
|
elif kwargs['ray'] and kwargs['test']:
|
||||||
analysis = Analysis(outdir, default_metric="cv_loss", default_mode="min")
|
analysis = Analysis(outdir, default_metric="cv_loss", default_mode="min")
|
||||||
|
|||||||
9
experiments/experiments.sh
Executable file
9
experiments/experiments.sh
Executable 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
|
||||||
|
|
||||||
@@ -7,21 +7,28 @@ import numpy as np
|
|||||||
import intersim
|
import intersim
|
||||||
from intersim.utils import get_map_path, get_svt, SVT_to_stateactions
|
from intersim.utils import get_map_path, get_svt, SVT_to_stateactions
|
||||||
from intersim import collisions
|
from intersim import collisions
|
||||||
|
from intersim.graphs import ConeVisibilityGraph
|
||||||
import os
|
import os
|
||||||
opj = os.path.join
|
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
|
Function to save (joint) states and observations from simulated frame
|
||||||
Args:
|
Args:
|
||||||
path (str): directory to save data
|
path (str): directory to save data
|
||||||
loc (int): location index
|
loc (int): location index
|
||||||
track (int): track 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
|
kwargs: arguments for environment instantiation
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
action_reg = 0.002 if regularize_actions else 0
|
||||||
|
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(path):
|
||||||
os.mkdir(path)
|
os.makedirs(path)
|
||||||
filestr = opj(path,intersim.LOCATIONS[loc]+'_track%03i'%(track))
|
filestr = opj(path,intersim.LOCATIONS[loc]+'_track%03i'%(track))
|
||||||
|
|
||||||
svt, svt_path = get_svt(base='InteractionSimulator', loc=loc, track=track)
|
svt, svt_path = get_svt(base='InteractionSimulator', loc=loc, track=track)
|
||||||
@@ -31,6 +38,11 @@ def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, *
|
|||||||
states, actions = SVT_to_stateactions(svt)
|
states, actions = SVT_to_stateactions(svt)
|
||||||
|
|
||||||
# animate from environment
|
# animate from environment
|
||||||
|
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,
|
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm, **kwargs,
|
||||||
min_acc=-np.inf, max_acc=np.inf)
|
min_acc=-np.inf, max_acc=np.inf)
|
||||||
|
|
||||||
@@ -47,7 +59,7 @@ def generate_expert_data(path: str='expert_data', loc: int = 0, track:int = 0, *
|
|||||||
max_devs.append(norms.max())
|
max_devs.append(norms.max())
|
||||||
|
|
||||||
# propagate environment
|
# 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)
|
obs.append(ob)
|
||||||
actions_taken.append(info['action_taken'])
|
actions_taken.append(info['action_taken'])
|
||||||
i += 1
|
i += 1
|
||||||
@@ -165,9 +177,25 @@ if __name__ == '__main__':
|
|||||||
help='track number (default 0)')
|
help='track number (default 0)')
|
||||||
parser.add_argument('--all-tracks', action='store_true',
|
parser.add_argument('--all-tracks', action='store_true',
|
||||||
help='whether to process all tracks at location')
|
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()
|
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:
|
if args.all_tracks:
|
||||||
for i in range(intersim.MAX_TRACKS):
|
for i in range(intersim.MAX_TRACKS):
|
||||||
generate_expert_data(loc=args.loc, track=i)
|
kwargs['track'] = i
|
||||||
|
generate_expert_data(**kwargs)
|
||||||
else:
|
else:
|
||||||
generate_expert_data(loc=args.loc,track=args.track)
|
generate_expert_data(**kwargs)
|
||||||
@@ -59,14 +59,14 @@ def main(config, method='bc', train=False, test=False, loc=0, datadir='./expert_
|
|||||||
|
|
||||||
# simulate policy
|
# simulate policy
|
||||||
track = 4
|
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
|
# run test metrics
|
||||||
test_dataset = InteractionDatasetSingleAgent(output_dir=datadir, loc=loc, tracks=[track])
|
test_dataset = InteractionDatasetSingleAgent(output_dir=datadir, loc=loc, tracks=[track])
|
||||||
metrics(filestr, test_dataset, policy)
|
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
|
Simulate a trained policy
|
||||||
Args:
|
Args:
|
||||||
@@ -79,6 +79,10 @@ def simulate_policy(policy, loc=0, track=0, filestr='', nframes=float('inf')):
|
|||||||
basepath = os.path.abspath('./InteractionSimulator')
|
basepath = os.path.abspath('./InteractionSimulator')
|
||||||
svt, svt_path = get_svt(base=basepath, loc=loc, track=track)
|
svt, svt_path = get_svt(base=basepath, loc=loc, track=track)
|
||||||
osm = get_map_path(base=basepath, loc=loc)
|
osm = get_map_path(base=basepath, loc=loc)
|
||||||
|
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,
|
env = gym.make('intersim:intersim-v0', svt=svt, map_path=osm,
|
||||||
min_acc=-np.inf, max_acc=np.inf)
|
min_acc=-np.inf, max_acc=np.inf)
|
||||||
# env = gym.make('intersim:intersim-v0', loc=loc, track=track,
|
# env = gym.make('intersim:intersim-v0', loc=loc, track=track,
|
||||||
|
|||||||
Reference in New Issue
Block a user