|
| 1 | +from __future__ import annotations |
| 2 | +import codac |
| 3 | + |
| 4 | + |
| 5 | +def extract_boundary(init_interval, init_set, eps: float = 0.04, |
| 6 | + simplify_result: bool = True, simplify_eps: float = 0.005): |
| 7 | + from pybdr.geometry import Geometry, Interval, Polytope, Zonotope |
| 8 | + from pybdr.geometry.operation.convert import cvt2 |
| 9 | + |
| 10 | + if isinstance(init_set, codac.AnalyticFunction): |
| 11 | + return extract_boundary_analytic_function(init_interval, init_set, eps, simplify_result, simplify_eps) |
| 12 | + elif isinstance(init_set, Polytope) or isinstance(init_set, Zonotope): |
| 13 | + if isinstance(init_set, Zonotope): |
| 14 | + init_set = cvt2(init_set, Geometry.TYPE.POLYTOPE) |
| 15 | + return extract_boundary_polytope(init_interval, init_set, eps) |
| 16 | + else: |
| 17 | + raise NotImplementedError() |
| 18 | + |
| 19 | + |
| 20 | +def extract_boundary_polytope(init_interval, init_set, eps: float = 0.04): |
| 21 | + from pybdr.geometry import Geometry, Polytope, Interval, Zonotope |
| 22 | + from pybdr.geometry.operation.convert import cvt2 |
| 23 | + assert isinstance(init_set, Polytope) |
| 24 | + n = init_set.a.shape[1] |
| 25 | + m = init_set.a.shape[0] |
| 26 | + |
| 27 | + x = codac.VectorVar(n) |
| 28 | + |
| 29 | + funcs = [sum(init_set.a[i, j] * x[j] for j in range(n)) - init_set.b[i] for i in range(m)] |
| 30 | + g = codac.AnalyticFunction([x], funcs) |
| 31 | + |
| 32 | + Y = codac.IntervalVector([[float("-inf"), 0.0]] * m) |
| 33 | + ctc = codac.CtcInverse(g, Y) |
| 34 | + |
| 35 | + boxes = [] |
| 36 | + pave_boxes(init_interval, ctc, eps, boxes) |
| 37 | + |
| 38 | + boundary_boxes = [] |
| 39 | + for box in boxes: |
| 40 | + if box.is_empty(): |
| 41 | + continue |
| 42 | + vals = g.eval(box) |
| 43 | + for i in range(m): |
| 44 | + if vals[i].lb() <= 0.0 <= vals[i].ub(): |
| 45 | + boundary_boxes.append(box) |
| 46 | + break |
| 47 | + |
| 48 | + x_init = [] |
| 49 | + for i in boundary_boxes: |
| 50 | + if not i.is_empty(): |
| 51 | + i_interval = Interval(i.lb(), i.ub()) |
| 52 | + # x_init.append(i_interval) |
| 53 | + x_init.append(cvt2(i_interval, Geometry.TYPE.ZONOTOPE)) |
| 54 | + return x_init |
| 55 | + |
| 56 | + |
| 57 | +def extract_boundary_analytic_function(init_interval, init_set, eps: float = 0.04, |
| 58 | + simplify_result: bool = True, simplify_eps: float = 0.005): |
| 59 | + from pybdr.geometry import Geometry, Interval |
| 60 | + from pybdr.geometry.operation.convert import cvt2 |
| 61 | + |
| 62 | + ctc = codac.CtcInverse(init_set, [0.0]) |
| 63 | + # ctc = codac.CtcInverse(init_set_func, codac.IntervalVector([codac.Interval(0, 1e9)])) |
| 64 | + |
| 65 | + init_sub_interval_list = [] |
| 66 | + for i in range(init_interval.shape[0]): |
| 67 | + init_sub_interval_list.append(codac.Interval(init_interval[i].inf[0], init_interval[i].sup[0])) |
| 68 | + init_domain = codac.IntervalVector(init_sub_interval_list) |
| 69 | + result = [] |
| 70 | + pave_boxes(init_domain, ctc, eps, result) |
| 71 | + |
| 72 | + if simplify_result: |
| 73 | + isolated_boxes = extract_isolated_boxes(result) |
| 74 | + |
| 75 | + for i in isolated_boxes: |
| 76 | + boxes_i = [] |
| 77 | + i_copy = codac.IntervalVector(i) |
| 78 | + pave_boxes(i_copy, ctc, simplify_eps, boxes_i) |
| 79 | + |
| 80 | + result.remove(i) |
| 81 | + result += boxes_i |
| 82 | + |
| 83 | + x_init = [] |
| 84 | + for i in result: |
| 85 | + if not i.is_empty(): |
| 86 | + i_interval = Interval(i.lb(), i.ub()) |
| 87 | + # x_init.append(i_interval) |
| 88 | + x_init.append(cvt2(i_interval, Geometry.TYPE.ZONOTOPE)) |
| 89 | + return x_init |
| 90 | + |
| 91 | + |
| 92 | +def pave_boxes(box, ctc: codac.CtcInverse, eps, result): |
| 93 | + ctc.contract(box) |
| 94 | + if box.is_empty(): |
| 95 | + return |
| 96 | + |
| 97 | + if box.max_diam() <= eps: |
| 98 | + result.append(box) |
| 99 | + return |
| 100 | + |
| 101 | + i = box.max_diam_index() |
| 102 | + left, right = box.bisect(i) |
| 103 | + pave_boxes(left, ctc, eps, result) |
| 104 | + pave_boxes(right, ctc, eps, result) |
| 105 | + |
| 106 | + |
| 107 | +def boxes_touch(b1: codac.IntervalVector, b2: codac.IntervalVector, tol=1e-15): |
| 108 | + n = b1.size() |
| 109 | + for i in range(n): |
| 110 | + if b1[i].ub() < b2[i].lb() - tol or b2[i].ub() < b1[i].lb() - tol: |
| 111 | + return False |
| 112 | + return True |
| 113 | + |
| 114 | + |
| 115 | +def extract_isolated_boxes(boxes, tol=1e-12): |
| 116 | + isolated = [] |
| 117 | + for i, b in enumerate(boxes): |
| 118 | + connected = False |
| 119 | + for j, b2 in enumerate(boxes): |
| 120 | + if i == j: |
| 121 | + continue |
| 122 | + if boxes_touch(b, b2, tol): |
| 123 | + connected = True |
| 124 | + break |
| 125 | + if not connected: |
| 126 | + isolated.append(b) |
| 127 | + return isolated |
| 128 | + |
| 129 | + |
0 commit comments