Exemplo n.º 1
0
def Demo():

    # __MACHINE_LEARNING_NETWORK____#################################################

    # Define Domain
    Network = CreateRandomNetwork()
    Domain = MLN(Network)

    # Set Method
    Method = Euler(Domain=Domain,FixStep=True,P=BoxProjection(lo=Domain.los,hi=Domain.his))

    # Set Options
    Term = Termination(MaxIter=500)
    Repo = Reporting(Requests=['Step', 'F Evaluations',
                               'Projections','Data'])
    Misc = Miscellaneous()
    Init = Initialization(Step=-1e-3)
    Options = DescentOptions(Init,Term,Repo,Misc)

    # Initialize Starting Point
    Start = np.random.rand(Domain.dim)*(Domain.his-Domain.los) + Domain.los
    Start[20:22] = [-.5,.5]
    Start[22:24] = [.5,.5]
    Start[24:26] = [.5,.5]
    Start[26:28] = [1.,0.]
    Start[28:32] = [-.5,-.25,-.75,-.5]

    # Print Stats
    PrintSimStats(Domain,Method,Options)

    # Start Solver
    tic = time.time()
    MLN_Results = Solve(Start,Method,Domain,Options)  # Use same Start
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options,MLN_Results,Method,toc)

    ########################################################
    # Animate Network
    ########################################################

    # Construct MP4 Writer
    fps = 5
    FFMpegWriter = animation.writers['ffmpeg']
    metadata = dict(title='MLN', artist='Matplotlib')
    writer = FFMpegWriter(fps=fps, metadata=metadata)

    # Collect Frames
    Frames = MLN_Results.PermStorage['Data'][::fps]

    # Save Animation to File
    fig, ax = plt.subplots()
    MLN_ani = animation.FuncAnimation(fig, Domain.UpdateVisual,
                                      init_func=Domain.InitVisual,
                                      frames=len(Frames),
                                      fargs=(ax, Frames))
    MLN_ani.save('MLN.mp4', writer=writer)
def Demo():

    # __CONSTRAINED_OPTIMIZATION__##############################################

    # Define Domain: f(x) = x_1 + 2*x_2 - 1
    Domain = NewDomain(F=lambda x: np.array([1, 2]), Dim=2)
    Domain.Min = 0.
    Domain.f_Error = lambda x: x[0] + 2 * x[1] - 1 - Domain.Min

    # Define Polytope Constraints: Gx<=h & Ax=b
    G = -np.eye(Domain.Dim)
    h = np.zeros(Domain.Dim)
    A = np.ones((1, Domain.Dim))
    b = 1.

    # Set Method
    P = PolytopeProjection(G=G, h=h, A=A, b=b)
    Method = Euler(Domain=Domain, FixStep=True, P=P)

    # Initialize Starting Point
    Start = np.array([0, 1])

    # Set Options
    Init = Initialization(Step=-0.1)
    Term = Termination(MaxIter=1000, Tols=[(Domain.f_Error, 1e-6)])
    Repo = Reporting(Requests=[
        Domain.f_Error, 'Step', 'F Evaluations', 'Projections', 'Data'
    ])
    Misc = Miscellaneous()
    Options = DescentOptions(Init, Term, Repo, Misc)

    # Print Stats
    PrintSimStats(Domain, Method, Options)

    # Start Solver
    tic = time.time()
    Results = Solve(Start, Method, Domain, Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results, Method, toc)

    error = np.asarray(Results.PermStorage[Domain.f_Error])

    plt.plot(error, '-o', lw=2)
    plt.title('Projected Subgradient')
    plt.xlabel('Iterations')
    plt.ylabel('Objective error')
    plt.ylim([-.1, 1.1])
    plt.show()
Exemplo n.º 3
0
def score_mixturemean(train, test, mask, step=-1e-3, iters=100):
    # Define Domain
    Domain = MixtureMean(Data=train)

    # Set Method
    Method = Euler(Domain=Domain, P=BoxProjection(lo=0., hi=1.))
    # Method = Euler(Domain=Domain,P=EntropicProjection())
    # Method = HeunEuler(Domain=Domain,P=EntropicProjection(),Delta0=1e-5,
    #                    MinStep=-1e-1,MaxStep=-1e-4)

    # Initialize Starting Point
    # Start = np.array([.452,.548])
    Start = np.array([.452])

    # Set Options
    Init = Initialization(Step=step)
    Term = Termination(MaxIter=iters)
    Repo = Reporting(Requests=['Step', 'F Evaluations'])
    Misc = Miscellaneous()
    Options = DescentOptions(Init, Term, Repo, Misc)

    # Print Stats
    PrintSimStats(Domain, Method, Options)

    # Start Solver
    tic = time.time()
    Results = Solve(Start, Method, Domain, Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results, Method, toc)

    # Retrieve result
    parameters = np.asarray(Results.TempStorage['Data'][-1])
    pred = Domain.predict(parameters)

    return rmse(pred, test, mask)
Exemplo n.º 4
0
def Demo():

    #__LQ_GAN__##############################################

    # NEED TO WRITE CODE TO GENERATE N LQ-GANS PER DIMENSION FOR M DIMENSIONS
    # THEN RUN EACH ALGORITHM FROM L STARTING POINTS AND MEASURE RUNTIME AND STEPS
    # UNTIL DESIRED DEGREE OF ACCURACY IS MET, MEASURE ACCURACY WITH EUCLIDEAN DISTANCE TO X^*
    # AND KL DIVERGENCE https://stats.stackexchange.com/questions/60680/kl-divergence-between-two-multivariate-gaussians/60699
    # COMPUTE MIN/MAX/AVG/STD RUNTIME OVER N X L RUNS PER DIMENSION

    # Interpolation between F and RipCurl should probably be nonlinear
    # in terms of L2 norms of matrices if the norms essentially represent the largest eigenvalues

    # what's an example of a pseudomonotone field? stretch vertically linearly a bit? quasimonotone?

    # the extragradient method uses the same step size for the first and second step, as the step size goes
    # to zero, extragradient asymptotes to the projection method
    # modified extragradient methods use different step sizes. if we keep the first step size fixed to some
    # positive value and shrink the second, the dynamics of extragradient remain as desired
    # this is essentially what HE_PhaseSpace is showing
    # HE and HE_PhaseSpace are designed to "simulate" a trajectory - they do not actually change the effective
    # dynamics of the vector field

    # Define Network and Domain
    dim = 1
    s = (dim**2 + dim) // 2
    mu = 10 * np.random.rand(dim)
    mu = np.array([0])
    L = 10 * np.random.rand(dim, dim)
    L[range(dim), range(dim)] = np.clip(L[range(dim), range(dim)], 1e-1,
                                        np.inf)
    L = np.tril(L)
    sig = np.dot(L, L.T)
    sig = np.array([[1]])

    np.set_printoptions(linewidth=200)

    print('mu, sig, sig eigs')
    print(mu)
    print(sig)
    print(np.linalg.eigvals(sig))

    # Set Constraints
    loA = -np.inf * np.ones((dim, dim))
    loA[range(dim), range(dim)] = 1e-2
    lo = np.hstack(
        ([-np.inf] * (dim + s), loA[np.tril_indices(dim)], [-np.inf] * dim))
    P = BoxProjection(lo=lo)

    xoff = 0
    yoff = 0
    scale = 30

    datas = []
    dists = []
    methods = ['ccGD', 'simGD', 'preEG', 'conGD', 'regGD', 'EG']
    # methods = ['simGD']
    # methods = ['ccGD']
    for method in methods:

        if method == 'EG':
            Step = -1e-2
            Iters = 10000
            Domain = LQ(mu=mu, sig=sig, method='simGD')
            Method = EG(Domain=Domain, FixStep=True, P=P)
        elif method == 'simGD':
            Step = -1e-3
            Iters = 100000
            Domain = LQ(mu=mu, sig=sig, method='simGD')
            Method = Euler(Domain=Domain, FixStep=True, P=P)
            # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,MinStep=-1.,P=P)
        elif method == 'conGD':
            Step = -1e-6
            # Iters = 2750
            Iters = 3000
            Domain = LQ(mu=mu, sig=sig, method=method)
            Method = HeunEuler_PhaseSpace(Domain=Domain, Delta0=1e-5, P=P)
        else:
            Step = -1e-5
            Iters = 10000
            Domain = LQ(mu=mu, sig=sig, method=method)
            Method = HeunEuler_PhaseSpace(Domain=Domain, Delta0=1e-5, P=P)

        # Initialize Starting Point
        Start = np.array([50., 0., 30., 0.])

        # Set Options
        Init = Initialization(Step=Step)
        Term = Termination(MaxIter=Iters, Tols=[(Domain.dist, 1e-4)])
        Repo = Reporting(Requests=[
            'Step', 'F Evaluations', 'Projections', 'Data', Domain.dist
        ])
        Misc = Miscellaneous()
        Options = DescentOptions(Init, Term, Repo, Misc)

        # Print Stats
        PrintSimStats(Domain, Method, Options)

        # Start Solver
        tic = time.time()
        LQ_Results = Solve(Start, Method, Domain, Options)
        toc = time.time() - tic

        # Print Results
        PrintSimResults(Options, LQ_Results, Method, toc)

        datas += [np.array(LQ_Results.PermStorage['Data'])]
        dists += [np.array(LQ_Results.PermStorage[Domain.dist])]

    X, Y = np.meshgrid(
        np.arange(-2 * scale + xoff, 2 * scale + xoff, .2 * scale),
        np.arange(1e-2 + yoff, 4 * scale + yoff, .2 * scale))
    U = np.zeros_like(X)
    V = np.zeros_like(Y)
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            vec = -Domain.F(np.array([X[i, j], 0., Y[i, j], 0.]))
            U[i, j] = vec[0]
            V[i, j] = vec[2]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    Q = plt.quiver(X[::3, ::3],
                   Y[::3, ::3],
                   U[::3, ::3],
                   V[::3, ::3],
                   pivot='mid',
                   units='inches')
    colors = ['r', 'gray', 'b', 'k', 'g', 'm']
    # colors = ['gray']
    # colors = ['r']
    for data, color, method in zip(datas, colors, methods):
        if method == 'EG':
            ax.plot(data[:, 0], data[:, 2], '--', color=color, label=method)
        else:
            ax.plot(data[:, 0], data[:, 2], color=color, label=method)
    ax.plot([data[0, 0]], [data[0, 2]], 'k*')
    ax.plot(mu, sig[0], 'c*')
    ax.set_xlim([-2 * scale + xoff, 2 * scale + xoff])
    ax.set_ylim([-.1 * scale + yoff, 4 * scale + yoff])
    ax.set_xlabel(r'$w_2$')
    ax.set_ylabel(r'$a$')
    plt.title('Trajectories for Various Equilibrium Algorithms')
    plt.legend()
    # plt.show()
    # plt.savefig('original.png')
    # plt.savefig('EGoriginal.png')
    # plt.savefig('RipCurl.png')
    # plt.savefig('RipCurl2.png')
    # plt.savefig('EG.png')
    # plt.savefig('GReg.png')
    # plt.savefig('Testing.png')
    # plt.savefig('trajcomp_ccGD.png')
    # plt.savefig('trajcomp.png')
    plt.savefig('trajcomp_test.png')
