Esempio n. 1
0
 def check_complementarity(self, places):
     # TODO: once dual variables are stored for attributes
     #   (e.g. X = Variable(shape=(n,n), PSD=True)), check
     #   complementarity against the dual variable of the
     #   attribute constraint.
     for con in self.constraints:
         if isinstance(con, cp.constraints.PSD):
             dv = con.dual_value
             pv = con.args[0].value
             comp = cp.scalar_product(pv, dv).value
         elif isinstance(con, (cp.constraints.ExpCone, cp.constraints.SOC,
                               cp.constraints.NonPos, cp.constraints.Zero)):
             comp = cp.scalar_product(con.args, con.dual_value).value
         elif isinstance(con, cp.constraints.PowCone3D):
             comp = cp.scalar_product(con.args[:3], con.dual_value).value
         elif isinstance(
                 con, (cp.constraints.Inequality, cp.constraints.Equality)):
             comp = cp.scalar_product(con.expr, con.dual_value).value
         elif isinstance(con, cp.constraints.PowConeND):
             msg = '\nPowConeND dual variables not implemented;' \
                    + '\nSkipping complementarity check.'
             warnings.warn(msg)
         else:
             raise ValueError('Unknown constraint type %s.' % type(con))
         self.tester.assertAlmostEqual(comp, 0, places)
Esempio n. 2
0
    def test_scalar_product(self) -> None:
        """Test scalar product.
        """
        p = np.ones((4, ))
        v = cp.Variable((4, ))

        p = np.ones((4, ))
        obj = cp.Minimize(cp.scalar_product(v, p))
        prob = cp.Problem(obj, [v >= 1])
        prob.solve(solver=cp.SCS)
        assert np.allclose(v.value, p)

        # With a parameter.
        p = cp.Parameter((4, ))
        v = cp.Variable((4, ))

        p.value = np.ones((4, ))
        obj = cp.Minimize(cp.scalar_product(v, p))
        prob = cp.Problem(obj, [v >= 1])
        prob.solve(solver=cp.SCS)
        assert np.allclose(v.value, p.value)