示例#1
0
#sum([x0**2, x1**2, x2**2]) - 1.0 = 0.0
@with_penalty(quadratic_equality)
def penalty(x):  # == 0.0
    x = [xi**2 for xi in x]  # x**2
    return abs(sum(x) - 1)


from mystic.constraints import as_constraint

solver = as_constraint(penalty)

if __name__ == '__main__':
    x = xs(3)
    y = ys(len(x))
    bounds = bounds(len(x))

    from mystic.solvers import diffev2
    from mystic.math import almostEqual

    result = diffev2(objective,
                     x0=bounds,
                     bounds=bounds,
                     penalty=penalty,
                     npop=40,
                     gtol=500,
                     disp=False,
                     full_output=True)

    assert almostEqual(result[0], x, tol=1e-2)
    assert almostEqual(result[1], y, tol=1e-2)
示例#2
0
from mystic.constraints import with_penalty

#sum([x0**2, x1**2, x2**2]) - 1.0 = 0.0
@with_penalty(quadratic_equality)
def penalty(x): # == 0.0
    x = [xi**2 for xi in x] # x**2
    return abs(sum(x) - 1)

from mystic.constraints import as_constraint

solver = as_constraint(penalty)



if __name__ == '__main__':
    x = xs(3)
    y = ys(len(x))
    bounds = bounds(len(x))

    from mystic.solvers import diffev2
    from mystic.math import almostEqual

    result = diffev2(objective, x0=bounds, bounds=bounds, penalty=penalty, npop=40, gtol=500, disp=False, full_output=True)

    assert almostEqual(result[0], x, tol=1e-2)
    assert almostEqual(result[1], y, tol=1e-2)



# EOF