我试图解决混合问题的整数版本。我想要最大化的线性目标,我有几个线性约束。守则是:
# we'll need both cvxpy and numpy
import cvxpy as cp
import numpy as np
N = 5 # the number of products
M = 5 # the number of materials
# material availability of each item
material_bounds = np.random.uniform(50, 80, size=M)
# value of each product
v = cp.Constant(np.random.uniform(1, 15, size=N))
# material needed for each item
materials_needed = np.random.uniform(5, 10, size=(M,N))
# define the x vector this time it is integer
x = cp.Variable(N, integer=True)
# define the constraint
constraints = []
for i in range(M):
constraints.append(
cp.Constant(materials_needed[i]) @ x <= cp.Constant(material_bounds[i]))
# define the target function
target = v @ x
# define the problem
mix_problem = cp.Problem(cp.Maximize(target), constraints)
print(mix_problem)
# solve the problem.
mix_problem.solve(verbose=True)
print("Solution:", x.value)
print("Total value:", v @ x.value)
print("Total weight:", materials_needed @ x.value)
在打印问题时,它是按照预期的方式制定的。但求解者的输出是:
===============================================================================
CVXPY
v1.2.2
===============================================================================
(CVXPY) Nov 22 08:51:07 AM: Your problem has 5 variables, 5 constraints, and 0 parameters.
(CVXPY) Nov 22 08:51:07 AM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Nov 22 08:51:07 AM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Nov 22 08:51:07 AM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Nov 22 08:51:07 AM: Compiling problem (target solver=GLPK_MI).
(CVXPY) Nov 22 08:51:07 AM: Reduction chain: FlipObjective -> Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> GLPK_MI
(CVXPY) Nov 22 08:51:07 AM: Applying reduction FlipObjective
(CVXPY) Nov 22 08:51:07 AM: Applying reduction Dcp2Cone
(CVXPY) Nov 22 08:51:07 AM: Applying reduction CvxAttr2Constr
(CVXPY) Nov 22 08:51:07 AM: Applying reduction ConeMatrixStuffing
(CVXPY) Nov 22 08:51:07 AM: Applying reduction GLPK_MI
(CVXPY) Nov 22 08:51:07 AM: Finished problem compilation (took 1.960e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Nov 22 08:51:07 AM: Invoking solver GLPK_MI to obtain a solution.
* 0: obj = 0.000000000e+00 inf = 0.000e+00 (5)
* 1: obj = -7.818018602e+01 inf = 0.000e+00 (4)
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Nov 22 08:51:07 AM: Problem status: unbounded
(CVXPY) Nov 22 08:51:07 AM: Optimal value: inf
(CVXPY) Nov 22 08:51:07 AM: Compilation took 1.960e-02 seconds
(CVXPY) Nov 22 08:51:07 AM: Solver (including time spent in interface) took 3.681e-04 seconds
Solution: None
我不明白为什么问题是无限的,因为我有<=约束。有人能帮我吗?
cvxpy版本: 1.2.2 版本: 3.8
我读过cvxpy文档,但它没有太大帮助。我试图改变我建立约束的方式。最初它是materials_needed @ x <= material_bounds
,但是到目前为止,我看到的所有例子都有一个列表,其中包含了几个缩略语,而不是使用矩阵形式。
发布于 2022-11-23 09:15:37
感谢Michal Adamaszek和AirSquid的评论,我找到了一个解决方案。
我还不明白为什么这是必要的,但我添加了限制x >= 0
,以明确地强制解决方案是非负的。这是代码:
import cvxpy as cp
import numpy as np
N = 5 # the number of products
M = 5 # the number of materials
# material availability of each item
material_bounds = np.random.uniform(50, 80, size=M)
# value of each product
v = cp.Constant(np.random.uniform(1, 15, size=N))
# material needed for each item
materials_needed = np.random.uniform(5, 10, size=(M,N))
# define the x vector this time it is integer
x = cp.Variable(N, integer=True)
# define the constraint
constraints = [
materials_needed @ x <= material_bounds,
x >= 0 # additional non-negativity constraint
]
# define the target function
target = v @ x
# define the problem
mix_problem = cp.Problem(cp.Maximize(target), constraints)
# solve the problem.
mix_problem.solve()
print("Solution:", x.value)
print("Total value:", v.value @ x.value)
print("Materials used:", materials_needed @ x.value)
我还修改了约束,以使用矩阵形式,这是更优雅的海事组织。
我认为原来的问题已经解决了,但我仍然想知道这个约束是什么必要的,因为我是最大化的,解决方案应该永远是非负的。
https://stackoverflow.com/questions/74530049
复制相似问题