Exemplo n.º 5
0
def Demo():

    # __POWER_ITERATION__##################################################

    # Define Domain
    A = np.asarray([[-4, 10], [7, 5]])
    A = A.dot(A)  # symmetrize
    # mars = np.load('big_means.npy')
    # A = mars.T.dot(mars)
    eigs = np.linalg.eigvals(A)
    rho = max(eigs) - min(eigs)
    rank = np.count_nonzero(eigs)
    # Domain = PowerIteration(A=A)
    Domain = Rayleigh(A=A)

    # Set Method
    Method_Standard = Euler(Domain=Domain,
                            FixStep=True,
                            P=NormBallProjection())

    # Initialize Starting Point
    Start = np.ones(Domain.Dim)

    # Set Options
    Init = Initialization(Step=-1e-3)
    Term = Termination(MaxIter=100, Tols=[(Domain.res_norm, 1e-6)])
    Repo = Reporting(Requests=[
        Domain.res_norm, 'Step', 'F Evaluations', 'Projections', 'Data'
    ])
    Misc = Miscellaneous()
    Options = DescentOptions(Init, Term, Repo, Misc)

    # Print Stats
    PrintSimStats(Domain, Method_Standard, Options)

    # Start Solver
    tic = time.time()
    Results_Standard = Solve(Start, Method_Standard, Domain, Options)
    toc_standard = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results_Standard, Method_Standard, toc_standard)

    # data_standard = Results_Standard.PermStorage['Data']
    # eigval_standard = (A.dot(data_standard[-1])/data_standard[-1]).mean()
    # eigvec_standard = data_standard[-1]
    res_standard = Results_Standard.PermStorage[Domain.res_norm]

    # Set Method
    # Method_CK = CashKarp(Domain=Domain,Delta0=1e-4,P=NormBallProjection())
    Method_CK = HeunEuler(Domain=Domain, Delta0=1e-4, P=NormBallProjection())

    # Print Stats
    PrintSimStats(Domain, Method_CK, Options)

    # Start Solver
    tic = time.time()
    Results_CK = Solve(Start, Method_CK, Domain, Options)
    toc_CK = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results_CK, Method_CK, toc_CK)

    # data_CK = Results_CK.PermStorage['Data']
    # eigval_CK = (A.dot(data_CK[-1])/data_CK[-1]).mean()
    # eigvec_CK = data_CK[-1]
    res_CK = Results_CK.PermStorage[Domain.res_norm]

    # Set Method
    # Method_CKPS = CashKarp_PhaseSpace(Domain=Domain,Delta0=1e-4,
    #                                   P=NormBallProjection())
    Method_CKPS = HeunEuler_PhaseSpace(Domain=Domain,
                                       Delta0=1e-1,
                                       P=NormBallProjection())

    # Print Stats
    PrintSimStats(Domain, Method_CKPS, Options)

    # Start Solver
    tic = time.time()
    Results_CKPS = Solve(Start, Method_CKPS, Domain, Options)
    toc_CKPS = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results_CK, Method_CK, toc_CKPS)

    # data_CKPS = Results_CKPS.PermStorage['Data']
    # eigval_CKPS = (A.dot(data_CKPS[-1])/data_CKPS[-1]).mean()
    # eigvec_CKPS = data_CKPS[-1]
    res_CKPS = Results_CKPS.PermStorage[Domain.res_norm]

    # tic = time.time()
    # eigval_NP, eigvec_NP = eigh(A,eigvals=(Domain.Dim-1,Domain.Dim-1))
    # toc_NP = time.time() - start

    # Plot Results
    fig = plt.figure()
    ax = fig.add_subplot(2, 1, 1)

    # label = 'Standard Power Iteration with scaling' +\
    #     r' $A \cdot v / ||A \cdot v||$'
    label = 'Standard'
    ax.plot(res_standard, label=label)

    fevals_CK = Results_CK.PermStorage['F Evaluations'][-1]
    # label = Method_CK.__class__.__name__+r' Power Iteration'
    # label += r' $\Delta_0=$'+'{:.0e}'.format(Method_CK.Delta0)
    label = 'CK'
    x = np.linspace(0, fevals_CK, len(res_CK))
    ax.plot(x, res_CK, label=label)

    fevals_CKPS = Results_CKPS.PermStorage['F Evaluations'][-1]
    # label = Method_CKPS.__class__.__name__+' Power Iteration'
    # label += r' $\Delta_0=$'+'{:.0e}'.format(Method_CKPS.Delta0)
    label = 'CKPS'
    x = np.linspace(0, fevals_CKPS, len(res_CKPS))
    ax.plot(x, res_CKPS, '-.', label=label)

    xlabel = r'# of $A \cdot v$ Evaluations'
    ax.set_xlabel(xlabel)

    ylabel = r'Norm of residual ($||\frac{A \cdot v}{||A \cdot v||}$'
    ylabel += r'$ - \frac{v}{||v||}||$)'
    ax.set_ylabel(ylabel)

    sizestr = str(A.shape[0]) + r' $\times$ ' + str(A.shape[1])
    if rho > 100:
        rhostr = r'$\rho(A)=$' + '{:.0e}'.format(rho)
    else:
        rhostr = r'$\rho(A)=$' + str(rho)
    rnkstr = r'$rank(A)=$' + str(rank)
    plt.title(sizestr + ' Matrix with ' + rhostr + ', ' + rnkstr)

    ax.legend()

    xlim = min(max(len(res_standard), fevals_CK, fevals_CKPS), Term.Tols[0])
    xlim = int(np.ceil(xlim / 10.) * 10)
    ax.set_xlim([0, xlim])

    ax.set_yscale('log', nonposy='clip')

    ax2 = fig.add_subplot(2, 1, 2)

    # label = 'Standard Power Iteration with scaling' +\
    #     r' $A \cdot v / ||A \cdot v||$'
    label = 'Standard'
    ax2.plot(res_standard, label=label)

    # label = Method_CK.__class__.__name__+r' Power Iteration'
    # label += r' $\Delta_0=$'+'{:.0e}'.format(Method_CK.Delta0)
    label = 'CK'
    ax2.plot(res_CK, label=label)

    # label = Method_CKPS.__class__.__name__+' Power Iteration'
    # label += r' $\Delta_0=$'+'{:.0e}'.format(Method_CKPS.Delta0)
    label = 'CKPS'
    ax2.plot(res_CKPS, '-.', label=label)

    xlabel = r'# of Iterations'
    ax2.set_xlabel(xlabel)

    ylabel = r'Norm of residual ($||\frac{A \cdot v}{||A \cdot v||}$'
    ylabel += r'$ - \frac{v}{||v||}||$)'
    ax2.set_ylabel(ylabel)

    ax2.legend()

    xlim = min(max(len(res_standard), len(res_CK), len(res_CKPS)),
               Term.Tols[0])
    xlim = int(np.ceil(xlim / 10.) * 10)
    ax2.set_xlim([0, xlim])

    ax2.set_yscale('log', nonposy='clip')

    plt.show()

    embed()
