数学建模-建模算法(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)
```

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

相关推荐
糖葫芦君39 分钟前
Policy Gradient【强化学习的数学原理】
算法
向阳@向远方2 小时前
第二章 简单程序设计
开发语言·c++·算法
wwer1425263633 小时前
数学建模_时间序列
数学建模
github_czy3 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁4 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子4 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z4 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao5 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx5 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背5 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft