def analyze_search_set(cost_by_ep, seq_by_ep, epochs, ds_filename, add_best=False, add_good=False, scen_filename=None, overwrite=False): best_idx = np.argmin(cost_by_ep) best_dur, best_seqn = cost_by_ep.flatten()[best_idx], seq_by_ep.flatten( )[best_idx] print(f'min cost {best_dur:.3f} {best_seqn}') good_range = 1.01 good_idx = cost_by_ep < best_dur * good_range good_costs, good_seqns = cost_by_ep[good_idx], seq_by_ep[good_idx] print( f'found {len(good_costs)} solutions within {(good_range-1)*100:.2f}% of optimal' ) print(f'{good_costs}') if scen_filename is not None: scen = pmu.Scenario(filename=scen_filename) target_by_name = {str(_t.name): _t for _t in scen.targets} def tg_seq_of_names(names): target_names = [f'{_:d}' for _ in [int(_) for _ in names.split('-')]] return [target_by_name[_tn] for _tn in target_names] if add_best: scen.add_solution('best', best_dur, tg_seq_of_names(best_seqn)) if add_good: for _i, (_d, _seqn) in enumerate(zip(good_costs, good_seqns)): scen.add_solution(f'best__{_i}', _d, tg_seq_of_names(_seqn)) if overwrite: scen.save(scen_filename)
def test2(filename='../../data/scenario_60_6.yaml', ntest=100): scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver() s.init(scen.drone, scen.targets) for i in range(ntest): seq = np.random.permutation(scen.targets).tolist() _seq = [_s.name - 1 for _s in seq] #print(_seq) c_dur = s.run_sequence(_seq) c_psis = s.debug() py_drone, py_dur = pm.intercept_sequence_copy(scen.drone, seq) #pdb.set_trace() c_psis1 = [pmu.norm_angles_mpi_pi(_psi) for _psi in c_psis] p_psis1 = [pmu.norm_angles_mpi_pi(_psi) for _psi in py_drone.psis] #passed1 = np.allclose(c_psis1, p_psis1, rtol=1e-02, atol=1e-03) # rtol=1e-05, atol=1e-08 passed1 = np.allclose(c_psis1, p_psis1, rtol=1e-03, atol=1e-08) # rtol=1e-05, atol=1e-08 #passed1 = np.allclose(c_psis, py_drone.psis, rtol=1e-05, atol=1e-06) # rtol=1e-05, atol=1e-08 passed2 = np.allclose(c_dur, py_dur, rtol=1e-05, atol=1e-06) print(f'Test passed: {passed1} {passed2}') if not passed1: #print(f'{c_psis} \n{py_drone.psis}') for i in range(nb_tg): if abs(c_psis[i] - py_drone.psis[i]) > 1e-5: print(f'failed idx {i} {c_psis1[i]} {p_psis1[i]}') print([_s.name for _s in seq]) if not passed2: print(f'durations (c/py): {c_dur}, {py_dur}')
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 test5(filename='../../data/scen_small/scenario_9_6.yaml', nepoch=int(10)): scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver(scen.drone, scen.targets) start_seq = np.random.permutation(scen.targets).tolist() _start_seq = [_s.name - 1 for _s in start_seq] _start = time.perf_counter() best_dur, best_seq = s.search_sa(_start_seq, nepoch, T0=1., display=2) _end = time.perf_counter() dt = _end - _start print(f'search took {dt:.0f}s best dur {best_dur:.3f}')
def test11(filename='../../data/scen_120/1.yaml' ): #'../../data/scenario_7820_2_2.yaml'): scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver(scen.drone, scen.targets) for i in range(1): #seq = np.random.permutation(scen.targets).tolist()#[:nb_tg] seq = scen.targets _seq = [_s.name - 1 for _s in seq] c_dur = s.run_sequence(_seq) c_psis = s.debug() print(f'intercepted {len(c_psis)} targets ({c_dur} s)')
def create_search_set(scen_filename, nb_searches, epochs, cache_filename, method, T0=2.): time_tags = [] time_tags.append(time.perf_counter()) scen = pmu.Scenario(filename=scen_filename) cost_by_ep, seq_by_ep = [], [] for _ep in epochs: print(f'-{_ep:e} epochs') _drones, _seqs, _costs = [], [], [] for i in range(nb_searches): if method == 'sa': _drone, _seq = pm_sa.search(scen.drone, scen.targets, epochs=_ep, display=0) elif method == 'sa2': _drone, _seq = pm_sa.search(scen.drone, scen.targets, epochs=_ep, display=0, T0=T0, use_native=True) elif method == 'sa3': _drone, _seq = pm_nc.search_sa(scen.drone, scen.targets, start_seq=None, nepoch=_ep, display=0, T0=T0) _drones.append(_drone) _seqs.append(_seq) _costs.append(_drone.flight_duration()) _display_run(_ep, i, nb_searches, _costs) cost_by_ep.append(_costs) seq_by_ep.append([pmu.format_seq(_s) for _s in _seqs]) time_tags.append(time.perf_counter()) _display_ep(_ep, _costs, time_tags[-1] - time_tags[-2]) print( f'computed search set in {datetime.timedelta(seconds=time_tags[-1]-time_tags[0])}' ) print(f'saving to {cache_filename}') np.savez(cache_filename, cost_by_ep=cost_by_ep, seq_by_ep=seq_by_ep, epochs=epochs) return np.asarray(cost_by_ep), np.asarray(seq_by_ep), np.asarray(epochs)
def test4(filename='../../data/scenario_9_6.yaml'): scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver() s.init(scen.drone, scen.targets) n_targ = len(scen.targets) n_seq = np.math.factorial(n_targ) print(f'searching in all {n_targ} targets permutations ({n_seq:.2e})') _start = time.perf_counter() best_cost, best_seq = s.run_exhaustive() _end = time.perf_counter() dt = _end - _start ips = n_seq / dt print(f'best cost: {best_cost}') print(best_seq) print(f'search in C took {dt:.3f}s ({ips:.2e} cost_evals/s)')
def test22(filename='../../data/scenario_60_6.yaml'): #seq = [30, 47, 12, 20, 39, 36, 28, 29, 18, 2, 41, 24, 35, 54, 58, 23, 53, 11, 4, 15, 10, 9, 34, 43, 44, 45, 42, 14, 37, 17, 19, 52, 8, 33, 48, 27, 56, 5, 55, 21, 59, 26, 40, 50, 49, 7, 22, 31, 38, 25, 57, 32, 3, 60, 6, 13, 51, 46, 16, 1] seq = [ 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17 ] scen = pmu.Scenario(filename=filename) s = pm_cpp_ext.Solver() s.init(scen.drone, scen.targets) py_seq = [scen.targets[_n - 1] for _n in seq] py_drone, py_dur = pm.intercept_sequence_copy(scen.drone, py_seq) print(f'{py_dur}') c_drone, c_dur = s.intercept_sequence_copy(scen.drone, py_seq) print(f'{c_dur}') pdb.set_trace()
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 sample_solutions(filenames, nb_samples=int(1e5), force_recompute=False, use_native=True, show_2d=True): _scens = [pmu.Scenario(filename=_f) for _f in filenames] _durs = [] for _scen in _scens: cache_filename = f'/tmp/samples__{_scen.name}__{nb_samples}.npz' if force_recompute or not os.path.exists(cache_filename): print(f'sampling and storing to {cache_filename}') solver = pm_cpp_ext.Solver(_scen.drone, _scen.targets) if use_native else pm _durs.append([solver.intercept_sequence_copy(_scen.drone, np.random.permutation(_scen.targets).tolist())[1] for _i in range(nb_samples)]) np.savez(cache_filename, durs=_durs[-1]) else: print(f'loading samples from {cache_filename}') _durs.append(np.load(cache_filename)['durs']) nb_scen = len(filenames) nr, nc = nb_scen, 2 if show_2d else 1 fig = plt.figure(tight_layout=True, figsize=[6.40*nc, 2.56*nr]); fig.canvas.set_window_title("Random Sampling of Solutions") gs = fig.add_gridspec(nr, 3) axes_2d = [fig.add_subplot(gs[_i,:1]) for _i in range(nb_scen)] axes_histo = [fig.add_subplot(gs[_i,1:]) for _i in range(nb_scen)] np.set_printoptions(precision=3) for ax, _d, _scen in zip(axes_histo, _durs, _scens): _dens, _, _ = ax.hist(_d, label=f'random sampling ({nb_samples:.0e} samples)', density=True) for sol_name in ['best', 'hc', 'optimal']: try: _name, _dur, _seq = _scen.solution_by_name(sol_name) #ax.annotate(f'{sol_name}', xy=(_dur, 1))#, xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05),) ax.plot([_dur, _dur],[0, np.max(_dens)*0.8], label=f'{sol_name}: {_dur:.3f}s', alpha=0.6, linewidth=2) except KeyError: pass pmu.decorate(ax, _fmt_mmmm2(_d), xlab='time in s', legend=True) for ax, _scen in zip(axes_2d, _scens): pmu.plot_scenario(_scen, title=f'scenario: {_scen.name}', annotate=False, fig=fig, ax=ax)
def plot_search_chronograms(filename, epoch, nrun, force=False): scen = pmu.Scenario(filename=filename) # set of quasi lin #Ts = [lambda i: pm_sa._f1(2, epoch/10, 1e-2, 9*epoch/10, i)]*5 # set of quasi lin #Ts = [lambda i: pm_sa._f1(5, epoch/10, 1e-2, 9*epoch/10, i), lambda i: pm_sa._f1(2, epoch/10, 1e-2, 9*epoch/10, i), lambda i: pm_sa._f1(1, epoch/10, 1e-2, 9*epoch/10, i)] #Ts = [lambda i: pm_sa._f1(2, epoch/10, 1e-2, 9*epoch/10, i), lambda i: pm_sa._f1(1, epoch/10, 1e-2, 9*epoch/10, i)] # lin vs quasi lin #Ts = [lambda i: pm_sa._aff(2, 1e-2, epoch, i), lambda i: pm_sa._f1(2, epoch/10, 1e-2, 9*epoch/10, i), lambda i: pm_sa._f1(2, 0, 1e-2, 9*epoch/10, i)] #Ts = [lambda i: pm_sa._aff(2, 0.01, epoch, i), lambda i: pm_sa._f1(2, epoch/10, 1e-2, 9*epoch/10, i)] ##Ts = [lambda i: pm_sa._aff(1, 1e-2, epoch, i), lambda i: pm_sa._f1(1, 0, 1e-2, 0.9*epoch, i), lambda i: pm_sa._f1(1, 0, 1e-2, 0.8*epoch, i)] # set of lin #Ts = [(lambda i: pm_sa._f1(T0, 0., 1e-2, 0.8*epoch, i)) for T0 in [5, 2, 1,]] Ts = [lambda i: pm_sa._f1(1, 0., 1e-2, 0.8*epoch, i), lambda i: pm_sa._f1(2, 0., 1e-2, 0.8*epoch, i), lambda i: pm_sa._f1(3, 0., 1e-2, 0.8*epoch, i), lambda i: pm_sa._f1(5, 0., 1e-2, 0.8*epoch, i)] #Ts = [lambda i: pm_sa._aff(5, 1e-2, epoch, i), lambda i: pm_sa._aff(2, 1e-2, epoch, i), lambda i: pm_sa._aff(1, 1e-2, epoch, i)] # step #Ts = [lambda i: pm_sa._aff(1, 1e-2, epoch, i), lambda i: pm_sa._step(1, 1e-2, epoch/2, i)] # 2 aff #Ts = [lambda i: pm_sa._2aff(1, 1e-2, epoch/2, 0.5, 1e-2, epoch, i)] #Ts = [lambda i: pm_sa._aff(1, 1e-2, epoch, i), lambda i: pm_sa._2aff(1, 1e-2, 2*epoch/3, 0.25, 1e-2, epoch, i), lambda i: pm_sa._f1(1, 0, 1e-2, 2*epoch/3, i)] # lin vs exp #Ts = [lambda i: pm_sa._aff(2, 0.01, epoch, i), lambda i: pm_sa._exp(2, epoch/2, i), lambda i: pm_sa._exp(2, epoch/3, i)] # default lin #Ts = [lambda i: pm_sa._aff(1, 1e-2, epoch, i)] cache_filename = f'/tmp/psc_{scen.name}_{epoch}.npz' if force or not os.path.exists(cache_filename): print(f'running optimizations and storing to {cache_filename} ({nrun} runs of {epoch} epochs)') res = [] for _i, Tf in enumerate(Ts): res.append([pm_sa.search(scen.drone, scen.targets, epochs=epoch, debug=True, Tf=Tf, display=0, use_native=True) for _nr in range(nrun)]) _print_summary(_i, [_r[0].flight_duration() for _r in res[-1]]) np.savez(cache_filename, res=res)#, allow_pickle=True) else: print(f'loading optimization results from {cache_filename}') _d = np.load(cache_filename, allow_pickle=True) res = _d['res'] costs_by_runs = [[_l[0].flight_duration() for _l in _r] for _r in res ] fig = plt.figure(constrained_layout=True, figsize=[10.24, 5.12])#, tight_layout=True) fig.canvas.set_window_title(filename) gs = fig.add_gridspec(max(3, len(Ts)), 3) ax1 = fig.add_subplot(gs[0, :-1]) ax2 = fig.add_subplot(gs[1, :-1], sharex=ax1) ax3 = fig.add_subplot(gs[2, :-1], sharex=ax1) axes_chrono = [fig.add_subplot(gs[0,-1])] for _i in range(1, len(Ts)): axes_chrono.append(fig.add_subplot(gs[_i,-1], sharex=axes_chrono[0], sharey=axes_chrono[0])) colors_by_cases = [] _start = 0#int(9./10*epoch) for k, (Tf, _res) in enumerate(zip(Ts, res)): for _nr, (best_drone, best_seq, all_durs, kept_durs, Paccept, max_dur) in enumerate(_res): if _nr==0: _l = ax1.plot(kept_durs[_start:], label=f'{k}', alpha=0.6) colors_by_cases.append(_l[0].get_color()) ax1.plot(max_dur[_start:], color='black', alpha=0.4) ax3.plot([Ts[k](i) for i in range(_start, epoch)], label=f'{k}') else: ax1.plot(kept_durs[_start:], color=colors_by_cases[-1], alpha=0.6) ax1.plot(max_dur[_start:], color='black', alpha=0.4) ax2.plot(Paccept[_start:], color=colors_by_cases[-1], alpha=0.4)#, label=f'{k}') #pdb.set_trace() ylim = (0, 200) pmu.decorate(ax1, 'Cost', ylab='s', legend=True, ylim=ylim) pmu.decorate(ax2, 'Paccept')#, legend=True) pmu.decorate(ax3, 'Temperature', xlab='episode', ylab='s', legend=True) #plt.figure() #fig = plt.gcf(); fig.canvas.set_window_title(filename) #axes = fig.subplots(len(Ts),1, sharex=True, sharey=True, squeeze=False) for _i, (_c, _ax, _co) in enumerate(zip(costs_by_runs, axes_chrono, colors_by_cases)): _ax.hist(_c, alpha=0.6, color=_co) pmu.decorate(_ax, f'Final cost case {_i}', xlab='time in s')
def plot_scenarios(idxs, sol_name): _fs = [pmu.ScenarioFactory.filename(idx) for idx in idxs] filenames = [pmu.ressource('data/' + _f) for _f in _fs] scens = [pmu.Scenario(filename=_f) for _f in filenames] pmu.plot_scenarios(scens, _fs, sol_name) plt.show()