Skip to content

Commit 722e2b0

Browse files
committed
cleanup
1 parent e2f6cce commit 722e2b0

File tree

9 files changed

+46
-110
lines changed

9 files changed

+46
-110
lines changed

pybdr/algorithm/alk2011hscc.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from concurrent.futures import ProcessPoolExecutor, as_completed
1919
from functools import partial
2020

21-
from pybdr.dynamic_system import LinearSystemSimple
21+
from pybdr.dynamic_system import LinSys
2222
from pybdr.geometry import Geometry, Zonotope, Interval
2323
from pybdr.geometry.operation import cvt2
2424
from .algorithm import Algorithm
@@ -51,7 +51,7 @@ def validation(self, dim: int):
5151
return True
5252

5353
@staticmethod
54-
def exponential(sys: LinearSystemSimple, opt: Options):
54+
def exponential(sys: LinSys, opt: Options):
5555
xa_abs = abs(sys.xa)
5656
xa_power = [sys.xa]
5757
xa_power_abs = [xa_abs]
@@ -69,7 +69,7 @@ def exponential(sys: LinearSystemSimple, opt: Options):
6969
opt.taylor_err = e
7070

7171
@classmethod
72-
def compute_time_interval_err(cls, sys: LinearSystemSimple, opt: Options):
72+
def compute_time_interval_err(cls, sys: LinSys, opt: Options):
7373
# initialize asum
7474
asum_pos = np.zeros((sys.dim, sys.dim), dtype=float)
7575
asum_neg = np.zeros((sys.dim, sys.dim), dtype=float)
@@ -95,7 +95,7 @@ def compute_time_interval_err(cls, sys: LinearSystemSimple, opt: Options):
9595
opt.taylor_f = asum + opt.taylor_err
9696

9797
@classmethod
98-
def input_time_interval_err(cls, sys: LinearSystemSimple, opt: Options):
98+
def input_time_interval_err(cls, sys: LinSys, opt: Options):
9999
# initialize asum
100100
asum_pos = np.zeros((sys.dim, sys.dim), dtype=float)
101101
asum_neg = np.zeros((sys.dim, sys.dim), dtype=float)
@@ -124,7 +124,7 @@ def input_time_interval_err(cls, sys: LinearSystemSimple, opt: Options):
124124
opt.taylor_input_f = asum + e_input
125125

126126
@classmethod
127-
def input_solution(cls, sys: LinearSystemSimple, opt: Options):
127+
def input_solution(cls, sys: LinSys, opt: Options):
128128
v = opt.u if sys.ub is None else sys.ub @ opt.u
129129
# compute vTrans
130130
opt.is_rv = True
@@ -182,7 +182,7 @@ def error_solution(cls, v_dyn: Geometry.Base, opt: Options):
182182
return a_sum + f
183183

184184
@classmethod
185-
def delta_reach(cls, sys: LinearSystemSimple, r: Geometry.Base, opt: Options):
185+
def delta_reach(cls, sys: LinSys, r: Geometry.Base, opt: Options):
186186
rhom_tp_delta = (opt.taylor_ea_t - np.eye(sys.dim)) @ r + opt.taylor_r_trans
187187

188188
if r.type == Geometry.TYPE.ZONOTOPE:
@@ -198,7 +198,7 @@ def delta_reach(cls, sys: LinearSystemSimple, r: Geometry.Base, opt: Options):
198198
return rhom + rv
199199

200200
@classmethod
201-
def reach_one_step(cls, sys: LinearSystemSimple, r: Zonotope, opt: Options):
201+
def reach_one_step(cls, sys: LinSys, r: Zonotope, opt: Options):
202202
cls.exponential(sys, opt)
203203
cls.compute_time_interval_err(sys, opt)
204204
cls.input_solution(sys, opt)
@@ -216,7 +216,7 @@ def reach_one_step(cls, sys: LinearSystemSimple, r: Zonotope, opt: Options):
216216
return r_hom + rv, r_hom_tp + rv
217217

218218
@classmethod
219-
def reach(cls, sys: LinearSystemSimple, opt: Options, x: Zonotope):
219+
def reach(cls, sys: LinSys, opt: Options, x: Zonotope):
220220
assert opt.validation(sys.dim)
221221
ri_set, rp_set = [x], []
222222

