Matlab's "linprog" function and Python CVXPY library offer optimal solutions for production.
It can calculate how to achieve maximum profit with limited resources, constraints, and requirements, or simulate the lowest-cost solution.
动态规划中的背包问题:
某工厂生产3种不同的产品,各产品重量与利润关系
种类 | 1 | 2 | 3 |
---|---|---|---|
单位重量(吨) | 2 | 3 | 4 |
单元利润 | 80 | 130 | 180 |
x1 = cp.Variable(integer=True)
x2 = cp.Variable(integer=True)
x3 = cp.Variable(integer=True)
objective = cp.Maximize(80 * x1 + 130 * x2 + 180 * x3)
constraints = [
2 * x1 + 3 * x2 + 4 * x3 <= 6,
x1 >= 0,
x2 >= 0,
x3 >= 0,
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最大利润" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
print("产品C:" + str(x3.value))
题目1:https://blog.csdn.net/clear_lantern/article/details/127622092
import cvxpy as cp
def print_hi(name):
x1 = cp.Variable()
x2 = cp.Variable()
x3 = cp.Variable()
x4 = cp.Variable()
objective = cp.Maximize(375 * x1 + 275 * x2 + 475 * x3 + 325 * x4)
constraints = [
2.5 * x1 + 1.5 * x2 + 2.75 * x3 + 2 * x4 <= 640,
3.5 * x1 + 3 * x2 + 3 * x3 + 2 * x4 <= 960,
x1 >= 0,
x2 >= 0,
x3 >= 0,
x4 >= 0
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最大利润" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
print("产品C:" + str(x3.value))
print("产品D:" + str(x4.value))
题目2: https://blog.csdn.net/weixin_43838785/article/details/106335542
import cvxpy as cp
def print_hi(name):
x1 = cp.Variable()
x2 = cp.Variable()
x3 = cp.Variable()
objective = cp.Maximize(3 * x1 + 5 * x2 + 4 * x3)
constraints = [
2 * x1 + 3 * x2 <= 1500,
0 * x1 + 2 * x2 + 4 * x3 <= 800,
3 * x1 + 2 * x2 + 5 * x3 <= 2000,
x1 >= 0,
x2 >= 0,
x3 >= 0,
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最大利润" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
print("产品C:" + str(x3.value))
import cvxpy as cp
def print_hi(name):
x1 = cp.Variable()
x2 = cp.Variable()
x3 = cp.Variable()
objective = cp.Minimize(2 * x1 + 3 * x2 + 1 * x3)
constraints = [
x1 + 4 * x2 + 2 * x3 >= 8,
3 * x1 + 2 * x2 >= 6,
x1 >= 0,
x2 >= 0,
x3 >= 0,
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最小消耗" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
print("产品C:" + str(x3.value))
import cvxpy as cp
def print_hi(name):
x1 = cp.Variable()
x2 = cp.Variable()
objective = cp.Maximize(2 * x1 + 3 * x2)
constraints = [
3 * x1 + 6 * x2 <= 24,
2 * x1 + 1 * x2 <= 10,
x1 >= 0,
x2 >= 0,
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最大利润" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
题目3:https://www.doc88.com/p-9962714762805.html
import cvxpy as cp
def print_hi(name):
x1 = cp.Variable()
x2 = cp.Variable()
objective = cp.Maximize(2 * x1 + 3 * x2)
constraints = [
3 * x1 + 6 * x2 <= 24,
2 * x1 + 1 * x2 <= 10,
x1 >= 0,
x2 >= 0,
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最大利润" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
题目4:https://qb.zuoyebang.com/xfe-question/question/5883e50dd60a35e161a23c43eb3c23b8.html
import cvxpy as cp
def print_hi(name):
x1 = cp.Variable()
x2 = cp.Variable()
objective = cp.Maximize(4 * x1 + 3 * x2)
constraints = [
2 * x1 + x2 <= 10,
x1 + x2 <= 8,
x2 <= 7,
x1 >= 0,
x2 >= 0,
]
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()
print("最大利润" + str(optimal_value))
print("产品A:" + str(x1.value))
print("产品B:" + str(x2.value))
网友评论