首页 > 解决方案 > mystic 可以解决有约束的黑盒优化问题吗?

问题描述

我想知道是否可以在 python 中使用 mystic 进行约束黑盒优化。如果是这样,此优化包中将提供哪些算法?

标签: python-3.xblack-boxmystic

解决方案


我是 的作者mystic。是的,黑盒约束优化在 mystic 中是可能的。要了解更多信息,请查看此处的文档:https ://github.com/uqfoundation/mystic以及其中的文档链接。

在约束优化方面,repo 中有大约 50 个示例: https ://github.com/uqfoundation/mystic/tree/master/examples2 。约束可以是象征性的或功能性的,相等和不等式,硬的或软的,可以与 and,or,not 组合,并且可以应用于任何优化器。Mystic 的约束也是可移植的,可以应用于其他优化代码,如scipy.optimize,以及机器学习代码,如sklearn.

Mystic 没有很多优化器,但优化器非常可定制,并且允许您调整优化算法的几乎每个方面。更多的优化器大部分都在开发中,并将在今年夏天/秋天的一个版本中添加。

这是来自上述链接的一个明确示例:

"""
Maximize: f = 2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2
Subject to: -2*x[0] + 2*x[1] <= -2
             2*x[0] - 4*x[1] <= 0
               x[0]**3 -x[1] == 0
where: 0 <= x[0] <= inf
       1 <= x[1] <= inf
"""
import numpy as np
import mystic.symbolic as ms
import mystic.solvers as my
import mystic.math as mm

# generate constraints and penalty for a nonlinear system of equations 
ieqn = '''
   -2*x0 + 2*x1 <= -2
    2*x0 - 4*x1 <= 0'''
eqn = '''
     x0**3 - x1 == 0'''
cons = ms.generate_constraint(ms.generate_solvers(ms.simplify(eqn,target='x1')))
pens = ms.generate_penalty(ms.generate_conditions(ieqn), k=1e3)
bounds = [(0., None), (1., None)]

# get the objective
def objective(x, sign=1):
  x = np.asarray(x)
  return sign * (2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2)

# solve    
x0 = np.random.rand(2)
sol = my.fmin_powell(objective, x0, constraint=cons, penalty=pens, disp=True,
                     bounds=bounds, gtol=3, ftol=1e-6, full_output=True,
                     args=(-1,))

print('x* = %s; f(x*) = %s' % (sol[0], -sol[1]))

推荐阅读