forked from panzhang83/catn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
487 lines (463 loc) · 27 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import numpy as np
import matplotlib.pyplot as plt
import os
import networkx as nx
params = {'font.family': 'serif',
'font.serif': 'Times New Roman',
'font.style': 'normal',
'font.weight': 'normal', # or 'blod'
'figure.figsize': [37, 7],
# 'font.size': 25
}
plt.rcParams.update(params)
def plot_combine(result_dir, chi):
plt.rc('xtick', labelsize=40)
plt.rc('ytick', labelsize=40)
plt.rc('legend', fontsize=40)
plt.rc('lines', lw=5, markersize=15)
font_title = {'family': 'Verdana',
'style': 'normal',
'weight': 'normal',
'size': 60,
}
fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(55, 13))
plt.subplots_adjust(wspace=0.05)
ax = ax.flatten()
graph_pool = ['lattice', 'rrg', 'sw', 'complete']
annotate = ['(a)', '(b)', '(c)', '(d)']
Jij_pool = ['ones', 'randn', 'randn', 'sk']
n = [256, 80, 70, 20]
for i in range(4):
if i == 0:
beta = np.arange(0.1, 1.1, 0.1)
L = 16
node = 'mps'
if os.path.exists('results/squarelatticeL16/kacward{}.txt'.format(L)):
exact = np.loadtxt('results/squarelatticeL16/kacward{}.txt'.format(L))
# fe_tn = np.loadtxt('{}n256chi{}{}.txt'.format(result_dir, chi, node))
fe_tn_np = np.loadtxt('results/squarelatticeL16/n256chi{}{}_np.txt'.format(chi, node))
free_energy = np.loadtxt('results/squarelatticeL16/results.txt')
fe_MF = np.loadtxt('results/squarelatticeL16/MFn{}.txt'.format(L ** 2))
# left, bottom, width, height = 0.12, 0.12, 0.8, 0.8
"""
ax1 = fig.add_axes([left, bottom, width, height])
ax1.set_xlim((0.301, 1.02))
ax1.set_ylim((-2.2, -1.85))
ax1.set_yticks(np.arange(-2.2, -1.9, 0.1))
ax1.tick_params(labelsize=15)
ax1.set_xlabel(r'$\beta$', fontsize=22)
ax1.set_ylabel('Free Energy', fontsize=22)
ax1.plot(betas[33:], exact[33:], c='k')
ax1.scatter(beta[3:], free_energy[1][3:], facecolors='none', edgecolors='y', marker='o')
ax1.scatter(beta[3:], free_energy[3][3:], c='b', marker="x")
ax1.scatter(beta[3:], free_energy[2][3:], c='tab:orange', marker="d")
ax1.scatter(beta[3:], free_energy[0][3:], c='tab:cyan', marker='*')
ax1.scatter(beta[3:], fe_tn[0][3:], c='r')
ax1.legend(['Exact', 'Bethe', 'Dense', 'Conv', 'FVS', 'TN'], loc=2, ncol=1, fontsize=15, frameon=False)
"""
# plt.axes([0.55, 0.2, 0.35, 0.45])
# plt.axes([left, bottom, width, height])
ax[0].axvline(0.4406868, color='k', linestyle='--', label='_nolegend_')
"""
plt.scatter(beta, np.log10(np.abs(np.array(fe_tn[0]) - exact[0:100:11])),
c='r')
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[1]) - exact[0:100:11])),
facecolors='none', edgecolors='y', marker="o")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[2]) - exact[0:100:11])),
c='tab:orange', marker="d")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[3]) - exact[0:100:11])),
c='b', marker="x")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[0]) - exact[0:100:11])),
c='tab:cyan', marker='*')
"""
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[0]) - exact)), c='r', marker='<', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[2]) - exact)), c='r', marker='>', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[4]) - exact)), c='r', marker='^', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[6]) - exact)), c='r', marker='v', mfc='none')
'''
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[0]) - exact)),
c='r', marker='<', mfc='none', label='Dmax: 1')
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[2]) - exact)),
c='r', marker='>', mfc='none', label='Dmax: 10')
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[4]) - exact)),
c='r', marker='^', mfc='none', label='Dmax: 20')
'''
lines = ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[6]) - exact)),
c='r', marker='v', mfc='none', label='Our method')
lines += ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[2]) - exact)),
c='c', marker="*", label='Conv VAN') # Conv
lines += ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[3]) - exact)),
c='k', marker='*', label='VAN') # Dense
# plt.plot(beta, np.log10(np.abs(np.array(free_energy[0]) - exact[0:100:11])),
# c='tab:cyan', marker='*') # FVS
lines += ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[1]) - exact)),
c='y', mfc='none', mec='y', marker="o", label='Bethe') # Bethe
lines += ax[0].plot(beta, np.log10(np.abs(fe_MF[1] - exact)),
c='g', marker="d", label='TAP') # TAP
lines += ax[0].plot(beta, np.log10(np.abs(fe_MF[2] - exact)),
c='b', marker='x', label='NMF') # NMF
lines.reverse()
ax[0].set_ylim((-18, 0))
ax[0].set_yticks([-1, -4, -7, -10, -13, -16])
ax[0].set_yticklabels(['$10^{-1}$', '$10^{-4}$', '$10^{-7}$', '$10^{-10}$', '$10^{-13}$', '$10^{-16}$'],
fontsize=40)
ax[0].set_xticks(beta[0:10:2])
# ax[0].set_xticklabels(ax[0].get_xticks(), fontsize=40)
ax[0].set_ylabel('Relative Error', fontsize=45)
ax[0].set_xlabel(r'$\beta$', fontsize=45)
# ax[0].set_title(annotate[0], fontsize=50)
ax[0].text(0.5, -0.2, annotate[0], fontdict=font_title, transform=ax[0].transAxes, ha="center")
ax0 = ax[0].inset_axes([0.5, 0.275, 0.4, 0.4])
L = 5
grid = nx.grid_2d_graph(L, L)
pos = list(grid.nodes)
pos_grid = {}.fromkeys(np.arange(len(pos)))
for i in pos_grid.keys():
pos_grid[i] = pos[i]
edges_2d = list(grid.edges)
edges = [(i[0] * L + i[1], j[0] * L + j[1]) for i, j in edges_2d]
graph = nx.Graph()
graph.add_nodes_from(np.arange(len(pos)))
graph.add_edges_from(edges)
nodes = nx.draw_networkx_nodes(graph, pos_grid, ax=ax0)
nodes.set_color('white')
nodes.set_edgecolor('k')
nx.draw_networkx_edges(graph, pos_grid, ax=ax0, width=3)
ax0.set_axis_off()
# ax[0].legend(loc='center left', ncol=2, fontsize=20, frameon=False)
else:
D = 16
beta = np.arange(0.1, 2.1, 0.1)
results = np.loadtxt('{}{}_{}_Dmax=50_chi={}_Jij={}.txt'.format(
result_dir, graph_pool[i], n[i], chi, Jij_pool[i]))
exact = results[:, 1]
tn = np.log10(abs(results[:, 2]).reshape(-1, 10) + 1e-20)
results_van = np.loadtxt('{}{}_{}_Jij={}_van.txt'.format(
result_dir, graph_pool[i], n[i], Jij_pool[i]))
nmf = np.log10(abs(results[:, 4]).reshape(-1, 10))
tap = np.log10(abs(results[:, 6]).reshape(-1, 10))
bp = np.log10(abs(results[:, 8]).reshape(-1, 10))
van = np.log10(abs(results_van[:, 1] - exact).reshape(-1, 10))
ax[i].plot(beta, nmf.mean(axis=1), c='b', marker='x', label='NMF')
ax[i].plot(beta, tap.mean(axis=1), c='g', marker="d", label='TAP')
ax[i].plot(beta, bp.mean(axis=1), c='y', mfc='none', mec='y', marker="o", label='Bethe')
ax[i].plot(beta, van.mean(axis=1), c='k', marker='*', label='VAN')
ax[i].plot(beta, tn.mean(axis=1), c='r', marker='v', mfc='none', label='TN')
'''
Dmax_list = [1, 10, 20, 50]
marker_list = ['<', '>', '^', 'v']
for Dmax in Dmax_list:
results = np.loadtxt('{}{}_{}_Dmax={}_chi={}_Jij={}.txt'.format(
result_dir, graph_pool[i], n[i], Dmax, chi, Jij_pool[i]))
exact = results[:, 1]
tn = np.log10(abs(results[:, 2]).reshape(-1, 10) + 1e-20)
if Dmax == 1:
results_van = np.loadtxt('{}{}_{}_Jij={}_van.txt'.format(
result_dir, graph_pool[i], n[i], Jij_pool[i]))
nmf = np.log10(abs(results[:, 4]).reshape(-1, 10))
tap = np.log10(abs(results[:, 6]).reshape(-1, 10))
bp = np.log10(abs(results[:, 8]).reshape(-1, 10))
van = np.log10(abs(results_van[:, 1] - exact).reshape(-1, 10))
ax[i].plot(beta, nmf.mean(axis=1), c='b', marker='x', label='NMF')
ax[i].plot(beta, tap.mean(axis=1), c='tab:orange', marker="d", label='TAP')
ax[i].plot(beta, bp.mean(axis=1), c='y', mfc='none', mec='y', marker="o", label='Bethe')
ax[i].plot(beta, van.mean(axis=1), c='k', marker='*', label='VAN')
ax[i].plot(beta, tn.mean(axis=1), c='r', marker=marker_list[Dmax_list.index(Dmax)], mfc='none',
label='Dmax: {}'.format(Dmax))
"""
plt.errorbar(beta, nmf.mean(axis=1), yerr=nmf.std(axis=1),
c='b', fmt='-x', capsize=7, ms=3, linewidth=2, label='NMF')
plt.errorbar(beta, tap.mean(axis=1), yerr=tap.std(axis=1),
c='tab:orange', fmt='-d', capsize=7, ms=3, linewidth=2, label='TAP')
plt.errorbar(beta, bp.mean(axis=1), yerr=bp.std(axis=1),
c='y', fmt='-o', capsize=7, ms=3, linewidth=2, label='BP')
plt.errorbar(beta, van.mean(axis=1), yerr=van.std(axis=1),
c='k', fmt='-*', capsize=7, ms=3, linewidth=2, label='VAN')
plt.errorbar(beta, tn.mean(axis=1), yerr=tn.std(axis=1),
c='r', fmt='-{}'.format(marker_list[Dmax_list.index(Dmax)]),
capsize=7, ms=3, linewidth=2, label='Dmax: {}'.format(Dmax))
"""
'''
ax[i].set_ylim((-18, 0))
ax[i].set_yticks([-1, -4, -7, -10, -13, -16])
ax[i].set_yticklabels([], fontsize=40)
# ax[i].set_yticklabels(['$10^{-1}$', '$10^{-4}$', '$10^{-7}$', '$10^{-10}$', '$10^{-13}$', '$10^{-16}$'],
# fontsize=40)
ax[i].set_xticks(beta[0:-1:4])
# ax[i].set_xticklabels(ax[i].get_xticks(), fontsize=40)
# ax[i].set_ylabel('Relative Error', fontsize=22)
ax[i].set_xlabel(r'$\beta$', fontsize=45)
ax[i].text(0.5, -0.2, annotate[i], fontdict=font_title, transform=ax[i].transAxes, ha="center")
# ax[i].set_title(annotate[i], fontsize=50)
graph_ax = ax[i].inset_axes([0.5, 0.275, 0.45, 0.45])
graph_ax.set_axis_off()
if i == 1:
rrg = nx.random_regular_graph(3, D, seed=2)
pos = nx.circular_layout(rrg)
nodes = nx.draw_networkx_nodes(rrg, pos, ax=graph_ax)
nodes.set_color('white')
nodes.set_edgecolor('k')
nx.draw_networkx_edges(rrg, pos, ax=graph_ax, width=3)
elif i == 2:
sw = nx.watts_strogatz_graph(D, 4, 0.4, seed=1)
pos = nx.circular_layout(sw)
nodes = nx.draw_networkx_nodes(sw, pos, ax=graph_ax)
nodes.set_color('white')
nodes.set_edgecolor('k')
nx.draw_networkx_edges(sw, pos, ax=graph_ax, width=3)
else:
sk = nx.complete_graph(D)
pos = nx.circular_layout(sk)
nodes = nx.draw_networkx_nodes(sk, pos, ax=graph_ax)
nodes.set_color('white')
nodes.set_edgecolor('k')
nx.draw_networkx_edges(sk, pos, ax=graph_ax)
labels = ['Our method', 'Conv VAN', 'VAN', 'Bethe', 'TAP', 'NMF']
labels.reverse()
ax[i].legend(lines, labels,
loc='center left', bbox_to_anchor=(0, 0.44), ncol=1,
fontsize=40, frameon=False)
plt.savefig('fig/relative_errorv2.eps', bbox_inches='tight', dpi=300)
def plot_combine_tn(result_dir, chi):
plt.rc('xtick', labelsize=30)
plt.rc('ytick', labelsize=30)
plt.rc('legend', fontsize=30)
plt.rc('lines', lw=3, markersize=10, mew=2)
plt.rc('axes', labelsize=30)
fig, ax = plt.subplots(nrows=1, ncols=4)
plt.subplots_adjust(wspace=0.05)
ax = ax.flatten()
# params['figure.figsize'] = [40, 10]
# plt.rcParams.update(params)
graph_pool = ['lattice', 'rrg', 'sw', 'complete']
Jij_pool = ['ones', 'randn', 'randn', 'sk']
n = [256, 80, 70, 20]
for i in range(4):
if i == 0:
beta = np.arange(0.1, 1.1, 0.1)
L = 16
node = 'mps'
if os.path.exists('results/squarelatticeL16/kacward{}.txt'.format(L)):
exact = np.loadtxt('results/squarelatticeL16/kacward{}.txt'.format(L))
# fe_tn = np.loadtxt('{}n256chi{}{}.txt'.format(result_dir, chi, node))
fe_tn_np = np.loadtxt('results/squarelatticeL16/n256chi{}{}_np.txt'.format(chi, node))
free_energy = np.loadtxt('results/squarelatticeL16/results.txt')
fe_MF = np.loadtxt('results/squarelatticeL16/MFn{}.txt'.format(L ** 2))
# left, bottom, width, height = 0.12, 0.12, 0.8, 0.8
"""
ax1 = fig.add_axes([left, bottom, width, height])
ax1.set_xlim((0.301, 1.02))
ax1.set_ylim((-2.2, -1.85))
ax1.set_yticks(np.arange(-2.2, -1.9, 0.1))
ax1.tick_params(labelsize=15)
ax1.set_xlabel(r'$\beta$', fontsize=22)
ax1.set_ylabel('Free Energy', fontsize=22)
ax1.plot(betas[33:], exact[33:], c='k')
ax1.scatter(beta[3:], free_energy[1][3:], facecolors='none', edgecolors='y', marker='o')
ax1.scatter(beta[3:], free_energy[3][3:], c='b', marker="x")
ax1.scatter(beta[3:], free_energy[2][3:], c='tab:orange', marker="d")
ax1.scatter(beta[3:], free_energy[0][3:], c='tab:cyan', marker='*')
ax1.scatter(beta[3:], fe_tn[0][3:], c='r')
ax1.legend(['Exact', 'Bethe', 'Dense', 'Conv', 'FVS', 'TN'], loc=2, ncol=1, fontsize=15, frameon=False)
"""
# plt.axes([0.55, 0.2, 0.35, 0.45])
# plt.axes([left, bottom, width, height])
ax[0].axvline(0.4406868, color='k', linestyle='--', label='_nolegend_')
"""
plt.scatter(beta, np.log10(np.abs(np.array(fe_tn[0]) - exact[0:100:11])),
c='r')
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[1]) - exact[0:100:11])),
facecolors='none', edgecolors='y', marker="o")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[2]) - exact[0:100:11])),
c='tab:orange', marker="d")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[3]) - exact[0:100:11])),
c='b', marker="x")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[0]) - exact[0:100:11])),
c='tab:cyan', marker='*')
"""
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[0]) - exact)), c='r', marker='<', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[2]) - exact)), c='r', marker='>', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[4]) - exact)), c='r', marker='^', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[6]) - exact)), c='r', marker='v', mfc='none')
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[0]) - exact)),
c='y', marker='<', mfc='none', label='Dmax: 1')
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[2]) - exact)),
c='b', marker='>', mfc='none', label='Dmax: 10')
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[4]) - exact)),
c='tab:orange', marker='^', mfc='none', label='Dmax: 20')
ax[0].plot(beta, np.log10(np.abs(np.array(fe_tn_np[6]) - exact)),
c='r', marker='v', mfc='none', label='Dmax: 50')
'''
ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[2]) - exact)),
c='c', marker="*", label='Conv') # Conv
ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[3]) - exact)),
c='k', marker='*', label='Dense') # Dense
# plt.plot(beta, np.log10(np.abs(np.array(free_energy[0]) - exact[0:100:11])),
# c='tab:cyan', marker='*') # FVS
ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[1]) - exact)),
c='y', mfc='none', mec='y', marker="o", label='Bethe') # Bethe
ax[0].plot(beta, np.log10(np.abs(fe_MF[1] - exact)),
c='tab:orange', marker="d", label='TAP') # TAP
ax[0].plot(beta, np.log10(np.abs(fe_MF[2] - exact)),
c='b', marker='x', label='NMF') # NMF
'''
# ax[0].legend(loc=4, ncol=2, fontsize=15, frameon=False)
ax[0].set_ylim((-18, 0))
ax[0].set_yticks([-1, -4, -7, -10, -13, -16])
ax[0].set_yticklabels(['$10^{-1}$', '$10^{-4}$', '$10^{-7}$', '$10^{-10}$', '$10^{-13}$', '$10^{-16}$'])
ax[0].set_xticks(beta[0:10:2])
ax[0].set_ylabel('Relative Error')
ax[0].set_xlabel(r'$\beta$')
else:
beta = np.arange(0.1, 2.1, 0.1)
Dmax_list = [1, 10, 20, 50]
marker_list = ['<', '>', '^', 'v']
color_list = ['y', 'b', 'tab:orange', 'r']
for Dmax in Dmax_list:
results = np.loadtxt('{}{}_{}_Dmax={}_chi={}_Jij={}.txt'.format(
result_dir, graph_pool[i], n[i], Dmax, chi, Jij_pool[i]))
exact = results[:, 1]
tn = np.log10(abs(results[:, 2]).reshape(-1, 10) + 1e-20)
ax[i].plot(beta, tn.mean(axis=1), c=color_list[Dmax_list.index(Dmax)],
marker=marker_list[Dmax_list.index(Dmax)], mfc='none', label='Dmax: {}'.format(Dmax))
"""
plt.errorbar(beta, nmf.mean(axis=1), yerr=nmf.std(axis=1),
c='b', fmt='-x', capsize=7, ms=3, linewidth=2, label='NMF')
plt.errorbar(beta, tap.mean(axis=1), yerr=tap.std(axis=1),
c='tab:orange', fmt='-d', capsize=7, ms=3, linewidth=2, label='TAP')
plt.errorbar(beta, bp.mean(axis=1), yerr=bp.std(axis=1),
c='y', fmt='-o', capsize=7, ms=3, linewidth=2, label='BP')
plt.errorbar(beta, van.mean(axis=1), yerr=van.std(axis=1),
c='k', fmt='-*', capsize=7, ms=3, linewidth=2, label='VAN')
plt.errorbar(beta, tn.mean(axis=1), yerr=tn.std(axis=1),
c='r', fmt='-{}'.format(marker_list[Dmax_list.index(Dmax)]),
capsize=7, ms=3, linewidth=2, label='Dmax: {}'.format(Dmax))
"""
if i == 3:
ax[i].legend(loc='center right', ncol=1, frameon=False)
ax[i].set_ylim((-18, 0))
ax[i].set_yticks([-1, -4, -7, -10, -13, -16])
ax[i].set_yticklabels([])
ax[i].set_xticks(beta[0:-1:4])
# ax[i].set_ylabel('Relative Error', fontsize=22)
ax[i].set_xlabel(r'$\beta$')
plt.savefig('fig/relative_error_tnv1.eps', bbox_inches='tight', dpi=300)
def plot_combine_time(result_dir, chi):
plt.rc('xtick', labelsize=30)
plt.rc('ytick', labelsize=30)
plt.rc('legend', fontsize=30)
plt.rc('lines', lw=3, markersize=10, mew=2)
plt.rc('axes', labelsize=30)
fig, ax = plt.subplots(nrows=1, ncols=4)
ax = ax.flatten()
graph_pool = ['lattice', 'rrg', 'sw', 'complete']
Jij_pool = ['ones', 'randn', 'randn', 'sk']
n = [256, 80, 70, 20]
for i in range(4):
if i == 0:
beta = np.arange(0.1, 1.1, 0.1)
L = 16
node = 'mps'
if os.path.exists('results/squarelatticeL16/kacward{}.txt'.format(L)):
exact = np.loadtxt('results/squarelatticeL16/kacward{}.txt'.format(L))
# fe_tn = np.loadtxt('{}n256chi{}{}.txt'.format(result_dir, chi, node))
fe_tn_np = np.loadtxt('results/squarelatticeL16/n256chi{}{}_np.txt'.format(chi, node))
free_energy = np.loadtxt('results/squarelatticeL16/results.txt')
fe_MF = np.loadtxt('results/squarelatticeL16/MFn{}.txt'.format(L ** 2))
# left, bottom, width, height = 0.12, 0.12, 0.8, 0.8
"""
ax1 = fig.add_axes([left, bottom, width, height])
ax1.set_xlim((0.301, 1.02))
ax1.set_ylim((-2.2, -1.85))
ax1.set_yticks(np.arange(-2.2, -1.9, 0.1))
ax1.tick_params(labelsize=15)
ax1.set_xlabel(r'$\beta$', fontsize=22)
ax1.set_ylabel('Free Energy', fontsize=22)
ax1.plot(betas[33:], exact[33:], c='k')
ax1.scatter(beta[3:], free_energy[1][3:], facecolors='none', edgecolors='y', marker='o')
ax1.scatter(beta[3:], free_energy[3][3:], c='b', marker="x")
ax1.scatter(beta[3:], free_energy[2][3:], c='tab:orange', marker="d")
ax1.scatter(beta[3:], free_energy[0][3:], c='tab:cyan', marker='*')
ax1.scatter(beta[3:], fe_tn[0][3:], c='r')
ax1.legend(['Exact', 'Bethe', 'Dense', 'Conv', 'FVS', 'TN'], loc=2, ncol=1, fontsize=15, frameon=False)
"""
# plt.axes([0.55, 0.2, 0.35, 0.45])
# plt.axes([left, bottom, width, height])
ax[0].axvline(0.4406868, color='k', linestyle='--', label='_nolegend_')
"""
plt.scatter(beta, np.log10(np.abs(np.array(fe_tn[0]) - exact[0:100:11])),
c='r')
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[1]) - exact[0:100:11])),
facecolors='none', edgecolors='y', marker="o")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[2]) - exact[0:100:11])),
c='tab:orange', marker="d")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[3]) - exact[0:100:11])),
c='b', marker="x")
plt.scatter(beta, np.log10(np.abs(np.array(free_energy[0]) - exact[0:100:11])),
c='tab:cyan', marker='*')
"""
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[0]) - exact)), c='r', marker='<', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[2]) - exact)), c='r', marker='>', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[4]) - exact)), c='r', marker='^', mfc='none')
# plt.plot(beta, np.log10(np.abs(np.array(fe_tn[6]) - exact)), c='r', marker='v', mfc='none')
ax[0].plot(beta, np.array(fe_tn_np[1]),
c='y', marker='<', mfc='none', label='Dmax: 1')
ax[0].plot(beta, np.array(fe_tn_np[3]),
c='b', marker='>', mfc='none', label='Dmax: 10')
ax[0].plot(beta, np.array(fe_tn_np[5]),
c='tab:orange', marker='^', mfc='none', label='Dmax: 20')
ax[0].plot(beta, np.array(fe_tn_np[7]),
c='r', marker='v', mfc='none', label='Dmax: 50')
'''
ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[2]) - exact)),
c='c', marker="*", label='Conv') # Conv
ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[3]) - exact)),
c='k', marker='*', label='Dense') # Dense
# plt.plot(beta, np.log10(np.abs(np.array(free_energy[0]) - exact[0:100:11])),
# c='tab:cyan', marker='*') # FVS
ax[0].plot(beta, np.log10(np.abs(np.array(free_energy[1]) - exact)),
c='y', mfc='none', mec='y', marker="o", label='Bethe') # Bethe
ax[0].plot(beta, np.log10(np.abs(fe_MF[1] - exact)),
c='tab:orange', marker="d", label='TAP') # TAP
ax[0].plot(beta, np.log10(np.abs(fe_MF[2] - exact)),
c='b', marker='x', label='NMF') # NMF
'''
# ax[0].legend(loc=4, ncol=2, fontsize=15, frameon=False)
ax[0].set_xticks(beta[0:10:2])
ax[0].set_ylabel('Time(s)')
ax[0].set_xlabel(r'$\beta$')
else:
beta = np.arange(0.1, 2.1, 0.1)
Dmax_list = [1, 10, 20, 50]
marker_list = ['<', '>', '^', 'v']
color_list = ['y', 'b', 'tab:orange', 'r']
for Dmax in Dmax_list:
results = np.loadtxt('{}{}_{}_Dmax={}_chi={}_Jij={}.txt'.format(
result_dir, graph_pool[i], n[i], Dmax, chi, Jij_pool[i]))
exact = results[:, 1]
time_tn = results[:, 3].reshape(-1, 10)
ax[i].plot(beta, time_tn.mean(axis=1), c=color_list[Dmax_list.index(Dmax)],
marker=marker_list[Dmax_list.index(Dmax)], mfc='none', label='Dmax: {}'.format(Dmax))
"""
plt.errorbar(beta, nmf.mean(axis=1), yerr=nmf.std(axis=1),
c='b', fmt='-x', capsize=7, ms=3, linewidth=2, label='NMF')
plt.errorbar(beta, tap.mean(axis=1), yerr=tap.std(axis=1),
c='tab:orange', fmt='-d', capsize=7, ms=3, linewidth=2, label='TAP')
plt.errorbar(beta, bp.mean(axis=1), yerr=bp.std(axis=1),
c='y', fmt='-o', capsize=7, ms=3, linewidth=2, label='BP')
plt.errorbar(beta, van.mean(axis=1), yerr=van.std(axis=1),
c='k', fmt='-*', capsize=7, ms=3, linewidth=2, label='VAN')
plt.errorbar(beta, tn.mean(axis=1), yerr=tn.std(axis=1),
c='r', fmt='-{}'.format(marker_list[Dmax_list.index(Dmax)]),
capsize=7, ms=3, linewidth=2, label='Dmax: {}'.format(Dmax))
"""
if i == 3:
ax[i].legend(loc='center right', ncol=1, frameon=False)
ax[i].set_xticks(beta[0:-1:4])
# ax[i].set_ylabel('Relative Error', fontsize=22)
ax[i].set_xlabel(r'$\beta$')
plt.savefig('fig/time_tnv1.eps', bbox_inches='tight', dpi=300)
if __name__ == '__main__':
plot_combine('results/', 500)
plot_combine_tn('results/', 500)
plot_combine_time('results/', 500)