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+bi)_{complex}=\left( \begin{array}{1} a & -b \\ b & a \end{array} \right)_{real}$
建议用方法1.而不是此方法,但是这是一个值得学习的思路,故作记录。
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))))
posted @ 2023-12-20 10:36  工大鸣猪  阅读(23)  评论(0编辑  收藏  举报