plot_sparsity (generic function with 1 method)
Difference-of-Convex Algorithm with Frank-Wolfe
This example shows the optimization of a difference-of-convex problem of the form:
\[min\limits_{x \in \mathcal{X}} \phi(x) = f(x) - g(x)\]
with $f$, $g$ convex functions with access to subgradients and $f$ smooth.
The DCA-FW algorithm constructs local convex models of $\phi$ by linearizing $g$ and approximately optimizes them with FW. It is a local method that converges to a stationary point
using FrankWolfe
using LinearAlgebra
using Random
using SparseArrays
using StableRNGs
using Random
The convex functions $f$, $g$ will be generated as random convex quadratics:
\[\begin{align} f(x) & = \frac12 x^\top A x + a^\top x + c \\ g(x) & = \frac12 x^\top B x + b^\top x + d \end{align}\]
Setting up the problem functions and data
const n = 500 # Reduced dimension
# Generate random positive definite matrices to ensure convexity
function generate_problem_data(rng)
A_raw = randn(rng, n, n)
A_raw ./= opnorm(A_raw)
A = A_raw' * A_raw + 0.1 * I
B_raw = randn(rng, n, n)
B_raw ./= opnorm(B_raw)
B = B_raw' * B_raw + 0.1 * I
a = randn(rng, n)
b = randn(rng, n)
c = randn(rng)
d = -359434.0
return A, B, a, b, c, d
end
const rng = StableRNGs.StableRNG(1)
const A, B, a, b, c, d = generate_problem_data(rng)
([0.3664716461545837 0.007434201971305414 … 0.00445178537588482 0.01551441152702277; 0.007434201971305414 0.348228139474143 … 0.013654308840956 -0.025910486878046337; … ; 0.00445178537588482 0.013654308840956 … 0.38062614047229637 0.011896910948147394; 0.01551441152702277 -0.025910486878046337 … 0.011896910948147394 0.34299337924542395], [0.3685823592850501 0.0036477368657639037 … -0.01474711849204956 -0.009390758842324275; 0.0036477368657639037 0.36832277324228346 … -0.004708423922308731 0.019319726914150855; … ; -0.01474711849204956 -0.004708423922308731 … 0.3718244447067389 -0.007058980450873029; -0.009390758842324275 0.019319726914150855 … -0.007058980450873029 0.3697697085359215], [-0.5056985849854947, 1.8254695540945816, 1.1686948924955953, 0.025865043318803068, 0.7618207158940428, -1.9599078996573032, -0.28701522778989075, -1.210555966368952, -0.3476555815275666, 0.36622748810084865 … -1.2418975260665193, -2.042717239142079, -0.977247840514734, -0.9480533072182336, 0.2354971732415276, 1.1489207372892907, 0.26713737924876685, -0.11111819389928967, 1.2106291210589706, -0.36135542009200855], [-1.7352231531269595, -1.0889348569606767, -1.9696069675033783, -0.6944538146281424, 0.12594479576421444, 2.43890963984164, -0.3332102207771672, -1.598587279152474, 0.8970898814075347, 0.7441204116527911 … -0.07885951683533911, 0.921491279700674, 0.8755353895807455, 0.4142785039140312, 1.0607172005644177, -0.9845905380360871, 0.7603797972500105, 1.0314248598279463, -1.0727076372535254, -0.1591576537893938], -0.6622940864130684, -359434.0)
We can now define the two functions
function f(x)
return 0.5 * FrankWolfe.fast_dot(x, A, x) + dot(a, x) + c
end
function grad_f!(storage, x)
mul!(storage, A, x)
storage .+= a
return nothing
end
function g(x)
return 0.5 * FrankWolfe.fast_dot(x, B, x) + dot(b, x) + d
end
function grad_g!(storage, x)
mul!(storage, B, x)
storage .+= b
return nothing
end
# True objective function for verification
# It is not needed by the solver
function phi(x)
return f(x) - g(x)
end
lmo = FrankWolfe.KSparseLMO(5, 1000.0)
x0 = FrankWolfe.compute_extreme_point(lmo, randn(n))
x_final, primal_final, dca_gap_final, iterations, status, traj_data = FrankWolfe.dca_fw(
f,
grad_f!,
g,
grad_g!,
lmo,
copy(x0),
max_iteration=200, # Outer iterations
max_inner_iteration=10000, # Inner iterations
epsilon=1e-5, # Tolerance for DCA gap
line_search=FrankWolfe.Secant(),
verbose=true,
trajectory=true,
verbose_inner=false,
print_iter=10,
use_corrective_fw=true,
use_dca_early_stopping=true,
grad_f_workspace=collect(x0),
grad_g_workspace=collect(x0),
)
res_boost = FrankWolfe.dca_fw(
f,
grad_f!,
g,
grad_g!,
lmo,
copy(x0),
boosted=true,
max_iteration=200, # Outer iterations
max_inner_iteration=10000, # Inner iterations
epsilon=1e-5, # Tolerance for DCA gap
line_search=FrankWolfe.Secant(),
verbose=true,
trajectory=true,
verbose_inner=false,
print_iter=10,
use_corrective_fw=true,
use_dca_early_stopping=true,
grad_f_workspace=collect(x0),
grad_g_workspace=collect(x0),
)
(x = [192] = -1000.0
[223] = -1000.0
[300] = -1000.0
[312] = -978.002
[369] = 1000.0
[459] = -21.998, primal = 53053.62668657419, dca_gap = 9.078721632249653e-6, iterations = 115, status = FrankWolfe.STATUS_OPTIMAL, traj_data = Any[(1, 308788.794777679, 202045.62131051338, 106743.17346716564, 0.000170202), (2, 257652.16798525793, 202229.8060662927, 55422.36191896525, 0.001568898), (3, 228635.67581443622, 198470.99287506266, 30164.682939373575, 0.002579314), (4, 206670.81057861546, 183816.54082363035, 22854.269754985125, 0.003726686), (5, 190076.91977128672, 171140.900621136, 18936.0191501507, 0.004875947), (6, 174828.53082975262, 159283.4774603285, 15545.053369424137, 0.005965634), (7, 160916.72737909562, 148709.69063325447, 12207.036745841162, 0.007088256), (8, 146014.1295669526, 131489.11752210348, 14525.012044849092, 0.008101774), (9, 126564.64600150601, 109594.265785741, 16970.38021576502, 0.009048954), (10, 110610.96728404437, 88755.31082824763, 21855.65645579673, 0.010166023) … (107, 53053.62693696562, 53053.626906946076, 3.0019548376003513e-5, 0.044301969), (108, 53053.62688712508, 53053.626861273864, 2.58512154687196e-5, 0.044568839), (109, 53053.62684420473, 53053.626821943326, 2.2261403501033783e-5, 0.044815384), (110, 53053.626807244145, 53053.626788073816, 1.917032932396978e-5, 0.045087601), (111, 53053.62677541585, 53053.626758907405, 1.6508444787177723e-5, 0.045495412), (112, 53053.626748007024, 53053.626733791156, 1.4215866031008773e-5, 0.045826431), (113, 53053.6267244037, 53053.626712161764, 1.2241939657542389e-5, 0.046135457), (114, 53053.6267040777, 53053.6266935352, 1.0542498785071075e-5, 0.046411581), (115, 53053.62668657419, 53053.62667749547, 9.078721632249653e-6, 0.046703235), (115, 53053.62668657419, 53053.62667749547, 9.078721632249653e-6, 0.046983038)])
Plotting the resulting trajectory
We modify the y axis to highlight that we are plotting the DCA gap, not the FW gap
data = [traj_data, traj_data_boosted.traj_data]
label = ["DCA-FW", "DCA-FW-B"]
p_res = plot_trajectories(data, label, marker_shapes=[:o, :x])
ylabel!(p_res.subplots[3], "DCA gap")
display(p_res)
This page was generated using Literate.jl.