コード例 #1
0
 def simple_ite_optim(self):
     # Create random input and output data
     #xL = torch.ones((1, 2), dtype=torch.float16, requires_grad=True)
     #xH = torch.ones((1, 2), dtype=torch.float16, requires_grad=True)
     xL = torch.tensor([-10.0])
     xH = torch.tensor([1.0])
     var_map = {'x': 0, 'c': 1}
     # x in [-10, 1.0]
     # c approaches 0 but c < 0
     program = StatementBlock([
         # if c <= 0
         IfThenElse(
             IntervalBool(torch.tensor([0.0, 1.0]), torch.tensor([0.0,
                                                                  0.0])),
             # x = 2x + c
             AssignStatement(torch.tensor([[2.0, 1.0], [0.0, 1.0]]),
                             torch.tensor([0.0, 0.0])),
             # x = 700
             AssignStatement(torch.tensor([[0.0, 0.0], [0.0, 1.0]]),
                             torch.tensor([700.0, 0.0]))),
         # return x
         ReturnStatement(torch.tensor([1.0, 0.0]), torch.tensor([0.0, 0.0]))
     ])
     result_c = optimize(xL, xH, [InferParameter(var_map['c'])], program)[0]
     self.assertTrue(result_c >= 0.0)
     self.assertTrue(result_c <= 0.25)
コード例 #2
0
 def test_ite_bad_ex(self):
     val = AbsInterval(torch.tensor([-10.0, 0.0]),
                       torch.tensor([1.0, -0.0001]))
     program = StatementBlock([
         IfThenElse(
             IntervalBool(torch.tensor([0.0, 1.0]), torch.tensor([0.0,
                                                                  0.0])),
             AssignStatement(torch.tensor([[2.0, 1.0], [0.0, 1.0]]),
                             torch.tensor([0.0, 0.0])),
             AssignStatement(torch.tensor([[0.0, 0.0], [0.0, 1.0]]),
                             torch.tensor([700.0, 0.0]))),
         ReturnStatement(torch.tensor([1.0, 0.0]), torch.tensor([0.0, 0.0]))
     ])
コード例 #3
0
 def test_ite_alwaystrue(self):
     # x in [-1, 5]
     x = AbsInterval(torch.tensor([-1.0]), torch.tensor([5.0]), 1.0)
     # if 0x + 3 >= 0
     # then x = 2x
     # else x = -5
     program = IfThenElse(
         IntervalBool(torch.tensor([0.0]), torch.tensor([3.0])),
         AssignStatement(torch.tensor([[2.0]]), torch.tensor([0.0])),
         AssignStatement(torch.tensor([[0.0]]), torch.tensor([-5.0])))
     # x in [-2, 10]
     self.assertEqual(
         program.propagate(x, t_os),
         AbsInterval(torch.tensor([-2.0]), torch.tensor([10.0]), 1.0))
コード例 #4
0
 def test_simple_ite(self):
     # x in [-1, 5]
     x = AbsInterval(torch.tensor([-1.0]), torch.tensor([5.0]), 1.0)
     # if x > c
     # then x = 2x
     # else x = -0.5
     program = IfThenElse(
         IntervalBool(torch.tensor([1.0]), torch.tensor([0.0])),
         AssignStatement(torch.tensor([[2.0]]), torch.tensor([0.0])),
         AssignStatement(torch.tensor([[0.0]]), torch.tensor([-0.5])))
     # x in [-0.5, 10]
     self.assertEqual(
         program.propagate(x, t_os),
         AbsInterval(torch.tensor([-0.5]), torch.tensor([10.0]), 1.0))
コード例 #5
0
 def test_ite_alwaysfalse(self):
     # x in [-5, 5]
     x = AbsInterval(torch.tensor([-5.0]), torch.tensor([5.0]), 1.0)
     # if 2x - 11 >= 0
     # then x = 2x
     # else x = -5
     program = IfThenElse(
         IntervalBool(torch.tensor([2.0]), torch.tensor([-11.0])),
         AssignStatement(torch.tensor([[2.0]]), torch.tensor([0.0])),
         AssignStatement(torch.tensor([[0.0]]), torch.tensor([-5.0])))
     # x in [-5, -5]
     self.assertEqual(
         program.propagate(x, t_os),
         AbsInterval(torch.tensor([-5.0]), torch.tensor([-5.0]), 1.0))
     # Discontinuity exhibit 1: as input bound increases by 1, output jumps by 17
     # x in [-5, 6]
     x = AbsInterval(torch.tensor([-5.0]), torch.tensor([6.0]), 1.0)
     # x in [-5, 12]
     self.assertEqual(
         program.propagate(x, t_os),
         AbsInterval(torch.tensor([-5.0]), torch.tensor([12.0]), 1.0))
コード例 #6
0
def basic_example():
    # Create random input and output data
    #xL = torch.ones((1, 2), dtype=torch.float16, requires_grad=True)
    #xH = torch.ones((1, 2), dtype=torch.float16, requires_grad=True)
    xL = torch.tensor([float(-10.0)])
    xH = torch.tensor([float(1.0)])
    var_list = ['x', 'c']
    diag = torch.diag(torch.ones(len(var_list)))
    var_map = {v: k for (k, v) in enumerate(var_list)}
    var_b = {v: diag[k] for (k, v) in enumerate(var_list)}
    # x in [-10, 1.0]
    # c approaches 0 but c < 0
    program = StatementBlock([
        # if c >= 0
        IfThenElse(
            IntervalBool(var_b['c'], 0.0),
            # x = 2x + c
            AssignStatement(
                var_map['x'],
                ConstExpr(2.0) * VarExpr(var_map['x']) +
                VarExpr(var_map['c'])),
            # x = 700
            AssignStatement(var_map['x'], ConstExpr(700.0))),
        # return x
        ReturnStatement(VarExpr(var_map['x']))
    ])

    #program = Program([var_map["lin"], var_map["ltarget"]],
    #                  StatementBlock([
    #                          InferAssignStatement(0, )
    #                          ])))
    #repl(program)
    print("Arguments are found to be: ")
    res = optimizeNM(xL, xH, program, 1)
    print("Result:")
    print(res)
コード例 #7
0
    def test_simple_ite_full(self):
        x1 = AbsInterval(torch.tensor([-10.0, 0.5]), torch.tensor([1.0, 0.5]),
                         1.0)
        x2 = AbsInterval(torch.tensor([-10.0, -0.5]),
                         torch.tensor([1.0, -0.5]), 1.0)

        program = StatementBlock([
            IfThenElse(
                IntervalBool(torch.tensor([0.0, 1.0]), torch.tensor([0.0,
                                                                     0.0])),
                AssignStatement(torch.tensor([[2.0, -1.0], [0.0, 1.0]]),
                                torch.tensor([0.0, 0.0])),
                AssignStatement(torch.tensor([[0.0, 0.0], [0.0, 1.0]]),
                                torch.tensor([700.0, 0.0]))),
            ReturnStatement(torch.tensor([1.0, 0.0]), torch.tensor([0.0, 0.0]))
        ])
        self.assertEqual(
            program.propagate(x1, t_os),
            AbsInterval(torch.tensor([-20.5, 0.5]), torch.tensor([1.5, 0.5]),
                        1.0))
        self.assertEqual(
            program.propagate(x2, t_os),
            AbsInterval(torch.tensor([700.0, -0.5]),
                        torch.tensor([700.0, -0.5]), 1.0))
コード例 #8
0
def therm_example():
    num_infer = 7
    num_input = 2
    var_list = ['lin', 'ltarget', "i", "isOn", "K", "curL", 'h', 'tOn', 'tOff']
    xL = torch.ones(num_input) * -1.0
    xH = torch.ones(num_input) * 10.0
    diag = torch.diag(torch.ones(len(var_list)))
    var_map = {v: k for (k, v) in enumerate(var_list)}
    var_b = {v: diag[k] for (k, v) in enumerate(var_list)}
    print(var_map)
    program = StatementBlock([
        # isOn = 0.0;
        AssignStatement(var_map['isOn'], ConstExpr(0.0)),
        # K = 0.1;
        AssignStatement(var_map['K'], ConstExpr(0.1)),
        # curL = lin;
        AssignStatement(var_map['curL'], VarExpr(var_map['lin'])),
        # assert(tOn < tOff) => assert(tOn - tOff <= 0) => assert(tOff - tOn >= 0)
        AssertStatement(IntervalBool(var_b['tOff'] - var_b['tOn'], 0)),
        # assert(h >= 0)
        AssertStatement(IntervalBool(var_b['h'], 0)),
        # assert(h < 20) => assert(-h + 20 >= 0)
        AssertStatement(IntervalBool(-var_b['h'], 20.0)),
        # if isOn >= 0.5 ==> isOn - 0.5 >= 0
        IfThenElse(
            IntervalBool(var_b['isOn'], -0.5),
            StatementBlock([
                # curL = curL + h - K * (curL - lin)
                AssignStatement(
                    var_map['curL'],
                    VarExpr(var_map['curL']) + VarExpr(var_map['h']) -
                    VarExpr(var_map['K']) *
                    (VarExpr(var_map['curL']) - VarExpr(var_map['lin']))),
                # if (curL - tOff >= 0.0)
                IfThenElse(
                    IntervalBool(var_b['curL'] - var_b['tOff'], 0.0),
                    # isOn = 0.0
                    AssignStatement(var_map['isOn'], ConstExpr(0.0)),
                    NoOp())
            ]),
            StatementBlock([
                # curL = curL - K * (curL - lin)
                AssignStatement(
                    var_map['curL'],
                    VarExpr(var_map['curL']) - VarExpr(var_map['K']) *
                    (VarExpr(var_map['curL']) - VarExpr(var_map['lin']))),
                # if (-curL + tOn >= 0)
                IfThenElse(
                    IntervalBool(-var_b['curL'] + var_b['tOn'], 0.0),
                    # isOn = 1.0
                    AssignStatement(var_map['isOn'], ConstExpr(1.0)),
                    NoOp())
            ])),
        # assert (-curL + 120 > 0)
        AssertStatement(IntervalBool(-var_b['curL'], 120.0)),
        # return curL - ltarget
        ReturnStatement(
            VarExpr(var_map['curL']) - VarExpr(var_map['ltarget']))
    ])
    #infer = torch.tensor([5.0, -5.0, 5.0, 0.0, 0.1, 1.0])
    #print("xL: %s, xH: %s, infer: %s" % (str(xL.size()), str(xH.size()), str(infer.size())))
    print("Arguments are found to be: ")
    print(optimizeNM(xL, xH, program, num_infer))