pyMetaheuristic是一个强大的Python库,封装了多种启发式算法,适用于解决复杂的优化任务。
可通过pip安装pyMetaheuristic库,然后使用其中提供的算法来解决优化问题。该库还提供了一些测试函数,可用于评估算法性能。
封装算法
该启发式算法库,封装的部分算法如下:
使用案例
下面是一个使用pyMetaheuristic库中粒子群优化算法(Particle Swarm Optimization, PSO)的案例:
导入包
# 导入PSO算法
from pyMetaheuristic.algorithm import particle_swarm_optimization
# 导入测试函数。可用测试函数列表:https://bit.ly/3KyluPp
from pyMetaheuristic.test_function import easom
import numpy as np
定义函数
# 定义函数
def easom(variables_values=[0, 0]):
x1, x2 = variables_values
func_value = -np.cos(x1) * np.cos(x2) * np.exp(-(x1 - np.pi)**2 - (x2 - np.pi)**2)
return func_value
设置参数
本案例使用的粒子算法(pso),可以设置粒子算法的相应参数。详细参数含义,可参阅前文粒子算法的原理介绍。
# 设置PSO参数
parameters = {
'swarm_size': 250,
'min_values': (-5, -5),
'max_values': (5, 5),
'iterations': 500,
'decay': 0,
'w': 0.9,
'c1': 2,
'c2': 2,
'verbose': True,
'start_init': None,
'target_value': None
}
运行PSO算法
pso = particle_swarm_optimization(target_function=easom, **parameters)
输出解决方案
variables = pso[:-1]
minimum = pso[-1]
print('变量值: ', np.around(variables, 4), ' 最小值: ', round(minimum, 4))
绘制解决方案
from pyMetaheuristic.utils import graphs
plot_parameters = {
'min_values': (-5, -5),
'max_values': (5, 5),
'step': (0.1, 0.1),
'solution': [variables],
'proj_view': '3D',
'view': 'browser'
}
graphs.plot_single_function(target_function=easom, **plot_parameters)
完整代码
# 导入PSO算法
from pyMetaheuristic.algorithm import particle_swarm_optimization
# 导入测试函数。可用测试函数列表:https://bit.ly/3KyluPp
from pyMetaheuristic.test_function import easom
import numpy as np
# 定义函数
def easom(variables_values=[0, 0]):
x1, x2 = variables_values
func_value = -np.cos(x1) * np.cos(x2) * np.exp(-(x1 - np.pi)**2 - (x2 - np.pi)**2)
return func_value
# 设置PSO参数
parameters = {
'swarm_size': 250,
'min_values': (-5, -5),
'max_values': (5, 5),
'iterations': 500,
'decay': 0,
'w': 0.9,
'c1': 2,
'c2': 2,
'verbose': True,
'start_init': None,
'target_value': None
}
# 运行PSO算法
pso = particle_swarm_optimization(target_function=easom, **parameters)
# 打印解决方案
variables = pso[:-1]
minimum = pso[-1]
print('变量值: ', np.around(variables, 4), ' 最小值: ', round(minimum, 4))
# 绘制解决方案
from pyMetaheuristic.utils import graphs
plot_parameters = {
'min_values': (-5, -5),
'max_values': (5, 5),
'step': (0.1, 0.1),
'solution': [variables],
'proj_view': '3D',
'view': 'browser'
}
graphs.plot_single_function(target_function=easom, **plot_parameters)
输出结果
Iteration = 0 f(x) = -0.8920526661783159
Iteration = 1 f(x) = -0.8920526661783159
Iteration = 2 f(x) = -0.9945631979041981
Iteration = 3 f(x) = -0.9945631979041981
Iteration = 4 f(x) = -0.9945631979041981
...
Iteration = 388 f(x) = -0.9999997363239875
Iteration = 389 f(x) = -0.9999997363239875
Iteration = 390 f(x) = -0.9999997363239875
Iteration = 391 f(x) = -0.9999997363239875
Iteration = 392 f(x) = -0.9999997363239875
Iteration = 393 f(x) = -0.9999997363239875
Iteration = 394 f(x) = -0.9999997363239875
Iteration = 395 f(x) = -0.9999997363239875
Iteration = 396 f(x) = -0.9999997363239875
Iteration = 397 f(x) = -0.9999997363239875
Iteration = 398 f(x) = -0.9999997363239875
Iteration = 399 f(x) = -0.9999997363239875
Iteration = 400 f(x) = -0.9999997363239875
Iteration = 401 f(x) = -0.9999997363239875
Iteration = 402 f(x) = -0.9999997363239875
Iteration = 403 f(x) = -0.9999997363239875
Iteration = 404 f(x) = -0.9999997363239875
Iteration = 405 f(x) = -0.9999997363239875
Iteration = 406 f(x) = -0.9999997363239875
Iteration = 407 f(x) = -0.9999997363239875
Iteration = 408 f(x) = -0.9999997363239875
Iteration = 409 f(x) = -0.9999997363239875
Iteration = 410 f(x) = -0.9999997363239875
Iteration = 411 f(x) = -0.9999997363239875
Iteration = 412 f(x) = -0.9999997363239875
Iteration = 413 f(x) = -0.9999997363239875
Iteration = 414 f(x) = -0.9999997363239875
Iteration = 415 f(x) = -0.9999997363239875
Iteration = 416 f(x) = -0.9999997363239875
Iteration = 417 f(x) = -0.9999997363239875
Iteration = 418 f(x) = -0.9999997363239875
Iteration = 419 f(x) = -0.9999997363239875
Iteration = 420 f(x) = -0.9999997363239875
Iteration = 421 f(x) = -0.9999997363239875
Iteration = 422 f(x) = -0.9999997363239875
Iteration = 423 f(x) = -0.9999997363239875
Iteration = 424 f(x) = -0.9999997363239875
Iteration = 425 f(x) = -0.9999997363239875
Iteration = 426 f(x) = -0.9999997363239875
Iteration = 427 f(x) = -0.9999997363239875
Iteration = 428 f(x) = -0.9999997363239875
Iteration = 429 f(x) = -0.9999997363239875
Iteration = 430 f(x) = -0.9999997363239875
Iteration = 431 f(x) = -0.9999997363239875
Iteration = 432 f(x) = -0.9999997363239875
Iteration = 433 f(x) = -0.9999997363239875
Iteration = 434 f(x) = -0.9999997363239875
Iteration = 435 f(x) = -0.9999997363239875
Iteration = 436 f(x) = -0.9999997363239875
Iteration = 437 f(x) = -0.9999997363239875
Iteration = 438 f(x) = -0.9999997363239875
Iteration = 439 f(x) = -0.9999997363239875
Iteration = 440 f(x) = -0.9999997363239875
Iteration = 441 f(x) = -0.9999997363239875
Iteration = 442 f(x) = -0.9999997363239875
Iteration = 443 f(x) = -0.9999997363239875
Iteration = 444 f(x) = -0.9999997363239875
Iteration = 445 f(x) = -0.9999997363239875
Iteration = 446 f(x) = -0.9999997363239875
Iteration = 447 f(x) = -0.9999997363239875
Iteration = 448 f(x) = -0.9999997363239875
Iteration = 449 f(x) = -0.9999997363239875
Iteration = 450 f(x) = -0.9999997363239875
Iteration = 451 f(x) = -0.9999997363239875
Iteration = 452 f(x) = -0.9999997363239875
Iteration = 453 f(x) = -0.9999997363239875
Iteration = 454 f(x) = -0.9999997363239875
Iteration = 455 f(x) = -0.9999997363239875
Iteration = 456 f(x) = -0.9999997363239875
Iteration = 457 f(x) = -0.9999997363239875
Iteration = 458 f(x) = -0.9999997363239875
Iteration = 459 f(x) = -0.9999997363239875
Iteration = 460 f(x) = -0.9999997363239875
Iteration = 461 f(x) = -0.9999997363239875
Iteration = 462 f(x) = -0.9999997363239875
Iteration = 463 f(x) = -0.9999997363239875
Iteration = 464 f(x) = -0.9999997363239875
Iteration = 465 f(x) = -0.9999997363239875
Iteration = 466 f(x) = -0.9999997363239875
Iteration = 467 f(x) = -0.9999997363239875
Iteration = 468 f(x) = -0.9999997363239875
Iteration = 469 f(x) = -0.9999997363239875
Iteration = 470 f(x) = -0.9999997363239875
Iteration = 471 f(x) = -0.9999997363239875
Iteration = 472 f(x) = -0.9999997363239875
Iteration = 473 f(x) = -0.9999997363239875
Iteration = 474 f(x) = -0.9999997363239875
Iteration = 475 f(x) = -0.9999997363239875
Iteration = 476 f(x) = -0.9999997363239875
Iteration = 477 f(x) = -0.9999997363239875
Iteration = 478 f(x) = -0.9999997363239875
Iteration = 479 f(x) = -0.9999997363239875
Iteration = 480 f(x) = -0.9999997363239875
Iteration = 481 f(x) = -0.9999997363239875
Iteration = 482 f(x) = -0.9999997363239875
Iteration = 483 f(x) = -0.9999997363239875
Iteration = 484 f(x) = -0.9999997363239875
Iteration = 485 f(x) = -0.9999997363239875
Iteration = 486 f(x) = -0.9999997363239875
Iteration = 487 f(x) = -0.9999997363239875
Iteration = 488 f(x) = -0.9999997363239875
Iteration = 489 f(x) = -0.9999997363239875
Iteration = 490 f(x) = -0.9999997363239875
Iteration = 491 f(x) = -0.9999997363239875
Iteration = 492 f(x) = -0.9999997363239875
Iteration = 493 f(x) = -0.9999997363239875
Iteration = 494 f(x) = -0.9999997363239875
Iteration = 495 f(x) = -0.9999997363239875
Iteration = 496 f(x) = -0.9999997363239875
Iteration = 497 f(x) = -0.9999997363239875
Iteration = 498 f(x) = -0.9999997363239875
Iteration = 499 f(x) = -0.9999997363239875
Iteration = 500 f(x) = -0.9999997363239875
Variables: [3.1415 3.142 ] Minimum Value Found: -1.0
pyMetaheuristic封装的其他算法使用方法与本文介绍案例类似,想详细了解可参考官网文档。
官方文档地址 https://github.com/Valdecy/pyMetaheuristic
微信公众号后台回复
加群:加入全球华人OR|AI|DS社区硕博微信学术群
资料:免费获得大量运筹学相关学习资料
人才库:加入运筹精英人才库,获得独家职位推荐
电子书:免费获取平台小编独家创作的优化理论、运筹实践和数据科学电子书,持续更新中ing...
加入我们:加入「运筹OR帷幄」,参与内容创作平台运营
知识星球:加入「运筹OR帷幄」数据算法社区,免费参与每周「领读计划」、「行业inTalk」、「OR会客厅」等直播活动,与数百位签约大V进行在线交流
文章须知
文章作者:用户007
微信编辑:疑疑
文章转载自『Python学习杂记』公众号,原文链接:pyMetaheuristic,一个封装几十种元启发式算法的Python库
关注我们
FOLLOW US