Exemplo n.º 6
0
def Demo_NdLQGAN(root='lqganresults/', Dims=None, Algorithms=None, File=None):
    # Dims = [1]
    Dims = [1,2]
    Nmusigmas = 10
    Lstarts = 10
    Algorithms = dict(zip(['Fsim','EG','Fcon','Freg','Feg','Fcc'],range(6)))
    # Algorithms = dict(zip(['Fcc','Fsim','Feg','Fcon','Freg','EG'],range(6)))
    # Algorithms = dict(zip(['Fcc','Feg','Freg'],range(3)))
    # Algorithms = dict(zip(['Fcc','Feg'],range(2)))
    # Algorithms = dict(zip(['Fcc','Feg','Fccprime','Fegprime'],range(4)))
    # Algorithms = dict(zip(['Fccprime','Fegprime'],range(2)))
    # Algorithms = dict(zip(['Fcc'],range(1)))
    # Algorithms = ['Fcon']
    # Algorithms = dict(zip(['Fcc','Feg','Fcon','Freg','Falt','Funr'],range(6)))

    Iters = 10000

    data = np.empty((len(Dims),Nmusigmas,Lstarts,len(Algorithms),10))

    for i, dim in enumerate(tqdm.tqdm(Dims)):
        Domain = LQGAN(dim=dim, var_only=True)

        # Dimensionality
        s = Domain.s
        pdim = Domain.Dim

        # Set Constraints
        loA = -np.inf*np.ones((dim,dim))
        loA[range(dim),range(dim)] = 1e-2
        lo = np.hstack(([-np.inf]*(dim+s), loA[np.tril_indices(dim)], [-np.inf]*dim))
        P = BoxProjection(lo=lo)

        for musigma in tqdm.tqdm(range(Nmusigmas)):
            # Reset LQGAN to random mu and Sigma
            mu, Sigma = rand_mu_Sigma(dim=dim)
            # print(Sigma.flatten().item())
            mu = np.zeros(dim)
            # Sigma = np.array([[1]])
            # Sigma = np.diag(np.random.rand(dim)*10+1.)
            lams = np.linalg.eigvals(Sigma)
            K = lams.max()/lams.min()
            # print(np.linalg.eigvals(Sigma))
            Domain.set_mu_sigma(mu=mu,sigma=Sigma)
            # print(mu,Sigma)

            for l in tqdm.tqdm(range(Lstarts)):
                # Intialize G and D variables to random starting point
                # should initialize A to square root of random Sigma
                # Start = P.P(10*np.random.rand(pdim)-5.)
                Start = np.zeros(pdim)
                Start[:s] = np.random.rand(s)*10-5
                Start[s:s+dim] = 0.  # set w_1 to zero
                Start[-dim:] = mu  # set b to mu
                Start[-dim-s:-dim] = np.linalg.cholesky(rand_mu_Sigma(dim=dim)[1])[np.tril_indices(dim)]
                Start = P.P(Start)

                # Calculate Initial KL and Euclidean distance
                KL_0 = Domain.dist_KL(Start)
                Euc_0 = Domain.dist_Euclidean(Start)
                norm_F_0 = Domain.norm_F(Start)
                # print(KL_0,Euc_0,norm_F_0)

                for j, alg in enumerate(tqdm.tqdm(Algorithms)):

                    # Step = -1e-3
                    if alg == 'EG':
                        Step = -1e-3
                        Domain.preconditioner = 'Fsim'
                        Method = EG(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Fsim':
                        Step = -1e-4
                        Domain.preconditioner='Fsim'
                        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-3,P=P,MinStep=-1e-2)
                        # Method = Euler(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Fcon':
                        Step = -1e-4
                        Domain.preconditioner = alg
                        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-3,P=P,MinStep=-1e-2)
                        # Method = Euler(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Falt' or alg == 'Funr':
                        Step = -5e-3
                        Domain.preconditioner = alg
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,P=P)
                        Method = Euler(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Fccprime' or alg == 'Fegprime':
                        Step = -1e-1
                        Domain.preconditioner = alg
                        Method = Euler(Domain=Domain,FixStep=True,P=P)
                    else:
                        # Step = -1e-1
                        Step = -1e-4
                        Domain.preconditioner = alg
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-0,P=P,MinStep=-10.)  # for 2d+
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-2,P=P,MinStep=-1e-1) # 13 slow
                        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-3,P=P,MinStep=-1e-2) # slow
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1,P=P,MinStep=-1e-1) better
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1,P=P,MinStep=-1e-10) slow
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1,P=P,MinStep=-1e-2) best so far, Feg is not working well?
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-2,P=P,MinStep=-1e-2) ok
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-2,P=P,MinStep=-1e-1)
                        # Delta0=1e-2,MinStep=-1e-1 for speed on 1d, scaled versions are surprisingly worse
                    
                    # Set Options
                    Init = Initialization(Step=Step)
                    Tols = [#(Domain.norm_F,1e-3*norm_F_0),
                            # (Domain.dist_KL,1e-3*KL_0),
                            (Domain.dist_Euclidean,1e-3*Euc_0),
                            (Domain.isNotNaNInf,False)]
                    Term = Termination(MaxIter=Iters,Tols=Tols,verbose=False)
                    Repo = Reporting(Requests=[Domain.norm_F, Domain.dist, 
                                               Domain.dist_KL, Domain.dist_Euclidean,
                                               Domain.isNotNaNInf])
                    # 'Step','Data',
                    Misc = Miscellaneous()
                    Options = DescentOptions(Init,Term,Repo,Misc)

                    # Start Solver
                    tic = time.time()
                    LQ_Results = Solve(Start,Method,Domain,Options)
                    toc = time.time() - tic

                    KL = LQ_Results.PermStorage[Domain.dist_KL][-1]
                    Euc = LQ_Results.PermStorage[Domain.dist_Euclidean][-1]
                    norm_F = LQ_Results.PermStorage[Domain.norm_F][-1]
                    # x = np.array(LQ_Results.PermStorage['Data'])
                    # Steps = np.array(LQ_Results.PermStorage['Step'])
                    runtime = toc
                    steps = LQ_Results.thisPermIndex
                    # embed()

                    data[i,musigma,l,j,:] = np.array([dim,musigma,Algorithms[alg],l,K,KL/KL_0,Euc/Euc_0,norm_F/norm_F_0,runtime,steps])
                    # embed()
            np.save(root+'results15.npy', data)
