def __init__(self, *inputs, label=""): self.inputs = inputs self.plot = Plot() self.max_num_steps = 0 'Assuming that all models use the same dynamics and same initial set for now' self.model = inputs[0]['model'] self.label = label
def test_lin_HarOsc(): NUM_STEPS = 5 model = HarOsc() #trajs = generate_traj(model, 10, 200) mod_reach = ReachSet(model) #mod_flow = mod_reach.computeReachSet() sir_plot = Plot() #mod_flow = mod_reach.computeReachSet(NUM_STEPS) SIR_LIN_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. lin_strat = LinStrat(model, iter_steps=SIR_LIN_ITER_STEPS) mod_lin_flow = mod_reach.computeReachSet(NUM_STEPS, tempstrat=lin_strat, transmode=BundleMode.AFO) trajs = [Traj(model, point, steps=NUM_STEPS) for point in product([-5,-4],[0,1])] 'Generaste the trajectories and add them to the plot.' sir_plot.add(mod_lin_flow, "HarOsc LINAPP") for t in trajs: sir_plot.add(t) sir_plot.plot2DPhase(0,1, separate=True, plotvertices=True) Timer.generate_stats()
def test_pca_lin_Phos(): NUM_STEPS = 30 PHOS_LIN_ITER_STEPS = 1 #Number of steps between each recomputation of LinApp Templates. PHOS_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' PHOS_PCA_TRAJ_STEPS = 5 #Number of steps our sample trajectories should run. PHOS_PCA_NUM_TRAJ = 200 #Number of sample trajectories we should use for the PCA routine. PHOS_LIFE_SPAN = 3 unit_model = Phosphorelay_UnitBox() unit_mod_reach = ReachSet(unit_model) #points = [[1,1,1], [1.005, 1,1], [1.01,1.01,1.01], [1.005,1.01,1.01], [1,1.005,1], [1.01,1,1.05]] #trajs = [Traj(unit_model , point, NUM_STEPS) for point in points] multi_strat = MultiStrategy(LinStrat(unit_model, iter_steps=PHOS_LIN_ITER_STEPS), \ DelayedPCAStrat(unit_model, traj_steps=PHOS_PCA_TRAJ_STEPS, num_trajs=PHOS_PCA_NUM_TRAJ, life_span=PHOS_LIFE_SPAN)) #PCAStrat(unit_model, traj_steps=PHOS_PCA_TRAJ_STEPS, num_trajs=PHOS_PCA_NUM_TRAJ, iter_steps=PHOS_PCA_ITER_STEPS+PHOS_PCA_DELAY)) #PCAStrat(unit_model, traj_steps=PHOS_PCA_TRAJ_STEPS, num_trajs=PHOS_PCA_NUM_TRAJ, iter_steps=PHOS_PCA_ITER_STEPS+2*PHOS_PCA_DELAY)) mod_lin_flow = unit_mod_reach.computeReachSet(NUM_STEPS, tempstrat=multi_strat) # points = [[0,1.97], [0.01, 1.97], [0.01,2], [0,2], [0.005,1.97], [0.005,2], [0,1.97], [0,1.985], [0.01,1.985]] #trajs = [Traj(unit_model, point, NUM_STEPS) for point in points] phos_plot = Plot() phos_plot.add(mod_lin_flow) 'Add trajectories' for traj in trajs: phos_plot.add(traj) phos_plot.plot2DPhase(0, 1, separate=False, plotvertices=True)
def test_sir_pca_strat(): #Compute Sapo's version.3 sir_pca = SIR_UnitBox(delta=0.5) sir = SIR(delta=0.5) sir_reach = ReachSet(sir) sir_flow = sir_reach.computeReachSet(NUM_STEPS) sir_plot = Plot() sir_plot.add(sir_flow, "SIR SAPO") for i in range(ITER_SPREAD, ITER_SPREAD + 1): print( colored("Generating PCA with Iterative Step Size: {}".format(i), "white", attrs=['reverse', 'blink'])) sir_pca_reach = ReachSet(sir_pca) sir_flow_pca = sir_pca_reach.computeReachSet( NUM_STEPS, tempstrat=PCAStrat(sir_pca, iter_steps=i), transmode=BundleMode.AFO) sir_plot.add(sir_flow_pca, "SIR_PCA_{}".format(i)) sir_plot.plot2DPhase(0, 1, separate=False, plotvertices=True) #sir_plot.plot(0,1,2) Timer.generate_stats() """
def test_sir_lin_pca_strat(): NUM_STEPS = 70 SIR_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' SIR_PCA_TRAJ_STEPS = 1 #Number of steps our sample trajectories should run. SIR_PCA_NUM_TRAJ = 100 #Number of sample trajectories we should use for the PCA routine. SIR_LIN_ITER_STEPS = 1 # SIR_PCA_LIFE_SPAN = 3 sir_pca = SIR_UnitBox(delta=0.5) sir_plot = Plot() points = [[0.79,0.19,0], [0.79, 0.2,0], [0.8,0.19,0], [0.8,0.2,0], [0.79,0.195,0], [0.8,0.195,0], [0.795,0.19,0], [0.795,0.2,0]] trajs = [Traj(sir_pca, point, NUM_STEPS) for point in points] pca_strat = MultiStrategy(LinStrat(sir_pca, iter_steps=SIR_LIN_ITER_STEPS), \ DelayedPCAStrat(sir_pca, traj_steps=SIR_PCA_TRAJ_STEPS, num_trajs=SIR_PCA_NUM_TRAJ, life_span=SIR_PCA_LIFE_SPAN)) sir_pca_reach = ReachSet(sir_pca) sir_flow_pca = sir_pca_reach.computeReachSet(NUM_STEPS, tempstrat=pca_strat) sir_plot.add(sir_flow_pca, "SIR_LinApp&PCA") 'Add trajectories' for traj in trajs: sir_plot.add(traj) # sir_plot.plot2DPhase(0,1,separate=False, plotvertices=True) sir_plot.plot2DPhase(1,2,separate=False, plotvertices=True) sir_plot.plot2DPhase(0,2,separate=False, plotvertices=True) Timer.generate_stats()
def test_pca_lin_Rossler(): NUM_STEPS = 5 ROSS_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' ROSS_PCA_TRAJ_STEPS = 1 #Number of steps our sample trajectories should run. ROSS_PCA_NUM_TRAJ = 200 #Number of sample trajectories we should use for the PCA routine. rossler_pca = Rossler_UnitBox(delta=0.5) rossler_plot = Plot() points = [[0.05,4.95,0.05], [0.1,4.95,0.05], [0.05,5,0.05], [0.1,5,0.05], [0.05,4.95,0.05], [0.05,4.95,0.1], [0.1,4.95,0.1], [0.1,5,0.1]] trajs = [Traj(rossler_pca, point, NUM_STEPS) for point in points] pca_strat = PCALinStrat(rossler_pca, traj_steps=ROSS_PCA_TRAJ_STEPS, num_trajs=ROSS_PCA_NUM_TRAJ, iter_steps=ROSS_PCA_ITER_STEPS) ross_pca_reach = ReachSet(rossler_pca) ross_flow_pca = ross_pca_reach.computeReachSet(NUM_STEPS, tempstrat=pca_strat) rossler_plot.add(ross_flow_pca, "SIR_LinApp&PCA") 'Add trajectories' for traj in trajs: rossler_plot.add(traj) rossler_plot.plot2DPhase(0,1,separate=True, plotvertices=True) Timer.generate_stats()
def test_sir_cut_strat(): #Compute Sapo's version. sir_cut = SIR() sir = SIR_UnitBox() sir_reach = ReachSet(sir) sir_flow = sir_reach.computeReachSet(NUM_STEPS) sir_plot = Plot() sir_plot.add(sir_flow) sir_cut_reach = ReachSet(sir_cut) sir_cut_flow = sir_cut_reach.computeReachSet(NUM_STEPS, CutStrat(sir_cut)) sir_sapo_flow = sir_cut_reach.computeReachSet(NUM_STEPS) sir_plot.add(sir_cut_flow, "SIR_CUT_150") sir_plot.add(sir_sapo_flow, "SIR_SAPO") sir_plot.plot(0, 1, 2) Timer.generate_stats()
def test_pca_Quad(): NUM_STEPS = 5 QUAD_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' QUAD_PCA_TRAJ_STEPS = 1 #Number of steps our sample trajectories should run. QUAD_PCA_NUM_TRAJ = 200 #Number of sample trajectories we should use for the PCA routine. quad_pca = Quadcopter_UnitBox() quad_plot = Plot() #trajs = [Traj(rossler_pca, point, NUM_STEPS) for point in points] pca_strat = PCAStrat(quad_pca, traj_steps=QUAD_PCA_TRAJ_STEPS, num_trajs=QUAD_PCA_NUM_TRAJ, iter_steps=QUAD_PCA_ITER_STEPS) quad_pca_reach = ReachSet(quad_pca) quad_flow_pca = quad_pca_reach.computeReachSet(NUM_STEPS, tempstrat=pca_strat) 'Add trajectories' #for traj in trajs: # rossler_plot.add(traj) quad_plot.add(quad_flow_pca, "Quad_PCA") quad_plot.plot2DPhase(2, 5, separate=True, plotvertices=True)
def test_sir_lin_strat(): #Compute Sapo's version. sir_lin = SIR_UnitBox(delta=0.5) sir = SIR() #sir_reach = ReachSet(sir) #sir_flow = sir_reach.computeReachSet(NUM_STEPS) sir_plot = Plot() #sir_plot.add(sir_flow) for i in range(10, 11): print( colored( "Generating Lin_Approx with Iterative Step Size: {}".format(i), "white", attrs=['reverse', 'blink'])) sir_lin_reach = ReachSet(sir_lin) sir_flow_lin = sir_lin_reach.computeReachSet( NUM_STEPS, LinStrat(sir_lin, iter_steps=i)) sir_plot.add(sir_flow_lin, "SIR_LIN_{}".format(i)) #sir_plot.plot(0,1,2) sir_plot.plot2DPhase(0, 1, separate=True) Timer.generate_stats() """
def test_pca_HarOsc(): NUM_STEPS = 4 model = HarOsc() #trajs = generate_traj(model, 10, 200) mod_reach = ReachSet(model) #mod_flow = mod_reach.computeReachSet() sir_plot = Plot() SIR_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' SIR_PCA_TRAJ_STEPS = 2 #Number of steps our sample trajectories should run. SIR_PCA_NUM_TRAJ = 100 #Number of sample trajectories we should use for the PCA routine. pca_strat = PCAStrat(model, traj_steps=SIR_PCA_TRAJ_STEPS, num_trajs=SIR_PCA_NUM_TRAJ, iter_steps=SIR_PCA_ITER_STEPS) mod_pca_flow = mod_reach.computeReachSet(NUM_STEPS, tempstrat=[pca_strat], transmode=BundleMode.AFO) #trajs = generate_traj(model, 10, 200) 'Generaste the trajectories and add them to the plot.' sir_plot.add(mod_pca_flow, "HarOsc PCA") sir_plot.plot2DPhase(0,1, separate=True, plotvertices=True) Timer.generate_stats()
def test_all_pca(): num_steps = 100 model_var_dict = { Basic: [0], Rossler_PCA: [0, 1, 2], SIR_PCA: [0, 1, 2], LotkaVolterra_PCA: [0, 1, 2], Quadcopter_PCA: [2, 5, 13], Phosphorelay_PCA: [0, 1] } model_path_dict = { Basic: "/Users/edwardkim/Work/kaa-optimize/figures/Basic", Rossler_PCA: "/Users/edwardkim/Work/kaa-optimize/figures/Rossler", SIR_PCA: "/Users/edwardkim/Work/kaa-optimize/figures/SIR", LotkaVolterra_PCA: "/Users/edwardkim/Work/kaa-optimize/figures/LotVolt", Quadcopter_PCA: "/Users/edwardkim/Work/kaa-optimize/figures/Quad", Phosphorelay_PCA: "/Users/edwardkim/Work/kaa-optimize/figures/Phos" } for model, var_ind_list in model_var_dict.items(): print("\n Generating Model: {}\n".format(model)) flowpipe = ReachSet(model()).computeReachSet(num_steps, PCAStrat) plot = Plot() plot.add(flowpipe) plot.plot(*var_ind_list, path=model_path_dict[model])
def test_phase_sir(): sir_mod = SIR_UnitBox(delta=0.5) sir_reach = ReachSet(sir_mod) flowpipe = sir_reach.computeReachSet(50) plot = Plot() plot.add(flowpipe) plot.plot2DPhase(1, 2)
def test_basic(): basic_mod = Basic() basic_reach = ReachSet(basic_mod) flowpipe = basic_reach.computeReachSet(300) basic_plot = Plot() basic_plot.add_flowpipe(flowpipe) basic_plot.plot(0)
def pca_lin_comp(): sir = SIR_UnitBox() sir_reach = ReachSet(sir) sir_flow = sir_reach.computeReachSet(NUM_STEPS) sir_plot = Plot() sir_plot.add(sir_flow) sir_lin_flow = sir_reach.computeReachSet(NUM_STEPS, LinStrat(sir, iter_steps=150)) #sir_pca_flow = sir_reach.computeReachSet(NUM_STEPS, PCAStrat(sir, iter_steps=50)) sir_plot.add(sir_lin_flow, "SIR_LIN") #sir_plot.add(sir_pca_flow, "SIR_PCA") sir_plot.plot(0, 1, 2) Timer.generate_stats()
def test_LL(): model = LL() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(150) ll_plot = Plot() ll_plot.add(mod_flow) ll_plot.plot(0) Timer.generate_stats()
def test_rossler_phase(): model = Rossler() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(200) rossler_plot = Plot() rossler_plot.add(mod_flow) rossler_plot.plot2DPhase(0,1) Timer.generate_stats()
def test_Quad(): model = Quadcopter() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(3) quad_plot = Plot() quad_plot.add(mod_flow) quad_plot.plot(2, 5, 13) Timer.generate_stats()
def test_Rossler(): model = Rossler() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(300) rossler_plot = Plot() rossler_plot.add(mod_flow) rossler_plot.plot(0,1,2) Timer.generate_stats()
def test_basic2(): basic_mod = Basic2() basic_reach = ReachSet(basic_mod) flowpipe = basic_reach.computeReachSet(300) basic_plot = Plot() basic_plot.add(flowpipe) basic_plot.plot(0) Timer.generate_stats()
def test_LV(): model = LotkaVolterra() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(100) plot = Plot() plot.add(mod_flow) plot.plot(0,1,2) Timer.generate_stats()
def test_Phos(): model = Phosphorelay() #unit_model = Phosphorelay_UnitBox() mod_reach = ReachSet(model) #mod_unit_reach = ReachSet(unit_model) #unit_flow = mod_unit_reach.computeReachSet(200) mod_flow = mod_reach.computeReachSet(30) phos_plot = Plot() phos_plot.add(mod_flow) phos_plot.plot2DPhase(0, 1, separate=False, plotvertices=True) Timer.generate_stats()
def test_OscPart(): model = OscPart() #trajs = generate_traj(model, 10, 200) mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(20) sir_plot = Plot() #trajs = generate_traj(model, 10, 200) 'Generaste the trajectories and add them to the plot.' sir_plot.add(mod_flow) sir_plot.plot(0, 1, 2) Timer.generate_stats()
def test_rossler_lin_strat(): #Compute Sapo's version. rossler_lin = Rossler_UnitBox() rossler = Rossler() rossler_reach = ReachSet(rossler) rossler_flow = rossler_reach.computeReachSet(NUM_STEPS) rossler_plot = Plot() rossler_plot.add(rossler_flow) for i in range(10, 11): print( colored("Generating LIN with Iterative Step Size: {}".format(i), "white", attrs=['reverse', 'blink'])) rossler_lin_reach = ReachSet(rossler_lin) rossler_flow_lin = rossler_lin_reach.computeReachSet( NUM_STEPS, LinStrat(rossler_lin, iter_steps=i)) rossler_plot.add(rossler_flow_lin, "Rossler_LIN_{}".format(i)) rossler_plot.plot(0, 1, 2) Timer.generate_stats()
def test_lv_pca_strat(): #Compute Sapo's version. lv_pca = LotkaVolterra_UnitBox() lv = LotkaVolterra() lv_reach = ReachSet(lv) lv_flow = lv_reach.computeReachSet(NUM_STEPS) lv_plot = Plot() lv_plot.add(lv_flow) for i in range(1, ITER_SPREAD): print( colored("Generating PCA with Iterative Step Size: {}".format(2 * i), "white", attrs=['reverse', 'blink'])) lv_pca_reach = ReachSet(lv_pca) lv_flow_pca = lv_pca_reach.computeReachSet( NUM_STEPS, PCAStrat(lv_pca, iter_steps=2 * i)) lv_plot.add(lv_flow_pca, "LV_PCA_{}".format(2 * i)) lv_plot.plot(0, 1, 2) Timer.generate_stats()
def test_quad_pca_strat(): #Compute Sapo's version. quad_pca = Quadcopter_UnitBox() quad = Quadcopter() quad_reach = ReachSet(quad) quad_flow = quad_reach.computeReachSet(NUM_STEPS) quad_plot = Plot() quad_plot.add(quad_flow) for i in range(1, ITER_SPREAD): print( colored("Generating PCA with Iterative Step Size: {}".format(2 * i), "white", attrs=['reverse', 'blink'])) quad_pca_reach = ReachSet(quad_pca) quad_flow_pca = quad_pca_reach.computeReachSet( NUM_STEPS, PCAStrat(quad_pca, iter_steps=2 * i)) quad_plot.add(quad_flow_pca, "QUAD_PCA_{}".format(2 * i)) quad_plot.plot(2, 5, 13) Timer.generate_stats()
def test_SIR(): model = SIR(delta=0.5) model_unit = SIR_UnitBox() #trajs = generate_traj(model, 10, 200) mod_reach = ReachSet(model) mod_unit_reach = ReachSet(model_unit) mod_flow = mod_reach.computeReachSet(70) #mod_unit_flow = mod_unit_reach.computeReachSet(300) sir_plot = Plot() #trajs = generate_traj(model, 10, 200) 'Generaste the trajectories and add them to the plot.' #for traj in trajs: # sir_plot.add(traj) sir_plot.add(mod_flow) #sir_plot.add(mod_unit_flow) sir_plot.plot2DPhase(0,1) Timer.generate_stats()
def test_rossler_pca_strat(): #Compute Sapo's version. rossler_pca = Rossler_UnitBox() rossler = Rossler() rossler_reach = ReachSet(rossler) #rossler_flow = rossler_reach.computeReachSet(NUM_STEPS) rossler_plot = Plot() #rossler_plot.add(rossler_flow) for i in range(3, 4): print( colored("Generating PCA with Iterative Step Size: {}".format(i), "white", attrs=['reverse', 'blink'])) rossler_pca_reach = ReachSet(rossler_pca) rossler_flow_pca = rossler_pca_reach.computeReachSet( NUM_STEPS, PCAStrat(rossler_pca, iter_steps=i)) rossler_plot.add(rossler_flow_pca, "Rossler_PCA_{}".format(i)) rossler_plot.plot(0, 1, 2) Timer.generate_stats()
def test_haroscrotate(): NUM_STEPS = 4 model = HarOscRotate() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(NUM_STEPS) SIR_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' SIR_PCA_TRAJ_STEPS = 1 #Number of steps our sample trajectories should run. SIR_PCA_NUM_TRAJ = 100 #Number of sample trajectories we should use for the PCA routine. #pca_strat = PCAStrat(model, traj_steps=SIR_PCA_TRAJ_STEPS, num_trajs=SIR_PCA_NUM_TRAJ, iter_steps=SIR_PCA_ITER_STEPS) #mod_pca_flow = mod_reach.computeReachSet(NUM_STEPS, tempstrat=pca_strat) vdp_plot = Plot() vdp_plot.add(mod_flow, "HarOsc") #vdp_plot.add(mod_pca_flow, "HarOsc PCA") vdp_plot.plot2DPhase(0, 1) Timer.generate_stats()
class Experiment: def __init__(self, *inputs, label=""): self.inputs = inputs self.plot = Plot() self.max_num_steps = 0 'Assuming that all models use the same dynamics and same initial set for now' self.model = inputs[0]['model'] self.label = label """ Execute the reachable set simulations and add the flowpipes to the Plot. """ def execute(self): self.output_flowpipes = [] for experi_input in self.inputs: model = experi_input['model'] strat = experi_input['strat'] label = experi_input['label'] num_steps = experi_input['num_steps'] mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(num_steps, tempstrat=strat) self.plot.add(mod_flow, label=label) self.output_flowpipes.append(mod_flow) self.max_num_steps = max(self.max_num_steps, num_steps) """ Plot the results fed into the Plot object """ def plot_results(self, *var_tup, plottrajs=True): border_sim_trajs = self.__simulate_border_points(self.max_num_steps) if plottrajs: self.plot.add(border_sim_trajs) self.plot.plot(*var_tup) def get_total_vol_results(self): assert self.output_flowpipes is not None, "Execute Experiment with ExperimentInputs before retrieving volume data." return [flowpipe.total_volume for flowpipe in self.output_flowpipes] """ Extract the initial box intervals from the model """ def __get_init_box(self): init_offu = self.model.bund.offu[:self.model. dim] #Assume first dim # of offsets are associated to initial box init_offl = self.model.bund.offl[:self.model.dim] return [[-lower_off, upper_off] for lower_off, upper_off in zip(init_offl, init_offu)] """ Sample points from the edges of the box and propagate them for a number of steps. """ def __simulate_border_points(self, num_steps): init_box_inter = self.__get_init_box() border_points = get_init_box_borders(init_box_inter) trajs = [Traj(self.model, point, num_steps) for point in border_points] return TrajCollection(trajs)