当前位置: 首页>>代码示例>>Python>>正文


Python Parameter.value方法代码示例

本文整理汇总了Python中cvxpy.expressions.constants.Parameter.value方法的典型用法代码示例。如果您正苦于以下问题:Python Parameter.value方法的具体用法?Python Parameter.value怎么用?Python Parameter.value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cvxpy.expressions.constants.Parameter的用法示例。


在下文中一共展示了Parameter.value方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_parameter_expressions

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_parameter_expressions(self):
        """Test that expressions with parameters are updated properly.
        """
        x = Variable()
        y = Variable()
        x0 = Parameter()
        xSquared = x0*x0 + 2*x0*(x - x0)

        # initial guess for x
        x0.value = 2

        # make the constraint x**2 - y == 0
        g = xSquared - y

        # set up the problem
        obj = abs(x - 1)
        prob = Problem( Minimize( obj ), [ g == 0 ] )
        prob.solve()
        x0.value = 1
        prob.solve()
        self.assertAlmostEqual(g.value, 0)

        # Test multiplication.
        prob = Problem( Minimize( x0*x ), [ x == 1 ] )
        x0.value = 2
        prob.solve()
        x0.value = 1
        prob.solve()
        self.assertAlmostEqual(prob.value, 1)
开发者ID:gvanzin,项目名称:cvxpy,代码行数:31,代码来源:test_problem.py

示例2: test_parameters

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_parameters(self):
        p = Parameter(name='p')
        self.assertEqual(p.name(), "p")
        self.assertEqual(p.size, (1,1))

        p = Parameter(4, 3, sign="positive")
        with self.assertRaises(Exception) as cm:
            p.value = 1
        self.assertEqual(str(cm.exception), "Invalid dimensions (1,1) for Parameter value.")

        val = -np.ones((4,3))
        val[0,0] = 2

        p = Parameter(4, 3, sign="positive")
        with self.assertRaises(Exception) as cm:
            p.value = val
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        p = Parameter(4, 3, sign="negative")
        with self.assertRaises(Exception) as cm:         
            p.value = val
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        # No error for unknown sign.
        p = Parameter(4, 3)
        p.value = val
开发者ID:Russell91,项目名称:cvxpy,代码行数:28,代码来源:test_expressions.py

示例3: test_parameter_problems

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
 def test_parameter_problems(self):
     """Test problems with parameters.
     """
     p1 = Parameter()
     p2 = Parameter(3, sign="negative")
     p3 = Parameter(4, 4, sign="positive")
     p = Problem(Maximize(p1*self.a), [self.a + p1 <= p2, self.b <= p3 + p3 + 2])
     p1.value = 2
     p2.value = -numpy.ones((3,1))
     p3.value = numpy.ones((4, 4))
     result = p.solve()
     self.assertAlmostEqual(result, -6)
开发者ID:mirage007,项目名称:cvxpy,代码行数:14,代码来源:test_problem.py

示例4: test_presolve_parameters

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_presolve_parameters(self):
        """Test presolve with parameters.
        """
        # Test with parameters.
        gamma = Parameter(sign="positive")
        x = Variable()
        obj = Minimize(x)
        prob = Problem(obj, [gamma == 1, x >= 0])
        gamma.value = 0
        prob.solve(solver=s.SCS)
        self.assertEqual(prob.status, s.INFEASIBLE)

        gamma.value = 1
        prob.solve(solver=s.CVXOPT)
        self.assertEqual(prob.status, s.OPTIMAL)
开发者ID:gvanzin,项目名称:cvxpy,代码行数:17,代码来源:test_problem.py

示例5: test_huber

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_huber(self):
        # Valid.
        huber(self.x, 1)

        with self.assertRaises(Exception) as cm:
            huber(self.x, -1)
        self.assertEqual(str(cm.exception),
            "M must be a non-negative scalar constant.")

        with self.assertRaises(Exception) as cm:
            huber(self.x, [1,1])
        self.assertEqual(str(cm.exception),
            "M must be a non-negative scalar constant.")

        # M parameter.
        M = Parameter(sign="positive")
        # Valid.
        huber(self.x, M)
        M.value = 1
        self.assertAlmostEquals(huber(2, M).value, 3)
        # Invalid.
        M = Parameter(sign="negative")
        with self.assertRaises(Exception) as cm:
            huber(self.x, M)
        self.assertEqual(str(cm.exception),
            "M must be a non-negative scalar constant.")
开发者ID:jacklzhu,项目名称:cvxpy,代码行数:28,代码来源:test_atoms.py