@@ -230,7 +230,7 @@ def reach(cls, sys: LinearSystemSimple, opt: Options, x: Zonotope):
230230
return ri_set, rp_set
231231

232232
@classmethod
233-
def reach_parallel(cls, sys: LinearSystemSimple, opts: Options, xs: [Zonotope]):
233+
def reach_parallel(cls, sys: LinSys, opts: Options, xs: [Zonotope]):
234234
def ll_decompose(ll):
235235
return [list(group) for group in zip(*ll)]
236236

pybdr/algorithm/gira2005hscc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import numpy as np
4-
from pybdr.dynamic_system import LinearSystemSimple
4+
from pybdr.dynamic_system import LinSys
55
from pybdr.geometry import Interval, Geometry, Zonotope
66
from pybdr.geometry.operation import enclose, cvt2
77
from dataclasses import dataclass
@@ -123,7 +123,7 @@ def compute_r(cls, rc, pr, prc):
123123
return rc + pr + prc
124124

125125
@classmethod
126-
def pre_compute(cls, lin_sys: LinearSystemSimple, opts: Options, x: Zonotope):
126+
def pre_compute(cls, lin_sys: LinSys, opts: Options, x: Zonotope):
127127
a_inv = np.linalg.pinv(lin_sys.xa)
128128
e_ar, er = cls.compute_e_ar(opts.eta, opts.step, lin_sys.xa)
129129
f = cls.compute_f(opts.eta, lin_sys.xa, er, opts.step)
@@ -146,7 +146,7 @@ def reach_one_step(cls, e_ar, rc_cur, v_cur, pr_cur):
146146
return r_next, rc_next, v_next, pr_next
147147

148148
@classmethod
149-
def reach(cls, lin_sys: LinearSystemSimple, opts: Options, x: Zonotope):
149+
def reach(cls, lin_sys: LinSys, opts: Options, x: Zonotope):
150150
assert opts.validation()
151151

152152
r0, e_ar, rc_cur, v_cur, pr_cur = cls.pre_compute(lin_sys, opts, x)
@@ -160,7 +160,7 @@ def reach(cls, lin_sys: LinearSystemSimple, opts: Options, x: Zonotope):
160160
return None, ri, None, None
161161

162162
@classmethod
163-
def reach_parallel(cls, lin_sys: LinearSystemSimple, opts: Options, xs: [Zonotope]):
163+
def reach_parallel(cls, lin_sys: LinSys, opts: Options, xs: [Zonotope]):
164164

165165
def ll_decompose(ll):
166166
return [list(group) for group in zip(*ll)]

pybdr/dynamic_system/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .continuous_system import *
22

3-
__all__ = ["LinSys", "NonLinSys", "LinearSystemSimple"]
3+
__all__ = ["LinSys", "NonLinSys", "LinSys"]
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from .linear_system import LinSys
21
from .nonlinear_system import NonLinSys
3-
from .linear_system_simple import LinearSystemSimple
2+
from .linear_system import LinSys
43

5-
__all__ = ["LinSys", "NonLinSys", "LinearSystemSimple"]
4+
__all__ = ["LinSys", "NonLinSys", "LinSys"]
Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,31 @@
1-
from __future__ import annotations
21
import numpy as np
32

43
"""
5-
x(t)' = A*x(t) + B*u(t) + c
6-
y(t) = C*x(t) + D*u(t) + k
4+
x(t)' = A*x(t) + B*u(t)
75
"""
86

97

108
class LinSys:
11-
def __init__(
12-
self,
13-
xa: np.ndarray,
14-
ub: np.ndarray = None,
15-
c: float = None,
16-
xc: np.ndarray = None,
17-
ud: np.ndarray = None,
18-
k: float = None,
19-
):
20-
"""
21-
22-
@param xa: state matrix
23-
@param ub: input matrix
24-
@param c: constant input
25-
@param xc: output matrix
26-
@param ud: feedthrough matrix
27-
@param k: output offset
28-
"""
29-
assert isinstance(xa, np.ndarray)
30-
self._xa = xa
31-
self._ub = ub
32-
self._c = c
33-
self._xc = xc
34-
self._ud = ud
35-
self._k = k
9+
def __init__(self, xa, ub):
10+
self._xa = np.atleast_2d(xa)
11+
self._ub = np.atleast_2d(ub)
12+
assert self._xa.shape[1] == self._ub.shape[0]
3613

3714
@property
38-
def dim(self) -> int:
15+
def dim(self):
3916
return self._xa.shape[1]
4017

4118
@property
42-
def xa(self) -> np.ndarray:
43-
return self._xa
19+
def type(self):
20+
return 'linear_simple'
4421

4522
@property
46-
def ub(self) -> np.ndarray:
47-
return self._ub
48-
49-
@property
50-
def c(self) -> float:
51-
return self._c
52-
53-
@property
54-
def xc(self) -> np.ndarray:
55-
return self._xc
23+
def xa(self):
24+
return self._xa
5625

5726
@property
58-
def ud(self) -> np.ndarray:
59-
return self._ud
27+
def ub(self):
28+
return self._ub
6029

61-
@property
62-
def k(self) -> float:
63-
return self._k
30+
def evaluate(self, x: np.ndarray, u: np.ndarray):
31+
return (self._xa[..., :, :] @ x[:, :, None] + self._ub[..., :, :] @ u[:, :, None]).squeeze(axis=-1)

pybdr/dynamic_system/continuous_system/linear_system_simple.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/algorithm/test_alk2011hscc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22

33
from pybdr.geometry import Zonotope, Geometry, Interval
4-
from pybdr.dynamic_system import LinearSystemSimple
4+
from pybdr.dynamic_system import LinSys
55
from pybdr.algorithm import ALK2011HSCC
66
from pybdr.geometry.operation import cvt2, boundary
77
from pybdr.util.visualization import plot, plot_cmp
@@ -11,7 +11,7 @@ def test_case_0():
1111
a = np.array([[-0.7, -2], [2, -0.7]])
1212
b = np.identity(2)
1313

14-
lin_dyn = LinearSystemSimple(a, b)
14+
lin_dyn = LinSys(a, b)
1515

1616
# settings for the computation
1717
options = ALK2011HSCC.Options()

test/algorithm/test_gira2005hscc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
from pybdr.geometry import Zonotope, Geometry, Interval
33
from pybdr.geometry.operation import cvt2, boundary
4-
from pybdr.dynamic_system import LinearSystemSimple
4+
from pybdr.dynamic_system import LinSys
55
from pybdr.algorithm import GIRA2005HSCC
66
from pybdr.util.visualization import plot, plot_cmp
77
from pybdr.util.functional import performance_counter, performance_counter_start
@@ -13,7 +13,7 @@ def test_reach_linear_zono_algo3_case_00():
1313
xa = [[-1, -4], [4, -1]]
1414
ub = np.array([[1], [1]])
1515

16-
lin_sys = LinearSystemSimple(xa, ub)
16+
lin_sys = LinSys(xa, ub)
1717
opts = GIRA2005HSCC.Options()
1818
opts.t_end = 5
1919
opts.step = 0.01
@@ -40,7 +40,7 @@ def test_reach_linear_zono_algo3_case_01():
4040
xa = np.array([[-1, -4, 0, 0, 0], [4, - 1, 0, 0, 0], [0, 0, -3, 1, 0], [0, 0, -1, -3, 0], [0, 0, 0, 0, -2]])
4141
ub = np.eye(5)
4242

43-
lin_sys = LinearSystemSimple(xa, ub)
43+
lin_sys = LinSys(xa, ub)
4444
opts = GIRA2005HSCC.Options()
4545
opts.t_end = 5
4646
opts.step = 0.04
@@ -71,7 +71,7 @@ def test_reach_linear_zono_algo3_case_02():
7171
xa = np.array([[-1, -4, 0, 0, 0], [4, - 1, 0, 0, 0], [0, 0, -3, 1, 0], [0, 0, -1, -3, 0], [0, 0, 0, 0, -2]])
7272
ub = np.eye(5)
7373

74-
lin_sys = LinearSystemSimple(xa, ub)
74+
lin_sys = LinSys(xa, ub)
7575
opts = GIRA2005HSCC.Options()
7676
opts.t_end = 5
7777
opts.step = 0.01
@@ -105,7 +105,7 @@ def test_reach_linear_zono_algo3_parallel_case_03():
105105
xa = [[-1, -4], [4, -1]]
106106
ub = np.array([[1], [1]])
107107

