当前位置: 首页 > news >正文

数学建模-建模算法(4)

python虽然不是完全为数学建模而生的,但是它完整的库让它越来越适合建模了。

- 线性规划:使用scipy.optimize.linprog()函数
 

```python
from scipy.optimize import linprogc = [-1, 4]
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs')
print(res)
```



- 整数规划:使用scipy.optimize.linprog()函数,并将目标函数系数转换为整数
 

```python
from scipy.optimize import linprogc = [-1, 4]
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs', integer=True)
print(res)
```



- 多元规划:使用scipy.optimize.linprog()函数
 

```python
from scipy.optimize import linprogc = [-1, 4]
A = [[-3, 1, 1], [1, 2, 3]]
b = [6, 4, 5]
x0_bounds = (None, None, None)
x1_bounds = (-3, -3, -3)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs')
print(res)
```



- 二次规划:使用scipy.optimize.linprog()函数,并将目标函数系数转换为平方项
 

```python
from scipy.optimize import linprogc = [-1, 4]
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs', square_root=True)
print(res)
```



- 遗传算法:使用DEAP库
 

```python
from deap import base, creator, tools, algorithms
import randomcreator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)def evalOneMax(individual):return sum(individual),toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)population = toolbox.population(n=300)
algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.2, ngen=40)
```



- 动态规划:使用scipy.optimize.linprog()函数,并将目标函数转换为动态规划问题
 

```python
from scipy.optimize import linprogc = [-1, 4]
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs')
print(res)
```



- 贪心算法:使用scipy.optimize.linprog()函数,并将目标函数转换为贪心策略
 

```python
from scipy.optimize import linprogc = [-1, 4]
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs', options={'disp': True})
while not res.success:if not res.fun:print("Objective function value is 0 at point %s" % res.x)breakif res.status == 4:print("The algorithm could not find a feasible solution for the problem")breakprint(res)res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs', options={'disp': True})
print(res)
```

下次再更新一些高难度的常见算法。
 

http://www.lryc.cn/news/141853.html

相关文章:

  • python之函数返回数据框
  • 电子商务安全体系架构技术方面
  • 新安装IDEA 常用插件、设置
  • ChromeOS 的 Linux 操作系统和 Chrome 浏览器分离
  • 哔哩哔哩 B站 bilibili 视频倍速设置 视频倍速可自定义
  • Lazada商品详情接口 获取Lazada商品详情数据 Lazada商品价格接
  • 路由攻击(ospf attack)及C/C++代码实现
  • nginx配置站点强制开启https
  • Jacoco XML 解析
  • 【面试题】JDK(工具包)、JRE(运行环境和基础库)、JVM(java虚拟机)之间的关系?
  • 软件设计师学习笔记7-输入输出技术+总线+可靠性+性能指标
  • Windows下MATLAB调用Python函数操作说明
  • 【android12-linux-5.1】【ST芯片】驱动与HAL移植后数据方向异常
  • JavaScript Es6_3笔记
  • Qt产生随机数
  • postgresql常用函数-数学函数
  • 【排序】快速排序(前后指针法)—— 考的最少的一种算法
  • 软考:中级软件设计师:关系代数:中级软件设计师:关系代数,规范化理论函数依赖,它的价值和用途,键,范式,模式分解
  • openCV实战-系列教程2:阈值与平滑处理(图像阈值/图像平滑处理/高斯/中值滤波)、源码解读
  • C语言第五章-循环结构练习
  • Echarts面积图2.0(范围绘制)
  • flink checkpoint时exact-one模式和atleastone模式的区别
  • QEMU 仿真RISC-V freeRTOS 程序
  • 用大白话来讲讲多线程的知识架构
  • 【uniapp】微信小程序 , 海报轮播图弹窗,点击海报保存到本地,长按海报图片分享,收藏或保存
  • SpringBoot与前端交互遇到的一些问题
  • Maven介绍与配置+IDEA集成Maven+使用Maven命令
  • 毕业设计题目源码-毕业论文参考
  • SSH报错-Terminal shell path: C:\WINDOWS\System32\cmd.exe 此时不应有
  • Docker 轻量级可视化工具Portainer