コード例 #1
0
ファイル: lp.py プロジェクト: Magellen/mystic
-3*x0 + x1 - 6.0 <= 0.0
x0 + 2*x1 - 4.0 <= 0.0
"""
bounds = [(None, None),(-3.0, None)]

# with penalty='penalty' applied, solution is:
xs = [10.0, -3.0]
ys = -22.0
# alternately, if solving for the maximum, the solution is:
_xs = [-1.14285714,  2.57142857]
_ys = 11.428571428571429

from mystic.symbolic import generate_conditions, generate_penalty
pf = generate_penalty(generate_conditions(equations))
from mystic.symbolic import generate_constraint, generate_solvers, simplify
cf = generate_constraint(generate_solvers(simplify(equations)))

# inverted objective, used in solving for the maximum
_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, disp=False, full_output=True)
  assert almostEqual(result[0], xs, rel=1e-2)
  assert almostEqual(result[1], ys, rel=1e-2)

  result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True)
コード例 #2
0
ファイル: slsqp3.py プロジェクト: uqfoundation/mystic
"""

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)

x0 = np.random.rand(2)

# compare against the exact minimum
xs = np.array([2., 1.])
ys = objective(xs, -1)

コード例 #3
0
ファイル: slsqp.py プロジェクト: uqfoundation/mystic
    Subject to: x[0] + 1*x[1] <= 7
               -x[0] + 2*x[1] <= 4

    where:  0 <= x[0] <= inf
            0 <= x[1] <= 4
"""
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 linear system of equations 
A = np.array([[1, 1],[-1, 2]])
b = np.array([7,4])
eqns = ms.linear_symbolic(G=A, h=b)
cons = ms.generate_constraint(ms.generate_solvers(ms.simplify(eqns)))
pens = ms.generate_penalty(ms.generate_conditions(eqns), k=1e3)
bounds = [(0., None), (0., 4.)]

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

x0 = np.random.rand(2)

# compare against the exact minimum
xs = np.array([2., 3.])
ys = objective(xs)

コード例 #4
0
ファイル: g07.py プロジェクト: shirangi/mystic
ys = 24.3062091

from mystic.symbolic import generate_constraint, generate_solvers, simplify
from mystic.symbolic import generate_penalty, generate_conditions

equations = """
4.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0
10.0*x0 - 8.0*x1 - 17.0*x6 + 2.0*x7 <= 0.0
-8.0*x0 + 2.0*x1 + 5.0*x8 - 2.0*x9 - 12.0 <= 0.0
3.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0
5.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0
0.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0
x0**2 + 2.0*(x1-2)**2 - 2.0*x0*x1 + 14.0*x4 - 6.0*x5 <= 0.0
-3.0*x0 + 6.0*x1 + 12.0*(x8-8)**2 - 7.0*x9 <= 0.0
"""
cf = generate_constraint(generate_solvers(simplify(equations, target=['x5','x3'])))
pf = generate_penalty(generate_conditions(equations))



if __name__ == '__main__':
    x = [0]*len(xs)

    from mystic.solvers import fmin_powell
    from mystic.math import almostEqual
    from mystic.monitors import VerboseMonitor
    mon = VerboseMonitor(10)

    result = fmin_powell(objective, x0=x, bounds=bounds, penalty=pf, maxiter=1000, maxfun=100000, ftol=1e-12, xtol=1e-12, gtol=10, disp=False, full_output=True)

    assert almostEqual(result[0], xs, tol=1e-2)
コード例 #5
0
ファイル: g09.py プロジェクト: resurgo-genetics/mystic
bounds = [(-10., 10.)] * 7
# with penalty='penalty' applied, solution is:
xs = [2.330499, 1.951372, -0.4775414, 4.365726, -0.6244870, 1.038131, 1.594227]
ys = 680.6300573

from mystic.symbolic import generate_constraint, generate_solvers, simplify
from mystic.symbolic import generate_penalty, generate_conditions

equations = """
2.0*x0**2 + 3.0*x1**4 + x2 + 4.0*x3**2 + 5.0*x4 - 127.0 <= 0.0
7.0*x0 + 3.0*x1 + 10.0*x2**2 + x3 - x4 - 282.0 <= 0.0
23.0*x0 + x1**2 + 6.0*x5**2 - 8.0*x6 - 196.0 <= 0.0
4.0*x0**2 + x1**2 - 3.0*x0*x1 + 2.0*x2**2 + 5.0*x5 - 11.0*x6 <= 0.0
"""
cf = generate_constraint(generate_solvers(simplify(equations)))
pf = generate_penalty(generate_conditions(equations), k=1e12)

if __name__ == '__main__':

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

    result = diffev2(objective,
                     x0=bounds,
                     bounds=bounds,
                     constraints=cf,
                     penalty=pf,
                     npop=40,
                     gtol=200,
                     disp=False,
コード例 #6
0
ファイル: inequalities.py プロジェクト: vishalbelsare/mystic
import mystic.symbolic as ms
from mystic.constraints import and_ as _and, or_ as _or
from mystic.coupler import and_, or_


if __name__ == '__main__':
    eps = 1e-16

    equations = """
    A*B + C > 1
    B < 0
    """
    print(equations)

    var = list('ABC')
    eqns = ms.simplify(equations, variables=var, all=True)
    if isinstance(eqns, str):
      _join = join_ = None
      print(eqns)
    else:
      _join,join_ = _or,or_
      for eqn in eqns:
        print(eqn + '\n----------------------')

    constrain = ms.generate_constraint(ms.generate_solvers(eqns, var, locals=dict(e_=eps)), join=_join)
    solution = constrain([1,-2,1])

    print('solved: %s' % dict(zip(var, solution)))

    penalty = ms.generate_penalty(ms.generate_conditions(eqns, var, locals=dict(e_=eps)), join=join_)
    print('penalty: %s' % penalty(solution))
コード例 #7
0
ファイル: g07.py プロジェクト: vishalbelsare/mystic
from mystic.symbolic import generate_constraint, generate_solvers, simplify
from mystic.symbolic import generate_penalty, generate_conditions

equations = """
4.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0
10.0*x0 - 8.0*x1 - 17.0*x6 + 2.0*x7 <= 0.0
-8.0*x0 + 2.0*x1 + 5.0*x8 - 2.0*x9 - 12.0 <= 0.0
3.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0
5.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0
0.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0
x0**2 + 2.0*(x1-2)**2 - 2.0*x0*x1 + 14.0*x4 - 6.0*x5 <= 0.0
-3.0*x0 + 6.0*x1 + 12.0*(x8-8)**2 - 7.0*x9 <= 0.0
"""
cf = generate_constraint(
    generate_solvers(simplify(equations, target=['x5', 'x3'])))
pf = generate_penalty(generate_conditions(equations))

if __name__ == '__main__':
    x = [0] * len(xs)

    from mystic.solvers import fmin_powell
    from mystic.math import almostEqual
    from mystic.monitors import VerboseMonitor
    mon = VerboseMonitor(10)

    result = fmin_powell(objective,
                         x0=x,
                         bounds=bounds,
                         penalty=pf,
                         maxiter=1000,
コード例 #8
0
ファイル: slsqp.py プロジェクト: vishalbelsare/mystic
    Subject to: x[0] + 1*x[1] <= 7
               -x[0] + 2*x[1] <= 4

    where:  0 <= x[0] <= inf
            0 <= x[1] <= 4
"""
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 linear system of equations
A = np.array([[1, 1], [-1, 2]])
b = np.array([7, 4])
eqns = ms.linear_symbolic(G=A, h=b)
cons = ms.generate_constraint(ms.generate_solvers(ms.simplify(eqns)))
pens = ms.generate_penalty(ms.generate_conditions(eqns), k=1e3)
bounds = [(0., None), (0., 4.)]


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


