本文整理汇总了Python中cvxpy.expressions.constants.Parameter类的典型用法代码示例。如果您正苦于以下问题:Python Parameter类的具体用法?Python Parameter怎么用?Python Parameter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_div_expression
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)
示例2: test_huber
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.")
示例3: test_parameters
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
示例4: test_parameter_expressions
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)
示例5: test_1D_array
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))
示例6: test_param_copy
def test_param_copy(self):
"""Test the copy function for Parameters.
"""
x = Parameter(3, 4, name="x", sign="positive")
y = x.copy()
self.assertEqual(y.size, (3, 4))
self.assertEqual(y.name(), "x")
self.assertEqual(y.sign, "POSITIVE")
示例7: test_presolve_parameters
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)
示例8: test_partial_optimize_params
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)
示例9: test_1D_array
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.")
示例10: test_huber
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)
示例11: test_div_expression
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)
示例12: test_parameter_problems
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)
示例13: test_parameters
def test_parameters(self):
p = Parameter(name='p')
self.assertEqual(p.name(), "p")
self.assertEqual(p.size, (1,1))
示例14: test_readme_examples
def test_readme_examples(self):
import cvxopt
import numpy
# Problem data.
m = 30
n = 20
A = cvxopt.normal(m,n)
b = cvxopt.normal(m)
# Construct the problem.
x = Variable(n)
objective = Minimize(sum(square(A*x - b)))
constraints = [0 <= x, x <= 1]
p = Problem(objective, constraints)
# The optimal objective is returned by p.solve().
result = p.solve()
# The optimal value for x is stored in x.value.
print x.value
# The optimal Lagrange multiplier for a constraint
# is stored in constraint.dual_value.
print constraints[0].dual_value
####################################################
# Scalar variable.
a = Variable()
# Column vector variable of length 5.
x = Variable(5)
# Matrix variable with 4 rows and 7 columns.
A = Variable(4,7)
####################################################
# Positive scalar parameter.
m = Parameter(sign="positive")
# Column vector parameter with unknown sign (by default).
c = Parameter(5)
# Matrix parameter with negative entries.
G = Parameter(4,7,sign="negative")
# Assigns a constant value to G.
G.value = -numpy.ones((4,7))
# Raises an error for assigning a value with invalid sign.
with self.assertRaises(Exception) as cm:
G.value = numpy.ones((4,7))
self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")
####################################################
a = Variable()
x = Variable(5)
# expr is an Expression object after each assignment.
expr = 2*x
expr = expr - a
expr = sum(expr) + norm2(x)
####################################################
import numpy as np
import cvxopt
from multiprocessing import Pool
# Problem data.
n = 10
m = 5
A = cvxopt.normal(n,m)
b = cvxopt.normal(n)
gamma = Parameter(sign="positive")
# Construct the problem.
x = Variable(m)
objective = Minimize(sum(square(A*x - b)) + gamma*norm1(x))
p = Problem(objective)
# Assign a value to gamma and find the optimal x.
def get_x(gamma_value):
gamma.value = gamma_value
result = p.solve()
return x.value
gammas = np.logspace(-1, 2, num=2)
# Serial computation.
x_values = [get_x(value) for value in gammas]
####################################################
n = 10
mu = cvxopt.normal(1, n)
sigma = cvxopt.normal(n,n)
sigma = sigma.T*sigma
gamma = Parameter(sign="positive")
gamma.value = 1
x = Variable(n)
#.........这里部分代码省略.........
示例15: test_index
#.........这里部分代码省略.........
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
size = (5, 4)
key = (slice(0,2,None), slice(0,1,None))
param = Parameter(*size)
value = np.array(range(20)).reshape(size)
param.value = value
A = create_param(param, 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, param.value[key])