108-
lin_sys = LinearSystemSimple(xa, ub)
108+
lin_sys = LinSys(xa, ub)
109109
opts = GIRA2005HSCC.Options()
110110
opts.t_end = 5
111111
opts.step = 0.1
@@ -150,7 +150,7 @@ def test_reach_linear_zono_algo3_parallel_case_04():
150150
xa = np.array([[-1, -4, 0, 0, 0], [4, -1, 0, 0, 0], [0, 0, -3, 1, 0], [0, 0, -1, -3, 0], [0, 0, 0, 0, -2]])
151151
ub = np.eye(5)
152152

153-
lin_sys = LinearSystemSimple(xa, ub)
153+
lin_sys = LinSys(xa, ub)
154154
opts = GIRA2005HSCC.Options()
155155
opts.t_end = 5
156156
opts.step = 0.01
@@ -176,7 +176,7 @@ def test_reach_linear_zono_algo3_parallel_case_04():
176176
xa = [[-1, 0], [-1, 1]]
177177
ub = np.array([[1], [1]])
178178

179-
lin_sys = LinearSystemSimple(xa, ub)
179+
lin_sys = LinSys(xa, ub)
180180
opts = GIRA2005HSCC.Options()
181181
opts.t_end = 4
182182
opts.step = 0.01

test/util/test_simulator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pybdr.util.functional import Simulator
2-
from pybdr.dynamic_system import LinearSystemSimple, NonLinSys
2+
from pybdr.dynamic_system import LinSys, NonLinSys
33
from pybdr.model import *
44
from pybdr.util.visualization import plot
55
import numpy as np
@@ -18,7 +18,7 @@ def test_case_00():
1818
init_x = [1, 1]
1919
init_u = [0]
2020

21-
lin_sys = LinearSystemSimple(xa, ub)
21+
lin_sys = LinSys(xa, ub)
2222
trajs = Simulator.simulate(lin_sys, t_end, step, init_x, init_u)
2323

2424
plot(trajs, [0, 1])
@@ -37,7 +37,7 @@ def test_case_01():
3737
init_x = [1, 1]
3838
init_u = np.random.rand(100, 1) * 0.1
3939

40-
lin_sys = LinearSystemSimple(xa, ub)
40+
lin_sys = LinSys(xa, ub)
4141
trajs = Simulator.simulate(lin_sys, t_end, step, init_x, init_u)
4242

4343
plot(trajs, [0, 1])
@@ -56,7 +56,7 @@ def test_case_02():
5656
init_x = np.random.rand(100, 2) * 0.2 + 1
5757
init_u = [0]
5858

59-
lin_sys = LinearSystemSimple(xa, ub)
59+
lin_sys = LinSys(xa, ub)
6060
trajs = Simulator.simulate(lin_sys, t_end, step, init_x, init_u)
6161

6262
plot(trajs, [0, 1])
@@ -75,7 +75,7 @@ def test_case_03():
7575
init_x = np.random.rand(100, 2) * 0.2 + 1
7676
init_u = np.random.rand(100, 1) * 0.1
7777

78-
lin_sys = LinearSystemSimple(xa, ub)
78+
lin_sys = LinSys(xa, ub)
7979
trajs = Simulator.simulate(lin_sys, t_end, step, init_x, init_u)
8080

8181
plot(trajs, [0, 1])
@@ -94,7 +94,7 @@ def test_case_04():
9494
init_x = np.ones((1, 5))
9595
init_u = [1, 0, 0, 0.5, -0.5]
9696

97-
lin_sys = LinearSystemSimple(xa, ub)
97+
lin_sys = LinSys(xa, ub)
9898
trajs = Simulator.simulate(lin_sys, t_end, step, init_x, init_u)
9999

100100
plot(trajs, [1, 2])
@@ -114,7 +114,7 @@ def test_case_05():
114114
init_x = np.random.rand(100, 5) * 0.2 + 1
115115
init_u = np.random.rand(100, 5) * 0.1 + [1, 0, 0, 0.5, -0.5]
116116

117-
lin_sys = LinearSystemSimple(xa, ub)
117+
lin_sys = LinSys(xa, ub)
118118
trajs = Simulator.simulate(lin_sys, t_end, step, init_x, init_u)
119119

120120
plot(trajs, [1, 2])

0 commit comments

Comments
 (0)