-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
min_distortion.jl
65 lines (56 loc) · 2.34 KB
/
min_distortion.jl
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
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See http://github.com/jump-dev/JuMP.jl
#############################################################################
using JuMP, SCS, Test
"""
example_min_distortion()
This example arises from computational geometry, in particular the problem of
embedding a general finite metric space into a euclidean space.
It is known that the 4-point metric space defined by the star graph:
x
\\
x — x
/
x
where distances are computed by length of the shortest path between vertices,
cannot be exactly embedded into a euclidean space of any dimension.
Here we will formulate and solve an SDP to compute the best possible embedding,
that is, the embedding f() that minimizes the distortion c such that
(1 / c) * D(a, b) ≤ ||f(a) - f(b)|| ≤ D(a, b)
for all points (a, b), where D(a, b) is the distance in the metric space.
Any embedding can be characterized by its Gram matrix Q, which is PSD, and
||f(a) - f(b)||^2 = Q[a, a] + Q[b, b] - 2 * Q[a, b]
We can therefore constrain
D[i, j]^2 ≤ Q[i, i] + Q[j, j] - 2 * Q[i, j] ≤ c^2 * D[i, j]^2
and minimize c^2, which gives us the SDP formulation below.
For more detail, see "Lectures on discrete geometry" by J. Matoušek, Springer,
2002, pp. 378-379.
"""
function example_min_distortion()
model = Model(SCS.Optimizer)
set_silent(model)
D = [0.0 1.0 1.0 1.0;
1.0 0.0 2.0 2.0;
1.0 2.0 0.0 2.0;
1.0 2.0 2.0 0.0]
@variable(model, c² >= 1.0)
@variable(model, Q[1:4, 1:4], PSD)
for i in 1:4
for j in (i + 1):4
@constraint(model, D[i, j]^2 <= Q[i, i] + Q[j, j] - 2 * Q[i, j])
@constraint(model, Q[i, i] + Q[j, j] - 2 * Q[i, j] <= c² * D[i, j]^2)
end
end
@objective(model, Min, c²)
JuMP.optimize!(model)
@test JuMP.termination_status(model) == MOI.OPTIMAL
@test JuMP.primal_status(model) == MOI.FEASIBLE_POINT
@test JuMP.objective_value(model) ≈ 4/3 atol = 1e-4
end
example_min_distortion()