令和与。定义,并假设它是非空的。
题
- (A)如何制定和解决?
- (B)与和
相关: https ://math.stackexchange.com/q/3080805/168758
免责声明:我以前从未做过 cvxopt / cvxpy。我计划稍后学习语法。现在,我只想要即插即用的东西。谢谢!
令和与。定义,并假设它是非空的。
相关: https ://math.stackexchange.com/q/3080805/168758
免责声明:我以前从未做过 cvxopt / cvxpy。我计划稍后学习语法。现在,我只想要即插即用的东西。谢谢!
您可以像这样将问题 A 解决为二阶锥程序:
#!/usr/bin/env python3
import cvxpy as cp
import numpy as np
##########################
#Question A
##########################
n = 50 #Arbitrary number of dimensions
r = 10 #Arbitrary radius
e = 3 #Epsilon value
a = np.random.random(size=50) #Generate random a vector
a = a/np.linalg.norm(a, ord=2) #Scale to unit length
a = r*a #Scale to radius
x = cp.Variable(shape=n) #x, a variable to be optimized
cons = [] #List of constraints
#See cvxpy's atoms here: https://www.cvxpy.org/api_reference/cvxpy.atoms.html
cons += [cp.norm(x+a,2)<=r] #Note that we are using cvxpy's `norm` function!
cons += [cp.norm_inf(x)<=e]
#Objective
obj = cp.Maximize(cp.sum(a*x))
#Formulate problem
prob = cp.Problem(obj, cons)
#Solve problem
optval = prob.solve()
print("Optimum value = {0}".format(optval))
print("x = {0}".format(x.value))
问题 B 您无法使用 cvxpy 即插即用解决问题,因为问题是非凸的(您正在对椭球表面进行优化)。