用tensorflowv2实现协方差矩阵自适应进化策略(CMAES)
cma-es的Python项目详细描述
协方差矩阵自适应进化策略(CMA-ES)
TensorFlowV2实现。在
什么是CMA-ES?在
引用The CMA Evolution Strategy主页:
The CMA-ES (Covariance Matrix Adaptation Evolution Strategy) is an evolutionary algorithm for difficult non-linear non-convex black-box optimisation problems in continuous domain. It is considered as state-of-the-art in evolutionary computation and has been adopted as one of the standard tools for continuous optimisation in many (probably hundreds of) research labs and industrial environments around the world.
安装
软件包是available on PyPI,可以与pip一起安装:
pip install cma-es
示例用法
1。定义健身功能
CMA类需要具有以下签名的适应度函数:
^{pr2}$其中M
是要计算的解的数目,N
是单个解的维数。在
deffitness_fn(x):""" Six-Hump Camel Function https://www.sfu.ca/~ssurjano/camel6.html """return((4-2.1*x[:,0]**2+x[:,0]**4/3)*x[:,0]**2+x[:,0]*x[:,1]+(-4+4*x[:,1]**2)*x[:,1]**2)
2。配置CMA-ES
fromcmaimportCMAcma=CMA(initial_solution=[1.5,-0.4],initial_step_size=1.0,fitness_function=fitness_fn,)
初始解和初始步长(即搜索分布的初始标准差)是特定于问题的。在
默认情况下,填充大小是自动设置的,但是可以通过指定参数population_size
来覆盖它。在
对于有界约束优化问题,可以设置参数enforce_bounds
,例如,二维函数的enforce_bounds=[[-2, 2], [-1, 1]]
。在
3。运行优化器
搜索方法将一直运行,直到达到最大生成数或满足其中一个早期终止条件。默认情况下,最大生成数为500。在
best_solution,best_fitness=cma.search()
笔记本^{
测井
可以指定用户定义的回调函数来在搜索期间检查变量。在
主要用于测井目的,例如:
max_epochs=500deflogging_function(cma,logger):ifcma.generation%10==0:fitness=cma.best_fitness()logger.info(f'Generation {cma.generation} - fitness {fitness}')ifcma.termination_criterion_metorcma.generation==max_epochs:sol=cma.best_solution()fitness=cma.best_fitness()logger.info(f'Final solution at gen {cma.generation}: {sol} (fitness: {fitness})')cma=CMA(initial_solution=[1.5,-0.4],initial_step_size=1.0,fitness_function=fitness_fn,callback_function=logging_function,)cma.search(max_epochs)
查看一个将进度记录到TensorBoard的示例:tensorboard_example.py
在GPU上运行
由于使用TensorFlow编写,在GPU上运行CMA非常简单:
withtf.device('/GPU:0'):cma.search()
更多示例
- 提供带有示例的Jupyter笔记本电脑: 在
- 单元测试提供了更多的例子:
cma/core_test.py
资源
- 项目
标签: