from scipy.optimize import linprog
# 目标函数可以自定义,这里假设最大化 x + y
c = [-1, -1] # 负号表示最大化
# 不等式约束 Ax <= b
A = [[4, -1], # 4x - y ≤ 18
[2, 2], # 2x + 2y ≤ 19
[1, 0]] # x ≤ 6
b = [18, 19, 6]
# x, y >= 0
x_bounds = (0, None)
y_bounds = (0, None)
# 求解
result = linprog(c, A_ub=A, b_ub=b, bounds=[x_bounds, y_bounds])
print("最佳解:", result.x)
print("最大值:", -result.fun)
# 目标函数可以自定义,这里假设最大化 x + y
c = [-1, -1] # 负号表示最大化
# 不等式约束 Ax <= b
A = [[4, -1], # 4x - y ≤ 18
[2, 2], # 2x + 2y ≤ 19
[1, 0]] # x ≤ 6
b = [18, 19, 6]
# x, y >= 0
x_bounds = (0, None)
y_bounds = (0, None)
# 求解
result = linprog(c, A_ub=A, b_ub=b, bounds=[x_bounds, y_bounds])
print("最佳解:", result.x)
print("最大值:", -result.fun)