示例6: test_div_expression

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_div_expression(self):
        # Vectors
        exp = self.x/2
        self.assertEqual(exp.curvature, u.Curvature.AFFINE_KEY)
        self.assertEqual(exp.sign, u.Sign.UNKNOWN_KEY)
        self.assertEqual(exp.canonical_form[0].size, (2,1))
        self.assertEqual(exp.canonical_form[1], [])
        # self.assertEqual(exp.name(), c.name() + " * " + self.x.name())
        self.assertEqual(exp.size, (2,1))

        with self.assertRaises(Exception) as cm:
            (self.x/[2,2,3])
        print cm.exception
        self.assertEqual(str(cm.exception), "Can only divide by a scalar constant.")

        # Constant expressions.
        c = Constant(2)
        exp = c/(3 - 5)
        self.assertEqual(exp.curvature, u.Curvature.CONSTANT_KEY)
        self.assertEqual(exp.size, (1,1))
        self.assertEqual(exp.sign, u.Sign.NEGATIVE_KEY)

        # Parameters.
        p = Parameter(sign="positive")
        exp = 2/p
        p.value = 2
        self.assertEquals(exp.value, 1)
开发者ID:JudsonWilson,项目名称:cvxpy,代码行数:29,代码来源:test_expressions.py

示例7: test_parameters

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_parameters(self):
        p = Parameter(name='p')
        self.assertEqual(p.name(), "p")
        self.assertEqual(p.size, (1, 1))

        p = Parameter(4, 3, sign="positive")
        with self.assertRaises(Exception) as cm:
            p.value = 1
        self.assertEqual(str(cm.exception), "Invalid dimensions (1, 1) for Parameter value.")

        val = -np.ones((4, 3))
        val[0, 0] = 2

        p = Parameter(4, 3, sign="positive")
        with self.assertRaises(Exception) as cm:
            p.value = val
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        p = Parameter(4, 3, sign="negative")
        with self.assertRaises(Exception) as cm:
            p.value = val
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        # No error for unknown sign.
        p = Parameter(4, 3)
        p.value = val

        # Initialize a parameter with a value.
        p = Parameter(value=10)
        self.assertEqual(p.value, 10)

        # Test assigning None.
        p.value = 10
        p.value = None
        assert p.value is None

        with self.assertRaises(Exception) as cm:
            p = Parameter(2, 1, sign="negative", value=[2, 1])
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        with self.assertRaises(Exception) as cm:
            p = Parameter(4, 3, sign="positive", value=[1, 2])
        self.assertEqual(str(cm.exception), "Invalid dimensions (2, 1) for Parameter value.")

        # Test repr.
        p = Parameter(4, 3, sign="negative")
        self.assertEqual(repr(p), 'Parameter(4, 3, sign="NEGATIVE")')
开发者ID:heath9,项目名称:cvxpy,代码行数:49,代码来源:test_expressions.py

示例8: test_1D_array

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
 def test_1D_array(self):
     """Test NumPy 1D arrays as constants.
     """
     c = np.array([1, 2])
     p = Parameter(2)
     p.value = [1, 1]
     self.assertEqual((c*p).value, 3)
     self.assertEqual((c*self.x).size, (1, 1))
开发者ID:heath9,项目名称:cvxpy,代码行数:10,代码来源:test_expressions.py

示例9: test_div_expression

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_div_expression(self):
        # Vectors
        exp = self.x/2
        self.assertEqual(exp.curvature, s.AFFINE)
        self.assertEqual(exp.sign, s.UNKNOWN)
        self.assertEqual(exp.canonical_form[0].size, (2, 1))
        self.assertEqual(exp.canonical_form[1], [])
        # self.assertEqual(exp.name(), c.name() + " * " + self.x.name())
        self.assertEqual(exp.size, (2, 1))

        with self.assertRaises(Exception) as cm:
            (self.x/[2, 2, 3])
        print(cm.exception)
        self.assertEqual(str(cm.exception), "Can only divide by a scalar constant.")

        # Constant expressions.
        c = Constant(2)
        exp = c/(3 - 5)
        self.assertEqual(exp.curvature, s.CONSTANT)
        self.assertEqual(exp.size, (1, 1))
        self.assertEqual(exp.sign, s.NEGATIVE)

        # Parameters.
        p = Parameter(sign="positive")
        exp = 2/p
        p.value = 2
        self.assertEqual(exp.value, 1)

        rho = Parameter(sign="positive")
        rho.value = 1

        self.assertEqual(rho.sign, s.POSITIVE)
        self.assertEqual(Constant(2).sign, s.POSITIVE)
        self.assertEqual((Constant(2)/Constant(2)).sign, s.POSITIVE)
        self.assertEqual((Constant(2)*rho).sign, s.POSITIVE)
        self.assertEqual((rho/2).sign, s.POSITIVE)
开发者ID:heath9,项目名称:cvxpy,代码行数:38,代码来源:test_expressions.py

