|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from pybdr.dynamic_system import LinearSystemSimple |
| 5 | +from pybdr.geometry import Interval, Geometry, Zonotope |
| 6 | +from pybdr.geometry.operation import enclose |
| 7 | +from dataclasses import dataclass |
| 8 | +from scipy.special import factorial |
| 9 | +from concurrent.futures import ProcessPoolExecutor, as_completed |
| 10 | +from functools import partial |
| 11 | + |
| 12 | +""" |
| 13 | +Reachable Sets of Linear Time-invariant Systems without inputs |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +class ReachLinearZonoAlgo1Parallel: |
| 18 | + @dataclass |
| 19 | + class Settings: |
| 20 | + t_end: float = 0 |
| 21 | + step: float = 0 |
| 22 | + eta: int = 4 # number of taylor terms in approximating using taylor series |
| 23 | + x0: Zonotope = None |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + self._num_steps = 0 |
| 27 | + |
| 28 | + def validation(self): |
| 29 | + assert self.t_end >= self.step >= 0 |
| 30 | + assert self.eta >= 3 # at least 3 considering accuracy |
| 31 | + self._num_steps = round(self.t_end / self.step) |
| 32 | + return True |
| 33 | + |
| 34 | + @property |
| 35 | + def num_steps(self): |
| 36 | + return self._num_steps |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def compute_epsilon(cls, a, t, eta): |
| 40 | + assert eta > 0 |
| 41 | + a_inf_norm = np.linalg.norm(a, np.inf) |
| 42 | + epsilon = (a_inf_norm * t) / (eta + 2) |
| 43 | + if epsilon >= 1: |
| 44 | + raise Exception("Epsilon must be less than 1") |
| 45 | + return epsilon |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def compute_et(cls, a, t, eta, epsilon): |
| 49 | + a_norm = np.linalg.norm(a, np.inf) |
| 50 | + cof = ((a_norm * t) ** (eta + 1) / factorial(eta + 1)) * 1 / (1 - epsilon) |
| 51 | + return Interval.identity(a.shape) * cof |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def compute_e_ar(cls, eta, r, a): |
| 55 | + # compute epsilon |
| 56 | + epsilon = cls.compute_epsilon(a, r, eta) |
| 57 | + # compute sums of taylor terms |
| 58 | + taylor_sums = 0 |
| 59 | + for i in range(eta + 1): |
| 60 | + taylor_sums += 1 / factorial(i) * np.linalg.matrix_power(a * r, i) |
| 61 | + |
| 62 | + er = cls.compute_et(a, r, eta, epsilon) |
| 63 | + |
| 64 | + return taylor_sums + er, er |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def compute_f(cls, eta, a, er, r): |
| 68 | + # compute taylor sums |
| 69 | + taylor_sums = 0 |
| 70 | + |
| 71 | + for i in range(2, eta + 1): |
| 72 | + cof = np.linalg.matrix_power(a, i) / factorial(i) |
| 73 | + box_inf = (np.power(i, -i / (i - 1)) - np.power(i, -1 / (i - 1))) * np.power(r, i) |
| 74 | + box = Interval(box_inf, 0) |
| 75 | + taylor_sums += box * cof |
| 76 | + |
| 77 | + return taylor_sums + er |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def pre_compute(cls, lin_sys: LinearSystemSimple, opts: Settings): |
| 81 | + e_ar, er = cls.compute_e_ar(opts.eta, opts.step, lin_sys.xa) |
| 82 | + f = cls.compute_f(opts.eta, lin_sys.xa, er, opts.step) |
| 83 | + return e_ar, f |
| 84 | + |
| 85 | + @classmethod |
| 86 | + def compute_hr(cls, e_ar, x0, f): |
| 87 | + return enclose(x0, e_ar @ x0, Geometry.TYPE.ZONOTOPE) + f @ x0 |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def reach_one_step(cls, e_ar, hr_cur): |
| 91 | + return e_ar @ hr_cur |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def _reach(cls, lin_sys: LinearSystemSimple, opts: Settings, x0: Zonotope): |
| 95 | + assert opts.validation() |
| 96 | + |
| 97 | + e_ar, f = cls.pre_compute(lin_sys, opts) |
| 98 | + hr_cur = cls.compute_hr(e_ar, x0, f) |
| 99 | + |
| 100 | + # ri -> time interval reachable sets |
| 101 | + # rp -> time point reachable sets |
| 102 | + # ti -> time intervals |
| 103 | + # tp -> time points |
| 104 | + ri = [x0, hr_cur] |
| 105 | + |
| 106 | + for k in range(opts.num_steps): |
| 107 | + hr_cur = cls.reach_one_step(e_ar, hr_cur) |
| 108 | + ri.append(hr_cur) |
| 109 | + |
| 110 | + return None, ri, None, None |
| 111 | + |
| 112 | + @classmethod |
| 113 | + def reach(cls, lin_sys, opts: Settings, xs: [Zonotope]): |
| 114 | + with ProcessPoolExecutor() as executor: |
| 115 | + partial_reach = partial(cls._reach, lin_sys, opts) |
| 116 | + |
| 117 | + futures = [executor.submit(partial_reach, x) for x in xs] |
| 118 | + |
| 119 | + for future in as_completed(futures): |
| 120 | + try: |
| 121 | + return future.result() |
| 122 | + except Exception as exc: |
| 123 | + raise exc |
0 commit comments