In [2]:
from lpfgopt import minimize
import numpy as np
import matplotlib.pyplot as plt

In [3]:
# set up the objective funciton, 
# constraint fuction and bounds
f = lambda x: sum([i**2 for i in x])
g = lambda x: -x[0]**2 + 10 - x[1] 
bounds = [[-5,5] for i in range(2)]

# run the optimization
sol = minimize(f, bounds, fconstraint=g)['x']
print(f"Solution is: {sol}")

# plot the results on a contour plot
gg = lambda x: -x**2 + 10 # for plotting purposes

plt.figure(figsize=(8,8))
x, y = np.linspace(-5,5,1000), np.linspace(-5,5,1000)
X, Y = np.meshgrid(x,y)
Z = f([X,Y])

plt.contourf(X,Y,Z)
plt.plot(x, gg(x), "r", label="constraint")
plt.plot(*sol, 'x', 
         markersize=14, 
         markeredgewidth=4, 
         color="lime", 
         label="optimum")
plt.ylim(-5,5)
plt.xlim(-5,5)
plt.legend()
plt.show()


Solution is: [-3.082908474275612, 0.4956754075098558]

In [13]:
def height(x):
    return f([x,gg(x)])

xs = np.linspace(-4,4,100)
plt.plot(xs, [height(i) for i in xs])
sol = minimize(height, [[-4,0]])
for key, val in sol.items():
    print(key, val)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-2a02f39b7113> in <module>
      4 xs = np.linspace(-4,4,100)
      5 plt.plot(xs, [height(i) for i in xs])
----> 6 sol = minimize(height, [[-4,0]])
      7 for key, val in sol.items():
      8     print(key, val)

~\Documents\Mark\lpfgopt\lpfgopt\__init__.py in minimize(fun, bounds, args, points, fconstraint, discrete, maxit, tol, seedval, pointset, callback)
    102         }
    103 
--> 104     lf = LeapFrog(**options)
    105     return lf.minimize()

~\Documents\Mark\lpfgopt\lpfgopt\leapfrog.py in __init__(self, fun, bounds, args, points, fconstraint, discrete, maxit, tol, seedval, pointset, callback, **kwargs)
    184             self.pointset[row][1:] = self.enforce_discrete(
    185                                                     self.pointset[row][1:])
--> 186             self.pointset[row][0] = self.f(self.pointset[row][1:])
    187 
    188         self.enforce_constraints()

~\Documents\Mark\lpfgopt\lpfgopt\leapfrog.py in f(self, x)
    207     def f(self, x):
    208         self.nfev += 1
--> 209         return self.fun(x, *self.args)
    210 
    211 

<ipython-input-13-2a02f39b7113> in height(x)
      1 def height(x):
----> 2     return f([x,gg(x)])
      3 
      4 xs = np.linspace(-4,4,100)
      5 plt.plot(xs, [height(i) for i in xs])

<ipython-input-3-a8687f140bbc> in <lambda>(x)
     10 
     11 # plot the results on a contour plot
---> 12 gg = lambda x: -x**2 + 10 # for plotting purposes
     13 
     14 plt.figure(figsize=(8,8))

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

In [ ]: