Created
March 24, 2019 15:50
-
-
Save speps/024bb0f878df74dcef7814ba595d7916 to your computer and use it in GitHub Desktop.
Julia: simple example of least squares L2 norm optimization using JuMP
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
# very simple example of L2 norm optimization using JuMP | |
using Images, TestImages, JuMP, Ipopt | |
# crop a test image to 50x50 and then flatten it | |
original = Gray.(testimage("lighthouse"))[250:299, 150:199] | |
w = size(original, 1) | |
h = size(original, 2) | |
t = w * h | |
y = reshape(float.(original), t) | |
# model the problem | |
model = Model(solver=IpoptSolver(tol=1e-6)) | |
@variable(model, 0 <= v[1:t] <= 1) # grayscale only | |
# `LinearAlgebra.norm` isn't accepted so make our own L2 norm | |
@NLobjective(model, Min, sqrt(sum((v[i] - y[i])^2 for i in 1:t))) | |
solve(model) | |
solution = Gray.(reshape(getvalue(v), w, h)) | |
# side by side result + diff | |
d = (float.(solution) - float.(original))^2 | |
hcat(original, solution, Gray.(scaleminmax(minimum(d), maximum(d)).(d))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment