<jupyter_start><jupyter_text>Example: D-optimal experiment design with random datasetsSolve the D-Optimal experiment design problem$$\begin{array}{ll}\textrm{minimize}   & F(x):=\log\left(\det\left(\sum_{i=1}^n x_i V_i V_i^T\right)\right) \\\textrm{subject to} & \sum_{i=1}^n x_i = 1, \\                     & x_i\geq 0, \quad i=1,\ldots,n\end{array}$$where $V_i\in R^m$ for $i=1,\ldots,n$.<jupyter_code>#cd C:\\github\accbpg
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams.update({'font.size': 16, 'legend.fontsize':14, 'font.family': 'serif'})
# matplotlib.rcParams.update({'text.usetex': True})<jupyter_output><empty_output><jupyter_text>Compare BPG and ABPG with different values of gamma<jupyter_code>import accbpg

# Generate a random instance of the D-optimal design problem of size m by n
m = 80
n = 200
f, h, L, x0 = accbpg.D_opt_design(m, n, randseed=10)

# Solve the problem using BPG and ABPG with different values of gamma (TSE) 
x00, F00, G00, T00 = accbpg.BPG( f, h, L, x0, maxitrs=1000, linesearch=False, verbskip=100)
x10, F10, G10, T10 = accbpg.ABPG(f, h, L, x0, gamma=1.0, maxitrs=1000, theta_eq=True, verbskip=100)
x15, F15, G15, T15 = accbpg.ABPG(f, h, L, x0, gamma=1.5, maxitrs=1000, theta_eq=True, verbskip=100)
x20, F20, G20, T20 = accbpg.ABPG(f, h, L, x0, gamma=2.0, maxitrs=1000, theta_eq=True, verbskip=100)
x22, F22, G22, T22 = accbpg.ABPG(f, h, L, x0, gamma=2.2, maxitrs=2000, theta_eq=True, verbskip=100)
# Plot the objective gap and estimated gains for triangle scaling
fig, _ = plt.subplots(1, 2, figsize=(11, 4))

labels = [r"BPG", r"ABPG $\gamma=1.0$", r"ABPG $\gamma=1.5$", r"ABPG $\gamma=2.0$", r"ABPG $\gamma=2.2$"]
styles = ['k:', 'g-', 'b-.', 'k-', 'r--']
dashes = [[1,2], [], [4,2,1,2], [], [4,2]]

ax1 = plt.subplot(1, 2, 1)
y_vals = [F00, F10, F15, F20, F22]
accbpg.plot_comparisons(ax1, y_vals, labels, x_vals=[], plotdiff=True, yscale="log", xlim=[-10, 1000], ylim=[1e-5, 2], 
                        xlabel=r"Iteration number $k$", ylabel=r"$F(x_k)-F_\star$", legendloc="upper right", 
                        linestyles=styles, linedash=dashes)

ax2 = plt.subplot(1, 2, 2)
y_vals = [G10, G15, G20, G22]
accbpg.plot_comparisons(ax2, y_vals, labels[1:], x_vals=[], plotdiff=False, yscale="log", xlim=[-10, 1000], ylim=[1e-3, 5], 
                        xlabel=r"Iteration number $k$", ylabel=r'$\hat{G}_k$', legendloc="lower right", 
                        linestyles=styles[1:], linedash=dashes[1:])

plt.tight_layout(w_pad=4)
plt.show()
# pad = 0.05
# extent = ax1.get_tightbbox(fig.canvas.renderer).expanded(1+pad, 1+pad).transformed(fig.dpi_scale_trans.inverted())
# fig.savefig('./figures/D_opt_m80n200gamma_semilog.pdf', format='pdf', bbox_inches=extent)
# extent = ax2.get_tightbbox(fig.canvas.renderer).expanded(1+pad, 1+pad).transformed(fig.dpi_scale_trans.inverted())
# fig.savefig('./figures/D_opt_m80n200gamma_gains.pdf', format='pdf', bbox_inches=extent)<jupyter_output><empty_output><jupyter_text>Comparing BPG with line search and adaptive ABPG algorithms<jupyter_code># Solve the problem using BPG w/o line seach and adaptive ABPG with gamma=2 (TSE) 
x00, F00, G00, T00 = accbpg.BPG( f, h, L, x0, maxitrs=1000, linesearch=False, verbskip=100)
xLS, FLS, GLS, TLS = accbpg.BPG( f, h, L, x0, maxitrs=1000, linesearch=True, verbskip=100)
x20, F20, G20, T20 = accbpg.ABPG(f, h, L, x0, gamma=2.0, maxitrs=1000, theta_eq=True, verbskip=100)
x2e, F2e, Gamma2e, G2e, T2e = accbpg.ABPG_expo(f, h, L, x0, gamma0=3, maxitrs=1000, theta_eq=True, verbskip=100)
x2g, F2g, G2g, Gdiv2g, Gavg2g, T2g = accbpg.ABPG_gain(f, h, L, x0, gamma=2, maxitrs=3000, G0=0.1, theta_eq=True, verbskip=100)
# Plot the objective gap and estimated gains for triangle scaling
fig, _ = plt.subplots(1, 2, figsize=(11, 4))

labels = [r"BPG", r"BPG-LS", r"ABPG", r"ABPG-e", r"ABPG-g"]
styles = ['k:', 'g-', 'b-.', 'k-', 'r--']
dashes = [[1,2], [], [4,2,1,2], [], [4,2]]