示例10: test_partial_optimize_params

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_partial_optimize_params(self):
        """Test partial optimize with parameters.
        """
        x, y = Variable(1), Variable(1)
        gamma = Parameter()
        # Solve the (simple) two-stage problem by "combining" the two stages (i.e., by solving a single linear program)
        p1 = Problem(Minimize(x+y), [x+y>=gamma, y>=4, x>=5])
        gamma.value = 3
        p1.solve()

        # Solve the two-stage problem via partial_optimize
        p2 = Problem(Minimize(y), [x+y>=gamma, y>=4])
        g = partial_optimize(p2, [y], [x])
        p3 = Problem(Minimize(x+g), [x>=5])
        p3.solve()
        self.assertAlmostEqual(p1.value, p3.value)
开发者ID:TPNguyen,项目名称:cvxpy,代码行数:18,代码来源:test_atoms.py

示例11: test_1D_array

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_1D_array(self):
        """Test NumPy 1D arrays as constants.
        """
        c = np.array([1,2])
        p = Parameter(2)

        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger a warning.
            Constant(c)
            self.x + c
            p.value = c
            # Verify some things
            self.assertEqual(len(w), 3)
            for warning in w:
                self.assertEqual(str(warning.message), "NumPy 1D arrays are treated as column vectors.")
开发者ID:bakhtiary,项目名称:cvxpy,代码行数:19,代码来源:test_expressions.py

示例12: test_huber

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
    def test_huber(self):
        # Valid.
        huber(self.x, 1)

        with self.assertRaises(Exception) as cm:
            huber(self.x, -1)
        self.assertEqual(str(cm.exception),
            "M must be a non-negative scalar constant.")

        with self.assertRaises(Exception) as cm:
            huber(self.x, [1,1])
        self.assertEqual(str(cm.exception),
            "M must be a non-negative scalar constant.")

        # M parameter.
        M = Parameter(sign="positive")
        # Valid.
        huber(self.x, M)
        M.value = 1
        self.assertAlmostEquals(huber(2, M).value, 3)
        # Invalid.
        M = Parameter(sign="negative")
        with self.assertRaises(Exception) as cm:
            huber(self.x, M)
        self.assertEqual(str(cm.exception),
            "M must be a non-negative scalar constant.")

        # Test copy with args=None
        atom = huber(self.x, 2)
        copy = atom.copy()
        self.assertTrue(type(copy) is type(atom))
        # A new object is constructed, so copy.args == atom.args but copy.args
        # is not atom.args.
        self.assertEqual(copy.args, atom.args)
        self.assertFalse(copy.args is atom.args)
        # As get_data() returns a Constant, we have to check the value
        self.assertEqual(copy.get_data().value, atom.get_data().value)
        # Test copy with new args
        copy = atom.copy(args=[self.y])
        self.assertTrue(type(copy) is type(atom))
        self.assertTrue(copy.args[0] is self.y)
        self.assertEqual(copy.get_data().value, atom.get_data().value)
开发者ID:TPNguyen,项目名称:cvxpy,代码行数:44,代码来源:test_atoms.py

