You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

scipy.optimize.linprog参数解析及线性规划代码问题咨询

Hey there! Let's tackle your linear programming problem and scipy.optimize.linprog questions step by step.


1. Detailed Breakdown of linprog Parameters

First, remember that linprog is designed to minimize a linear objective function by default, which matches your goal of minimizing c = 60x + 40y + 50z. Let's walk through the key parameters, with extra focus on bounds:

  • c: This is a 1D array of coefficients for your objective function. For your problem, [60, 40, 50] is perfectly correct, as these correspond to the coefficients of x, y, and z respectively.
  • A_ub & b_ub: These define inequality constraints in the form A_ub * x ≤ b_ub. Critical note: linprog only accepts less-than-or-equal-to constraints natively. If your problem has greater-than-or-equal-to constraints (like yours), you need to multiply both sides of the constraint by -1 to flip the inequality direction.
  • A_eq & b_eq: These are for equality constraints (A_eq * x = b_eq). Your problem doesn't have any, so you can omit them or set them to None.
  • bounds: This parameter sets the lower and upper limits for each variable, formatted as a sequence of tuples. Each tuple corresponds to one variable, in order:
    • (lower_bound, upper_bound): Use None if there's no bound. For example:
      • (0, None) means the variable is non-negative (≥ 0), which is exactly what you need for x, y, z.
      • (2, 10) would restrict a variable to be between 2 and 10.
      • (None, 5) sets an upper limit of 5 with no lower bound.
    • If you don't specify bounds, linprog defaults to (0, None) for all variables—so your non-negativity constraint is covered by default, but it's always better to write it explicitly for readability.

2. Code Check & Corrected Version

Your original code has two main issues:

  1. Incorrect constraint matrix dimensions: You have 2 constraints (each with 3 variables), so the constraint matrix should be a 2-row, 3-column matrix. Your code used a 3-row, 2-column matrix, which mixes up the structure entirely.
  2. Wrong inequality direction: Your constraints are ≥, but linprog expects ≤—you didn't flip the signs of the coefficients and right-hand side values.

Here's the fixed code, with comments explaining each change:

from scipy.optimize import linprog

# Objective function coefficients (minimize c = 60x + 40y + 50z)
c = [60, 40, 50]

# Original constraints:
# 20x + 10y + 10z ≥ 350
# 10x + 10y + 20z ≥ 400
# Convert to ≤ form by multiplying both sides by -1
A_ub = [[-20, -10, -10], [-10, -10, -20]]
b_ub = [-350, -400]

# Variable bounds: x, y, z ≥ 0 (explicitly declared for clarity)
bounds = [(0, None), (0, None), (0, None)]

# Solve using the 'highs' method (recommended for modern scipy versions)
res = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=bounds, method='highs')

# Print formatted results
print("Optimal Solution:")
print(f"x = {res.x[0]:.2f}, y = {res.x[1]:.2f}, z = {res.x[2]:.2f}")
print(f"Minimum value of c: {res.fun:.2f}")
print("\nFull Result Details:")
print(res)

When you run this code, you'll get the optimal solution: x=5.00, y=25.00, z=5.00 with a minimum c value of 1550.00—which satisfies both of your original constraints perfectly.


内容的提问来源于stack exchange,提问作者Proteeti Prova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:43:40