我想知道在python中使用mystic是否可以进行受约束的黑盒优化。如果是这样的话,这个优化包中将提供哪些算法?
发布于 2020-06-12 20:41:59
我是mystic的作者。是的,黑盒约束优化在mystic中是可能的。要了解更多信息,请查看这里的文档:https://github.com/uqfoundation/mystic,以及其中的文档链接。
在约束优化方面,代码库中有大约50个示例:https://github.com/uqfoundation/mystic/tree/master/examples2。约束可以是符号的或函数的,等式和不等式,硬的或软的,可以与and或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]))https://stackoverflow.com/questions/62338990
复制相似问题