Exemplo n.º 7
0
def Demo_1dLQGAN(root='figures/'):
    ##############################################################################
    # Compre Trajectories Plot ###################################################
    ##############################################################################

    # Define Network and Domain
    dim = 1
    s = (dim**2+dim)//2
    mu = np.array([0])
    sig = np.array([[1]])

    np.set_printoptions(linewidth=200)

    # Set Constraints
    loA = -np.inf*np.ones((dim,dim))
    loA[range(dim),range(dim)] = 1e-2
    lo = np.hstack(([-np.inf]*(dim+s), loA[np.tril_indices(dim)], [-np.inf]*dim))
    P = BoxProjection(lo=lo)

    xoff = 0
    yoff = 0
    scale = 30

    datas = []
    dists = []
    methods = ['Fcc','Fsim','Feg','Fcon','Freg','EG','Falt','Funr']
    for method in methods:

        if method == 'EG':
            Step = -1e-2
            Iters = 10000
            Domain = LQGAN(mu=mu,sig=sig,preconditioner='Fsim')
            Method = EG(Domain=Domain,FixStep=True,P=P)
        elif method == 'Fsim':
            Step = -1e-3
            Iters = 100000
            Domain = LQGAN(mu=mu,sig=sig,preconditioner='Fsim')
            Method = Euler(Domain=Domain,FixStep=True,P=P)
            # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,MinStep=-1.,P=P)
        elif method == 'Fcon':
            Step = -1e-6
            # Iters = 2750
            Iters = 3000
            Domain = LQGAN(mu=mu,sig=sig,preconditioner=method)
            Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,P=P)
        elif method == 'Falt' or method == 'Funr':
            Step = -5e-3
            # Iters = 2750
            Iters = 10000
            Domain = LQGAN(mu=mu,sig=sig,preconditioner=method)
            # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,P=P)
            Method = Euler(Domain=Domain,FixStep=True,P=P)
        else:
            Step = -1e-5
            Iters = 10000
            Domain = LQGAN(mu=mu,sig=sig,preconditioner=method)
            Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,P=P)
        
        # Initialize Starting Point
        Start = np.array([50.,0.,30.,0.])

        # Set Options
        Init = Initialization(Step=Step)
        Term = Termination(MaxIter=Iters,Tols=[(Domain.dist_Euclidean,1e-4)])
        Repo = Reporting(Requests=['Step', 'F Evaluations',
                                   'Projections','Data',Domain.dist_Euclidean])
        Misc = Miscellaneous()
        Options = DescentOptions(Init,Term,Repo,Misc)

        # Print Stats
        PrintSimStats(Domain,Method,Options)

        # Start Solver
        tic = time.time()
        LQ_Results = Solve(Start,Method,Domain,Options)
        toc = time.time() - tic

        # Print Results
        PrintSimResults(Options,LQ_Results,Method,toc)

        datas += [np.array(LQ_Results.PermStorage['Data'])]
        dists += [np.array(LQ_Results.PermStorage[Domain.dist_Euclidean])]

    X, Y = np.meshgrid(np.linspace(-2*scale + xoff, 2*scale + xoff, 21), np.arange(1e-2 + yoff, 4*scale + yoff, .2*scale))
    U = np.zeros_like(X)
    V = np.zeros_like(Y)
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            vec = -Domain._F(np.array([X[i,j],0.,Y[i,j],0.]))
            U[i,j] = vec[0]
            V[i,j] = vec[2]

    fs = 18
    fig = plt.figure()
    ax = fig.add_subplot(111)
    Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
                   pivot='mid', units='inches')
    colors = ['r','c','b','k','g','m','gray','orange']
    methods = [r'$F_{cc}$',r'$F$',r'$F_{eg}$',r'$F_{con}$',r'$F_{reg}$',r'$EG$',r'$F_{alt}$',r'$F_{unr}$']
    for data, color, method in zip(datas,colors,methods):
        if method == 'EG' or method == 'Funr':
            ax.plot(data[:,0],data[:,2],'-.',color=color,label=method,zorder=0)
        else:
            ax.plot(data[:,0],data[:,2],color=color,label=method,zorder=0)
    ax.plot([data[0,0]],[data[0,2]],'k*')
    ax.plot(mu,sig[0],'*',color='gold')
    # ax.set_xlim([-2*scale + xoff,2*scale + xoff])
    # ax.set_ylim([-.1*scale + yoff,4*scale + yoff])
    ax.set_xlim([-60., 60.])
    ax.set_ylim([-.1*scale + yoff, 100.])
    ax.set_xlabel(r'$w_2$', fontsize=fs)
    ax.set_ylabel(r'$a$', fontsize=fs)
    for tick in ax.xaxis.get_major_ticks():
        tick.label.set_fontsize(14)
    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(14)
    plt.title('Trajectories for Various Equilibrium Algorithms', fontsize=16)
    # plt.legend(fontsize=fs, loc="upper left")

    fs = 24
    # locs = [(15,25),(-35,80),(0,40),(42.5,3.0),(20,2.5),(-30,35),(-40,20),(-20,20)]
    # locs = [(15,25),(-35,80),(2.5,40),(42.5,3.0),(20,2.5),(-45,45),(-42.5,5),(-22.5,5)]
    locs = [(15,25),(-35,75),(2.5,40),(42.5,3.0),(20,2.5),(-45,45),(-42.5,5),(-22.5,5)]
    for method,color,loc in zip(methods,colors,locs):
        ax.annotate(method, xy=loc, color=color, zorder=1, fontsize=fs,
                    bbox=dict(facecolor='white', edgecolor='white', pad=0.0))

    plt.savefig(root+'trajcomp.png',bbox_inches='tight')

    ##############################################################################
    # Euclidean Distance to Equilibrium vs Iterations ############################
    ##############################################################################

    # Old Version of Plot
    # # fig = plt.figure()
    # # ax = fig.add_subplot(111)
    # # colors = ['r','gray','b','k','g']
    # # for dist, color, method in zip(dists,colors,methods):
    # #     ax.plot(dist,color=color,label=method)
    # # ax.set_xlabel('Iterations')
    # # ax.set_ylabel('Distance to Equilibrium')
    # # plt.legend()
    # # plt.title('Iteration vs Distance to Equilibrium for Various Equilibrium Algorithms')
    # # plt.savefig('runtimecomp.png')

    # More Recent Version of Plot
    fig,(ax,ax2) = plt.subplots(1,2,sharey=True, facecolor='w')
    for dist, color, method in zip(dists,colors,methods):
        # if method == 'simGD':
        iters = np.arange(dist.shape[0])/1000
        # print(dist[:5])
        ax.plot(iters,dist,color=color,label=method)
        ax2.plot(iters,dist,color=color,label=method)
        # else:
        #     ax.plot(dist,color=color,label=method)
    ax.set_xlim(-.1,10)
    ax.set_xticks([0,2,4,6,8,10])
    ax2.set_xlim(50,100)
    ax.set_ylim(-5,95)
    ax2.set_ylim(-5,95)

    # hide the spines between ax and ax2
    ax.spines['right'].set_visible(False)
    ax2.spines['left'].set_visible(False)
    ax.yaxis.tick_left()
    ax.tick_params(labelright='off')
    ax2.yaxis.tick_right()
    ax2.set_xticks([75,100])

    d = .015 # how big to make the diagonal lines in axes coordinates
    # arguments to pass plot, just so we don't keep repeating them
    kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
    ax.plot((1-d,1+d), (-d,+d), **kwargs)
    ax.plot((1-d,1+d),(1-d,1+d), **kwargs)

    kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
    ax2.plot((-d,+d), (1-d,1+d), **kwargs)
    ax2.plot((-d,+d), (-d,+d), **kwargs)

    # ax.set_xlabel('Iterations')
    fig.text(0.5, 0.02, 'Thousand Iterations', ha='center', fontsize=12)
    ax.set_ylabel('Distance to Equilibrium',fontsize=12)
    # ax.legend()
    ax2.legend(fontsize=18)
    plt.suptitle('Iteration vs Distance to Equilibrium for Various Equilibrium Algorithms')
    plt.savefig(root+'runtimecomp.png')

    ##############################################################################
    # Individual Trajectory Plots ################################################
    ##############################################################################

    # Set Constraints
    loA = -np.inf*np.ones((dim,dim))
    loA[range(dim),range(dim)] = 1e-4
    lo = np.hstack(([-np.inf]*(dim+s), loA[np.tril_indices(dim)], [-np.inf]*dim))
    P = BoxProjection(lo=lo)

    xlo, xhi = -5, 5
    ylo, yhi = 0, 2
    methods = ['Fcc','Feg','Fcon','Freg','Falt','Funr']
    colors = ['r','b','k','g','gray','orange']
    for method, color in zip(methods,colors):
        Step = -1e-5
        Iters = 10000
        Domain = LQGAN(mu=mu,sig=sig,preconditioner=method)
        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,P=P)
        # Iters = 10000
        # Method = Euler(Domain=Domain,FixStep=True)
        
        # Initialize Starting Point
        Start = np.array([3.,0.,0.2,0.])

        # Set Options
        Init = Initialization(Step=Step)
        Term = Termination(MaxIter=Iters,Tols=[(Domain.dist_Euclidean,1e-4)])
        Repo = Reporting(Requests=['Step', 'F Evaluations',
                                   'Projections','Data',Domain.dist_Euclidean])
        Misc = Miscellaneous()
        Options = DescentOptions(Init,Term,Repo,Misc)

        # Print Stats
        PrintSimStats(Domain,Method,Options)

        # Start Solver
        tic = time.time()
        LQ_Results = Solve(Start,Method,Domain,Options)
        toc = time.time() - tic

        # Print Results
        PrintSimResults(Options,LQ_Results,Method,toc)

        data = np.array(LQ_Results.PermStorage['Data'])
        
        X, Y = np.meshgrid(np.linspace(xlo, xhi, 50), np.linspace(ylo, yhi, 50))
        U = np.zeros_like(X)
        V = np.zeros_like(Y)
        for i in range(X.shape[0]):
            for j in range(X.shape[1]):
                vec = -Domain.F(np.array([X[i,j],0.,Y[i,j],0.]))
                U[i,j] = vec[0]
                V[i,j] = vec[2]

        fig = plt.figure()
        ax = fig.add_subplot(111)
        Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
                       pivot='mid', units='inches')
        ax.plot(data[:,0],data[:,2],color=color,label=method)
        ax.plot([data[0,0]],[data[0,2]],'k*')
        ax.plot(mu,sig[0],'c*')
        ax.set_xlim([xlo, xhi])
        ax.set_ylim([0, yhi])
        ax.set_xlabel(r'$w_2$')
        ax.set_ylabel(r'$a$')
        plt.title('Dynamics for '+method)
        plt.savefig(root+method+'_dyn')
