Skip to content

Commit

Permalink
quasi-Newton models
Browse files Browse the repository at this point in the history
  • Loading branch information
dpo committed Sep 11, 2016
1 parent 9ed16b0 commit ba36781
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/NLPModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ function hess_op(nlp :: AbstractNLPModel, x :: Vector{Float64};
return LinearOperator(nlp.meta.nvar, Float64, v -> hprod(nlp, x, v; obj_weight=obj_weight, y=y))
end

update!(nlp :: AbstractNLPModel, args...; kwargs...) =
throw(NotImplementedError("update!"))
varscale(nlp :: AbstractNLPModel, args...; kwargs...) =
throw(NotImplementedError("varscale"))
lagscale(nlp :: AbstractNLPModel, args...; kwargs...) =
Expand All @@ -236,5 +238,6 @@ end
include("simple_model.jl")

include("slack_model.jl")
include("qn_model.jl")

end # module
64 changes: 64 additions & 0 deletions src/qn_model.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using LinearOperators

export QuasiNewtonModel, LBFGSModel, LSR1Model,
reset!,
obj, grad, grad!,
cons, cons!, jac_coord, jac, jprod, jprod!, jtprod, jtprod!,
hess, hprod

abstract QuasiNewtonModel <: AbstractNLPModel

type LBFGSModel <: QuasiNewtonModel
meta :: NLPModelMeta
model :: AbstractNLPModel
op :: LBFGSOperator
end

type LSR1Model <: QuasiNewtonModel
meta :: NLPModelMeta
model :: AbstractNLPModel
op :: LSR1Operator
end

"Construct a `LBFGSModel` from another type of model."
function LBFGSModel(nlp :: AbstractNLPModel; memory :: Int=5)
op = LBFGSOperator(nlp.meta.nvar, memory)
return LBFGSModel(nlp.meta, nlp, op)
end

"Construct a `LSR1Model` from another type of nlp."
function LSR1Model(nlp :: AbstractNLPModel; memory :: Int=5)
op = LSR1Operator(nlp.meta.nvar, memory)
return LSR1Model(nlp.meta, nlp, op)
end

# retrieve counters from underlying model
for counter in fieldnames(Counters)
@eval begin
$counter(nlp :: QuasiNewtonModel) = $counter(nlp.model)
export $counter
end
end

function reset!(nlp :: QuasiNewtonModel)
reset!(nlp.model.counters)
return nlp
end

# the following methods are not affected by the Hessian approximation
for meth in (:obj, :grad, :grad!, :cons, :cons!, :jac_coord, :jac, :jprod, :jprod!, :jtprod, :jtprod!)
@eval begin
$meth(nlp :: QuasiNewtonModel, args...) = $meth(nlp.model, args...)
end
end

# the following methods are affected by the Hessian approximation
hess_op(nlp :: QuasiNewtonModel, args...) = nlp.op
hprod(nlp :: QuasiNewtonModel, x :: Array{Float64}, v :: Array{Float64}; kwargs...) = nlp.op * v

function update!(nlp :: QuasiNewtonModel, args...)
nlp.op.push!(args...)
return nlp
end

# not implemented: hess_coord, hess, hprod!

0 comments on commit ba36781

Please sign in to comment.