def test_dependencies_on_vector_components_1(self): x = Interval(0, 1) y = Interval(-2, 3) a = Interval(1, 20) vx = IntervalVector(2, x) vy = IntervalVector(2, y) va = IntervalVector(2, a) vec = [4, 0.5] ctc_add = CtcFunction(Function("b", "c", "a", "b+c-a")) ctc_cos = CtcFunction(Function("b", "c", "a", "b+c-a")) ctc_minus = CtcFunction(Function("b", "c", "b-c-0")) cn = ContractorNetwork() cn.add(ctc_add, [x, y, a]) cn.add(ctc_add, [x, y, a]) cn.contract() self.assertEqual(x, Interval(0, 1)) self.assertEqual(y, Interval(0, 3)) self.assertEqual(a, Interval(1, 4)) self.assertEqual(cn.nb_dom(), 3) self.assertEqual(cn.nb_ctc(), 1)
def Erode(Sb, y_init): """Compute A+B.""" f1 = Function("p[2]", "a[2]", "(p[0] + a[0], p[1] + a[1])") f2 = Function("p[2]", "a[2]", "(a[0], a[1])") sep_invB = SepInverse(~Sb, f1) # sep_invA = SepInverse(Sa, f2) # sep2 = sep_invB # y_init = box sep = ~SepProj(sep_invB, y_init, 0.001) return sep
def SepSum2(Sa, Sb): """Compute A+B.""" f1 = Function("p[2]", "a[2]", "(p[0] + a[0], p[1] + a[1])") f2 = Function("p[2]", "a[2]", "(a[0], a[1])") sep_invB = SepInverse(Sb, f1) sep_invA = SepInverse(Sa, f2) sep2 = sep_invB & sep_invA y_init = IntervalVector(2, [-oo, oo]) sep = SepProj(sep2, y_init, 0.001) return sep
def test_singleton_variables_double_or_Vector_2(self): x = Interval(0, 1) y = Interval(-2, 3) a = Interval(1, 20) vector_y = [2, 1.] ivx = IntervalVector(2, x) iva = IntervalVector(2, a) ctc_add = CtcFunction(Function("b", "c", "a", "b+c-a")) cn = ContractorNetwork() cn.add(ctc_add, [ivx, vector_y, iva]) cn.add(ctc_add, [ivx, vector_y, iva]) # redundant adding cn.add(ctc_add, [ivx, vector_y, iva]) # redundant adding cn.contract() self.assertEqual(ivx, IntervalVector(2, [0, 1])) self.assertEqual(vector_y, [2, 1.]) self.assertEqual(iva, IntervalVector([[2, 3], [1, 2]])) #self.assertEqual(cn.nb_dom(), 3*3) # todo: not able to get reference from a py::list #self.assertEqual(cn.nb_ctc(), 3+2) # todo: not able to get reference from a py::list cn.set_name(vector_y, "y") cn.set_name(ivx, "x") cn.set_name(iva, "a") cn.set_name(ctc_add, "+") cn.print_dot_graph("cn_singleton")
def test_subvector(self): x = IntervalVector([[0, 1], [-2, 3], [1, 20]]) ctc_add = CtcFunction(Function("b", "c", "a", "b+c-a")) cn = ContractorNetwork() sub_x = cn.subvector(x, 1, 2) self.assertEqual(sub_x[0], Interval(-2, 3)) self.assertEqual(sub_x[1], Interval(1, 20)) cn.add(ctc_add, [x[0], x[1], x[2]]) cn.contract() self.assertEqual(x[0], Interval(0, 1)) self.assertEqual(x[1], Interval(0, 3)) self.assertEqual(x[2], Interval(1, 4)) self.assertEqual(sub_x[0], Interval(0, 3)) self.assertEqual(sub_x[1], Interval(1, 4)) sub_x[0] = Interval(1, 2) cn.trigger_all_contractors() cn.contract() self.assertEqual(x[1], Interval(1, 2))
def test_SIVIA(self): l = [[-1, 1, 0], [-1, -2, 1]] sep = SepPolygon(l) f = Function('x', 'y', '(2*x-y, 2*y - x)') finv = Function('x', 'y', '(1/3.0*(2*x+y),1/3.0*(2*y+x))') # sepInv = SepInverse(sep, f) # sepTrans = SepTransform(sep, f, finv) Xin = IntervalVector(2, Interval(-10, 10)) Xout = IntervalVector(2, Interval(-10, 10)) eps = 0.05 # sep.separate(Xin, Xout) pySIVIA( Xin, sep, eps, figureName='Sep', draw_boxes=False, )
def test_CtcFunction_on_heterogeneous_variables(self): x = IntervalVector([[0, 1], [-2, 3]]) a = Interval(1, 20) ctc_add = CtcFunction(Function("b[2]", "a", "b[0]+b[1]-a")) cn = ContractorNetwork() cn.add(ctc_add, [x, a]) cn.contract() self.assertEqual(x[0], Interval(0, 1)) self.assertEqual(x[1], Interval(0, 3)) self.assertEqual(a, Interval(1, 4))
def test_CN_simple(self): ctc_plus = CtcFunction(Function("a", "b", "c", "a+b-c")) a = Interval(0, 1) b = Interval(-1, 1) c = Interval(1.5, 2) cn = ContractorNetwork() cn.add(ctc_plus, [a, b, c]) cn.contract() self.assertEqual(a, Interval(0.5, 1)) self.assertEqual(b, Interval(0.5, 1)) self.assertEqual(c, Interval(1.5, 2)) self.assertEqual(cn.nb_dom(), 3) self.assertEqual(cn.nb_ctc(), 1)
def test_dependencies_on_vector_components(self): ctc_plus = CtcFunction(Function("a", "b", "c", "a+b-c")) # algebraic constraint a+b=c a = IntervalVector(2, [0, 1]) b = IntervalVector(2, [-1, 1]) c = IntervalVector(2, [1.5, 2]) d = IntervalVector(2, [0., 0.]) e = IntervalVectore(2) cn = ContractorNetwork() # Contractors are added to the graph, # the first one contracts the vector sets, the second one then contracts the components intervals, # and so it should trigger again the first one since components have changed. cn.add(ctc_plus, [b, d, e]) cn.add(ctc_plus, [b, d, e]) cn.add(ctc_plus, [b, d, e]) cn.add(ctc_plus, [a[0], b[0], c[0]]) cn.add(ctc_plus, [a[0], b[0], c[0]]) cn.add(ctc_plus, [a[0], b[0], c[0]]) cn.add(ctc_plus, [a[0], b[0], c[0]]) cn.contract() self.assertEqual(a[0], Interval(0.5, 1)) self.assertEqual(b[0], Interval(0.5, 1)) self.assertEqual(c[0], Interval(1.5, 2)) self.assertEqual(d[0], Interval(0)) self.assertEqual(e[0], Interval(0.5, 1)) # Before setting names (some vectors not added entirely): self.assertEqual(cn.nb_dom(), 3 * 3 + 2) # vector items contractors + ctc_plus in array mode + ctc_plus on scalar values self.assertEqual(cn.nb_ctc(), 3 + 2 + 1) cn.set_name(a, "a") cn.set_name(b, "b") cn.set_name(c, "c") cn.set_name(d, "d") cn.set_name(e, "e") cn.set_name(ctc_plus, "+") cn.print_dot_graph("cn_vector_dependencies") self.assertEqual(cn.nb_dom(), 3 * 5) # vector items contractors + ctc_plus in array mode + ctc_plus on scalar values self.assertEqual(cn.nb_ctc(), 5 + 2 + 1)
def test_heterogeneous_variables_with_CtcFunction_3(self): dt = 0.1 tdomain = Interval(0., 10.) x = Interval(2., 2.5) a = Tube(tdomain, dt, Interval(0., 10.)) b = Tube(tdomain, dt, Interval(7.)) ctc_add = CtcFunction(Function("x", "a", "b", "x+a-b")) cn = ContractorNetwork() cn.add(ctc_add, [x, a, b]) cn.contract() self.assertEqual(x, Interval(2., 2.5)) self.assertEqual(a.codomain(), Interval(4.5, 5.)) self.assertEqual(b.codomain(), Interval(7.))
def test_simple_static_case(self): ctc_plus = CtcFunction(Function("a", "b", "c", "a+b-c")) # algebraic constraint a+b=c a = IntervalVector(2, [0, 1]) b = IntervalVector(2, [-1, 1]) c = IntervalVector(2, [1.5, 2]) cn = ContractorNetwork() cn.add(ctc_plus, [a[0], b[0], c[0]]) cn.contract() self.assertEqual(a[0], Interval(0.5, 1)) self.assertEqual(b[0], Interval(0.5, 1)) self.assertEqual(c[0], Interval(1.5, 2)) self.assertEqual(cn.nb_dom(), 3) self.assertEqual(cn.nb_ctc(), 1)
def test_heterogeneous_variables_with_CtcFunction_4(self): dt = 0.1 tdomain = Interval(0., 10.) x = Interval(2., 2.5) a = Tube(tdomain, dt, TFunction("cos(t)")) b = Tube(tdomain, dt) ctc_add = CtcFunction(Function("x", "a", "b", "x+a-b")) cn = ContractorNetwork() cn.add(ctc_add, [x, a, b]) cn.contract() result = Tube(tdomain, dt, TFunction("cos(t)")) result += x self.assertEqual(x, Interval(2., 2.5)) self.assertEqual(b, result)
def test_heterogeneous_variables_with_CtcFunction_2(self): dt = 0.1 tdomain = Interval(0., 10.) x = Interval(-1., 3.) a = Tube(tdomain, dt, Interval(6., 7.)) b = Tube(tdomain, dt, Interval(7.)) ctc_add = CtcFunction(Function("x", "a", "b", "x+a-b")) cn = ContractorNetwork() cn.add(ctc_add, [x, a, b]) cn.contract() cn.set_name(x, "x") cn.set_name(a, "a") cn.set_name(b, "b") self.assertEqual(x, Interval(0., 1.)) self.assertEqual(a.codomain(), Interval(6., 7.)) self.assertEqual(b.codomain(), Interval(7.))
def test_dependencies_on_vector_components(self): dt = 5. domain = Interval(0., 20.) x = Tube(domain, dt, Interval(-10., 10.)) v = Tube(domain, dt, Interval(0.)) ctc_deriv = CtcDeriv() ctc_f = CtcFunction(Function("x", "xdot", "xdot+sin(x)")) cn = ContractorNetwork() cn.add(ctc_deriv, [x, v]) cn.add(ctc_f, [x, v]) self.assertEqual(v.codomain(), Interval(0.)) self.assertEqual(x.codomain(), Interval(-10., 10.)) self.assertEqual(x.nb_slices(), 4) self.assertEqual(v.nb_slices(), 4) self.assertEqual(cn.nb_ctc(), 16) self.assertEqual(cn.nb_dom(), 10) cn.set_fixedpoint_ratio(0.8) cn.contract()
def test_singleton_variables_double_or_Vector_1(self): x = Interval(0, 1) y = Interval(-2, 3) a = Interval(1, 20) double_y = 1. vector_y = [2, 1.] ivx = IntervalVector(2, x) ivy = IntervalVector(2, y) iva = IntervalVector(2, a) ctc_add = CtcFunction(Function("b", "c", "a", "b+c-a")) cn = ContractorNetwork() cn.add(ctc_add, [x, double_y, a]) cn.add(ctc_add, [x, double_y, a]) # redundant adding cn.add(ctc_add, [x, double_y, a]) # redundant adding cn.contract() self.assertEqual(x, Interval(0, 1)) self.assertEqual(double_y, 1.) self.assertEqual(a, Interval(1, 2))
if n.right.xin.is_flat(): n.right.xin.set_empty() stack.extendleft([n.left, n.right]) elif X.is_empty() or X.max_diam() < eps: n.left, n.right = None, None # else: # vibes.drawBox(X[0][0], X[0][1], X[1][0], X[1][1], '[y]') if __name__ == '__main__': from pyibex import Function, SepFwdBwd funcs_dict = { "SepMini": Function("x", "y", "(x - 1/2.)^2 + abs(2*y) - 1/4."), "SepNonMini": Function("x", "y", "x^2 + abs(2*y) - x"), } funcs_dict = {"SepNonMini": Function("x", "y", "x^2 + abs(2*y) - x")} # funcs_dict = { "SepNonMini" : Function("x", "y", "x^2 + y^2 + x - x + x^3 - x^3") } vibes.beginDrawing() X0 = IntervalVector(2, [-6, 6]) for name, func in funcs_dict.items(): vibes.newFigure(name) sep = SepFwdBwd(func, [3, 5]) vibes.setFigureSize(600, 600) # pySIVIA(X0, sep, 0.1, use_patch=True, color_out='#888888[#DDDDDD]', # color_in='#888888[#444444]', color_maybe='#AAAAAA[w]') # pySIVIA(X0, sep, 0.1, **siviaParams)