python:
使用spyder安装所需库,包时需要在控制台输入命令
!pip install xxxx
但是有可能源不太好,导致安装失败
因此最好去官网下载,然后将下载包放置到工作空间,使用!pip install xxx文件名
下面以cvxpy凸优化包为例
下载对应版本包
将包移至工作空间,使用在控制台输入命令!pip install cvxpy-1.0.10-cp36-cp36m-win_amd64.whl
ok
扫描二维码关注公众号,回复:
4619126 查看本文章
简单示例:
import cvxpy as cvx
#定义优化变量
x = cvx.Variable()
y = cvx.Variable()
# 定义约束条件
constraints = [x + y == 1,
x - y >= 1]
# 定义优化问题
obj = cvx.Minimize((x - y)**2)
# 定义优化问题
prob = cvx.Problem(obj, constraints) #求解问题
prob.solve() #返回最优值
print("status:", prob.status)#求解状态
print("optimal value", prob.value) #目标函数优化值
print("optimal var", x.value, y.value) #优化变量的值,相应变量加.value
matlab:
matlab凸优化工具箱下载处
m = 20; n = 10; p = 4;
A = randn(m,n); b = randn(m,1);
C = randn(p,n); d = randn(p,1); e = rand;
cvx_begin
variable x(n)
minimize( norm( A * x - b, 2 ) )
subject to
C * x == d
norm( x, Inf ) <= e
cvx_end
C++ :
qpOASES采用可行集策略,规模越大耗时越长
示例、
#include "include\qpOASES.hpp"
int main()
{
USING_NAMESPACE_QPOASES
/* Setup data of first QP. */
real_t H[2 * 2] = { 1.0, 0.0, 0.0, 0.5 };
real_t A[1 * 2] = { 1.0, 1.0 };
real_t g[2] = { 1.5, 1.0 };
real_t lb[2] = { 0.5, -2.0 };
real_t ub[2] = { 5.0, 2.0 };
real_t lbA[1] = { -1.0 };
real_t ubA[1] = { 2.0 };
/* Setup data of second QP. */
real_t g_new[2] = { 1.0, 1.5 };
real_t lb_new[2] = { 0.0, -1.0 };
real_t ub_new[2] = { 5.0, -0.5 };
real_t lbA_new[1] = { -2.0 };
real_t ubA_new[1] = { 1.0 };
/* Setting up QProblem object. */
QProblem example(2, 1);
/* Solve first QP. */
int_t nWSR = 10;
example.init(H, g, A, lb, ub, lbA, ubA, nWSR);
/* Solve second QP. */
nWSR = 10;
example.hotstart(g_new, lb_new, ub_new, lbA_new, ubA_new, nWSR);
/* Get and print solution of second QP. */
real_t xOpt[2];
example.getPrimalSolution(xOpt);
printf("\n xOpt = [ %e, %e ]; objVal = %e\n\n",
xOpt[0], xOpt[1], example.getObjVal());
return 0;
}
后续增加 :
由于python又方便可用的cvxpy包,是否可以用c/c++调用python的包cvxpy