def play_a_game(xd0, xi, xd1, param_file='traj_param_tst0.csv'): # game = FastDgame(LineTarget()) game = SlowDgame(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')
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)
def generate_data_for_exp(S, T, gmm, D, delta, offy=0.2, ni=1, nd=2, param_file='traj_param_test.csv'): game = SlowDgame(LineTarget()) xs_ref = game.generate_analytic_traj(S, T, gmm, D, delta, offy=offy, file=param_file) game.reset({ 'D0': xs_ref['D0'][0, :], 'I0': xs_ref['I0'][0, :], 'D1': xs_ref['D1'][0, :] }) ts, xs_play, _ = game.advance(10.) # for role in xs_ref: # xs_ref[role] = game.rotate_to_exp(xs_ref[role]) xplot = {'play': xs_play, 'ref': xs_ref} fname = '_'.join([strategy for role, strategy in game.pstrategy.items()]) figid = param_file.split('.')[0].split('_')[-1] game.plotter.plot(xs=xplot, geox='play', ps=game.pstrategy, traj=True)
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')
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
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, 0.1999999999, file='traj_param.csv') x0 = dict() for role in xref: if 'I' in role: x0[role] = xref[role][0] + np.array([0., .3]) else:
def plot_sim_barrier(): ratios = [1/0.8, 1/.97, 1/1.2, 1/1.56] # ratios = [25/24] linestyles=[(0, ((ratio-.6)*8, (ratio-.6)*3)) for ratio in ratios[::-1]] linestyles[-1] = 'solid' alphas = [(ratio*.8)**1.7 for ratio in ratios[::-1]] game = SlowDgame(LineTarget()) fig, ax = plt.subplots() colors = ['purple', 'magenta', 'red', 'orange'] for i, ratio in enumerate(ratios): # for ratio in [1/1.2,]: print((ratio/1.26)**1.7) xy = [] with open('sim_revision1/sim_barrier_%.2f.csv'%(ratio*100), 'r') as f: print('sim_revision1/sim_barrier_%.2f.csv'%(ratio*100)) lines = f.readlines()[1:] for line in lines: data = line.split(',') xy.append(np.array([float(data[0]), float(data[1])])) xy.append(np.array([-float(data[0]), float(data[1])])) # xy.append(np.array([-float(data[1]), float(data[0])])) # xy.append(np.array([-float(data[1]), -float(data[0])])) xy = np.asarray(sorted(xy, key=lambda xys: xys[0])) # xy = game.rotate_to_exp_point(xy) # line, = self.ax.plot(px[:,0], px[:,1], color=self.colors[pid], label='a=%.1f'%ratio, alpha=(ratio/1.4)**1.7, linestyle=(0, ((ratio-.6)*8, (ratio-.6)*3))) ax.plot(xy[:,0], xy[:,1], color='r', label='a=%.1f'%ratio, alpha=alphas[i], linestyle=linestyles[i]) print(xy) # ax.plot([-1.5, 1.5], [-.5, -.5], color='k', label='target') # ax.plot([-0.85], [0.2], 'bo') # ax.plot([0.85], [0.2], 'bo') ax.plot([-.5], [.75], 'rx') ax.plot([-.2], [.5], 'ro', ) ax.plot([-1.5, 1.5], [-.5, -.5], color='k', label='target') ax.plot([-0.85], [.2], 'bo') ax.plot([0.85], [.2], 'bo') ring = Circle((0.85, 0.2), game.r, fc='b', ec='b', alpha=0.6, linewidth=2) ax.add_patch(ring) ring = Circle((-0.85, 0.2), game.r, fc='b', ec='b', alpha=0.6, linewidth=2) ax.add_patch(ring) ring = Circle((0.85, 0.2), game.r*1.2, fc='b', ec='b', alpha=0.2) ax.add_patch(ring) ring = Circle((-0.85, 0.2), game.r*1.2, fc='b', ec='b', alpha=0.2) ax.add_patch(ring) # plt.xticks([-2.0, -1.5, -1.0, -0.5, 0, 0.5], [2.0, 1.5, 1.0, 0.5, 0, -0.5]) xrs, yrs = [], [] for t in np.linspace(0, 2*3.1416, 50): xrs.append(.85 + game.r*cos(t)) yrs.append(.2 + game.r*sin(t)) ax.plot(xrs, yrs, 'b') xrs, yrs = [], [] for t in np.linspace(0, 2*3.1416, 50): xrs.append(-.85 + game.r*cos(t)) yrs.append(.2 + game.r*sin(t)) ax.plot(xrs, yrs, 'b') ax.grid() ax.tick_params(axis='both', which='major', labelsize=15) plt.xlabel('y(m)', fontsize=15) plt.ylabel('x(m)', fontsize=15) plt.gca().legend(prop={'size': 14}, ncol=5, handletextpad=0.1, columnspacing=0.4, loc='lower center', numpoints=1, handlelength=1.2) plt.subplots_adjust(bottom=0.13) # print('set legend') ax.set_aspect('equal') ax.set_xlim([-1.2, 1.2]) ax.set_ylim([-.53, .92]) plt.show()
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