美文网首页
Python cvxpy 库解决 运筹学中的背包问题、目标规划、

Python cvxpy 库解决 运筹学中的背包问题、目标规划、

作者: Hamiltonian | 来源:发表于2023-04-12 14:48 被阅读0次

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))

相关文章

  • 算法学习收藏

    动态规划问题 动态规划(最优子结构和重叠子问题的比较) 动态规划解决01背包问题 01背包问题 最优二叉查找树 《...

  • 数学建模-方法合集

    线性规划 线性规划问题 线性规划(Linear programming,简称LP)是运筹学中研究较早、发展较快、应...

  • 动态规划-0-1背包问题

    动态规划-0-1背包问题 动态规划(dynamic programming)是解决多阶段决策问题常用的最优化理论,...

  • 简介运筹学中的规划问题

    运筹学这个名字听起来挺厉害的,我当初听这个名字时觉得如果学会了它,就能掌握一种玄妙的指挥艺术,像一个指挥官一样调度...

  • 背包系列问题——换零钱

    参考资料:1. 动态规划之背包问题系列 背包问题的定义参见参考资料1跟背包问题不同的是,目标是换的零钱的个数最少。...

  • 动态规划中的背包问题

    源自掘金 https://juejin.im/post/5c623ff3f265da2de1657f97, 此处有...

  • #沉淀打卡 第10天

    背完单词,看一天的运筹学和高数 运筹学分为确定性模型和随机性模型,只要学线性规划与目标规划,整数线性规划,和图与网...

  • 2020-08-16 Selenium 安装

    Python 的库基于Python安装路径,下面解决selenium 的问题: Selenium 的问题: 1,p...

  • 算法06-动态规划

    算法06-动态规划 一、介绍 动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程...

  • Java使用动态规划算法思想解决01背包问题

    Java使用动态规划算法思想解决背包问题 背包问题是一种组合优化的NP完全问题。问题可以描述为:给定一组物品,每种...

网友评论

      本文标题:Python cvxpy 库解决 运筹学中的背包问题、目标规划、

      本文链接:https://www.haomeiwen.com/subject/svcoddtx.html