- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我希望学生在作业中解决二次程序,而不必安装 cvxopt 等额外软件。是否有仅依赖于 NumPy/SciPy 的 python 实现?
最佳答案
我对二次规划不是很熟悉,但我认为你可以使用 scipy.optimize
的约束最小化算法来解决这类问题。这是一个例子:
import numpy as np
from scipy import optimize
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
# minimize
# F = x[1]^2 + 4x[2]^2 -32x[2] + 64
# subject to:
# x[1] + x[2] <= 7
# -x[1] + 2x[2] <= 4
# x[1] >= 0
# x[2] >= 0
# x[2] <= 4
# in matrix notation:
# F = (1/2)*x.T*H*x + c*x + c0
# subject to:
# Ax <= b
# where:
# H = [[2, 0],
# [0, 8]]
# c = [0, -32]
# c0 = 64
# A = [[ 1, 1],
# [-1, 2],
# [-1, 0],
# [0, -1],
# [0, 1]]
# b = [7,4,0,0,4]
H = np.array([[2., 0.],
[0., 8.]])
c = np.array([0, -32])
c0 = 64
A = np.array([[ 1., 1.],
[-1., 2.],
[-1., 0.],
[0., -1.],
[0., 1.]])
b = np.array([7., 4., 0., 0., 4.])
x0 = np.random.randn(2)
def loss(x, sign=1.):
return sign * (0.5 * np.dot(x.T, np.dot(H, x))+ np.dot(c, x) + c0)
def jac(x, sign=1.):
return sign * (np.dot(x.T, H) + c)
cons = {'type':'ineq',
'fun':lambda x: b - np.dot(A,x),
'jac':lambda x: -A}
opt = {'disp':False}
def solve():
res_cons = optimize.minimize(loss, x0, jac=jac,constraints=cons,
method='SLSQP', options=opt)
res_uncons = optimize.minimize(loss, x0, jac=jac, method='SLSQP',
options=opt)
print '\nConstrained:'
print res_cons
print '\nUnconstrained:'
print res_uncons
x1, x2 = res_cons['x']
f = res_cons['fun']
x1_unc, x2_unc = res_uncons['x']
f_unc = res_uncons['fun']
# plotting
xgrid = np.mgrid[-2:4:0.1, 1.5:5.5:0.1]
xvec = xgrid.reshape(2, -1).T
F = np.vstack([loss(xi) for xi in xvec]).reshape(xgrid.shape[1:])
ax = plt.axes(projection='3d')
ax.hold(True)
ax.plot_surface(xgrid[0], xgrid[1], F, rstride=1, cstride=1,
cmap=plt.cm.jet, shade=True, alpha=0.9, linewidth=0)
ax.plot3D([x1], [x2], [f], 'og', mec='w', label='Constrained minimum')
ax.plot3D([x1_unc], [x2_unc], [f_unc], 'oy', mec='w',
label='Unconstrained minimum')
ax.legend(fancybox=True, numpoints=1)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('F')
输出:
Constrained:
status: 0
success: True
njev: 4
nfev: 4
fun: 7.9999999999997584
x: array([ 2., 3.])
message: 'Optimization terminated successfully.'
jac: array([ 4., -8., 0.])
nit: 4
Unconstrained:
status: 0
success: True
njev: 3
nfev: 5
fun: 0.0
x: array([ -2.66453526e-15, 4.00000000e+00])
message: 'Optimization terminated successfully.'
jac: array([ -5.32907052e-15, -3.55271368e-15, 0.00000000e+00])
nit: 3
关于python - 仅依赖于 NumPy/SciPy 的二次规划 (QP) 求解器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17009774/
我正在创建我的第一个 WAR 文件。我一直在试验 ant buildfile 语法,我的 buildfile 的第一部分从我的 Eclipse 项目中获取内容并将其放入 /dist 文件夹中,然后将其
我是一名学习 SQL 和 PHP 的学生,我接到了一项任务,要使用 PHP 和 mySQLi 创建学生反馈表,我真的一直在思考如何为项目设计数据库! 我正在创建一个系统,用户可以在其中登录网页,如果用
这个问题在这里已经有了答案: Is it possbile to test for expected errors when the testee exits with failure using
我目前正在设计和开发一个 Web 应用程序,该应用程序有可能快速增长。我将提供一些一般信息,然后继续我的问题。我会说我是一名中级网络程序员。 以下是一些规范:MySQL - 数据库后端PHP - 用于
我不知何故无法在我的日志解析器应用程序中实现报告功能。 这是我目前所做的: 我正在编写一个应用程序,它读取日志文件并在字符串中搜索可以在用户配置文件中定义的多个正则表达式。对于从配置中解析的每个所谓的
我有兴趣学习如何在多开发团队场景中设计/规划 Web 应用程序开发。 假设“项目经理/负责人”的角色: 成功的 Web 应用程序开发需要哪些“文档”? 需要什么 UML 图,需要什么程度? 在设计/计
table a (t_a): id name last first email state country 0 sklass klass steve
我们建立了一个广泛使用 JQuery UI 的 AJAX 网站。我们有 30 多个自制的 JQuery UI 小部件(动态加载)。我们到处都使用 JQuery native 小部件:对话框、 slid
我是一名优秀的程序员,十分优秀!