Skip to content

New output #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Expand All @@ -39,6 +40,7 @@ ProgressLogging = "0.1"
Reexport = "0.2, 1.0"
Requires = "1.0"
ReverseDiff = "1.4"
SciMLBase = "1.7.3"
TerminalLoggers = "0.1"
Tracker = "0.2"
Zygote = "0.5, 0.6"
Expand Down
2 changes: 2 additions & 0 deletions src/GalacticOptim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GalacticOptim

using Reexport
@reexport using DiffEqBase
@reexport using SciMLBase
using Requires
using DiffResults, ForwardDiff, Zygote, ReverseDiff, Tracker, FiniteDiff
@reexport using Optim, Flux
Expand All @@ -10,6 +11,7 @@ using ArrayInterface, Base.Iterators

using ForwardDiff: DEFAULT_CHUNK_THRESHOLD
import DiffEqBase: OptimizationProblem, OptimizationFunction, AbstractADType
import SciMLBase: AbstractNoTimeSolution, build_solution, AbstractNonlinearProblem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the imports here


import ModelingToolkit
import ModelingToolkit: AutoModelingToolkit
Expand Down
62 changes: 31 additions & 31 deletions src/solve.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
abstract type AbstractOptimizationSolution end #experimental; comments welcome
mutable struct OptimizationSolution{O, Tx, Tf, Tls, Tsb} <: AbstractOptimizationSolution
method::O
initial_x::Tx
minimizer::Tx
abstract type AbstractOptimizationSolution{T, N} <: AbstractNoTimeSolution{T, N} end

struct OptimizationSolution{T, N, uType, P, A, Tf} <: AbstractOptimizationSolution{T, N}
u::uType # minimizer
prob::P # optimization problem
alg::A # algorithm
minimum::Tf
iterations::Int
iteration_converged::Bool
ls_success::Tls
time_run::Float64
stopped_by::Tsb
initial_x::Array{Float64,1}
retcode::Symbol
original::String # original output of the optimizer
end

function Base.show(io::IO, r::AbstractOptimizationSolution)
take = Iterators.take
failure_string = "failure"
if isa(r.ls_success, Bool) && !r.ls_success
failure_string *= " (line search failed)"
end
function build_solution(prob::AbstractNonlinearProblem,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this is not a PR to SciMLBase, you're just shadowing and this should be SciMLBase.build_solution. Why is this defined here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to upload the changed GalacticOptim.jl file where the build_solution is from SciMLBase. I edited it in two different places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function build_solution(prob::AbstractNonlinearProblem,
function build_solution(prob::AbstractOptimizationProblem,

alg, u, minimum;
initial_x = prob.u0,
retcode = :Default,
original = nothing,
kwargs...)

T = eltype(eltype(u))
N = ndims(u)

OptimizationSolution{T, N, typeof(u), typeof(prob), typeof(alg),
typeof(minimum)}
(u, prob, alg, minimum, initial_x,
retcode, original)
end

function Base.show(io::IO, A::AbstractNoTimeSolution)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function Base.show(io::IO, A::AbstractNoTimeSolution)
function Base.show(io::IO, A::AbstractOptimizationSolution)


@printf io " * Status: %s\n\n" r.iteration_converged ? "success" : failure_string
@printf io "\n * Status: %s\n\n" A.retcode === :Success ? "success" : "failure"
@printf io " * Candidate solution\n"
fmt = " Final objective value: %e "*repeat(", %e ",length(r.minimum)-1)*"\n"
@eval @printf($io, $fmt, $r.minimum...)
#@printf io " Final objective value: %e\n" r.minimum
@printf io " Final objective value: %e\n" A.minimum
@printf io "\n"
@printf io " * Found with\n"
@printf io " Algorithm: %s\n" r.method
@printf io " Algorithm: %s\n" A.alg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@printf io " Algorithm: %s\n" A.alg
@printf io " Optimization Algorithm: %s\n" A.alg

return
end

Expand Down Expand Up @@ -159,15 +167,7 @@ function __solve(prob::OptimizationProblem, opt, data = DEFAULT_DATA;

_time = time()

OptimizationSolution(opt,
prob.u0,# initial_x,
θ, #pick_best_x(f_incr_pick, state),
save_best ? first(min_err) : first(x), # pick_best_f(f_incr_pick, state, d),
maxiters, #iteration,
maxiters >= maxiters, #iteration == options.iterations,
true,
_time-t0,
NamedTuple())
# here should be build_solution to create the output message
end


Expand Down Expand Up @@ -447,7 +447,7 @@ function __init__()
end

_loss = function(θ)
x = ntuple(i->first(prob.prob[i].f(θ, prob.prob[i].p, cur...)),length(prob.prob))
x = ntuple(i->first(prob.prob[i].f(θ, prob.prob[i].p, cur...)),length(prob.prob))
return x
end

Expand Down