def relu(self, stmt: PyVar, active: bool = False, inactive: bool = False) -> 'BoxState': self.flag = None if active: # we only do the active case (h >= 0) expr1 = PyTexpr1.var(self.environment, stmt) cond = PyTcons1.make(expr1, ConsTyp.AP_CONS_SUPEQ) abstract1 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond])) self.state = self.state.meet(abstract1) self.flag = 1 return self if inactive: # we only do the inactive case (h < 0) expr1 = PyTexpr1.var(self.environment, stmt) neg = PyTexpr1.unop(TexprOp.AP_TEXPR_NEG, expr1, rtype, rdir) cond = PyTcons1.make(neg, ConsTyp.AP_CONS_SUP) abstract1 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond])) zero = PyTexpr1.cst(self.environment, PyMPQScalarCoeff(0.0)) self.state = self.state.meet(abstract1).assign(stmt, zero) self.flag = -1 return self # we do both cases _active, _inactive = deepcopy(self.state), deepcopy(self.state) expr1 = PyTexpr1.var(self.environment, stmt) cond1 = PyTcons1.make(expr1, ConsTyp.AP_CONS_SUPEQ) abstract1 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond1])) _active = _active.meet(abstract1) expr2 = PyTexpr1.var(self.environment, stmt) neg = PyTexpr1.unop(TexprOp.AP_TEXPR_NEG, expr2, rtype, rdir) cond2 = PyTcons1.make(neg, ConsTyp.AP_CONS_SUP) abstract2 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond2])) zero = PyTexpr1.cst(self.environment, PyMPQScalarCoeff(0.0)) _inactive = _inactive.meet(abstract2).assign(stmt, zero) if _active.is_bottom(): self.flag = -1 elif _inactive.is_bottom(): self.flag = 1 join = _active.join(_inactive) self.state = join return self
def test_init(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c = PyLincons1(ConsTyp.AP_CONS_SUPEQ, x) self.assertEqual(str(PyTcons1(c)), '8.0 + 3.0 · x0 - 9.0 · z >= 0') self.assertEqual(str(PyTcons1.unsat(e)), '-1.0 >= 0') z = PyLincons1(ConsTyp.AP_CONS_DISEQ, PyLinexpr1(e)) self.assertEqual(str(PyTcons1(z)), '0.0 != 0')
def test_substitute(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x0.set_cst(PyMPQScalarCoeff(3)) t0 = PyTexpr1(x0) c0 = PyTcons1.make(t0, ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c0), '3 + 1 · x0 >= 0') x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x1.set_cst(PyMPQScalarCoeff(-1)) t1 = PyTexpr1(x1) c1 = PyTcons1.make(t1, ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c1), '-1 + 1 · x0 >= 0') self.assertEqual(str(c0.substitute(PyVar('x0'), t1)), '3 + 1 · (-1 + 1 · x0) >= 0')
def visit_BinaryComparisonOperation(self, expr, environment=None, usub=False) -> PyTcons1: assert not usub left = expr.left right = expr.right sub = BinaryArithmeticOperation.Operator.Sub if expr.operator == BinaryComparisonOperation.Operator.GtE: expr = self.visit(BinaryArithmeticOperation(left, sub, right), environment) return PyTcons1.make(expr, ConsTyp.AP_CONS_SUPEQ) elif expr.operator == BinaryComparisonOperation.Operator.Gt: expr = self.visit(BinaryArithmeticOperation(left, sub, right), environment) return PyTcons1.make(expr, ConsTyp.AP_CONS_SUP) elif expr.operator == BinaryComparisonOperation.Operator.LtE: expr = self.visit(BinaryArithmeticOperation(right, sub, left), environment) return PyTcons1.make(expr, ConsTyp.AP_CONS_SUPEQ) elif expr.operator == BinaryComparisonOperation.Operator.Lt: expr = self.visit(BinaryArithmeticOperation(right, sub, left), environment) return PyTcons1.make(expr, ConsTyp.AP_CONS_SUP)
def test_make(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c = PyTcons1.make(PyTexpr1(x), ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c), '8.0 + 3.0 · x0 - 9.0 · z >= 0')
def test_deepcopy(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x = PyLinexpr1(e) x.set_coeff(PyVar('x0'), PyDoubleScalarCoeff(3)) x.set_coeff(PyVar('z'), PyDoubleScalarCoeff(-9)) x.set_cst(PyDoubleScalarCoeff(8)) c0 = PyTcons1.make(PyTexpr1(x), ConsTyp.AP_CONS_SUPEQ) c1 = deepcopy(c0) c2 = c0 self.assertNotEqual(id(c0), id(c1)) self.assertEqual(id(c0), id(c2))
def affine(self, left: List[PyVar], right: List[PyTexpr1]) -> 'Symbolic1State': array = list() assignments = dict() for lhs, expr in zip(left, right): rhs = expr for sym, val in self.symbols.values(): rhs = rhs.substitute(sym, val) assignments[str(lhs)] = (lhs, rhs) var = PyTexpr1.var(self.environment, lhs) binop = PyTexpr1.binop(TexprOp.AP_TEXPR_SUB, var, rhs, rtype, rdir) cond = PyTcons1.make(binop, ConsTyp.AP_CONS_EQ) array.append(cond) self.symbols = assignments self.state = self.state.meet(PyTcons1Array(array)) return self