In [4]: A
Out[4]:
array([[ 1, 16, 18],
[ 2, 17, 19],
[ 3, 18, 20],
[ 4, 19, 21],
[ 5, 20, 22],
[ 6, 21, 23],
[ 7, 22, 24],
[ 8, 23, 25],
[ 9, 24, 26]])
In [5]: import cvxpy
In [6]: m = cvxpy.Variable(A.shape, boolean=True)
In [7]: prob = cvxpy.Problem(cvxpy.Minimize(0), [cvxpy.sum(m[:, 0]) == 3, cvxpy.sum(m[:, 1]) == 1, cvxpy.sum(m[:, 2]) == 3, cvxpy.sum(cvxpy.multiply(A, m)) == 108])
In [8]: prob.solve(solver=cvxpy.MOSEK)
Out[8]: 0.0
In [9]: print(m.value.astype(int))
[[0 0 1]
[1 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[1 0 0]
[1 0 0]
[0 0 1]
[0 1 1]]
这要是面试题的话这么做肯定过不了,就想算个数的话可以凑合下
【 在 jahwa 的大作中提到: 】
: 如图:在excel里面遇到一个规划求解问题,有三组数据,从
: 在数组1选取3个值,数组2选取1个值,数组3选取3个值规划求解得出和值为108,excel
: 的规划求解功能比较单一,首先只能求一个解,其次只能从一个数组中求解,三个数组就没有办法了,
: ...................
--
FROM 68.197.87.*