Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
 def test_bound_texpr(self):
     e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')])
     man: PyManager = PyBoxMPQManager()
     variables = [PyVar('x0'), PyVar('y')]
     intervals = [PyMPQInterval(-3, 2), PyMPQInterval(-2, 2, 1, 1)]
     b = PyBox(man, e, variables=variables, intervals=intervals)
     x0 = PyTexpr1.var(e, PyVar('x0'))
     x1 = PyTexpr1.var(e, PyVar('y'))
     add = PyTexpr1.binop(TexprOp.AP_TEXPR_ADD, x0, x1,
                          TexprRtype.AP_RTYPE_REAL, TexprRdir.AP_RDIR_RND)
     self.assertEqual(str(b.bound_texpr(add)), '[-5,4]')
Ejemplo n.º 3
0
def dict_to_texpr(todict, env):
    texpr = PyTexpr1.cst(env, PyMPQScalarCoeff(PyMPQScalar(todict['_'])))
    for var, val in reversed(list(todict.items())):
        if var != '_':
            coeff = PyTexpr1.cst(env, PyMPQScalarCoeff(PyMPQScalar(val)))
            dim = PyTexpr1.var(env, PyVar(var))
            term = PyTexpr1.binop(TexprOp.AP_TEXPR_MUL, coeff, dim,
                                  TexprRtype.AP_RTYPE_REAL,
                                  TexprRdir.AP_RDIR_RND)
            texpr = PyTexpr1.binop(TexprOp.AP_TEXPR_ADD, term, texpr,
                                   TexprRtype.AP_RTYPE_REAL,
                                   TexprRdir.AP_RDIR_RND)
    return texpr
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 def visit_VariableIdentifier(self, expr, environment=None, usub=False) -> PyTexpr1:
     assert not usub
     return PyTexpr1.var(environment, PyVar(expr.name))
Ejemplo n.º 6
0
 def visit_VariableAccess(self,
                          stmt: 'VariableAccess',
                          environment=None,
                          usub=False) -> PyTexpr1:
     assert not usub
     return PyTexpr1.var(environment, PyVar(stmt.variable.name))
Ejemplo n.º 7
0
 def visit_Subscription(self,
                        expr: 'Subscription',
                        environment=None,
                        usub=False):
     assert not usub
     return PyTexpr1.var(environment, PyVar(expr.target.name))
Ejemplo n.º 8
0
 def visit_Slicing(self, expr: 'Slicing', environment=None, usub=False):
     assert not usub
     return PyTexpr1.var(environment, PyVar(expr.target.name))