示例13: test_index

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
 def test_index(self):
     """Test the get_coefficients function for index.
     """
     size = (5, 4)
     # Eye
     key = (slice(0,2,None), slice(0,2,None))
     x = create_var(size)
     expr = index(x, (2, 2), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(id_, x.data)
     self.assertEqual(mat.shape, (4, 20))
     test_mat = np.mat(range(20)).T
     self.assertItemsAlmostEqual((mat*test_mat).reshape((2, 2), order='F'),
         test_mat.reshape(size, order='F')[key])
     # Eye with scalar mult.
     key = (slice(0,2,None), slice(0,2,None))
     x = create_var(size)
     A = create_const(5, (1, 1))
     expr = mul_expr(A, x, size)
     expr = index(expr, (2, 2), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     test_mat = np.mat(range(20)).T
     self.assertItemsAlmostEqual((mat*test_mat).reshape((2, 2), order='F'),
         5*test_mat.reshape(size, order='F')[key])
     # Promoted
     key = (slice(0,2,None), slice(0,2,None))
     x = create_var((1, 1))
     value = np.array(range(20)).reshape(size)
     A = create_const(value, size)
     prom_x = promote(x, (size[1], 1))
     expr = mul_expr(A, diag_vec(prom_x), size)
     expr = index(expr, (2, 2), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (4, 1))
     self.assertItemsAlmostEqual(mat, value[key])
     # Normal
     size = (5, 5)
     key = (slice(0,2,None), slice(0,1,None))
     x = create_var((5, 1))
     A = create_const(np.ones(size), size)
     expr = mul_expr(A, x, (5, 1))
     expr = index(expr, (2, 1), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (2, 5))
     self.assertItemsAlmostEqual(mat.todense(), A.data[slice(0,2,None)])
     # Blocks
     size = (5, 5)
     key = (slice(0,2,None), slice(0,2,None))
     x = create_var(size)
     value = np.array(range(25)).reshape(size)
     A = create_const(value, size)
     expr = mul_expr(A, x, size)
     expr = index(expr, (2, 2), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (4, 25))
     test_mat = np.mat(range(25)).T
     self.assertItemsAlmostEqual((mat*test_mat).reshape((2, 2), order='F'),
         (A.data*test_mat.reshape(size, order='F'))[key])
     # Scalar constant
     size = (1, 1)
     A = create_const(5, size)
     key = (slice(0,1,None), slice(0,1,None))
     expr = index(A, (1, 1), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(intf.size(mat), (1, 1))
     self.assertEqual(mat, 5)
     # Dense constant
     size = (5, 4)
     key = (slice(0,2,None), slice(0,1,None))
     value = np.array(range(20)).reshape(size)
     A = create_const(value, size)
     expr = index(A, (2, 1), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (2, 1))
     self.assertItemsAlmostEqual(mat, value[key])
     # Sparse constant
     size = (5, 5)
     key = (slice(0,2,None), slice(0,1,None))
     A = create_const(sp.eye(5), size)
     expr = index(A, (2, 1), key)
     coeffs = get_coefficients(expr)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (2, 1))
     self.assertItemsAlmostEqual(mat, sp.eye(5).todense()[key])
     # Parameter
#.........这里部分代码省略.........
开发者ID:Jakobularius,项目名称:cvxpy,代码行数:103,代码来源:test_lin_ops.py

示例14: test_get_coefficients

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
 def test_get_coefficients(self):
     """Test the get_coefficients function.
     """
     size = (5, 4)
     # Eye
     x = create_var(size)
     coeffs = get_coefficients(x)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(id_, x.data)
     self.assertItemsAlmostEqual(mat.todense(), sp.eye(20).todense())
     # Eye with scalar mult.
     x = create_var(size)
     A = create_const(5, (1, 1))
     coeffs = get_coefficients(mul_expr(A, x, size))
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertItemsAlmostEqual(mat.todense(), 5*sp.eye(20).todense())
     # Promoted
     x = create_var((1, 1))
     coeffs = get_coefficients(promote(x, size))
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (20, 1))
     self.assertItemsAlmostEqual(mat, np.ones((20, 1)))
     # Normal
     size = (5, 5)
     x = create_var((5, 1))
     A = create_const(np.ones(size), size)
     coeffs = get_coefficients(mul_expr(A, x, (5, 1)))
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (5, 5))
     self.assertItemsAlmostEqual(mat.todense(), A.data)
     # Blocks
     size = (5, 5)
     x = create_var(size)
     A = create_const(np.ones(size), size)
     coeffs = get_coefficients(mul_expr(A, x, size))
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (25, 25))
     self.assertItemsAlmostEqual(mat.todense(),
      sp.block_diag(5*[np.ones(size)]).todense())
     # Scalar constant
     size = (1, 1)
     A = create_const(5, size)
     coeffs = get_coefficients(A)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(intf.size(mat), (1, 1))
     self.assertEqual(mat, 5)
     # Dense constant
     size = (5, 4)
     A = create_const(np.ones(size), size)
     coeffs = get_coefficients(A)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (size[0]*size[1], 1))
     self.assertItemsAlmostEqual(mat, np.ones(size))
     # Sparse constant
     size = (5, 5)
     A = create_const(sp.eye(5), size)
     coeffs = get_coefficients(A)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (size[0]*size[1], 1))
     self.assertItemsAlmostEqual(mat, sp.eye(5).todense())
     # Parameter
     size = (5, 4)
     param = Parameter(*size)
     param.value = np.ones(size)
     A = create_param(param, size)
     coeffs = get_coefficients(A)
     assert len(coeffs) == 1
     id_, mat = coeffs[0]
     self.assertEqual(mat.shape, (size[0]*size[1], 1))
     self.assertItemsAlmostEqual(mat, param.value)
开发者ID:Jakobularius,项目名称:cvxpy,代码行数:80,代码来源:test_lin_ops.py

示例15: test_parameter_promotion

# 需要导入模块: from cvxpy.expressions.constants import Parameter [as 别名]
# 或者: from cvxpy.expressions.constants.Parameter import value [as 别名]
 def test_parameter_promotion(self):
     a = Parameter()
     exp = [[1,2],[3,4]]*a
     a.value = 2
     assert not (exp.value - 2*numpy.array([[1,2],[3,4]]).T).any()
开发者ID:lixx1746,项目名称:cvxpy,代码行数:7,代码来源:test_problem.py


注:本文中的cvxpy.expressions.constants.Parameter.value方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。