Exemplo n.º 8
0
def Demo():

    # __KACZMARZ__###############################################################

    # Define Domain
    Domain = NewDomain(F=lambda x: 0, Dim=5)

    # Define Hyperplanes
    dim = max(Domain.Dim - 1, 1)
    N = 3
    hyps = [np.random.rand(Domain.Dim, dim) for n in xrange(N)]

    # Set Method
    P_random = HyperplaneProjection(hyperplanes=hyps, sequence='random')
    Method = Euler(Domain=Domain, FixStep=True, P=P_random)

    # Initialize Starting Point
    Start = np.ones(Domain.Dim)

    # Set Options
    Init = Initialization(Step=0.)
    Term = Termination(MaxIter=50)
    Repo = Reporting(Requests=['Step', 'Data'])
    Misc = Miscellaneous()
    Options = DescentOptions(Init, Term, Repo, Misc)

    # Print Stats
    PrintSimStats(Domain, Method, Options)

    # Start Solver
    tic = time.time()
    Results = Solve(Start, Method, Domain, Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results, Method, toc)

    data_random = Results.PermStorage['Data']

    # Set Method
    P_cyclic = HyperplaneProjection(hyperplanes=hyps, sequence='cyclic')
    Method = Euler(Domain=Domain, FixStep=True, P=P_cyclic)

    # Print Stats
    PrintSimStats(Domain, Method, Options)

    # Start Solver
    tic = time.time()
    Results = Solve(Start, Method, Domain, Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results, Method, toc)

    data_cyclic = Results.PermStorage['Data']

    # Set Method
    P_distal = HyperplaneProjection(hyperplanes=hyps, sequence='distal')
    Method = Euler(Domain=Domain, FixStep=True, P=P_distal)

    # Print Stats
    PrintSimStats(Domain, Method, Options)

    # Start Solver
    tic = time.time()
    Results = Solve(Start, Method, Domain, Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options, Results, Method, toc)

    data_distal = Results.PermStorage['Data']

    # diff_norm_random = np.linalg.norm(np.diff(data_random),axis=1)
    # diff_norm_cyclic = np.linalg.norm(np.diff(data_cyclic),axis=1)
    # diff_norm_distal = np.linalg.norm(np.diff(data_distal),axis=1)\
    # plt.plot(diff_norm_random,label='random')
    # plt.plot(diff_norm_cyclic,label='cyclic')
    # plt.plot(diff_norm_distal,label='distal')
    er = [
        np.linalg.norm(P_random.errors(d), axis=1).max() for d in data_random
    ]
    ec = [
        np.linalg.norm(P_cyclic.errors(d), axis=1).max() for d in data_cyclic
    ]
    ed = [
        np.linalg.norm(P_distal.errors(d), axis=1).max() for d in data_distal
    ]
    plt.plot(er, label='random')
    plt.plot(ec, label='cyclic')
    plt.plot(ed, label='distal')
    plt.legend()
    plt.title('Kaczmarz Algorithm')
    plt.xlabel('Iterations')
    plt.ylabel('Maximum distance from any hyperplane')
    plt.show()
