def infinity_loss(Domain, Start): # Set Method Method = HeunEuler(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-3) # 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)], verbose=False) Repo = Reporting(Requests=[Domain.gap_rplus, 'Data', Domain.F]) Misc = Miscellaneous(Timer=False) Options = DescentOptions(Init, Term, Repo, Misc) # Start Solver SOI_Results = Solve(Start, Method, Domain, Options) # Record X_Star Data = SOI_Results.PermStorage['Data'] dx = np.diff(Data, axis=0) F = SOI_Results.PermStorage[Domain.F][:-1] return -np.sum(F * dx)
def Demo(): #__BLOOD_BANK__################################################## # Define Network and Domain Network = CreateRandomNetwork(nC=2, nB=2, nD=2, nR=2, seed=0) Domain = BloodBank(Network=Network, alpha=2) # Set Method Method = CashKarp(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-6) # 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', 'F Evaluations', 'Projections']) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() BloodBank_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, BloodBank_Results, Method, toc)
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(): #__LIENARD_SYSTEM__################################################## # Define Network and Domain Domain = Lienard() # Set Method Method = HeunEuler_LEGS(Domain=Domain, Delta0=1e-5) # Set Options Init = Initialization(Step=1e-5) Term = Termination(MaxIter=5e4) Repo = Reporting(Requests=['Data', 'Step']) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) args = (Method, Domain, Options) sim = Solve # Print Stats PrintSimStats(Domain, Method, Options) # Construct grid grid = [np.array([-2.5, 2.5, 13])] * 2 grid = ListONP2NP(grid) grid = aug_grid(grid) Dinv = np.diag(1. / grid[:, 3]) r = 1.1 * max(grid[:, 3]) # Compute Results results = MCT(sim, args, grid, nodes=8, parallel=True, limit=5, AVG=-1, eta_1=1.2, eta_2=.95, eps=1., L=8, q=2, r=r, Dinv=Dinv) ref, data, p, i, avg, bndry_ids, starts = results # Plot BoAs figBoA, axBoA = plotBoA(ref, data, grid, wcolor=True, wscatter=True) # Plot Probablity Distribution figP, axP = plotDistribution(p, grid)
def Demo(): # __APPROXIMATE_LINEAR_FIELD__############################################## # Load Dummy Data X = np.random.rand(1000, 2) * 2 - 1 scale = np.array([0.8, 1.2]) y = 0.5 * np.sum(scale * X**2, axis=1) # Construct Field ALF = ApproxLF(X=X, dy=y, eps=1e-8) # Set Method # Method = Euler(Domain=ALF,FixStep=True) Method = HeunEuler(Domain=ALF, Delta0=1e-2) # Initialize Starting Field A = np.array([[0, 1], [-1, 0]]) # A = np.eye(LF.XDim) # A = np.random.rand(LF.XDim,LF.XDim) # A = np.array([[5,0.],[0.,5]]) b = np.zeros(ALF.XDim) Start = np.hstack([A.flatten(), b]) print(Start) # Set Options Init = Initialization(Step=-1.0) Term = Termination(MaxIter=1000) #,Tols=[(LF.error,1e-10)]) Repo = Reporting(Requests=[ALF.error, 'Step', 'F Evaluations', 'Data']) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(ALF, Method, Options) # Start Solver tic = time.time() Results = Solve(Start, Method, ALF, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, Results, Method, toc) error = np.asarray(Results.PermStorage[ALF.error]) params = Results.PermStorage['Data'][-1] A, b = ALF.UnpackFieldParams(params) print(A) print(b) print(error[-1])
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()
def Demo(): #__Gaussian_Mixture_GAN__############################################# # Define Network and Domain Domain = GMGAN(dyn='FCC') # Set Method # Method = Euler_LEGS(Domain=Domain,FixStep=True,NTopLEs=2) # Method = HeunEuler_LEGS(Domain=Domain,Delta0=1e-5,NTopLEs=2) # Method = CashKarp_LEGS(Domain=Domain,Delta0=1e-5,NTopLEs=2) # Method = HeunEuler(Domain=Domain,Delta0=1e-5) Method = HeunEuler_PhaseSpace(Domain=Domain, Delta0=1e-3, MaxStep=-.5) # Method = Euler(Domain=Domain,FixStep=True) # Initialize Starting Point Start = Domain.get_weights() # Set Options Init = Initialization(Step=-1.) Term = Termination(MaxIter=3e2) # Repo = Reporting(Requests=['Step','Data','Lyapunov',Domain.gap]) Repo = Reporting(Requests=['Step']) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() GMGAN_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, GMGAN_Results, Method, toc) Domain.set_weights(GMGAN_Results.TempStorage['Data'][-1]) fig, ax = Domain.visualize_dist() plt.savefig('gmgan_test.png') Steps = np.array(GMGAN_Results.PermStorage['Step']) plt.clf() plt.semilogy(-Steps) plt.savefig('steps.png') embed()
def Demo(): #__CLOUD_SERVICES__################################################## # Define Network and Domain Network = CreateNetworkExample(ex=5) Domain = CloudServices(Network=Network, gap_alpha=2) # Set Method eps = 1e-2 Method = HeunEuler_LEGS(Domain=Domain, P=BoxProjection(lo=eps), Delta0=1e0) # Initialize Starting Point Start = np.ones(Domain.Dim) # Calculate Initial Gap gap_0 = Domain.gap_rplus(Start) # Set Options Init = Initialization(Step=-1e-3) Term = Termination(MaxIter=1e5, Tols=[(Domain.gap_rplus, 1e-12 * gap_0)]) Repo = Reporting(Requests=[ Domain.gap_rplus, 'Step', 'F Evaluations', 'Projections', 'Data', Domain.eig_stats, 'Lyapunov' ]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() CloudServices_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, CloudServices_Results, Method, toc) x = CloudServices_Results.PermStorage['Data'][-1] print('p,q,Q,pi,Nash?') print(x[:Domain.Dim // 2]) print(x[Domain.Dim // 2:]) print(Domain.Demand(x)[0]) print(Domain.CloudProfits(x)) print(all(Domain.Nash(x)[0]))
def Demo(): #__LIENARD_SYSTEM__################################################## # Define Network and Domain Domain = Lienard() # Set Method Method = Euler_LEGS(Domain=Domain, FixStep=True, NTopLEs=2) # Method = HeunEuler_LEGS(Domain=Domain,Delta0=1e-5,NTopLEs=2) # Method = CashKarp_LEGS(Domain=Domain,Delta0=1e-5,NTopLEs=2) # Initialize Starting Point Start = np.array([1.5, 0]) # Set Options Init = Initialization(Step=1e-3) Term = Termination(MaxIter=1e5, Tols=[(Domain.gap, 1e-3)]) Repo = Reporting(Requests=['Step', 'Data', 'Lyapunov', Domain.gap]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() Lienard_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, Lienard_Results, Method, toc) # Plot Lyapunov Exponents plt.plot(Lienard_Results.PermStorage['Lyapunov']) plt.show() # Plot System Trajectory data = ListONP2NP(Lienard_Results.PermStorage['Data']) plt.plot(data[:, 0], data[:, 1]) ax = plt.gca() ax.set_xlim([-2.5, 2.5]) ax.set_ylim([-2.5, 2.5]) ax.set_aspect('equal') plt.show()
def score_matrixfac(train, test, mask, step=1e-5, iters=100, k=500): # Define Domain n, d = train.shape sh_P = (n, k) sh_Q = (d, k) Domain = MatrixFactorization(Data=train, sh_P=sh_P, sh_Q=sh_Q) # Set Method # Method = Euler(Domain=Domain,FixStep=True) Method = HeunEuler(Domain=Domain, Delta0=1e-1, MinStep=1e-7, MaxStep=1e-2) # Initialize Starting Point globalmean = train.sum() / train.nnz scale = np.sqrt(globalmean / k) # P = np.random.rand(n,k) # Q = np.random.rand(d,k) P = scale * np.ones(sh_P) Q = scale * np.ones(sh_Q) Start = np.hstack((P.flatten(), Q.flatten())) # 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)
def score_svdmethod(train, test, mask, tau=6e3, step=1.9, fixstep=True, iters=250): # Define Domain Domain = SVDMethod(Data=train, tau=tau) # Set Method # Method = Euler(Domain=Domain,FixStep=fixstep) Method = HeunEuler(Domain=Domain, Delta0=1e2, MinStep=1e0, MaxStep=1e3) # Initialize Starting Point # globalmean = train.sum()/train.nnz # Start = globalmean*np.ones(train.shape) Start = np.zeros(train.shape).flatten() # Set Options Init = Initialization(Step=step) Term = Termination(MaxIter=iters, Tols=[(Domain.rel_error, 0.2)]) Repo = Reporting(Requests=[Domain.rel_error, '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 Y = np.asarray(Results.TempStorage['Data'][-1]).reshape(train.shape) pred = Domain.shrink(Y, Domain.tau) return rmse(pred, test, mask), Results.PermStorage[Domain.rel_error]
def Demo(): #__SUPPLY_CHAIN__################################################## # Define Network and Domain Network = CreateRandomNetwork(I=2, Nm=2, Nd=2, Nr=1, seed=0) Domain = SupplyChain(Network=Network, alpha=2) # Set Method Method = CashKarp(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-5) # Initialize Starting Point x = 10 * np.ones(np.product(Domain.x_shape)) gam = np.ones(np.sum([np.product(g) for g in Domain.gam_shapes])) lam = np.zeros(np.sum([np.product(l) for l in Domain.lam_shapes])) Start = np.concatenate((x, gam, lam)) # 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-3 * gap_0)]) Repo = Reporting(Requests=[ Domain.gap_rplus, 'Step', 'F Evaluations', 'Projections', 'Data' ]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SupplyChain_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SupplyChain_Results, Method, toc)
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)
def Demo(): #__MHPH__################################################## trials = range(1000,8000+1,2000) MHPH_Results = [[] for i in trials] for n in trials: #Define Dimension and Domain Domain = MHPH(Dim=n) # Set Method Method = HeunEuler(Domain=Domain,P=EntropicProjection(),Delta0=1e-1) # Method = RipCurl(Domain=Domain,P=EntropicProjection(),factor=0.1,FixStep=True) # Set Options Init = Initialization(Step=-1) Term = Termination(MaxIter=100,Tols=[[Domain.gap_simplex,1e-3]]) Repo = Reporting(Requests=[Domain.gap_simplex]) Misc = Miscellaneous() Options = DescentOptions(Init,Term,Repo,Misc) #Initialize Starting Point Start = np.ones(Domain.Dim)/np.double(Domain.Dim) # Print Stats PrintSimStats(Domain,Method,Options) tic = time.time() ind = int(n/2000)-4 MHPH_Results[ind] = Solve(Start,Method,Domain,Options) toc = time.time() - tic # Print Results PrintSimResults(Options,MHPH_Results[ind],Method,toc)
def Demo(): #__SUPPLY_CHAIN__################################################## ############################################################# # Example 1 from Nagurney's Paper ############################################################# # Define Network and Domain Network = CreateNetworkExample(ex=1) Domain = SupplyChain(Network=Network, alpha=2) # Set Method Method = ABEuler(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-2) # Initialize Starting Point x = 10 * np.ones(np.product(Domain.x_shape)) gam = np.ones(np.sum([np.product(g) for g in Domain.gam_shapes])) lam = np.zeros(np.sum([np.product(l) for l in Domain.lam_shapes])) Start = np.concatenate((x, gam, lam)) # 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-3 * gap_0)]) Repo = Reporting(Requests=[ Domain.gap_rplus, 'Step', 'F Evaluations', 'Projections', 'Data' ]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SupplyChain_Results_Phase1 = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SupplyChain_Results_Phase1, Method, toc) ############################################################# # Increased Demand of Firm 2's Product ############################################################# # Define Network and Domain Network = CreateNetworkExample(ex=2) Domain = SupplyChain(Network=Network, alpha=2) # Set Method Method = ABEuler(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-2) # Initialize Starting Point Start = SupplyChain_Results_Phase1.PermStorage['Data'][-1] # 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-3 * gap_0)]) Repo = Reporting(Requests=[ Domain.gap_rplus, 'Step', 'F Evaluations', 'Projections', 'Data' ]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SupplyChain_Results_Phase2 = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SupplyChain_Results_Phase2, Method, toc) ######################################################## # Animate Network ######################################################## # Construct MP4 Writer fps = 15 FFMpegWriter = animation.writers['ffmpeg'] metadata = dict(title='SupplyChain', artist='Matplotlib') writer = FFMpegWriter(fps=fps, metadata=metadata) # Collect Frames frame_skip = 5 freeze = 5 Dyn_1 = SupplyChain_Results_Phase1.PermStorage['Data'] Frz_1 = [Dyn_1[-1]] * fps * frame_skip * freeze Dyn_2 = SupplyChain_Results_Phase2.PermStorage['Data'] Frz_2 = [Dyn_2[-1]] * fps * frame_skip * freeze Frames = np.concatenate((Dyn_1, Frz_1, Dyn_2, Frz_2), axis=0)[::frame_skip] # Normalize Colormap by Flow at each Network Level Domain.FlowNormalizeColormap(Frames, cm.rainbow) # Mark Annotations t1 = 0 t2 = t1 + len(SupplyChain_Results_Phase1.PermStorage['Data']) // frame_skip t3 = t2 + fps * freeze t4 = t3 + len(SupplyChain_Results_Phase2.PermStorage['Data']) // frame_skip Dyn_1_ann = 'Control Network\n(Equilibrating)' Frz_1_ann = 'Control Network\n(Converged)' Dyn_2_ann = 'Market Increases Demand for Firm 2' 's Product\n(Equilibrating)' Frz_2_ann = 'Market Increases Demand for Firm 2' 's Product\n(Converged)' anns = sorted([(t1, plt.title, Dyn_1_ann), (t2, plt.title, Frz_1_ann), (t3, plt.title, Dyn_2_ann), (t4, plt.title, Frz_2_ann)], key=lambda x: x[0], reverse=True) # Save Animation to File fig, ax = plt.subplots() SupplyChain_ani = animation.FuncAnimation(fig, Domain.UpdateVisual, init_func=Domain.InitVisual, frames=len(Frames), fargs=(ax, Frames, anns), blit=True) SupplyChain_ani.save('Videos/SupplyChain.mp4', writer=writer)
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')
def Demo(): #__SPHERE__################################################## # Define Dimension and Domain Domain = Sphere(Dim=100) # Set Method Method = HeunEuler(Domain=Domain, P=IdentityProjection(), Delta0=1e-2) # Set Options Init = Initialization(Step=-1e-1) Term = Termination(MaxIter=1000, Tols=[[Domain.f_Error, 1e-3]]) Repo = Reporting(Requests=[Domain.f_Error]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Initialize Starting Point Start = 100 * np.ones(Domain.Dim) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SPHERE_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SPHERE_Results, Method, toc) #__KOJIMA-SHINDO__################################################## # Define Dimension and Domain Domain = KojimaShindo() # Set Method Method = HeunEuler(Domain=Domain, P=EntropicProjection(), Delta0=1e-1) # Set Options Init = Initialization(Step=-1e-1) Term = Termination(MaxIter=1000, Tols=[[Domain.gap_simplex, 1e-3]]) Repo = Reporting(Requests=[Domain.gap_simplex]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Initialize Starting Point Start = np.ones(Domain.Dim) / np.double(Domain.Dim) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() KS_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, KS_Results, Method, toc) #__WATSON__################################################## trials = xrange(10) WAT_Results = [[] for i in trials] for p in trials: #Define Dimension and Domain Domain = Watson(Pos=p) # Set Method Method = HeunEuler(Domain=Domain, P=EntropicProjection(), Delta0=1e-1) # Set Options Init = Initialization(Step=-1e-1) Term = Termination(MaxIter=1000, Tols=[[Domain.gap_simplex, 1e-3]]) Repo = Reporting(Requests=[Domain.gap_simplex]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) #Initialize Starting Point Start = np.ones(Domain.Dim) / np.double(Domain.Dim) # Print Stats PrintSimStats(Domain, Method, Options) tic = time.time() WAT_Results[p] = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, WAT_Results[p], Method, toc) #__SUN__################################################## trials = xrange(8000, 10000 + 1, 2000) Sun_Results = [[] for i in trials] for n in trials: #Define Dimension and Domain Domain = Sun(Dim=n) # Set Method Method = HeunEuler(Domain=Domain, P=EntropicProjection(), Delta0=1e-1) # Set Options Init = Initialization(Step=-1e-1) Term = Termination(MaxIter=1000, Tols=[[Domain.gap_simplex, 1e-3]]) Repo = Reporting(Requests=[Domain.gap_simplex]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) #Initialize Starting Point Start = np.ones(Domain.Dim) / np.double(Domain.Dim) # Print Stats PrintSimStats(Domain, Method, Options) tic = time.time() ind = n / 2000 - 4 Sun_Results[ind] = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, Sun_Results[ind], Method, toc)
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)
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')
def Demo(): #__LQ_GAN__############################################## # 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 Domain = LQ(sig=1) # Set Method lo = [-np.inf, 1e-2] # Method = Euler(Domain=Domain,FixStep=True,P=BoxProjection(lo=lo)) # Method = EG(Domain=Domain,FixStep=True,P=BoxProjection(lo=lo)) # Method = HeunEuler(Domain=Domain,Delta0=1e-4,P=BoxProjection(lo=lo)) Method = HeunEuler_PhaseSpace(Domain=Domain, Delta0=1e-2, P=BoxProjection(lo=lo)) # Initialize Starting Point # Start = np.random.rand(Domain.Dim) scale = 30 Start = np.array([50., 50.]) xoff = 0 yoff = 0 # no difference between eigenvalues of J at [1.,3.5] and eigenvalues of an outward spiral: a = np.array([[-1,6.92],[-6.92,-1]]) j = Domain.J(Start) print('original evs') print(np.linalg.eigvals(j)) print(np.linalg.eigvals(j + j.T)) f = Domain.F(Start) jsym = j + j.T # print(f.dot(jsym.dot(f))) tf = Domain.TF(Start) print('tf') print(tf) print(np.linalg.eigvals(tf)) jrc = Domain.JRipCurl(Start) print('jrc') print(jrc) print(0.5 * (jrc + jrc.T)) print(np.linalg.eigvals(jrc + jrc.T)) jreg = Domain.JReg(Start) print('jreg') print(jreg) print(0.5 * (jreg + jreg.T)) print(np.linalg.eigvals(jreg + jreg.T)) jasy = j - j.T print('exact') print(0.5 * np.dot(jasy.T, jasy)) for gam in np.linspace(0, 1, 20): # print(Domain.JRegEV(Start,gam)) print(Domain.JRCEV(Start, gam)) jap = approx_jacobian(Domain.F, Start) print(jap) print(np.linalg.eigvals(0.5 * (jap + jap.T))) y = np.array([0, 1]) x = np.array([1, 1e-1]) pre = np.dot(Domain.F(y), x - y) post = np.dot(Domain.F(x), x - y) print(pre) print(post) d = 2 W2 = Domain.sym(np.random.rand(d, d)) w1 = np.random.rand(d) A = np.tril(np.random.rand(d, d)) A[range(d), range(d)] = np.clip(A[range(d), range(d)], 1e-6, np.inf) b = np.random.rand(d) dmult = np.hstack([W2.flatten(), w1, A.flatten(), b]) jmult = Domain.Jmult(dmult) jskew = (jmult - jmult.T) print(np.linalg.matrix_rank(jskew, tol=1e-16)) W2 = Domain.sym(np.ones((d, d))) w1 = np.ones(d) A = np.tril(np.ones((d, d))) A[range(d), range(d)] = np.clip(A[range(d), range(d)], 0, np.inf) b = np.ones(d) dmult = np.hstack([W2.flatten(), w1, A.flatten(), b]) jmult = Domain.Jmult(dmult) jskew = (jmult - jmult.T) print(np.linalg.matrix_rank(jskew)) W2 = Domain.sym(np.zeros((d, d))) w1 = np.zeros(d) A = np.tril(np.ones((d, d))) A[range(d), range(d)] = np.clip(A[range(d), range(d)], 0, np.inf) b = np.zeros(d) dmult = np.hstack([W2.flatten(), w1, A.flatten(), b]) jmult = Domain.Jmult(dmult) jskew = (jmult - jmult.T) print(np.linalg.matrix_rank(jskew)) np.set_printoptions(linewidth=200) s = (d**2 + d) // 2 jskewblock = jskew[:s, s + d:s + d + s] embed() # Set Options Init = Initialization(Step=-1e-5) Term = Termination(MaxIter=1000, 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) data = np.array(LQ_Results.PermStorage['Data']) 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([X[i, j], Y[i, j]]) U[i, j] = vec[0] V[i, j] = vec[1] 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[:, 1], '-r') ax.plot([data[0, 0]], [data[0, 1]], 'k*') ax.plot([data[-1, 0]], [data[-1, 1]], 'b*') ax.set_xlim([-2 * scale + xoff, 2 * scale + xoff]) ax.set_ylim([-.1 * scale + yoff, 4 * scale + yoff]) ax.set_xlabel('x') ax.set_ylabel('y') # 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')
def Demo(): #__AFFINE_GAN__############################################## # what happens if we force the diagonal of G to lie in R+? # G and -G are both equilibrium solutions -- need to rule one out # Define Network and Domain mean = np.array([1, 2]) cov = np.array([[1, 0.5], [0.5, 2]]) lam, phi = np.linalg.eig(cov) mat = np.diag(np.sqrt(lam)).dot(phi) # Domain = AffineGAN(u=mean,S=cov,batch_size=1000,alpha=0.,expansion=False) # Set Method # Method = Euler(Domain=Domain,FixStep=True) # Method = EG(Domain=Domain,FixStep=True) # Initialize Starting Point # Start = np.random.rand(Domain.Dim) Start = np.zeros(2 * mean.size + cov.size) # d0, G0 = Domain.get_args(Start) # u0 = G0[:,-1] # S0 = G0[:,:Domain.dim] # print(u0) # print(S0) lo = -np.inf * np.ones_like(Start) for d in range(mean.size): lo[mean.size + d * (mean.size + 1) + d] = 0. iter_schedule = [2000, 4000, 4000] step_schedule = [1e-1, 1e-2, 1e-2] batch_schedule = [100, 100, 10] Vs = [] for it, step, batch_size in zip(iter_schedule, step_schedule, batch_schedule): Domain = AffineGAN(u=mean, S=cov, batch_size=batch_size, alpha=0., expansion=False) # Set Method # Method = Euler(Domain=Domain,FixStep=True,P=BoxProjection(lo=lo)) Method = EG(Domain=Domain, FixStep=True, P=BoxProjection(lo=lo)) # Set Options Init = Initialization(Step=-step) Term = Termination(MaxIter=it) Repo = Reporting(Requests=[ Domain.V, 'Step', 'F Evaluations', 'Projections', 'Data' ]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() AG_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, AG_Results, Method, toc) Vs += AG_Results.PermStorage[Domain.V] Start = AG_Results.TempStorage['Data'][-1] # d, G = Domain.get_args(data) # u = G[:,-1] # S = G[:,:Domain.dim] # print(u) # print(S) print(mat) # embed() for data in AG_Results.PermStorage['Data'][::400]: num_samples = 1000 real, fake = Domain.generate(data, num_samples) # u_est = np.mean(fake,axis=0) # S_est = np.dot(fake.T,fake)/num_samples # print(u_est) # print(S_est) fig, axs = plt.subplots(2) axs[0].scatter(fake[:, 0], fake[:, 1], s=40, facecolors='none', edgecolors='r', label='fake', zorder=1) axs[0].scatter(real[:, 0], real[:, 1], s=40, marker='*', facecolors='none', edgecolors='k', label='real', zorder=0) axs[0].set_title('Comparing Distributions') axs[0].set_xlim([-5, 10]) axs[0].set_ylim([-5, 10]) axs[0].legend() axs[1].plot(Vs) axs[1].set_xlabel('Iteration') axs[1].set_ylabel('Minimax Objective (V)') plt.show()
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')
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()
def Demo(): #__SERVICE_ORIENTED_INTERNET__############################################## N = 2 # number of possible maps T = 100 # number of time steps eta = .01 # learning rate # Define Domains and Compute Equilbria Domains = [] X_Stars = [] CurlBounds = [] # for n in range(N): n = 0 while len(X_Stars) < N: # Create Domain Network = CreateRandomNetwork(m=3,n=2,o=2,seed=n) Domain = SOI(Network=Network,alpha=2) # Record Domain Domains += [Domain] # Set Method Method = HeunEuler(Domain=Domain,P=BoxProjection(lo=0),Delta0=1e-3) # Initialize Starting Point Start = np.zeros(Domain.Dim) # Calculate Initial Gap gap_0 = Domain.gap_rplus(Start) # Calculate Curl Bound J = approx_jacobian(Domain.F,Start) if not np.all(np.linalg.eigvals(J+J.T) >= 0): pass _J = approx_jacobian(Domain.F,Start+0.5) assert np.allclose(J,_J,atol=1e-5) CurlBounds += [np.sqrt(18)*svds(J,k=1,which='LM',return_singular_vectors=False).item()] # 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]) Misc = Miscellaneous() Options = DescentOptions(Init,Term,Repo,Misc) # Print Stats PrintSimStats(Domain,Method,Options) # Start Solver tic = time.time() SOI_Results = Solve(Start,Method,Domain,Options) toc = time.time() - tic # Print Results PrintSimResults(Options,SOI_Results,Method,toc) # Record X_Star X_Star = SOI_Results.TempStorage['Data'][-1] X_Stars += [X_Star] n += 1 X_Stars = np.asarray(X_Stars) # Compute Equilibrium of Average Domain Domain = AverageDomains(Domains) # Set Method Method = HeunEuler_PhaseSpace(Domain=Domain,P=BoxProjection(lo=0),Delta0=1e-3) # Initialize Starting Point Start = np.zeros(Domain.Dim) J = approx_jacobian(Domain.F,Start) assert np.all(np.linalg.eigvals(J+J.T) >= 0) # 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-10*gap_0)]) Repo = Reporting(Requests=[Domain.gap_rplus]) Misc = Miscellaneous() Options = DescentOptions(Init,Term,Repo,Misc) # Print Stats PrintSimStats(Domain,Method,Options) # Start Solver tic = time.time() SOI_Results = Solve(Start,Method,Domain,Options) toc = time.time() - tic # Print Results PrintSimResults(Options,SOI_Results,Method,toc) # Record X_Opt X_Opt = SOI_Results.TempStorage['Data'][-1] # X_Opt = X_Stars[0] print('Starting Online Learning') # Set First Prediction X = np.zeros(X_Stars.shape[1]) # Select First Domain idx = np.argmax(np.linalg.norm(X_Stars - X,axis=1)) distances = [] loss_infs = [] regret_standards = [] regret_news = [] stokes = [] ts = range(T) for t in ts: print('t = '+str(t)) # retrieve domain Domain = Domains[idx] # retrieve equilibrium / reference vector # equi = X_Stars[idx] equi = np.zeros_like(X_Stars[idx]) # calculate distance distances += [np.linalg.norm(X_Opt-X)] # calculate infinity loss # loss_infs += [infinity_loss(Domain,X)] # calculate standard regret ci_predict = ContourIntegral(Domain,LineContour(equi,X)) predict_loss = integral(ci_predict) ci_opt = ContourIntegral(Domain,LineContour(equi,X_Opt)) predict_opt = integral(ci_opt) regret_standards += [predict_loss - predict_opt] # calculate new regret ci_new = ContourIntegral(Domain,LineContour(X_Opt,X)) regret_news += [integral(ci_new)] # calculate bound # area = 0.5*np.prod(np.sort([np.linalg.norm(X_Opt-equi),np.linalg.norm(X-X_Opt),np.linalg.norm(equi-X)])[:2]) # area upper bound area = herons(X_Opt,X,equi) # exact area stokes += [CurlBounds[idx]*area] # update prediction X = BoxProjection(lo=0).P(X,-eta,Domain.F(X)) # update domain idx = np.argmax(np.linalg.norm(X_Stars - X,axis=1)) # embed() ts_p1 = range(1,T+1) distances_avg = np.divide(np.cumsum(distances),ts_p1) # loss_infs_avg = np.divide(np.cumsum(loss_infs),ts_p1) regret_standards_avg = np.divide(np.cumsum(regret_standards),ts_p1) regret_news_avg = np.divide(np.cumsum(regret_news),ts_p1) stokes = np.divide(np.cumsum(stokes),ts_p1) # np.savez_compressed('NoRegret_MLN.npz',d_avg=distances_avg, # linf_avg=loss_infs_avg,rs_avg=regret_standards_avg, # rn_avg=regret_news_avg,stokes=stokes) plt.subplot(2, 1, 2) plt.plot(ts, distances_avg, 'k',label='Average Distance') plt.title('Demonstration of No-Regret on MLN') plt.ylabel('Euclidean Distance') plt.legend() plt.subplot(2, 1, 1) # plt.plot(ts, loss_infs_avg, 'k--', label=r'loss$_{\infty}$') plt.plot(ts, regret_standards_avg, 'r--o', markevery=T//20, label=r'regret$_{s}$') plt.plot(ts, regret_news_avg, 'b-', label=r'regret$_{n}$') # plt.fill_between(ts, regret_news_avg-stokes, regret_news_avg+stokes, # facecolor='c', alpha=0.2, zorder=0, label='Stokes Bound') plt.plot(ts, np.zeros_like(ts), 'w-', lw=1) plt.xlabel('Time Step') plt.ylabel('Aggregate System-Wide Loss') plt.xlim([0,T]) # plt.ylim([-250,1000]) plt.legend() plt.title('Demonstration of No-Regret on MLN') plt.savefig('NoRegret_MLN2') # data = np.load('NoRegret2.npz') # distances_avg = data['d_avg'] # loss_infs_avg = data['linf_avg'] # regret_standards_avg = data['rs_avg'] # regret_news_avg = data['rn_avg'] # stokes = data['stokes'] # ts = range(len(distances_avg)) embed()
def Demo(): # __PENN_TREE_BANK____################################################# # Load Data # seq = np.arange(1000) train, valid, test, id_to_word, vocab = ptb_raw_data( '/Users/imgemp/Desktop/Data/simple-examples/data/') words, given_embeddings = pickle.load(open( '/Users/imgemp/Desktop/Data/polyglot-en.pkl', 'rb'), encoding='latin1') words_low = [word.lower() for word in words] word_to_id = dict(zip(words_low, range(len(words)))) word_to_id['<eos>'] = word_to_id['</s>'] y0 = get_y0(train, vocab) EDim = 5 fix_embedding = False learn_embedding = True if fix_embedding: EDim = given_embeddings.shape[1] learn_embedding = False # Define Domain Domain = PennTreeBank(seq=train, y0=None, EDim=EDim, batch_size=100, learn_embedding=learn_embedding, ord=1) # Set Method P = PTBProj(Domain.EDim) # Method = Euler(Domain=Domain,FixStep=True,P=P) Method = HeunEuler(Domain=Domain, P=P, Delta0=1e-4, MinStep=-3., MaxStep=0.) # Method = CashKarp(Domain=Domain,P=P,Delta0=1e-1,MinStep=-5.,MaxStep=0.) # Set Options Term = Termination(MaxIter=10000) Repo = Reporting( Interval=10, Requests=[Domain.Error, Domain.PercCorrect, Domain.Perplexity, 'Step']) #, # 'Step', 'F Evaluations', # 'Projections','Data']) Misc = Miscellaneous() Init = Initialization(Step=-1e-3) Options = DescentOptions(Init, Term, Repo, Misc) # Initialize Starting Point if fix_embedding: missed = 0 params = np.random.rand(Domain.param_len) avg_norm = np.linalg.norm(given_embeddings, axis=1).mean() embeddings = [] for i in range(vocab): word = id_to_word[i] if word in word_to_id: embedding = given_embeddings[word_to_id[word]] else: missed += 1 embedding = np.random.rand(Domain.EDim) embedding *= avg_norm / np.linalg.norm(embedding) embeddings += [embedding] polyglot_embeddings = np.hstack(embeddings) Start = np.hstack((params, polyglot_embeddings)) print(np.linalg.norm(polyglot_embeddings)) print( 'Missing %d matches in polyglot dictionary -> given random embeddings.' % missed) else: # params = np.random.rand(Domain.param_len)*10 # embeddings = np.random.rand(EDim*vocab)*.1 # Start = np.hstack((params,embeddings)) # assert Start.shape[0] == Domain.Dim Start = np.random.rand(Domain.Dim) Start = P.P(Start) # Compute Initial Error print('Initial training error: %g' % Domain.Error(Start)) print('Initial perplexity: %g' % Domain.Perplexity(Start)) print('Initial percent correct: %g' % Domain.PercCorrect(Start)) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() PTB_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, PTB_Results, Method, toc) # Plot Results err = np.asarray(PTB_Results.PermStorage[Domain.Error]) pc = np.asarray(PTB_Results.PermStorage[Domain.PercCorrect]) perp = np.asarray(PTB_Results.PermStorage[Domain.Perplexity]) steps = np.asarray(PTB_Results.PermStorage['Step']) t = np.arange(0, len(steps) * Repo.Interval, Repo.Interval) fig = plt.figure() ax = fig.add_subplot(411) ax.semilogy(t, err) ax.set_ylabel('Training Error') ax.set_title('Penn Tree Bank Training Evaluation') ax = fig.add_subplot(412) ax.semilogy(t, perp) ax.set_ylabel('Perplexity') ax = fig.add_subplot(413) ax.plot(t, pc) ax.set_ylabel('Percent Correct') ax = fig.add_subplot(414) ax.plot(t, steps) ax.set_ylabel('Step Size') ax.set_xlabel('Iterations (k)') plt.savefig('PTB') params_embeddings = np.asarray(PTB_Results.TempStorage['Data']).squeeze() params, embeddings = np.split(params_embeddings, [Domain.param_len]) embeddings_split = np.split(embeddings, vocab) dists_comp = pdist(np.asarray(embeddings_split)) dists_min = np.min(dists_comp) dists_max = np.max(dists_comp) dists = squareform(dists_comp) dists2 = np.asarray([np.linalg.norm(e) for e in embeddings_split]) print('pairwise dists', np.mean(dists), dists_min, dists_max) print('embedding norms', np.mean(dists2), np.min(dists2), np.max(dists2)) print('params', np.mean(params), np.min(np.abs(params)), np.max(np.abs(params)))
def Demo(): #__SERVICE_ORIENTED_INTERNET__############################################## N = 10 # number of possible maps T = 1000 # number of time steps eta = 1e-3 # learning rate print('Creating Domains') # Define Domains and Compute Equilbria Domains = [] X_Stars = [] CurlBounds = [] n = 0 while len(Domains) < N: # Create Domain Network = CreateRandomNetwork(m=3, n=2, o=2, seed=None) Domain = SOI(Network=Network, alpha=2) # Initialize Starting Point Start = np.zeros(Domain.Dim) # Assert PD J = approx_jacobian(Domain.F, Start) eigs = np.linalg.eigvals(J + J.T) if not np.all(eigs > 0): continue _J = approx_jacobian(Domain.F, Start + 0.5) assert np.allclose(J, _J, atol=1e-5) # assert J is constant (unique for SOI) # Record Domain Domains += [Domain] # Calculate Initial Gap gap_0 = Domain.gap_rplus(Start) # Calculate Curl Bound CurlBounds += [ np.sqrt(8) * svds(J, k=1, which='LM', return_singular_vectors=False).item() ] # Set Method Method = HeunEuler(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-3) # 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]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SOI_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SOI_Results, Method, toc) # Record X_Star X_Star = SOI_Results.TempStorage['Data'][-1] X_Stars += [X_Star] n += 1 X_Stars = np.asarray(X_Stars) print('Starting Online Learning') # Set First Prediction X = np.zeros(X_Stars.shape[1]) # Select First Domain idx = np.random.choice(len(Domains)) # Domain Sequence idx_seq = [] X_seq = [] F_seq = [] ts = range(T) for t in ts: print('t = ' + str(t), end='\r') # record prediction X_seq += [X] # record domain idx_seq += [idx] # retrieve domain Domain = Domains[idx] # record F FX = Domain.F(X) F_seq += [FX] # update prediction X = BoxProjection(lo=0).P(X, -eta, FX) # update domain idx = np.random.choice(len(Domains)) print('Computing Optimal Strategy') weights = np.bincount(idx_seq, minlength=len(Domains)) / len(idx_seq) print('Weights: ', weights) # Compute Equilibrium of Average Domain Domain = AverageDomains(Domains, weights=weights) # Set Method Method = HeunEuler_PhaseSpace(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-5) # Initialize Starting Point Start = np.zeros(Domain.Dim) # Assert PSD - sum of PSD is PSD doesn't hurt to check J = approx_jacobian(Domain.F, Start) eigs = np.linalg.eigvals(J + J.T) assert np.all(eigs > 0) sigma = min(eigs) # 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-10 * gap_0)]) Repo = Reporting(Requests=[Domain.gap_rplus]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SOI_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SOI_Results, Method, toc) print('Computing Regrets') # Record X_Opt X_Opt = SOI_Results.TempStorage['Data'][-1] # Record constants for bounds L = np.sqrt(np.mean(np.linalg.norm(F_seq, axis=1)**2.)) # B = np.linalg.norm(X_Opt) B = 2. * np.max(np.linalg.norm(X_Stars, axis=1)) eta_opt = B / (L * np.sqrt(2 * T)) bound_opt = B * L * np.sqrt(2 * T) reg_bound = (B**2) / (2 * eta) + eta * T * L**2 opt_distances = [] equi_distances = [] regret_standards = [] regret_news = [] Fnorms = [] stokes_exact = [] stokes = [] areas_exact = [] areas = [] ts = range(T) for t in ts: print('t = ' + str(t), end='\r') idx = idx_seq[t] X = X_seq[t] # retrieve domain Domain = Domains[idx] # retrieve equilibrium / reference vector if t > 0: equi = X_seq[t - 1] else: # equi = np.zeros_like(X) equi = X # calculate distance opt_distances += [np.linalg.norm(X_Opt - X)] equi_distances += [np.linalg.norm(equi - X)] # calculate standard regret ci_predict = ContourIntegral(Domain, LineContour(equi, X)) predict_loss = integral(ci_predict) ci_opt = ContourIntegral(Domain, LineContour(equi, X_Opt)) predict_opt = integral(ci_opt) regret_standards += [predict_loss - predict_opt] # calculate new regret ci_new = ContourIntegral(Domain, LineContour(X_Opt, X)) regret_news += [integral(ci_new)] # calculate bound area_exact = herons(X_Opt, X, equi) # exact area area = eta_opt * L * (np.linalg.norm(X) + B) areas_exact += [area_exact] areas += [area] stokes_exact += [CurlBounds[idx] * area_exact] stokes += [CurlBounds[idx] * area] # stokes += [np.max(CurlBounds[idx]*regret_news[-1]/sigma,0)] # calculate Fnorm Fnorms += [np.linalg.norm(F_seq[t])] ts_p1 = range(1, T + 1) opt_distances_avg = np.divide(np.cumsum(opt_distances), ts_p1) equi_distances_avg = np.divide(np.cumsum(equi_distances), ts_p1) regret_standards_avg = np.divide(np.cumsum(regret_standards), ts_p1) regret_news_avg = np.divide(np.cumsum(regret_news), ts_p1) areas_exact_avg = np.divide(np.cumsum(areas_exact), ts_p1) areas_avg = np.divide(np.cumsum(areas), ts_p1) stokes_exact_avg = np.divide(np.cumsum(stokes_exact), ts_p1) stokes_avg = np.divide(np.cumsum(stokes), ts_p1) Fnorms_avg = np.divide(np.cumsum(Fnorms), ts_p1) np.savez_compressed('NoRegret_MLN_new.npz', opt_d_avg=opt_distances_avg, equi_d_avg=equi_distances_avg, rs_avg=regret_standards_avg, rn_avg=regret_news_avg, stokes_exact=stokes_exact_avg, stokes=stokes_avg) plt.subplot(2, 1, 2) plt.semilogy(ts, opt_distances_avg, 'k', label='Average Distance to Optimal') plt.semilogy(ts, equi_distances_avg, 'r', label='Average Distance to Reference') plt.semilogy(ts, areas_exact_avg, 'g-', label='Area (exact)') plt.semilogy(ts, areas_avg, 'm-', label='Area') plt.semilogy(ts, Fnorms_avg, 'b-', label='Fnorms') # plt.title('Demonstration of No-Regret on MLN') plt.xlabel('Time Step') plt.ylabel('Euclidean Distance') # plt.legend() lgd1 = plt.legend(bbox_to_anchor=(1.05, 1), loc=2) plt.subplot(2, 1, 1) plt.plot(ts, regret_standards_avg, 'r--o', markevery=T // 20, label=r'regret$_{s}$') plt.plot(ts, regret_news_avg, 'b-', label=r'regret$_{n}$') plt.fill_between(ts, regret_news_avg - stokes, regret_news_avg + stokes, facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') plt.fill_between(ts, regret_news_avg - stokes_exact, regret_news_avg + stokes_exact, facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound (exact)') # plt.plot(ts, np.zeros_like(ts), 'w-', lw=1) # plt.xlabel('Time Step') plt.ylabel('Aggregate System-Wide Loss') plt.xlim([0, T]) plt.ylim([-5000, 5000]) # plt.legend(loc='lower right') lgd2 = plt.legend(bbox_to_anchor=(1.05, 1), loc=2) plt.title('Demonstration of No-Regret on MLN') plt.savefig('NoRegret_MLN_new.pdf', format='pdf', additional_artists=[lgd1, lgd2], bbox_inches='tight') fontsize = 18 plt.figure() plt.subplot(1, 1, 1) plt.plot(ts, regret_standards_avg, 'r--o', markevery=T // 20, label=r'regret$_{s}$') plt.plot(ts, regret_news_avg, 'b-', label=r'regret$_{n}$') # plt.fill_between(ts, regret_news_avg-stokes, regret_news_avg+stokes, # facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') plt.fill_between(ts, regret_news_avg - stokes_exact, regret_news_avg + stokes_exact, facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') plt.plot(ts, np.zeros_like(ts), 'w-', lw=1) plt.xlabel('Time Step', fontsize=fontsize) plt.ylabel('Negative Auto-Welfare', fontsize=fontsize) plt.xlim([0, T]) plt.ylim([0, 5000]) # plt.legend(loc='lower right') lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, fontsize=fontsize) plt.title('Demonstration of No-Regret on MLN', fontsize=fontsize) plt.savefig('NoRegret_MLN_new2.pdf', format='pdf', additional_artists=[lgd], bbox_inches='tight') plt.figure() plt.subplot(1, 1, 1) plt.plot(ts, -regret_standards_avg, 'r--o', markevery=T // 20, label=r'regret$_{2}$') plt.plot(ts, -regret_news_avg, 'b-', label=r'regret$_{1}$') # plt.fill_between(ts, regret_news_avg-stokes, regret_news_avg+stokes, # facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') plt.fill_between(ts, -regret_news_avg - stokes_exact, -regret_news_avg + stokes_exact, facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') plt.plot(ts, np.zeros_like(ts), 'w-', lw=1) plt.xlabel('Time Step', fontsize=fontsize) plt.ylabel('Auto-Welfare Regret', fontsize=fontsize) plt.xlim([0, T]) plt.ylim([-5000, 0]) # plt.legend(loc='lower right') lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, fontsize=fontsize) plt.title('Demonstration of No-Regret on MLN', fontsize=fontsize) plt.savefig('NoRegret_MLN_new3.pdf', format='pdf', additional_artists=[lgd], bbox_inches='tight') plt.figure() plt.subplot(1, 1, 1) plt.plot(ts, regret_news_avg, 'b-') # plt.fill_between(ts, regret_news_avg-stokes, regret_news_avg+stokes, # facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') # plt.fill_between(ts, -regret_news_avg-stokes_exact, -regret_news_avg+stokes_exact, # facecolor='c', alpha=0.2, zorder=5, label='Stokes Bound') plt.plot(ts, np.zeros_like(ts), 'w-', lw=1) plt.xlabel('Time Step', fontsize=fontsize) plt.ylabel('OMO Path Integral Regret', fontsize=fontsize) plt.xlim([0, T]) plt.ylim([0, 5000]) # plt.legend(loc='lower right') # lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, fontsize=fontsize) plt.title('Demonstration of No-Regret on MLN', fontsize=fontsize) plt.savefig('NoRegret_MLN_new4.pdf', format='pdf', bbox_inches='tight') sat_exact = np.logical_or( regret_standards_avg >= regret_news_avg - stokes_exact, regret_standards_avg <= regret_news_avg + stokes_exact) sat = np.logical_or(regret_standards_avg >= regret_news_avg - stokes, regret_standards_avg <= regret_news_avg + stokes) embed()
def Demo(): #__SERVICE_ORIENTED_INTERNET__############################################## N = 10 # number of possible maps T = 1000 # number of time steps eta = .01 # learning rate Ot = 0 # reference vector will be origin for all maps # Define Domains and Compute Equilbria Domains = [] X_Stars = [] for n in range(N): # Create Domain Network = CreateRandomNetwork(m=3, n=2, o=2, seed=n) Domain = SOI(Network=Network, alpha=2) # Record Domain Domains += [Domain] # Set Method Method = HeunEuler(Domain=Domain, P=BoxProjection(lo=0), Delta0=1e-3) # 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]) Misc = Miscellaneous() Options = DescentOptions(Init, Term, Repo, Misc) # Print Stats PrintSimStats(Domain, Method, Options) # Start Solver tic = time.time() SOI_Results = Solve(Start, Method, Domain, Options) toc = time.time() - tic # Print Results PrintSimResults(Options, SOI_Results, Method, toc) # Record X_Star X_Star = SOI_Results.TempStorage['Data'][-1] X_Stars += [X_Star] X_Stars = np.asarray(X_Stars) X_Opt = np.mean(X_Stars, axis=0) Ot = Ot * np.ones(X_Stars.shape[1]) print('Starting Online Learning') # Set First Prediction X = np.zeros(X_Stars.shape[1]) # Select First Domain idx = np.argmax(np.linalg.norm(X_Stars - X, axis=1)) distances = [] loss_infs = [] regret_standards = [] regret_news = [] ts = range(T) for t in ts: print('t = ' + str(t)) # retrieve domain Domain = Domains[idx] # retrieve equilibrium equi = X_Stars[idx] # calculate distance distances += [np.linalg.norm(equi - X)] # calculate infinity loss loss_infs += [infinity_loss(Domain, X)] # calculate standard regret ci_predict = ContourIntegral(Domain, LineContour(Ot, X)) predict_loss = integral(ci_predict) ci_opt = ContourIntegral(Domain, LineContour(Ot, X_Opt)) predict_opt = integral(ci_opt) regret_standards += [predict_loss - predict_opt] # calculate new regret ci_new = ContourIntegral(Domain, LineContour(X_Opt, X)) regret_news += [integral(ci_new)] # update prediction X = BoxProjection(lo=0).P(X, -eta, Domain.F(X)) # update domain idx = np.argmax(np.linalg.norm(X_Stars - X, axis=1)) ts_p1 = range(1, T + 1) distances_avg = np.divide(distances, ts_p1) loss_infs_avg = np.divide(loss_infs, ts_p1) regret_standards_avg = np.divide(regret_standards, ts_p1) regret_news_avg = np.divide(regret_news, ts_p1) np.savez_compressed('NoRegret.npz', d_avg=distances_avg, linf_avg=loss_infs_avg, rs_avg=regret_standards_avg, rn_avg=regret_news_avg) plt.subplot(2, 1, 1) plt.plot(ts, distances_avg, 'k', label='Average Distance') plt.title('Demonstration of No-Regret on MLN') plt.ylabel('Euclidean Distance') plt.legend() plt.subplot(2, 1, 2) plt.plot(ts, loss_infs_avg, 'k--', label=r'loss$_{\infty}$') plt.plot(ts, regret_standards_avg, 'r--o', markevery=T // 20, label=r'regret$_{s}$') plt.plot(ts, regret_news_avg, 'b-', label=r'regret$_{n}$') plt.xlabel('Time Step') plt.ylabel('Aggregate System-Wide Loss') plt.xlim([0, T]) plt.ylim([-500, 5000]) plt.legend() plt.savefig('NoRegret')
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()
def plotFigure6(saveFig=False): print('CloudServices BoA') msg = 'This method will run for about a week.\n' +\ 'Email [email protected] for the results .npy file directly.\n' +\ 'Continue? (y/n) ' cont = input(msg) if cont != 'y': return # Define Network and Domain Network = CreateNetworkExample(ex=2) Domain = CloudServices(Network=Network,gap_alpha=2) # Set Method eps = 1e-2 Method = HeunEuler_LEGS(Domain=Domain,P=BoxProjection(lo=eps),Delta0=1e0) # Set Options Init = Initialization(Step=-1e-3) Term = Termination(MaxIter=1e5) Repo = Reporting(Requests=['Data','Step']) Misc = Miscellaneous() Options = DescentOptions(Init,Term,Repo,Misc) args = (Method,Domain,Options) sim = Solve # Print Stats PrintSimStats(Domain,Method,Options) # Construct grid grid = [np.array([.5,3.5,6])]*Domain.Dim grid = ListONP2NP(grid) grid = aug_grid(grid) Dinv = np.diag(1./grid[:,3]) # Compute results results = MCT(sim,args,grid,nodes=16,limit=40,AVG=0.00, eta_1=1.2,eta_2=.95,eps=1., L=16,q=8,r=1.1,Dinv=Dinv) ref, data, p, iters, avg, bndry_ids, starts = results # Save results sim_data = [results,Domain,grid] np.save('cloud_'+time.strftime("%Y%m%d_%H%M%S"),sim_data) # Plot BoAs obs = (4,9) # Look at new green-tech company consts = np.array([3.45,2.42,3.21,2.27,np.inf,.76,.97,.75,1.03,np.inf]) txt_locs = [(1.6008064516129032, 1.6015625), (3.2, 3.2), (3.33, 2.53)] xlabel = '$p_'+repr(obs[0])+'$' ylabel = '$q_'+repr(obs[1])+'$' title = 'Boundaries of Attraction for Cloud Services Market' fig, ax = plotBoA(ref,data,grid,obs=obs,consts=consts,txt_locs=txt_locs, xlabel=xlabel,ylabel=ylabel,title=title) if saveFig: plt.savefig('BoA.png',bbox_inches='tight') return fig, ax
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()