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_ani_pca_lin_VDP(): NUM_STEPS = 70 VDP_LIN_ITER_STEPS = 1 #Number of steps between each recomputation of LinApp Templates. VDP_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' VDP_PCA_TRAJ_STEPS = 2 #Number of steps our sample trajectories should run. VDP_PCA_NUM_TRAJ = 200 #Number of sample trajectories we should use for the PCA routine. VDP_LIN_DELAY = 2 VDP_PCA_DELAY = 5 model = VanDerPol(delta=0.08) unit_model = VanDerPol_UnitBox(delta=0.08) lin_1 = LinStrat(unit_model, iter_steps=VDP_LIN_ITER_STEPS) lin_2 = LinStrat(unit_model, iter_steps=VDP_LIN_ITER_STEPS + VDP_LIN_DELAY) pca_1 = PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS) pca_2 = PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS + VDP_PCA_DELAY) lin_strat = MultiStrategy(lin_1, lin_2, pca_1, pca_2) inputs = ExperimentInput(unit_model, strat=lin_strat, label="VDP Kaa") vdp_pca = Animation(inputs) vdp_pca.execute(NUM_STEPS) vdp_pca.animate(0, 1, lin_1, lin_2) #vdp_pca.animate(0,1, lin_2) #vdp_pca.animate(0,1, pca_2) Timer.generate_stats()
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_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_VDP(): NUM_STEPS = 70 VDP_LIN_ITER_STEPS = 1 #Number of steps between each recomputation of LinApp Templates. VDP_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' VDP_PCA_TRAJ_STEPS = 2 #Number of steps our sample trajectories should run. VDP_PCA_NUM_TRAJ = 200 #Number of sample trajectories we should use for the PCA routine. VDP_LIN_DELAY = 2 VDP_PCA_DELAY = 5 model = VanDerPol(delta=0.08) unit_model = VanDerPol_UnitBox(delta=0.08) lin_strat = MultiStrategy(LinStrat(unit_model, iter_steps=VDP_LIN_ITER_STEPS), \ LinStrat(unit_model, iter_steps=VDP_LIN_ITER_STEPS+VDP_LIN_DELAY), \ PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS), \ PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS+VDP_PCA_DELAY)) inputs = [ ExperimentInput(model, label="VDP Sapo"), ExperimentInput(unit_model, strat=lin_strat, label="VDP Kaa") ] vdp_pca = PhasePlotExperiment(inputs) vdp_pca.execute(NUM_STEPS) vdp_pca.plot_results(0, 1) Timer.generate_stats()
def test_pca_VDP(): NUM_STEPS = 3 model = VanDerPol(delta=0.08) unit_model = VanDerPol_UnitBox(delta=0.08) VDP_PCA_ITER_STEPS = 1 #Number of steps between each recomputation of PCA Templates. 'PCA Strategy Parameters' VDP_PCA_TRAJ_STEPS = 5 #Number of steps our sample trajectories should run. VDP_PCA_NUM_TRAJ = 50 #Number of sample trajectories we should use for the PCA routine. VDP_PCA_DELAY = 5 pca_dirs = GeneratedPCADirs(model, VDP_PCA_NUM_TRAJ, NUM_STEPS) pca_strat = MultiStrategy(PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS, pca_dirs=pca_dirs), \ PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS+VDP_PCA_DELAY, pca_dirs=pca_dirs), \ PCAStrat(unit_model, traj_steps=VDP_PCA_TRAJ_STEPS, num_trajs=VDP_PCA_NUM_TRAJ, iter_steps=VDP_PCA_ITER_STEPS+2*VDP_PCA_DELAY, pca_dirs=pca_dirs)) inputs = [ ExperimentInput(model, label="VDP Sapo"), ExperimentInput(unit_model, strat=pca_strat, label="VDP Kaa PCA") ] vdp_pca = PhasePlotExperiment(inputs) vdp_pca.execute(NUM_STEPS) vdp_pca.plot_results(0, 1) Timer.generate_stats()
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 plot2DPhase(self, x, y, separate=False, lims=None): assert len( self.flowpipes ) != 0, "Plot Object must have at least one flowpipe to plot for 2DPhase." Timer.start('Phase') dim = self.model.dim figure = plt.figure(figsize=PlotSettings.fig_size) phase_ax = figure.add_subplot(1, 2, 1) vol_ax = figure.add_subplot(1, 2, 2) for flow_idx, (flow_label, flowpipe) in enumerate(self.flowpipes): self.__halfspace_inter_plot(flowpipe, flow_idx, flow_label, x, y, phase_ax, separate) #self.__support_plot(flowpipe, flow_idx, flow_label, x, y, ax) self.__plot_trajs(x, y, phase_ax) self.__phase_plot_legend(x, y, phase_ax, lims) 'Add volume data' self.__plot_volume(vol_ax) figure_name = "Kaa{}Phase{}.png".format(flowpipe.model.name, self.__create_var_str([x, y])) self.__plot_figure(figure, figure_name) phase_time = Timer.stop('Phase') print("Plotting phase for dimensions {}, {} done -- Time Spent: {}". format(x_var, y_var, phase_time))
def plot2DPhase(self, x, y): Timer.start('Phase') x_var, y_var = self.vars[x], self.vars[y] 'Define the following projected normal vectors.' norm_vecs = np.zeros([6, self.dim_sys]) norm_vecs[0][x] = 1 norm_vecs[1][y] = 1 norm_vecs[2][x] = -1 norm_vecs[3][y] = -1 norm_vecs[4][x] = 1 norm_vecs[4][y] = 1 norm_vecs[5][x] = -1 norm_vecs[5][y] = -1 fig, ax = plt.subplots(1) comple_dim = [i for i in range(self.dim_sys) if i not in [x, y]] 'Initialize objective function for Chebyshev intersection LP routine.' c = [0 for _ in range(self.dim_sys + 1)] c[-1] = 1 for bund in self.flowpipe: bund_A, bund_b = bund.getIntersect() 'Compute the normal vector offsets' bund_off = np.empty([len(norm_vecs), 1]) for i in range(len(norm_vecs)): bund_off[i] = minLinProg(np.negative(norm_vecs[i]), bund_A, bund_b).fun 'Remove irrelevant dimensions. Mostly doing this to make HalfspaceIntersection happy.' phase_intersect = np.hstack((norm_vecs, bund_off)) phase_intersect = np.delete(phase_intersect, comple_dim, axis=1) 'Compute Chebyshev center of intersection.' row_norm = np.reshape(np.linalg.norm(norm_vecs, axis=1), (norm_vecs.shape[0], 1)) center_A = np.hstack((norm_vecs, row_norm)) neg_bund_off = np.negative(bund_off) center_pt = maxLinProg(c, center_A, list(neg_bund_off.flat)).x center_pt = np.asarray( [b for b_i, b in enumerate(center_pt) if b_i in [x, y]]) 'Run scipy.spatial.HalfspaceIntersection.' hs = HalfspaceIntersection(phase_intersect, center_pt) inter_x, inter_y = zip(*hs.intersections) ax.set_xlabel('{}'.format(x_var)) ax.set_ylabel('{}'.format(y_var)) ax.fill(inter_x, inter_y, 'b') fig.show() phase_time = Timer.stop('Phase') print("Plotting phase for dimensions {}, {} done -- Time Spent: {}". format(x_var, y_var, phase_time))
def test_LL(): model = LL() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(150) FlowPipePlotter(mod_flow).plot2DProj(3) Timer.generate_stats()
def test_Quad(): model = Quadcopter() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(10) FlowPipePlotter(mod_flow).plot2DProj(2) FlowPipePlotter(mod_flow).plot2DProj(5) FlowPipePlotter(mod_flow).plot2DProj(13) Timer.generate_stats()
def test_VDP(): NUM_STEPS = 1 model = VanDerPol(delta=0.08) vdp_sapo = PhasePlotExperiment([ExperimentInput(model, label="VDP Sapo")]) vdp_sapo.execute(NUM_STEPS) vdp_sapo.plot_results(0, 1) 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_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_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(): 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_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_SIR(): model = SIR() mod_reach = ReachSet(model) mod_flow = mod_reach.computeReachSet(200) FlowPipePlotter(mod_flow).plot2DProj(0) FlowPipePlotter(mod_flow).plot2DProj(1) FlowPipePlotter(mod_flow).plot2DProj(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 getGeneratorRep(self): Timer.start('Generator Procedure') base_vertex = self._computeBaseVertex() gen_list = self._computeGenerators(base_vertex) 'Create list representing the linear transformation q + \sum_{j} a_j* g_j' expr_list = base_vertex for var_ind, var in enumerate(self.vars): for i in range(self.dim): expr_list[i] += gen_list[var_ind][i] * var Timer.stop('Generator Procedure') return expr_list
def __find_bounds(self, dir_vec, ptope, bund): 'Find the generator of the parallelotope.' genFun = ptope.getGeneratorRep() 'Create subsitutions tuples.' var_sub = [] for var_ind, var in enumerate(self.vars): var_sub.append((var, genFun[var_ind])) #print(f"Variable Sub for {dir_vec}: {var_sub}") Timer.start('Functional Composition') fog = [func.subs(var_sub, simultaneous=True) for func in self.f] Timer.stop('Functional Composition') 'Perform functional composition with exact transformation from unitbox to parallelotope.' bound_polyu = 0 for coeff_idx, coeff in enumerate(dir_vec): bound_polyu += coeff * fog[coeff_idx] 'Calculate min/max Bernstein coefficients.' Timer.start('Bound Computation') ub, lb = OptProd(bound_polyu, bund).getBounds() Timer.stop('Bound Computation') return ub, -1 * lb
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 computeReachSet(self, time_steps): initial_set = self.model.bund transformer = BundleTransformer(self.model.f) flowpipe = [initial_set] for ind in range(time_steps): Timer.start('Reachable Set Computation') trans_bund = transformer.transform(flowpipe[ind]) reach_time = Timer.stop('Reachable Set Computation') print("Computed Step {0} -- Time Elapsed: {1} sec".format( ind, reach_time)) flowpipe.append(trans_bund) return FlowPipe(flowpipe, self.model.vars)
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_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_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 plot2DProj(self, *vars_tup): num_var = len(vars_tup) pipe_len = len(self.flowpipe) fig, ax = plt.subplots(1, num_var) ax = [ax] if num_var == 1 else ax 'Time step vector for plotting' t = np.arange(0, pipe_len, 1) for ax_ind, var_ind in enumerate(vars_tup): Timer.start('Proj') curr_var = self.vars[var_ind] 'Vector of minimum and maximum points of the polytope represented by parallelotope bundle.' y_min, y_max = np.empty(pipe_len), np.empty(pipe_len) 'Initialize objective function' y_obj = [0 for _ in self.vars] y_obj[var_ind] = 1 'Calculate the minimum and maximum points through LPs for every iteration of the bundle.' for bund_ind, bund in enumerate(self.flowpipe): bund_A, bund_b = bund.getIntersect() y_min[bund_ind] = minLinProg(y_obj, bund_A, bund_b).fun y_max[bund_ind] = maxLinProg(y_obj, bund_A, bund_b).fun ax[ax_ind].fill_between(t, y_min, y_max) ax[ax_ind].set_xlabel("t: time steps") ax[ax_ind].set_ylabel("Reachable Set for {}".format(curr_var)) plot_time = Timer.stop('Proj') print( "Plotting projection for dimension {} done -- Time Spent: {}". format(curr_var, plot_time)) fig.show()