ax1 = plt.subplot(1, 2, 1)
y_vals = [F00, FLS, F20, F2e, F2g]
accbpg.plot_comparisons(ax1, y_vals, labels, x_vals=[], plotdiff=True, yscale="log", xlim=[-10, 1000], ylim=[1e-5, 2], 
                        xlabel=r"Iteration number $k$", ylabel=r"$F(x_k)-F_\star$", legendloc="upper right", 
                        linestyles=styles, linedash=dashes)

ax2 = plt.subplot(1, 2, 2)
accbpg.plot_comparisons(ax2, y_vals, labels, x_vals=[], plotdiff=True, yscale="log", xscale='log', xlim=[1, 1000], ylim=[1e-5, 2], 
                        xlabel=r"Iteration number $k$", ylabel=r"$F(x_k)-F_\star$", legendloc="lower left", 
                        linestyles=styles, linedash=dashes)

plt.tight_layout(w_pad=4)
plt.show()
# pad = 0.05
# extent = ax1.get_tightbbox(fig.canvas.renderer).expanded(1+pad, 1+pad).transformed(fig.dpi_scale_trans.inverted())
# fig.savefig('./figures/D_opt_m80n200adapt_semilog.pdf', format='pdf', bbox_inches=extent)
# extent = ax2.get_tightbbox(fig.canvas.renderer).expanded(1+pad, 1+pad).transformed(fig.dpi_scale_trans.inverted())
# fig.savefig('./figures/D_opt_m80n200adapt_loglog.pdf', format='pdf', bbox_inches=extent)<jupyter_output><empty_output><jupyter_text>Comparing ABPG and ABPG with restart for nearly strongly convex problems<jupyter_code># Generate a random instance of the D-optimal design problem of size m by n
ms = 80
ns = 120
fs, hs, Ls, x0s = accbpg.D_opt_design(ms, ns, randseed=10)

# Solve the problem using BPG w/o line seach and adaptive ABPG with gamma=2 (TSE), also with restart
xs00, Fs00, Gs00, _ = accbpg.BPG( fs, hs, Ls, x0s, maxitrs=1000, linesearch=False, verbskip=100)
xsLS, FsLS, GsLS, _ = accbpg.BPG( fs, hs, Ls, x0s, maxitrs=1000, linesearch=True, verbskip=100)
xs20, Fs20, Gs20, _ = accbpg.ABPG(fs, hs, Ls, x0s, gamma=2.0, maxitrs=1000, theta_eq=True, restart=False, verbskip=100)
xs20rs, Fs20rs, Gs20rs, _ = accbpg.ABPG(fs, hs, Ls, x0s, gamma=2.0, maxitrs=1000, theta_eq=True, restart=True, verbskip=100)
xs2g, Fs2g, Gs2g, _, _, _ = accbpg.ABPG_gain(fs, hs, Ls, x0s, gamma=2, maxitrs=3000, G0=0.1, theta_eq=True, restart=False, verbskip=100)
xs2grs, Fs2grs, Gs2grs, _, _, _ = accbpg.ABPG_gain(fs, hs, Ls, x0s, gamma=2, maxitrs=3000, G0=0.1, theta_eq=True, restart=True, verbskip=100)
# Plot the objective gap and estimated gains for triangle scaling
fig, _ = plt.subplots(1, 2, figsize=(11, 4))

labels = [r"BPG", r"BPG-LS", r"ABPG", r"ABPG RS", r"ABPG-g", r"ABPG-g RS"]
styles = ['k:', 'g-', 'b-.', 'm-', 'k-', 'r--']
dashes = [[1,2], [], [4,2,1,2], [4,2,1,2,1,2], [], [4,2]]

ax1 = plt.subplot(1, 2, 1)
y_vals = [Fs00, FsLS, Fs20, Fs20rs, Fs2g, Fs2grs]
accbpg.plot_comparisons(ax1, y_vals, labels, x_vals=[], plotdiff=True, yscale="log", xlim=[0, 50], ylim=[1e-10, 1], 
                        xlabel=r"Iteration number $k$", ylabel=r"$F(x_k)-F_\star$", legendloc="upper right", 
                        linestyles=styles, linedash=dashes) 

ax2 = plt.subplot(1, 2, 2)
y_vals = [GsLS, Gs20, Gs20rs, Gs2g, Gs2grs]
accbpg.plot_comparisons(ax2, y_vals, labels[1:], x_vals=[], plotdiff=False, yscale="log", xlim=[0, 50], ylim=[1e-1, 1e2], 
                        xlabel=r"Iteration number $k$", ylabel=r'$\hat{G}_k$', legendloc="upper left", 
                        linestyles=styles[1:], linedash=dashes[1:])

plt.tight_layout(w_pad=4)
plt.show()
# pad = 0.05
# extent = ax1.get_tightbbox(fig.canvas.renderer).expanded(1+pad, 1+pad).transformed(fig.dpi_scale_trans.inverted())
# fig.savefig('./figures/D_opt_m80n120restart_semilog.pdf', format='pdf', bbox_inches=extent)
# extent = ax2.get_tightbbox(fig.canvas.renderer).expanded(1+pad, 1+pad).transformed(fig.dpi_scale_trans.inverted())
# fig.savefig('./figures/D_opt_m80n120restart_gains.pdf', format='pdf', bbox_inches=extent)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18197146562905317, 'fraction_numerical': 0.06627756160830091, 'mean_word_length': 4.7846961740435106, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '494757', 'license': 'MIT', 'n_tokens_mistral': 3626, 'n_tokens_neox': 3301, 'n_words': 677, 'path': 'ipynb/ex_Dopt_random.ipynb', 'repo_name': 'Bhaskers-Blu-Org2/accbpg', 'size': '7710'}