1、安装CVX包
用pip安装cvxopt-1.2.1-cp36-cp36m-win_amd64.whl和cvxpy-1.0.9-cp36-cp36m-win_amd64.whl
因为我是python3.6所以是cp36
下载地址:
cvxpy:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#cvxpy
cvxopt:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#cvxopt
参考:https://blog.csdn.net/YEN_CSDN/article/details/80658452
https://blog.csdn.net/zhangjie19930729/article/details/54022924
2、遇到问题:
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
网址上不去,找到了这个:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
下载ecos:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#ecos
验证是否安装成功,可以用一个例子:
from cvxpy import * import cvxpy as cvx ''' problem : minimize (x-y)^2 subject to x+y=1 x-y>=1 ''' from cvxpy import * # Create two scalar optimization variables. # 在CVXPY中变量有标量(只有数值大小),向量,矩阵。 # 在CVXPY中有常量(见下文的Parameter) x = Variable() #定义变量x,定义变量y。两个都是标量 y = Variable() # Create two constraints. #定义两个约束式 constraints = [x + y == 1, x - y >= 1] #优化的目标函数 obj = Minimize(square(x - y)) #把目标函数与约束传进Problem函数中 prob = Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) #最优值 print("optimal var", x.value, y.value) #x与y的解 # Replace the objective. 不同的目标函数,相同的约束 prob2 = cvx.Problem(cvx.Maximize(x + y), prob.constraints) print("optimal p1 value", prob2.solve()) # Replace the constraint (x + y == 1).#不同的约束,相同的目标函数 constraints = [x + y <= 3] + prob.constraints[1:] #注意:此处是列表相加,prob.constraints[1:]取prob的约束集中 #从第二个开始的所有约束。 prob2 = cvx.Problem(prob.objective, constraints) print("optimal P2 value", prob2.solve())