This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop dependency on OptimalTransport and implement LP problem (#15)
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
- Loading branch information
Showing
8 changed files
with
153 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ jobs: | |
strategy: | ||
matrix: | ||
version: | ||
- '1.4' | ||
- '1.3' | ||
- '1' | ||
- 'nightly' | ||
os: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
function sqwasserstein(μ, ν, C, optimizer) | ||
P = optimal_transport_map(μ, ν, C, optimizer) | ||
return LinearAlgebra.dot(P, C) | ||
end | ||
|
||
""" | ||
optimal_transport_map(μ, ν, C, optimizer) | ||
Solve the discrete optimal transport problem with source `μ`, target `ν`, and | ||
cost matrix `C` as a linear programming (LP) problem with the given `optimizer`. | ||
More concretely, this function returns a solution `P` of the LP problem | ||
```math | ||
\\begin{aligned} | ||
\\min_{p} c^T p & \\\\ | ||
\\text{subject to } A_1p &= μ \\\\ | ||
A_2p &= ν \\\\ | ||
0 &\\leq p | ||
\\end{aligned} | ||
``` | ||
where | ||
```math | ||
\\begin{aligned} | ||
p &= [P_{1,1},P_{2,1},\\ldots,P_{n,1},P_{2,1},\\ldots,P_{n,m}]^T, \\\\ | ||
c &= [C_{1,1},C_{2,1},\\ldots,C_{n,1},C_{2,1},\\ldots,C_{n,m}]^T, \\\\ | ||
A_1 &= \\begin{bmatrix} | ||
1_n^T \\otimes I_m | ||
\\end{bmatrix}, \\\\ | ||
A_2 &= \\begin{bmatrix} | ||
I_n \\otimes 1_m^T | ||
\\end{bmatrix}. | ||
\\end{aligned} | ||
``` | ||
A possible choice of `optimizer` is `Tulip.Optimizer()` in the `Tulip` package. | ||
""" | ||
function optimal_transport_map(μ, ν, C, model::MOI.ModelLike) | ||
nμ = length(μ) | ||
nν = length(ν) | ||
size(C) == (nμ, nν) || error("size of `C` does not match size of `μ` and `ν`") | ||
nC = length(C) | ||
|
||
# define variables | ||
x = MOI.add_variables(model, nC) | ||
xmat = reshape(x, nμ, nν) | ||
|
||
# define objective function | ||
T = eltype(C) | ||
zero_T = zero(T) | ||
MOI.set( | ||
model, | ||
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(), | ||
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(vec(C), x), zero_T), | ||
) | ||
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) | ||
|
||
# add constraints | ||
for xi in x | ||
MOI.add_constraint(model, MOI.SingleVariable(xi), MOI.GreaterThan(zero_T)) | ||
end | ||
|
||
# add constraints for source | ||
for (xs, μi) in zip(eachrow(xmat), μ) | ||
f = MOI.ScalarAffineFunction( | ||
[MOI.ScalarAffineTerm(one(μi), xi) for xi in xs], zero(μi) | ||
) | ||
MOI.add_constraint(model, f, MOI.EqualTo(μi)) | ||
end | ||
|
||
# add constraints for target | ||
for (xs, νi) in zip(eachcol(xmat), ν) | ||
f = MOI.ScalarAffineFunction( | ||
[MOI.ScalarAffineTerm(one(νi), xi) for xi in xs], zero(νi) | ||
) | ||
MOI.add_constraint(model, f, MOI.EqualTo(νi)) | ||
end | ||
|
||
# compute optimal solution | ||
MOI.optimize!(model) | ||
p = MOI.get(model, MOI.VariablePrimal(), x) | ||
P = reshape(p, nμ, nν) | ||
|
||
return P | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@testset "optimaltransport.jl" begin | ||
M = 200 | ||
N = 250 | ||
μ = rand(M) | ||
ν = rand(N) | ||
μ ./= sum(μ) | ||
ν ./= sum(ν) | ||
|
||
# create random cost matrix | ||
C = pairwise(SqEuclidean(), rand(1, M), rand(1, N); dims=2) | ||
|
||
# compute optimal transport map and squared Wasserstein distance | ||
lp = Tulip.Optimizer() | ||
P = optimal_transport_map(μ, ν, C, lp) | ||
@test size(C) == size(P) | ||
@test MOI.get(lp, MOI.TerminationStatus()) == MOI.OPTIMAL | ||
|
||
lp = Tulip.Optimizer() | ||
cost = sqwasserstein(μ, ν, C, lp) | ||
@test dot(C, P) ≈ cost atol = 1e-5 | ||
@test MOI.get(lp, MOI.TerminationStatus()) == MOI.OPTIMAL | ||
|
||
# compute optimal transport map and cost with OptimalTransport.jl | ||
@static if VERSION >= v"1.4" | ||
P_ot = emd(μ, ν, C, Tulip.Optimizer()) | ||
@test maximum(abs, P .- P_ot) < 1e-2 | ||
|
||
cost_ot = emd2(μ, ν, C, Tulip.Optimizer()) | ||
@test cost ≈ cost_ot atol = 1e-5 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0388e26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
0388e26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/35725
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: