Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoine-levitt/95c84c3e75965913a63c9f11cef0368a to your computer and use it in GitHub Desktop.
Save antoine-levitt/95c84c3e75965913a63c9f11cef0368a to your computer and use it in GitHub Desktop.
using Optim
struct Box <: Optim.Manifold
min
max
end
import Optim.retract!
import Optim.project_tangent!
retract!(B::Box, x) = (x .= min.(B.max,max.(B.min, x)))
function project_tangent!(B::Box,g,x)
for i in eachindex(x)
if x[i] == B.min[i] || x[i] == B.max[i]
g[i] = 0
end
end
end
n = 10
f(x) = -sum(abs2,x)
g(x) = -2x
g!(stor,x) = copy!(stor,g(x))
x0 = randn(n)
manif = Box(fill(-1.0, (n,)),fill(1.0, (n,)))
res = Optim.optimize(f, g!, x0, Optim.GradientDescent(manifold=manif))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment