Beispiel #1
0
    def setUp(self):

        self.c = CConstraintL1(center=1, radius=1)
        self.c_array = CConstraintL1(center=CArray([1, 1]), radius=1)

        # create a point that lies inside the constraint
        self.p1_inside = CArray([1., 1.])
        # create a point that lies outside the constraint
        self.p2_outside = CArray([2., 2.])
        # create a point that lies on the constraint
        self.p3_on = CArray([0., 1.])
Beispiel #2
0
    def test_subgradient(self):
        """Check if the subgradient is computed correctly

        Subgradient should lie in the cone made up by the subgradients.

        """
        c = CConstraintL1(center=0, radius=1)

        x0 = CArray([0, 1])

        p_min = CArray([1, 1])
        p_max = CArray([-1, 1])

        gradient = c.gradient(x0)

        # normalize the points
        norm_center = x0 / x0.norm(2)
        norm_p_min = p_min / p_min.norm(2)
        norm_p_max = p_max / p_max.norm(2)
        norm_gradient = gradient / gradient.norm(2)

        angl1 = round(acos(norm_center.dot(norm_gradient)), 5)
        angl2 = round(acos(norm_p_min.dot(norm_p_max)) / 2.0, 5)

        self.logger.info("Subgrad in {:} is:\n{:}".format(x0, gradient))

        self.assertLessEqual(angl1, angl2, "Subgrad is not inside the cone of "
                                           "{:} and {:}".format(p_min, p_max))
Beispiel #3
0
    def _constr(evas, c):
        """Return the distance constraint depending on the used distance.

        Parameters
        ----------
        evas : CAttackEvasion

        Returns
        -------
        CConstraintL1 or CConstraintL2

        """
        # TODO: there is no way to cleanly extract it from evasion object
        if evas.distance is "l1":
            constr = CConstraintL1(center=c, radius=evas.dmax)
        else:
            constr = CConstraintL2(center=c, radius=evas.dmax)
        return constr
Beispiel #4
0
    def test_minimize_beale(self):
        """Test for COptimizer.minimize() method on 3h-camel fun.
        This function tests the optimization in discrete space,
        with a floating eta (l1 constraint) and an integer starting point.
        The solution expected by this test is a float vector.
        """
        opt_params = {
            'eta': 1e-6,
            'eta_min': 1e-4,
            'eps': 1e-12,
            'constr': CConstraintL1(center=CArray([2, 0]), radius=2),
            'bounds': CConstraintBox(lb=0, ub=4)
        }

        self._test_minimize(COptimizerPGDLS,
                            'beale',
                            opt_params=opt_params,
                            label='discrete-l1')
Beispiel #5
0
    def test_minimize_3h_camel_l1(self):
        """Test for COptimizer.minimize() method on 3h-camel fun.
        This function tests the optimization in discrete space,
        with a floating eta (l1 constraint) and an integer starting point.
        The solution  expected by this test is a float vector.
        """
        opt_params = {
            'eta': 0.5,
            'eta_min': 0.5,
            'eps': 1e-12,
            'constr': CConstraintL1(radius=2),
            'bounds': CConstraintBox(lb=-1, ub=1)
        }

        self._test_minimize(COptimizerPGDLS,
                            '3h-camel',
                            opt_params=opt_params,
                            label='discrete-l1')
Beispiel #6
0
    def test_minimize_quad100d_l1_sparse(self):
        """Test for COptimizer.minimize() method on a quadratic function in
        a 100-dimensional space.
        This function tests the optimization in discrete space, with an
        integer eta (l1 constraint), an integer sparse starting point
        with box constraint.
        The solution expected by this test is an integer sparse vector.
        """
        opt_params = {
            'eta': 1,
            'eta_min': 1,
            'eps': 1e-12,
            'constr': CConstraintL1(radius=100),
            'bounds': CConstraintBox(lb=-2, ub=3)
        }

        self._test_minimize(COptimizerPGDLS,
                            'quad-100-sparse',
                            opt_params=opt_params,
                            label='quad-100-sparse-discrete-bounded-l1',
                            out_int=True)