Python应用:CVXPY解凸优化
Introduction
Content source: https://www.cvxpy.org/tutorial/index.html
Basics
import cvxpy as cvx
import numpy as np
variable
unchangeable variable
# scalar or number
scalar_var = cvx.Variable()
# vector
vector_length = 10
vector_var = cvx.Variable(vector_length)
# matrix or tenser
matrix_shape = (10, 10)
matrix_var = cvx.Variable(matrix_shape)
changeable parameter
# scalar or number
scalar_para = cvx.Parameter()
scalar.value = 2
# vector
vector_length = 10
vector_para = cvx.Parameter(vector_length)
vector.value = np.ones(vector_length)
# matrix or tenser
matrix_shape = (10, 10)
matrix_para = cvx.Parameter(matrix_shape)
matrix.value = np.random.rand(matrix_shape[0], matrix_shape[1])
objective
# example: least square problem
convex_function = cvx.sum_squares(A@x-b)
# convex optimization
convex_objective = cvx.Minimize(convex_function)
# concave optimization
concave_objective = cvx.Maximize(convex_function)
constraint
# constraint expresions
constraint_expression_a = x >= 0
constraint_expression_b = cvx.sum(x) <= 1
# constraints
constraint = [constraint_expression_a, constraint_expression_b]
solution
# definde problem
problem = cvx.Problem(objective, constraint)
# get the result
resulte = problem.solve()
norm
norm_target = np.ones((3, 4))
norm_type = 1
# the 1-norm of 3×4 all-one metrix
cvx.norm(norm_traget, norm_type)
complex
1.using parameter 'complex=True' to constraint the complex values
# just add a parameter when define the variables or parameters, e.g.
matrix_var = cvx.Variable((10, 10),complex=True)
ORIGINAL:https://www.cvxpy.org/tutorial/advanced/index.html#complex-valued-expressions
2.transforming a matrix form complex number domain to real number domain
A = np.array([[1.0 + 1.0j, 1.1 + 1.2j],
[2.0 + 2.0j, 2.2 + 2.2j]])
A_real = np.vstack((np.hstack((np.real(A), -np.imag(A))),
np.hstack((np.imag(A), np.real(A))))
本文来自博客园,作者:工大鸣猪,转载请注明原文链接:https://www.cnblogs.com/hit-ztx/p/17915968.html