def restart_conjugate(_f, _x, n): fun = Function(_f) x = array(_x) while True: d = -fun.grad(x) k=0 if(np.linalg.norm(d)<0.01): break while fun.norm(x) > 0.01: g = np.mat(fun.grad(x)) alpha = wolfe(_f, x, d) x = x + alpha * d k=k+1 g1= np.mat(fun.grad(x)) if np.dot(g1,g.T)/np.dot(g1,g1.T)>0.1 or k>=n: if np.linalg.norm(g1)<0.01: return x break else: beta = np.dot(g1, g1.T) / np.dot(g, g.T) d = array(-g + beta * d)[0] if np.dot(mat(d),g1.T)>0: break return x
def newton(f, start): fun = Function(f) x = array(start) g = fun.grad(x) while fun.norm(x) > 0.01: G = fun.hesse(x) d = (-dot(linalg.inv(G), g)).tolist()[0] alpha = wolfe(f, x, d) x = x + alpha * array(d) g = fun.grad(x) return x
def conjugate(_f, _x): fun = Function(_f) x = np.array(_x) d = -fun.grad(x) while fun.norm(x) > 0.01: alpha = wolfe(_f, x, d) g = np.mat(fun.grad(x)) beta = 1 / np.dot(g, g.T) x = x + alpha * d g = np.mat(fun.grad(x)) beta = beta * np.dot(g, g.T) d = array(-g + beta * d)[0] return x
def sequence(_f, _x, a2): x = array(_x) m = x.size X = range(0, m) n = size(a2) N = range(0, n) def _l(val): value = array(val)[X] r = _f(value) for i in N: a = a2[i] r -= val[i] * a(value) return r def fi(mu): def _fi(x): r = _f(x) for a in a2: r += abs(a(x)) / mu return r return _fi f = Function(_f) l = Function(_l) k = 0 while k < 30: A = Jacobbi_val(a2, x) g = f.grad(x) lamb = solve(A, g) G = l.hesse(concatenate([x, lamb]).tolist())[X, :][:, X] c = range(0, n) for i in range(0, n): a = a2[i] c[i] = a(x) dx = equation_constraint(G, g, A.T, c) mu = 0.5 fi_x = fi(mu) if linalg.norm(c) + linalg.norm(g - dot(A, lamb)) < 0.01 or k == 29: return x alpha = wolfe(fi_x, x, dx.tolist()) x = x + alpha * dx print(x) k = k + 1
def simu_newton(f, start): n = size(start) fun = Function(f) x = array(start) g = fun.grad(x) B = eye(n) while fun.norm(x) > 0.01: d = (-dot(linalg.inv(B), g)).tolist() alpha = wolfe(f, x, d) x_d = array([alpha * array(d)]) x = x + alpha * array(d) g_d = array([fun.grad(x) - g]) g = fun.grad(x) B_d = dot(B, x_d.T) B = B + dot(g_d.T, g_d) / dot(g_d, x_d.T) - dot(B_d, B_d.T) / dot( x_d, B_d) return x