Example #1
0
def get_sim_barrier():

    if os.path.exists('exp_results/sim_barrier.csv'):
        os.remove('exp_results/sim_barrier.csv')

    x0 = dict()
    with open('exp_results/resfd10' + '/info.csv', 'r') as f:
        data = f.readlines()
        for line in data:
            if 'x' in line:
                ldata = line.split(',')
                role = ldata[0][1:]
                x0[role] = np.array([float(ldata[1]), float(ldata[2])])
            if 'vd' in line:
                vd = float(line.split(',')[-1])
            if 'vi' in line:
                vi = float(line.split(',')[-1])

    if vd < vi:
        game = SlowDgame(LineTarget(), exp_dir='resfd10/')
        lb0, ub0 = .1, .8
    else:
        game = FastDgame(LineTarget(), exp_dir='resfd10/')
        lb0, ub0 = -.1, .4

    xbs, ybs = [], []
    with open('exp_results/sim_barrier.csv', 'a') as f:
        for xI in np.linspace(-.6, 0, 30):
            # for xI in [-.2]:
            lb, ub = lb0, ub0
            print(xI)
            while abs(ub - lb) > 0.005:
                # print(ub, lb)
                yI = .5 * (lb + ub)
                x0['I0'] = np.array([xI, yI])
                # print(x0)
                game.reset(x0)
                _, xs, info = game.advance(20.)
                print(yI, info)
                # game.plotter.reset()
                # game.plotter.plot({'play':xs}, 'play', game.pstrategy, fname=None)
                if info == 'captured':
                    ub = yI
                elif info == 'entered':
                    lb = yI
            xbs.append(xI)
            ybs.append(.5 * (lb + ub))
            f.write(','.join(map(str, [xI, .5 * (lb + ub)])) + '\n')

    return xbs, ybs
Example #2
0
def play_fastD_game(xd0, xi, xd1, ni=1, nd=2, param_file='traj_param_100.csv'):
    game = FastDgame(LineTarget())
    x0 = {'D0': xd0, 'I0': xi, 'D1': xd1}
    game.reset(x0)
    game.record_data(x0, file=param_file)

    ts_play, xs_play, _ = game.advance(8.)
    fname = '_'.join([strategy for role, strategy in game.pstrategy.items()])
    figid = param_file.split('.')[0].split('_')[-1]
    # game.plotter.plot(xs={'play': xs_play}, geox='play', ps=game.pstrategy, traj=True, fname='traj_'+fname+'_'+figid+'.png')
    game.plotter.plot(xs={'play': xs_play},
                      geox='play',
                      ps=game.pstrategy,
                      traj=True)
Example #3
0
def sim_barrier(r, vd):

	x0 = {'D0': np.array([-.85, .2]), 'I0': np.array([-.2, 1.2]), 'D1': np.array([.85, .2])}
	if 1. < r:
		game = SlowDgame(LineTarget())
		lb0, ub0 = .0, .5
	else:
		game = FastDgame(LineTarget())
		lb0, ub0 = -.1, .4

	vi = r*vd
	game.reset(x0)
	game.set_vi(r*vd)
	game.set_vd(vd)

	xbs, ybs = [], []

	if os.path.exists('sim_revision1/sim_barrier_%.2f.csv'%((vd/vi)*100)):	
		os.remove('sim_revision1/sim_barrier_%.2f.csv'%((vd/vi)*100))

	with open('sim_revision1/sim_barrier_%.2f.csv'%((vd/vi)*100), 'a') as f:			
		for xI in np.linspace(-.6, 0, 13):
		# for xI in [-.6]:
			lb, ub = lb0, ub0
			print(xI)
			while abs(ub - lb) > 0.0005:
				# print(ub, lb)
				yI = .5*(lb + ub)
				x0['I0'] = np.array([xI, yI])
				# print(x0)
				game.reset(x0)
				# print('!!!!!!!!!! reseted x0 !!!!!!!!!!!!!')
				_, xs, info = game.advance(5000.)
				print(yI, info)
				# game.plotter.reset()
				# game.plotter.plot({'play':xs}, 'play', game.pstrategy, fname=None)
				if info == 'captured':
					ub = yI
				elif info =='entered':
					lb = yI
			xbs.append(xI)
			ybs.append(.5*(lb + ub))
			f.write(','.join(map(str, [xI, .5*(lb + ub)]))+'\n')
	
	# game.plotter.plot_sim_barrier_line()

	return xbs, ybs
Example #4
0
def sim_traj(rs, vd):

	x0 = {'D0': np.array([-.85, .2]), 'I0': np.array([-0.5, .75]), 'D1': np.array([.85, .2])}
	trajs = []
	# rs = []

	for r in rs:
		if 1. < r:
			game = SlowDgame(LineTarget())
		else:
			game = FastDgame(LineTarget())

		game.reset(x0)
		game.set_vi(r*vd)
		game.set_vd(vd)

		_, traj, _ = game.advance(8.)
		trajs.append(traj)
		# rs.append(vd/vi)

	game.plotter.plot_traj_compare(trajs, rs)
Example #5
0
def replay_exp(res_dir='res1/', ni=1, nd=2):
    x0s = dict()
    pdict = dict()
    with open('exp_results/' + res_dir + '/info.csv', 'r') as f:
        data = f.readlines()
        for line in data:
            if 'vd' in line:
                vd = float(line.split(',')[-1])
            if 'vi' in line:
                vi = float(line.split(',')[-1])
            if 'x' in line:
                ldata = line.split(',')
                role = ldata[0][1:]
                x0s[role] = np.array([float(ldata[1]), float(ldata[2])])

    if vd < vi:
        game = SlowDgame(LineTarget(), exp_dir=res_dir, ni=ni, nd=nd)
    else:
        game = FastDgame(LineTarget(), exp_dir=res_dir, ni=ni, nd=nd)

    game.reset({role: x for role, x in x0s.items()})
    # ts_ref, xs_ref, _ = game.advance(8.)
    ts_exp, xs_exp, ps_exp = game.replay_exp()
    for role, x in xs_exp.items():
        x0s[role] = x[0]
    game.reset({role: x for role, x in x0s.items()})
    ts_ref, xs_ref, _ = game.advance(8.)

    game.plotter.animate(ts_exp, xs_exp, game.pstrategy, xrs=xs_ref)
    game.plotter.plot({
        'ref': xs_ref,
        'exp': xs_exp
    },
                      'exp',
                      game.pstrategy,
                      dr=False,
                      fname='replay_traj.png')
Example #6
0
    },
                      'exp',
                      game.pstrategy,
                      dr=False,
                      fname='replay_traj.png')
    # game.plotter.plot({'ref':xs_ref, 'exp':xs_exp}, 'exp', pdict, dr=False, fname='replay_traj.png')


if __name__ == '__main__':

    # t0 = time.clock()
    # generate_data_for_exp(-.89, 2.8, acos(25/27)+0.5, 0.1, 0.3999999, param_file='traj_param_append01.csv')
    # play_fastD_game(np.array([-.85, 0.2]), np.array([-0.2, 0.4]), np.array([.85, 0.2]), param_file='traj_param_tst0.csv')
    # play_a_game(np.array([-.85, 0.2]), np.array([-0.2, 0.6]), np.array([.85, 0.2]), param_file='traj_param_tst0.csv')

    # replay_exp(res_dir='resfd24002/')
    # replay_exp(res_dir='resfd25001/')
    # replay_exp(res_dir='ressd26002/')
    # replay_exp(res_dir='ressd032/')

    # t1 = time.clock()
    # print(t1 - t0)
    # game = FastDgame(LineTarget(), exp_dir='ressd26002/')
    game = FastDgame(LineTarget(), exp_dir='ressd032/')
    # game = SlowDgame(LineTarget(), exp_dir='resfd24002/')
    # game.players['D0'].x = np.array([-0.85, 0.2])
    # game.players['D1'].x = np.array([ 0.85, 0.2])
    game.plotter.plot_barrier(dr='fd')

    # game.plotter.plot_velocity()
Example #7
0
from Games import FastDgame, SlowDgame
from geometries import LineTarget

with open('config.csv', 'r') as f:
    data = f.readlines()
    for line in data:
        if 'vd' in line:
            vd = float(line.split(',')[-1])
        if 'vi' in line:
            vi = float(line.split(',')[-1])
sim_dir = 'res5/'
xplot = dict()

if vd >= vi:
    game = FastDgame(LineTarget(), sim_dir=sim_dir)
    x0 = {
        'D0': np.array([-.8, 0.2]),
        'I0': np.array([-.1, .4]),
        'D1': np.array([.8, 0.2])
    }
else:
    game = SlowDgame(LineTarget(), sim_dir=sim_dir)
    # rgame = SlowDgame(LineTarget(), sim_dir=sim_dir)
    # for role in rgame.pstrategy:
    # 	rgame.pstrategy[role] = 'nn'

    xref = game.generate_analytic_traj(.0,
                                       5,
                                       acos(1 / 1.5) + 0.2,
                                       0,