x0 = np.random.rand(2)

# compare against the exact minimum
xs = np.array([2., 3.])
ys = objective(xs)
コード例 #9
0
ファイル: g07.py プロジェクト: Magellen/mystic
ys = 24.3062091

from mystic.symbolic import generate_constraint, generate_solvers, simplify
from mystic.symbolic import generate_penalty, generate_conditions

equations = """
4.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0
10.0*x0 - 8.0*x1 - 17.0*x6 + 2.0*x7 <= 0.0
-8.0*x0 + 2.0*x1 + 5.0*x8 - 2.0*x9 - 12.0 <= 0.0
3.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0
5.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0
0.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0
x0**2 + 2.0*(x1-2)**2 - 2.0*x0*x1 + 14.0*x4 - 6.0*x5 <= 0.0
-3.0*x0 + 6.0*x1 + 12.0*(x8-8)**2 - 7.0*x9 <= 0.0
"""
cf = generate_constraint(generate_solvers(simplify(equations, target=['x5','x3'])))
pf = generate_penalty(generate_conditions(equations))



if __name__ == '__main__':
    x = [0]*len(xs)

    from mystic.solvers import fmin_powell
    from mystic.math import almostEqual
    from mystic.monitors import VerboseMonitor
    mon = VerboseMonitor(10)

    result = fmin_powell(objective, x0=x, bounds=bounds, penalty=pf, maxiter=1000, maxfun=100000, ftol=1e-12, xtol=1e-12, gtol=10, disp=False, full_output=True)

    assert almostEqual(result[0], xs, tol=1e-2)
コード例 #10
0
def cf(len=3):
    return generate_constraint(generate_solvers(simplify(equations(len))))
