Exemple #1
0
def test_1_call():
    import example1

    ported_eq(example1.f(3), 3)
    ported_eq("description" in example1.f.__doc__, True)
    ported_eq(example1.f.__name__, "f")
delta = 0.025
x = np.arange(min([x0[0],x1[0],x2[0],x_star[0]])-0.5, \
              max([x0[0],x1[0],x2[0],x_star[0]])+0.5, \
              delta)
y = np.arange(min([x0[1],x1[1],x2[1],x_star[1]])-0.5, \
              max([x0[1],x1[1],x2[1],x_star[1]])+0.5, \
              delta)
X, Y = np.meshgrid(x, y)
dim = np.shape(X)
# Compute function values
Xv = X.flatten()
Yv = Y.flatten()
Input = zip(Xv, Yv)
Zv = []
for x in Input:
    Zv.append(f(x))
# Restore arrays to proper dimensions
Z = np.array(Zv).reshape(dim)

# Open figure
plt.figure()

# Plot contour
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Sequence of Iterates, Example 1')
# plt.title('Sequence of Iterates, Example 2')
plt.plot(x_star[0], x_star[1], 'ok')  # optimum
plt.plot(x0[0], x0[1], 'ob')  # starting point
plt.plot(x1[0], x1[1], 'or')
plt.plot(x2[0], x2[1], 'og')
Exemple #3
0

if __name__ == "__main__":
    # # Test ad on an external objective
    # from ad import gh
    # from example1 import g
    # s  = 1e-1
    # G0 = lambda x: ext_obj(g(adnumber(x)),s)
    # dG0, _ = gh(G0)
    # # Return ordinary values
    # G = lambda x: G0(x).x
    # dG= lambda x: dG0(x).T

    # # Evaluate
    # x0 = np.array([2.5,2.5])
    # val = G0(x0)
    # dif = dG0(x0)
    # # Run BFGS
    # from scipy.optimize import minimize
    # res = minimize(G, x0, method='BFGS', jac=dG0)

    # Test ad on interior objective
    from ad import gh
    from example1 import g, f
    s = 1e-1
    fcn = lambda x: f(x) + log_barrier(g(x)) / 5.0
    dfcn, _ = gh(fcn)
    # Run BFGS
    from scipy.optimize import minimize
    x0 = [0, 0]
    res = minimize(fcn, x0, method='BFGS', jac=dfcn)
Exemple #4
0
    return res

if __name__ == "__main__":
    # # Test ad on an external objective
    # from ad import gh
    # from example1 import g
    # s  = 1e-1
    # G0 = lambda x: ext_obj(g(adnumber(x)),s)
    # dG0, _ = gh(G0)
    # # Return ordinary values
    # G = lambda x: G0(x).x
    # dG= lambda x: dG0(x).T

    # # Evaluate
    # x0 = np.array([2.5,2.5])
    # val = G0(x0)
    # dif = dG0(x0)
    # # Run BFGS
    # from scipy.optimize import minimize
    # res = minimize(G, x0, method='BFGS', jac=dG0)

    # Test ad on interior objective
    from ad import gh
    from example1 import g,f
    s = 1e-1
    fcn = lambda x: f(x) + log_barrier(g(x))/5.0
    dfcn, _ = gh(fcn)
    # Run BFGS
    from scipy.optimize import minimize
    x0 = [0,0]
    res = minimize(fcn, x0, method='BFGS', jac=dfcn)
Exemple #5
0
 def test_1_call(self):
     import example1
     eq_(example1.f(3), 3)
     eq_('description' in example1.f.__doc__, True)
     eq_(example1.f.__name__, 'f')
def test_1_call():
    import example1

    assert example1.f(3) == 3
    assert "description" in example1.f.__doc__
    assert example1.f.__name__ == "f"