Exemplo n.º 9
0
def Demo():

    #__SPHERE__##################################################

    # Define Dimension and Domain
    Domain = MonotoneCycle()

    # Set Method
    MethodEuler = Euler(Domain=Domain,P=IdentityProjection(),FixStep=True)
    MethodEG = EG(Domain=Domain,P=IdentityProjection(),FixStep=True)

    # Set Options
    Init = Initialization(Step=-1e-1)
    Term = Termination(MaxIter=100)
    Repo = Reporting(Requests=['Data'])
    Misc = Miscellaneous()
    Options = DescentOptions(Init,Term,Repo,Misc)

    # Initialize Starting Point
    Start = np.ones(Domain.Dim)

    # Print Stats
    PrintSimStats(Domain,MethodEuler,Options)

    # Start Solver
    tic = time.time()
    Euler_Results = Solve(Start,MethodEuler,Domain,Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options,Euler_Results,MethodEuler,toc)

    # Print Stats
    PrintSimStats(Domain,MethodEG,Options)

    # Start Solver
    tic = time.time()
    EG_Results = Solve(Start,MethodEG,Domain,Options)
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options,EG_Results,MethodEG,toc)

    data_Euler = ListONP2NP(Euler_Results.PermStorage['Data'])
    data_EG = ListONP2NP(EG_Results.PermStorage['Data'])
    

    X, Y = np.meshgrid(np.arange(-2.5, 2.5, .2), np.arange(-2.5, 2.5, .2))
    U = np.zeros_like(X)
    V = np.zeros_like(Y)
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            vec = -Domain.F([X[i,j],Y[i,j]])
            U[i,j] = vec[0]
            V[i,j] = vec[1]

    # plt.figure()
    # plt.title('Arrows scale with plot width, not view')
    # Q = plt.quiver(X, Y, U, V, units='width')
    # qk = plt.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',
    #                    coordinates='figure')

    fig = plt.figure()
    ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])
    plt.title("Extragradient vs Simultaneous Gradient Descent")
    Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
                   pivot='mid', units='inches')
    vec_Euler = -Domain.F(data_Euler[-1,:])
    vec_EG = -Domain.F(data_EG[-1,:])
    print(data_Euler[-1])
    print(vec_Euler)
    plt.quiver(data_Euler[-1][0], data_Euler[-1][1], vec_Euler[0], vec_Euler[1], pivot='mid', units='inches', color='b',
            headwidth=5)
    plt.quiver(data_EG[-1][0], data_EG[-1][1], vec_EG[0], vec_EG[1], pivot='mid', units='inches', color='r',
            headwidth=5)
    plt.scatter(X[::3, ::3], Y[::3, ::3], color='gray', s=5)
    plt.plot(data_Euler[:,0],data_Euler[:,1],'b',linewidth=5,label='Simultaneous\nGradient Descent')
    plt.plot(data_EG[:,0],data_EG[:,1],'r',linewidth=5,label='Extragradient')
    plt.plot([0],[0],linestyle="None",marker=(5,1,0),markersize=20,color='gold',label='Equilibrium')
    plt.axis([-2.5,2.5,-2.5,2.5])
    plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), borderaxespad=0.5)
    # plt.ion()
    # plt.show()
    plt.savefig('EGvsEuler.png')
Exemplo n.º 10
0
def Demo():

    #__ONLINE_MONOTONE_EQUILIBRATION_DEMO_OF_A_SERVICE_ORIENTED_INTERNET__######

    # Define Number of Different VIs
    N = 10
    np.random.seed(0)

    # Define Initial Network and Domain
    World = np.random.randint(N)
    Worlds = [World]
    Network = CreateRandomNetwork(m=3, n=2, o=2, seed=World)
    Domain = SOI(Network=Network, alpha=2)

    # Define Initial Strategy
    Strategies = [np.zeros(Domain.Dim)]
    eta = 0.1

    for t in range(1000):

        #__PERFORM_SINGLE_UPDATE

        print('Time ' + str(t))

        # Set Method
        Method = Euler(Domain=Domain, P=BoxProjection(lo=0))

        # Set Options
        Init = Initialization(Step=-eta)
        Term = Termination(MaxIter=1)
        Repo = Reporting(Requests=['Data'])
        Misc = Miscellaneous()
        Options = DescentOptions(Init, Term, Repo, Misc)

        # Run Update
        Result = Solve(Strategies[-1], Method, Domain, Options)

        # Get New Strategy
        Strategy = Result.PermStorage['Data'][-1]
        Strategies += [Strategy]

        #__DEFINE_NEXT_VI

        # Define Initial Network and Domain
        World = np.random.randint(N)
        Worlds += [World]
        Network = CreateRandomNetwork(m=3, n=2, o=2, seed=World)
        Domain = SOI(Network=Network, alpha=2)

    # Scrap Last Strategy / World
    Strategies = np.asarray(Strategies[:-1])
    Worlds = Worlds[:-1]

    # Store Equilibrium Strategies
    Equilibria = dict()

    for w in np.unique(Worlds):

        print('World ' + str(w))

        #__FIND_EQUILIBRIUM_SOLUTION_OF_VI

        # Define Initial Network and Domain
        Network = CreateRandomNetwork(m=3, n=2, o=2, seed=w)
        Domain = SOI(Network=Network, alpha=2)

        # Set Method
        Method = HeunEuler(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-5)

        # Initialize Starting Point
        Start = np.zeros(Domain.Dim)

        # Calculate Initial Gap
        gap_0 = Domain.gap_rplus(Start)

        # Set Options
        Init = Initialization(Step=-1e-10)
        Term = Termination(MaxIter=25000,
                           Tols=[(Domain.gap_rplus, 1e-6 * gap_0)])
        Repo = Reporting(Requests=[Domain.gap_rplus, 'Step', 'Data'])
        Misc = Miscellaneous()
        Options = DescentOptions(Init, Term, Repo, Misc)

        # Print Stats
        PrintSimStats(Domain, Method, Options)

        # Start Solver
        tic = time.time()
        Results = Solve(Start, Method, Domain, Options)
        toc = time.time() - tic

        # Print Results
        PrintSimResults(Options, Results, Method, toc)

        # Get Equilibrium Strategy
        Equilibrium = Results.PermStorage['Data'][-1]
        Equilibria[w] = Equilibrium

    # Matched Equilibria & Costs
    Equilibria_Matched = np.asarray([Equilibria[w] for w in Worlds])

    # Compute Mean of Equilibria
    Mean_Equilibrium = np.mean(Equilibria_Matched, axis=0)

    # Compute Strategies Distance From Mean Equilibrium
    Distance_From_Mean = np.linalg.norm(Strategies - Mean_Equilibrium, axis=1)

    # Plot Results
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 1, 1)
    ax1.plot(Distance_From_Mean, label='Distance from Mean')
    ax1.set_title('Online Monotone Equilibration of Dynamic SOI Network')
    ax1.legend()
    ax1.set_xlabel('Time')

    plt.savefig('OMEfast.png')

    embed()
