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

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

  • 线性规划:使用scipy.optimize.linprog()函数
python 复制代码
```python
from scipy.optimize import linprog

c = [-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 复制代码
```python
from scipy.optimize import linprog

c = [-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 复制代码
```python
from scipy.optimize import linprog

c = [-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 复制代码
```python
from scipy.optimize import linprog

c = [-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 复制代码
```python
from deap import base, creator, tools, algorithms
import random

creator.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 复制代码
```python
from scipy.optimize import linprog

c = [-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 复制代码
```python
from scipy.optimize import linprog

c = [-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)
        break
    if res.status == 4:
        print("The algorithm could not find a feasible solution for the problem")
        break
    print(res)
    res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='highs', options={'disp': True})
print(res)
```

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

相关推荐
阿里matlab建模师10 分钟前
【直流电机鲁棒控制】matlab实现H无穷大控制的直流电机鲁棒控制研究
开发语言·数学建模·matlab·全国大学生数学建模竞赛·美赛·科研项目
MediaTea19 分钟前
Python 第三方库:SymPy(符号计算工具)
开发语言·python·数学建模
fangzelin51 小时前
算法-滑动窗口
数据结构·算法
zcz16071278211 小时前
LVS + Keepalived 高可用负载均衡集群
java·开发语言·算法
rit84324992 小时前
人工鱼群算法AFSA优化支持向量机SVM,提高故障分类精度
算法·支持向量机·分类
佩佩(@ 。 @)2 小时前
嵌入式:走馬燈-stm32GPIOF_LED9、10; GPIOE_D10、D12 流水綫蜂鸣器
stm32·嵌入式硬件·算法
知彼解己2 小时前
字符串大数相加:从初稿到优化的思路演进
java·开发语言·算法
haing20193 小时前
使用deboor法计算三次B样条曲线在参数为u处的位置的方法介绍
算法·b样条曲线·deboor
qq_352109523 小时前
旋转数字矩阵 od
算法
大阳1233 小时前
51单片机4(温度传感器DS18B20)
开发语言·单片机·嵌入式硬件·算法·51单片机