# creating dataframe without overwriting (so additing) # if you start a new set of data, make sure you start with a fresh file filename = 'data_2-regular.csv' overwrite = False if overwrite == True: output = pd.DataFrame() else: output = load_csv(filename) p_max = 10 # maxdepth n_min, n_max = 3, 20 N = 1 # number of graphs per node number n d = 2 # degree of regular graphs for n in range(n_min, n_max + 1): for s in range(N): G = nx.cycle_graph(n) graph_type = str(d) + '-regular_' + str(n) + '-nodal' results = interp_pyquil(G, p_max) for i, results_i in results.items(): results_i['graph_name'] = graph_type results_i['seed'] = s results_i['GW_cutvalue'] = goemans_williamson(G) output = output.append(results_i, ignore_index=True) # saving every round so the process can be stopped prematurely without losing all progress output.to_csv(filename)
# plt.ylabel(r'$F_p$') # plt.xlabel('p') # plt.xlim([0.8,p_max+0.2]) r_sum = np.zeros(p_max) label = True df = df[(df['n_nodes'] == n) & (df['seed'] < seed_max)] r_GW_mean = 0 for i in range(0, len(df), p_max): DF = df[df['n_nodes'] == n].iloc() Fp = np.array([DF[i + j]['Fp'] for j in range(p_max)]) Cmax = np.array([DF[i + j]['Cmax'] for j in range(p_max)]) GW = np.mean( [goemans_williamson(DF[i]['graph']) / Cmax[0] for _ in range(10)]) print(GW) r_GW_mean += GW / len(range(0, len(df), p_max)) r = Fp / Cmax r_sum += r xticks = np.arange(1, p_max + 1) #plt.title(r"$F_p$ for 12-nodal 3-regular graphs for $p = 1...10$") plt.yscale("log") if label: plt.plot(xticks, 1 - r, color='peachpuff', linestyle='dashed') plt.plot(xticks, 1 - r, color='darkorange',
@author: joost """ # graphs import networkx as nx # solvers from PSO import PSO from GW import goemans_williamson from INTERP import INTERP from FOURIER import FOURIER G = nx.random_regular_graph(3, 10) p = 4 # Goemans Williamson cut_value_GW = goemans_williamson(G) print("GW", cut_value_GW) # Particle swarm pso = PSO(G) gamma_pso, beta_pso = pso.get_angles_PSO(p, debug=True, maxiter=10) cut_value_pso = pso.best_sampled(gamma_pso, beta_pso) print("PSO", cut_value_pso) # INTERP interp = INTERP(G) gamma_int, beta_int = interp.get_angles_INTERP(p) cut_value_interp = interp.best_sampled(gamma_int, beta_int) print("INT", cut_value_interp) # FOURIER
plt.grid('on') plt.ylabel(r'$r$') plt.xlabel('p') # figure 2 plt.figure() r_sum = np.zeros(10) label = True r_GW_mean = 0 for i in range(0,280,10): Fp = [df['Fp'][i+j] for j in range(10)] Cmax = [ceil(df['Fp'][i+9]) for j in range(10)] GW = goemans_williamson(df['graph'][i]) r_GW_mean += GW/Cmax[0]/len(range(0,len(df),p_max)) r = np.array([Fp[i]/Cmax[i] for i in range(10)]) r_sum += r xticks = np.arange(1,10+1) #plt.title(r"$F_p$ for 12-nodal 3-regular graphs for $p = 1...10$") plt.yscale("log") if label: plt.plot(xticks,1-r, color = 'peachpuff', linestyle = 'dashed') plt.plot(xticks,1-r, color = 'darkorange', linestyle = 'None', marker = 'o', label = 'random instance') label = False else: plt.plot(xticks,1-r, color = 'peachpuff', linestyle = 'dashed')
#plt.title(r"$F_p$ for 12-nodal 3-regular graphs for $p = 1...10$") plt.plot(xticks,r, color = 'lightgreen', linestyle = 'dashed') plt.plot(xticks,r, color = 'green', linestyle = 'None', marker = 'o') plt.grid('on') plt.ylabel(r'$F_p$') plt.xlabel('p') plt.xlim([0.8,p_max+0.2]) plt.figure() r_sum = np.zeros(p_max) label = True r_GW_mean = 0 for i in range(0,len(df),p_max): Fp = np.array([df['Fp'][i+j] for j in range(p_max)]) Cmax = np.array([df['Cmax'][i+j] for j in range(p_max)]) GW = np.mean([goemans_williamson(df['graph'][i]) for _ in range(10)])/Cmax[0] print(GW) r_GW_mean += GW/len(range(0,len(df),p_max)) r = Fp/Cmax r_sum += r xticks = np.arange(1,p_max+1) #plt.title(r"$F_p$ for 12-nodal 3-regular graphs for $p = 1...10$") plt.yscale("log") if label: plt.plot(xticks,1-r, color = 'peachpuff', linestyle = 'dashed') plt.plot(xticks,1-r, color = 'darkorange', linestyle = 'None', marker = 'o', label = 'random instance') label = False else: plt.plot(xticks,1-r, color = 'peachpuff', linestyle = 'dashed')
df = load_csv(pre + filename + post) r_sum = np.zeros(p_max) r_GW = 0 label = True # looping over the different graphs for k, i in enumerate(range(0, len(df), p_max)): print(k, i) Fp = np.array([df['Fp'][i + j] for j in range(p_max)]) Cmax = np.array([df['Cmax'][i + j] for j in range(p_max)]) G = df['graph'][i] r = Fp / Cmax r_GW += np.mean([goemans_williamson(G) for i in range(10)]) / Cmax[0] r_sum += r xticks = np.arange(1, p_max + 1) r_mean = r_sum / len(range(0, len(df), p_max)) r_GW = r_GW / len(range(0, len(df), p_max)) # Mean, including a fit from scipy import optimize def test_func(p, a, b): if not SQR: return a * np.exp(-p / b) if SQR: return a * np.exp(-np.sqrt(p / b))