def test_1(filename, rnd=False): # first solution for a given scenario drone, targets = pmu.load_scenario(filename) if rnd: _p = rng.permutation(len(targets)) targets = np.array(targets)[_p] pm.intercept_sequence(drone, targets) plot(plt.gca(), drone, targets)
def test1(filename='../../data/scenario_60_6.yaml'): scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver() s.init(scen.drone, scen.targets) seq = [scen.targets[17]] #nb_t = len(scen.targets) #seq = np.random.permutation(scen.targets).tolist() _seq = [_s.name for _s in seq] print(_seq) s.run_sequence(_seq) c_psis = s.debug() #print(psis) # if 0: # psi, dt = pm.intercept_1(scen.drone, scen.targets[0]) # print(f'intercept seq py {psi}, {dt}') # scen.drone.add_leg(dt, psi) # psi, dt = pm.intercept_1(scen.drone, scen.targets[1]) # print(f'intercept seq py {psi}, {dt}') # scen.drone.add_leg(dt, psi) # else: pm.intercept_sequence(scen.drone, scen.targets[:nb_t]) #print(f'dur: {scen.drone.flight_duration()}') #print(f'{scen.drone.psis}') #passed = np.allclose(psis, scen.drone.psis, rtol=1e-05, atol=1e-08) passed = np.allclose(c_psis, scen.drone.psis, rtol=1e-05, atol=1e-06) if not passed: print(f'{s.debug()} \n{scen.drone.psis}') #pdb.set_trace() print(f'Test passed: {passed}')
def plot_all_sols(drone, targets, _nc=3): perms = set(permutations(targets)) fig = plt.figure(tight_layout=True, figsize=[12.8, 9.6]) fig.canvas.set_window_title('Interceptions') _nc = min(_nc, len(perms)) _nr, _r = np.divmod(len(perms), _nc) if _r > 0: _nr += 1 axes = fig.subplots(_nr, _nc, sharex=True) #, sharey=True) print(f'{len(perms)} permutation') for targets, ax in zip(perms, axes.flatten()): pm.intercept_sequence(drone, targets) plot(ax, drone, targets) drone.clear_traj()
def main(): drone, targets = pm.load_scenario('./scenario_6.yaml') pm.intercept_sequence(drone, targets) #[::-1]) print( f'duration: {drone.ts[-1]:.2f}s heading: {np.rad2deg(drone.psis[-1]):.1f} deg' ) anim = animate(plt.gcf(), plt.gca(), drone, targets, t0=0., t1=drone.ts[-1], dt=0.1, xlim=(-100, 200), ylim=(-150, 150)) #save_animation(anim, '/tmp/anim.mp4', dt=0.1) # ffmpeg -i /tmp/anim.mp4 -r 15 ../docs/images/animation_1.gif plt.show()
def test_6(): # plot exhaustive search time vs number of targets ntargs, dts = [2, 3, 4, 5, 6, 7, 8], [] for ntarg in ntargs: drone, targets = pm.make_scenario(ntarg=ntarg, dp0=(0, 0), dv=15) perms = set(permutations(targets)) _start = time.perf_counter() for targets in perms: pm.intercept_sequence(deepcopy(drone), targets) _end = time.perf_counter() dts.append(_end - _start) print(f'{ntarg} targets -> {dts[-1]:.1f} s') plt.plot(ntargs, dts, '--o') _decorate(plt.gca(), _l=[''], _t='Search time vs number of targets', _yl='time in s', _xl='number of targets') plt.savefig('ex_search_time_vs_size.png')
def test3(filename='../../data/scen_120/9.yaml', ntest=1000): # scen_7820/9.yaml scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver(scen.drone, scen.targets) seq = [_s.name - 1 for _s in scen.targets] #pdb.set_trace() _start1 = time.perf_counter() for i in range(ntest): s.run_sequence(seq) _end1 = time.perf_counter() _dt1 = _end1 - _start1 print(f'{ntest} evaluations in C took {_dt1:.3f} s') _start2 = time.perf_counter() for i in range(ntest): pm.intercept_sequence(scen.drone, scen.targets) scen.drone.clear_traj() _end2 = time.perf_counter() _dt2 = _end2 - _start2 print(f'{ntest} evaluations in Python took {_dt2:.3f} s') print(f'improvement {_dt2/_dt1:.1f}')
def test_5(filename): # compute all solutions, keeps worst and best #drone, targets = pmu.load_scenario(filename) drone, targets = pmu.make_random_scenario(ntarg=8, dp0=(0, 0), dv=15) perms = set(permutations(targets)) durations = [] best_dur, best_targets, best_drone = float('inf'), None, None worst_dur, worst_targets, worst_drone = 0., None, None for targets in perms: _drone = deepcopy(drone) dur = pm.intercept_sequence(_drone, targets) durations.append(dur) if dur < best_dur: best_dur, best_targets, best_drone = dur, targets, _drone if dur > worst_dur: worst_dur, worst_targets, worst_drone = dur, targets, _drone #best_id = np.argmin(durations) #print(best_id, durations[best_id]) plot(plt.gca(), best_drone, best_targets) plt.figure() plot(plt.gca(), worst_drone, worst_targets) #plt.hist(durations) plt.show()
def intercept_sequences_copy(drones, sequences): _drones = [copy.deepcopy(_d) for _d in drones] _durs = [pm.intercept_sequence(_d,_s) for _d,_s in zip(_drones, sequences)] return _drones, _durs