, CVXPY (http://www.cvxpy.org/en/latest), , , CVXOPT ( ). CVXOPT CVXPY, .
python , : https://scicomp.stackexchange.com/questions/83/is-there-a-high-quality-nonlinear-programming-solver-for-python... , , , .
mystic (https://pypi.python.org/pypi/mystic). , , , , . . , mystic , . , mystic , , ( ), mystic , .
100 , mystic:
'''
Maximize: f = 2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2
Subject to: x[0]**3 - x[1] == 0
x[1] >= 1
'''
( ):
def objective(x):
return 2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2
equations = """
x0**3 - x1 == 0.0
"""
bounds = [(None, None),(1.0, None)]
xs = [1,1]; ys = -1.0
from mystic.symbolic import generate_conditions, generate_penalty
pf = generate_penalty(generate_conditions(equations), k=1e4)
from mystic.symbolic import generate_constraint, generate_solvers, solve
cf = generate_constraint(generate_solvers(solve(equations)))
_objective = lambda x: -objective(x)
if __name__ == '__main__':
from mystic.solvers import diffev2, fmin_powell
from mystic.math import almostEqual
result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True)
assert almostEqual(result[0], xs, rel=2e-2)
assert almostEqual(result[1], ys, rel=2e-2)
result = fmin_powell(_objective, x0=[-1.0,1.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True)
assert almostEqual(result[0], xs, rel=2e-2)
assert almostEqual(result[1], ys, rel=2e-2)
:
"""
Fit linear and quadratic polynomial to noisy data:
y(x) ~ a + b * x --or-- y(x) ~ a + b * x + c * x**2
where:
0 >= x >= 4
y(x) = y0(x) + yn
y0(x) = 1.5 * exp(-0.2 * x) + 0.3
yn = 0.1 * Normal(0,1)
"""
():
from numpy import polyfit, poly1d, linspace, exp
from numpy.random import normal
from mystic.math import polyeval
from mystic import reduced
x = linspace(0, 4.0, 100)
y0 = 1.5 * exp(-0.2 * x) + 0.3
noise = 0.1 * normal(size=100)
y = y0 + noise
@reduced(lambda x,y: abs(x)+abs(y))
def objective(coeffs, x, y):
return polyeval(coeffs, x) - y
bounds = [(None, None), (None, None), (None, None)]
args = (x, y)
xs = polyfit(x, y, 2)
ys = objective(xs, x, y)
if __name__ == '__main__':
from mystic.solvers import diffev2, fmin_powell
from mystic.math import almostEqual
result = diffev2(objective, args=args, x0=bounds, bounds=bounds, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True)
assert almostEqual(result[0], xs, tol=1e-1)
assert almostEqual(result[1], ys, rel=1e-1)
result = fmin_powell(objective, args=args, x0=[0.0,0.0,0.0], bounds=bounds, disp=False, full_output=True)
assert almostEqual(result[0], xs, tol=1e-1)
assert almostEqual(result[1], ys, rel=1e-1)
mystic pathos pyina (. https://github.com/uqfoundation ), , . . , ( ) , (- ).