コード例 #11
0
    return a * x3**3 + b * x2**2 + c * x1 + d * x0


# generate some sparse data
xtrain = np.random.uniform(0, 100, size=(10, 4))
target = model(xtrain.T).T
xtest = np.random.uniform(0, 100, size=(10, 4))
test = model(xtest.T).T

# define some model constraints
equations = """
3*b + c > -0.75
4.5*b - d > 11.0
"""
var = list('abcd')
equations = simplify(equations, variables=var)
cf = generate_constraint(generate_solvers(equations, variables=var))

if __name__ == '__main__':
    # build a kernel-transformed regressor
    ta = pre.FunctionTransformer(func=vectorize(cf, axis=1))
    tp = pre.PolynomialFeatures(degree=3)
    e = lin.LinearRegression()

    # train and score, then test and score
    xtrain_ = tp.fit_transform(ta.fit_transform(xtrain))
    assert 1.0 == e.fit(xtrain_, target).score(xtrain_, target)
    xtest_ = tp.fit_transform(ta.fit_transform(xtest))
    assert 1 - e.score(xtest_, test) <= 1e-2

# EOF
コード例 #12
0
eqn = 'A = (B - 1/C)/(1 - (B*C))'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 1/C'])
eqn = 'A = (B - 1/C)/(1 - sin(B*C))'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', '(1 - sin(B*C)) = 0'])
eqn = 'A = (B - 1/C)/(1 - (B*C)**2)'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 1/C'])
eqn = 'A = (B - 1/C)/(4 - (B*C)**2)'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 2/C'])
eqn = 'A = (B - 1/C)/(B - 1)**2'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 1'])
eqn = 'A = B + 1'
assert _solve_zeros(eqn, var) == []

eqn = 'A**2 + B*(2 - C) < C*A'
var = list('ABC')
res = simplify(eqn, variables=var, target=list('CAB'), all=True)
#print('\n#####\n'.join(res))
bylen = lambda x: len(x)
res = ['\n'.join(sorted(eqns.split('\n'), key=bylen)) for eqns in res]
assert equals(eqn, res[0].split('\n')[-1], dict(A=-0.9, B=1., C=1.))
assert equals(eqn, res[0].split('\n')[-1], dict(A=-0.9, B=1., C=-1.))
assert equals(eqn, res[0].split('\n')[-1], dict(A=1.1, B=-1., C=1.))
assert equals(eqn, res[0].split('\n')[-1], dict(A=1.1, B=-1., C=-1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=-1.1, B=1., C=1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=-1.1, B=1., C=-1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=0.9, B=-1., C=-1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=0.9, B=-1., C=1.))

res = simplify(eqn, variables=var, target=list('BCA'), all=True)
#print('\n#####\n'.join(res))
bylen = lambda x: len(x)
コード例 #13
0
eqn = 'A = (B - 1/C)/(1 - (B*C))'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 1/C'])
eqn = 'A = (B - 1/C)/(1 - sin(B*C))'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', '(1 - sin(B*C)) = 0'])
eqn = 'A = (B - 1/C)/(1 - (B*C)**2)'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 1/C'])
eqn = 'A = (B - 1/C)/(4 - (B*C)**2)'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 2/C'])
eqn = 'A = (B - 1/C)/(B - 1)**2'
assert set(_solve_zeros(eqn, var)) == set(['C = 0', 'B = 1'])
eqn = 'A = B + 1'
assert _solve_zeros(eqn, var) == []

eqn = 'A**2 + B*(2 - C) < C*A'
var = list('ABC')
res = simplify(eqn, variables=var, target=list('CAB'), all=True)
#print('\n#####\n'.join(res))
bylen = lambda x: len(x)
res = ['\n'.join(sorted(eqns.split('\n'), key=bylen)) for eqns in res]
assert equals(eqn, res[0].split('\n')[-1], dict(A=-0.9,B=1.,C=1.))
assert equals(eqn, res[0].split('\n')[-1], dict(A=-0.9,B=1.,C=-1.))
assert equals(eqn, res[0].split('\n')[-1], dict(A=1.1,B=-1.,C=1.))
assert equals(eqn, res[0].split('\n')[-1], dict(A=1.1,B=-1.,C=-1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=-1.1,B=1.,C=1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=-1.1,B=1.,C=-1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=0.9,B=-1.,C=-1.))
assert equals(eqn, res[1].split('\n')[-1], dict(A=0.9,B=-1.,C=1.))

res = simplify(eqn, variables=var, target=list('BCA'), all=True)
#print('\n#####\n'.join(res))
bylen = lambda x: len(x)
コード例 #14
0
ファイル: slsqp3.py プロジェクト: murphyliu81/mystic
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)


x0 = np.random.rand(2)

# compare against the exact minimum
xs = np.array([2., 1.])
ys = objective(xs, -1)