Exemplo n.º 11
0
def Demo():

    # __ROSENBROCK__############################################################
    # __STEEPEST_DESCENT__######################################################

    # Define Domain
    Domain = Rosenbrock(Dim=2)

    # Set Method
    Method = Euler(Domain=Domain, FixStep=True)
    # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1)
    # Method = HeunEuler_AdaGrad_PhaseSpace(Domain=Domain,Delta0=1e-1)

    # Set Options
    Term = Termination(MaxIter=20000, Tols=[(Domain.f_Error, 1e-6)])
    Repo = Reporting(Requests=[
        Domain.f_Error, 'Step', 'F Evaluations', 'Projections', 'Data'
    ])
    Misc = Miscellaneous()

    # Set starting point and step size ranges
    r = 2
    rads = np.linspace(0, 2 * np.pi, num=4, endpoint=False)
    steps = -np.logspace(-5, -3, num=3, endpoint=True)
    # steps = -np.logspace(-8,-6,num=3,endpoint=True)

    # Construct figure
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.view_init(azim=-160.)
    X = np.arange(-1.5, 3.5, 0.25)
    Y = np.arange(-1.5, 3.5, 0.25)
    X, Y = np.meshgrid(X, Y)
    Z = np.zeros_like(X)
    for i in xrange(Z.shape[0]):
        for j in xrange(Z.shape[1]):
            Z[i, j] = Domain.f(np.array([X[i, j], Y[i, j]]))
    ax.plot_surface(X,
                    Y,
                    Z,
                    rstride=1,
                    cstride=1,
                    cmap=cm.coolwarm,
                    linewidth=0,
                    antialiased=False)
    ax.set_xlim3d(np.min(X), np.max(X))
    ax.set_ylim3d(np.min(Y), np.max(Y))
    ax.set_zlim3d(np.min(Z), np.max(Z))
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.zaxis.set_rotate_label(False)  # disable automatic rotation
    ax.set_zlabel('f(X,Y)', rotation=90)
    ax.text2D(0.05,
              0.95,
              'Steepest Descent on the\nRosenbrock Function',
              transform=ax.transAxes)
    lw = cycle(range(2 * len(steps), 0, -2))
    plt.ion()
    plt.show()

    # Compute trajectories
    for rad, step in itertools.product(rads, steps):

        # Initialize Starting Point
        assert Domain.Dim == 2
        Start = Domain.ArgMin + r * np.asarray([np.cos(rad), np.sin(rad)])

        # Set Options
        Init = Initialization(Step=step)
        Options = DescentOptions(Init, Term, Repo, Misc)

        # Print Stats
        PrintSimStats(Domain, Method, Options)

        # Start Solver
        tic = time.time()
        Rosenbrock_Results = Solve(Start, Method, Domain, Options)
        toc = time.time() - tic

        # Print Results
        PrintSimResults(Options, Rosenbrock_Results, Method, toc)

        # Plot Results
        data_SD = Rosenbrock_Results.PermStorage['Data']
        res_SD = np.asarray(Rosenbrock_Results.PermStorage[Domain.f_Error])
        res_SD[np.isnan(res_SD)] = np.inf
        trajX = []
        trajY = []
        trajZ = []
        for i in xrange(len(data_SD)):
            trajX.append(data_SD[i][0])
            trajY.append(data_SD[i][1])
            trajZ.append(res_SD[i])
        ax.plot(trajX, trajY, trajZ, lw=next(lw))
        plt.draw()
    plt.ioff()
    plt.show()

    # __NEWTONS_METHOD____######################################################

    # Define Domain
    Domain = Rosenbrock(Dim=2, Newton=True)

    # Set Method
    Method = Euler(Domain=Domain, FixStep=True)

    # Set Options
    Term = Termination(MaxIter=20000, Tols=[(Domain.f_Error, 1e-6)])
    Repo = Reporting(Requests=[
        Domain.f_Error, 'Step', 'F Evaluations', 'Projections', 'Data'
    ])
    Misc = Miscellaneous()
    Init = Initialization(Step=-1e-3)
    Options = DescentOptions(Init, Term, Repo, Misc)

    # Print Stats
    PrintSimStats(Domain, Method, Options)

    # Start Solver
    tic = time.time()
    Rosenbrock_Results = Solve(Start, Method, Domain,
                               Options)  # Use same Start
    toc = time.time() - tic

    # Print Results
    PrintSimResults(Options, Rosenbrock_Results, Method, toc)

    # Plot Results
    data_N = Rosenbrock_Results.PermStorage['Data']
    res_N = np.asarray(Rosenbrock_Results.PermStorage[Domain.f_Error])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(res_N, label='Newton' 's Method')
    ax.plot(res_SD, label='Steepest Descent')
    ax.set_yscale('log')
    ax.set_xlabel('Iterations (k)')
    ax.set_ylabel('f(X,Y) [log-scale]')
    ax.set_title('Rosenbrock Test: Newton' 's Method vs Steepest Descent')
    ax.legend()
    plt.show()

    fig = plt.figure()
    ax = fig.add_subplot(111)
    diff_N = [np.linalg.norm(d - Domain.ArgMin) for d in data_N]
    diff_SD = [np.linalg.norm(d - Domain.ArgMin) for d in data_SD]
    ax.plot(diff_N, label='Newton' 's Method')
    ax.plot(diff_SD, label='Steepest Descent')
    ax.set_yscale('log')
    ax.set_xlabel('Iterations (k)')
    ax.set_ylabel(r'$||x-x^*||$ [log-scale]')
    ax.set_title('Rosenbrock Test: Newton' 's Method vs Steepest Descent')
    ax.legend()
    plt.show()