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

文章介绍了如何使用Python的scipy.optimize.linprog()函数实现线性、整数、多元、二次规划,以及涉及遗传算法和将问题转化为动态规划和贪心策略的实例。
摘要由CSDN通过智能技术生成

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值