Skip to content

Commit

Permalink
🤖 Format .jl files (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: tmigot <[email protected]>
  • Loading branch information
github-actions[bot] and tmigot authored Mar 3, 2024
1 parent e7ef8ba commit b7429f9
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 126 deletions.
7 changes: 4 additions & 3 deletions benchmark/make_figures.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Pkg; Pkg.activate(".")
using Pkg;
Pkg.activate(".");
include("utils.jl")

solvers = [:ipopt, :DCILDL] # [:ipopt, :knitro, :DCILDL, :DCIMA57]
list=""
list = ""
for solver in solvers
global list=string(list,"_$(solver)")
global list = string(list, "_$(solver)")
end
file_prefix = "$(string(today()))_$(list)"
make_figures("2021-10-15__DCILDL_ipopt_pdeoptimizationproblems", file_prefix)
3 changes: 2 additions & 1 deletion benchmark/make_md.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Pkg; Pkg.activate(".")
using Pkg;
Pkg.activate(".");
include("utils.jl")

solvers = [:ipopt, :DCILDL]
Expand Down
23 changes: 14 additions & 9 deletions benchmark/solve_problems.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Pkg
Pkg.activate(".")
Pkg.add(url="https://github.com/tmigot/PDEOptimizationProblems")
Pkg.add(url = "https://github.com/tmigot/PDEOptimizationProblems")
Pkg.instantiate()
# This package
using Gridap, PDENLPModels, PDEOptimizationProblems
Expand Down Expand Up @@ -80,13 +80,18 @@ solvers = Dict(
max_cpu_time = max_time,
x0 = nlp.meta.x0,
),
:knitro =>nlp -> knitro(nlp, out_hints = 0, outlev = 0,
feastol = 1e-5,
feastol_abs = 1e-5,
opttol = 1e-5,
opttol_abs = 1e-5,
maxtime_cpu = max_time,
x0 = nlp.meta.x0),
:knitro =>
nlp -> knitro(
nlp,
out_hints = 0,
outlev = 0,
feastol = 1e-5,
feastol_abs = 1e-5,
opttol = 1e-5,
opttol_abs = 1e-5,
maxtime_cpu = max_time,
x0 = nlp.meta.x0,
),
:DCILDL =>
nlp -> dci(
nlp,
Expand All @@ -108,4 +113,4 @@ for solver in keys(solvers)
end
name = "$(string(today()))_$(list)_pdeoptimizationproblems"

@save string(name,".jld2") stats
@save string(name, ".jld2") stats
152 changes: 91 additions & 61 deletions benchmark/utils.jl
Original file line number Diff line number Diff line change
@@ -1,88 +1,118 @@
using DataFrames, Dates, JLD2, SolverTools, SolverBenchmark
using Plots; # pgfplots()

function make_figures(name, file_prefix::String="results")
function make_figures(name, file_prefix::String = "results")
legend = Dict(
:neval_obj => "number of f evals",
:neval_cons => "number of c evals",
:neval_grad => "number of ∇f evals",
:neval_jac => "number of ∇c evals",
:neval_jprod => "number of ∇c*v evals",
:neval_jtprod => "number of ∇cᵀ*v evals",
:neval_hess => "number of ∇²f evals",
:elapsed_time => "elapsed time"
)
styles = [:solid,:dash,:dot,:dashdot] #[:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
:neval_obj => "number of f evals",
:neval_cons => "number of c evals",
:neval_grad => "number of ∇f evals",
:neval_jac => "number of ∇c evals",
:neval_jprod => "number of ∇c*v evals",
:neval_jtprod => "number of ∇cᵀ*v evals",
:neval_hess => "number of ∇²f evals",
:elapsed_time => "elapsed time",
)
styles = [:solid, :dash, :dot, :dashdot] #[:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
perf_title(col) = "Performance profile on CUTEst w.r.t. $(string(legend[col]))"

@load string(name,".jld2") stats
@load string(name, ".jld2") stats

for col in keys(legend)
empty = false
for df in values(stats)
if all(df[!, col] .== 0)
empty = true
end
empty = false
for df in values(stats)
if all(df[!, col] .== 0)
empty = true
end
end

if !empty
ϵ = minimum(minimum(filter(x -> x > 0, df[!, col])) for df in values(stats))
first_order(df) = df.status .== :first_order
unbounded(df) = df.status .== :unbounded
solved(df) = first_order(df) .| unbounded(df)
cost(df) = (max.(df[!, col], ϵ) + .!solved(df) .* Inf)
#p = performance_profile(stats, cost)
p = performance_profile(stats, cost,
title=perf_title(col),
legend=:bottomright,
linestyles=styles)
# savefig("$(file_prefix)_perf-$col.tex")
png("$(file_prefix)_perf-$col")
#profile_solvers(stats, [cost], ["$(col)"])
costs = [cost]
solvers = collect(keys(stats))
nsolvers = length(solvers)
npairs = div(nsolvers * (nsolvers - 1), 2)
colors = get_color_palette(:auto, nsolvers)
if nsolvers > 2
ipairs = 0
# combinations of solvers 2 by 2
for i = 2 : nsolvers
for j = 1 : i-1
ipairs += 1
pair = [solvers[i], solvers[j]]
dfs = (stats[solver] for solver in pair)
Ps = [hcat([cost(df) for df in dfs]...) for cost in costs]
if !empty
ϵ = minimum(minimum(filter(x -> x > 0, df[!, col])) for df in values(stats))
first_order(df) = df.status .== :first_order
unbounded(df) = df.status .== :unbounded
solved(df) = first_order(df) .| unbounded(df)
cost(df) = (max.(df[!, col], ϵ) + .!solved(df) .* Inf)
#p = performance_profile(stats, cost)
p = performance_profile(
stats,
cost,
title = perf_title(col),
legend = :bottomright,
linestyles = styles,
)
# savefig("$(file_prefix)_perf-$col.tex")
png("$(file_prefix)_perf-$col")
#profile_solvers(stats, [cost], ["$(col)"])
costs = [cost]
solvers = collect(keys(stats))
nsolvers = length(solvers)
npairs = div(nsolvers * (nsolvers - 1), 2)
colors = get_color_palette(:auto, nsolvers)
if nsolvers > 2
ipairs = 0
# combinations of solvers 2 by 2
for i = 2:nsolvers
for j = 1:(i - 1)
ipairs += 1
pair = [solvers[i], solvers[j]]
dfs = (stats[solver] for solver in pair)
Ps = [hcat([cost(df) for df in dfs]...) for cost in costs]

clrs = [colors[i], colors[j]]
stls = [styles[i], styles[j]]
p = performance_profile(Ps[1], string.(pair), palette=clrs,
legend=:bottomright,
styles = stls)
ipairs < npairs && xlabel!(p, "")
# savefig("$(tod)_$(solvers[i])_$(solvers[j])_perf-$col.tex")
png("$(file_prefix)_$(solvers[i])_$(solvers[j])_perf-$col")
end
end
else
clrs = [colors[i], colors[j]]
stls = [styles[i], styles[j]]
p = performance_profile(
Ps[1],
string.(pair),
palette = clrs,
legend = :bottomright,
styles = stls,
)
ipairs < npairs && xlabel!(p, "")
# savefig("$(tod)_$(solvers[i])_$(solvers[j])_perf-$col.tex")
png("$(file_prefix)_$(solvers[i])_$(solvers[j])_perf-$col")
end
end
else
end
end
end
end

using JLD2, DataFrames, Dates, SolverCore, NLPModels, SolverBenchmark

function make_md(name, opt_val, file_prefix::String="results")
@load string(name,".jld2") stats
function make_md(name, opt_val, file_prefix::String = "results")
@load string(name, ".jld2") stats

open("$(file_prefix).md", "w") do io
for solver in collect(keys(stats))
println(io, "## $solver")
df = stats[solver]
insertcols!(df, 1, :opt_val => opt_val)
pretty_stats(io, df[!, [:name, :nvar, :ncon, :status, :objective, :opt_val, :elapsed_time, :neval_obj, :neval_cons, :dual_feas, :primal_feas]],
tf=tf_markdown,
hdr_override=Dict(:neval_obj => "#f", :neval_cons => "#c", :elapsed_time => "time (s)", :primal_feas => "feas"))
pretty_stats(
io,
df[
!,
[
:name,
:nvar,
:ncon,
:status,
:objective,
:opt_val,
:elapsed_time,
:neval_obj,
:neval_cons,
:dual_feas,
:primal_feas,
],
],
tf = tf_markdown,
hdr_override = Dict(
:neval_obj => "#f",
:neval_cons => "#c",
:elapsed_time => "time (s)",
:primal_feas => "feas",
),
)
end
end
end
14 changes: 12 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ ENV["GKSwstype"] = "100"
using PDEOptimizationProblems
using Documenter

DocMeta.setdocmeta!(PDEOptimizationProblems, :DocTestSetup, :(using PDEOptimizationProblems); recursive = true)
DocMeta.setdocmeta!(
PDEOptimizationProblems,
:DocTestSetup,
:(using PDEOptimizationProblems);
recursive = true,
)

makedocs(;
modules = [PDEOptimizationProblems],
Expand All @@ -17,7 +22,12 @@ makedocs(;
canonical = "https://tmigot.github.io/PDEOptimizationProblems",
assets = ["assets/style.css"],
),
pages = ["Home" => "index.md", "COPS" => "cops.md", "Rocket Control" => "tuto_rocket.md", "Reference" => "reference.md"],
pages = [
"Home" => "index.md",
"COPS" => "cops.md",
"Rocket Control" => "tuto_rocket.md",
"Reference" => "reference.md",
],
)

deploydocs(;
Expand Down
10 changes: 5 additions & 5 deletions docs/scripts/tuto_rocket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ gr()

h₀, m₀, mᵪ = 1.0, 1.0, 0.6
p = Plots.plot(
Plots.plot(0:T/n:T, vcat(h₀, hh); xlabel = "Time (s)", ylabel = "Altitude"),
Plots.plot(0:T/n:T, vcat(m₀, mh, mᵪ * m₀); xlabel = "Time (s)", ylabel = "Mass"),
Plots.plot(0:T/n:T, vcat(0., vh); xlabel = "Time (s)", ylabel = "Velocity"),
Plots.plot(0:T/(length(uh) - 1):T, uh; xlabel = "Time (s)", ylabel = "Thrust"),
Plots.plot(0:(T / n):T, vcat(h₀, hh); xlabel = "Time (s)", ylabel = "Altitude"),
Plots.plot(0:(T / n):T, vcat(m₀, mh, mᵪ * m₀); xlabel = "Time (s)", ylabel = "Mass"),
Plots.plot(0:(T / n):T, vcat(0.0, vh); xlabel = "Time (s)", ylabel = "Velocity"),
Plots.plot(0:(T / (length(uh) - 1)):T, uh; xlabel = "Time (s)", ylabel = "Thrust"),
layout = (2, 2),
legend = false,
margin = 1Plots.cm,
)
png("rocket")
png("rocket")
25 changes: 14 additions & 11 deletions src/PDEOptimizationProblems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct InterpolatedEnergyFETerm{M}
throw(error("Dimension error size(Ymes) != (nymes, ny) ($(size(Ymes)), $((nymes, ny)))"))
end
if typeof(Xmes) <: AbstractVector
if length(Xmes) != nymes
if length(Xmes) != nymes
throw(error("Dimension error length(Xmes) != nymes ($(length(Xmes)), $(nymes)) "))
end
elseif size(Xmes) != (ndim, nymes)
Expand All @@ -46,17 +46,22 @@ struct InterpolatedEnergyFETerm{M}
for j = 1:nymes
δ[j] = t -> exp(-(norm(t - Xmes[j])^2 / h))
end
= x -> sum(δ[j](x) for j=1:nymes)
= x -> sum(δ[j](x) for j = 1:nymes)
interpolated_y = Array{Function}(undef, ny)
for j = 1:ny
if ndim > 1
throw(error("Not implemented interpolation for dimension > 1. Please open an issue with an example."))
end
interpolated_y[j] = t -> begin
i = findfirst(x -> x t[1], Xmes) #Assuming 1D
h = isnothing(i) || i == 1 ? 1 : (Xmes[i] - t[1]) / (Xmes[i - 1] - Xmes[i])
return isnothing(i) || i == 1 ? Ymes[1, j] : Ymes[i - 1, j] + h * Ymes[i, j]
throw(
error(
"Not implemented interpolation for dimension > 1. Please open an issue with an example.",
),
)
end
interpolated_y[j] =
t -> begin
i = findfirst(x -> x t[1], Xmes) #Assuming 1D
h = isnothing(i) || i == 1 ? 1 : (Xmes[i] - t[1]) / (Xmes[i - 1] - Xmes[i])
return isnothing(i) || i == 1 ? Ymes[1, j] : Ymes[i - 1, j] + h * Ymes[i, j]
end
end
return new{typeof(dΩ)}(ny, nymes, Ymes, ndim, Xmes, dΩ, δ, Sδ, h, interpolated_y)
end
Expand Down Expand Up @@ -86,9 +91,7 @@ function interpolated_measurement(IT::InterpolatedEnergyFETerm, y)
if ny > 1
# Function get_data is not implemented for MultiFieldCellField at this moment.
# You need to extract the individual fields and then evaluate them separately.
return sum(
(Sδ dot(y[i] - interpolated_y[i], y[i] - interpolated_y[i]))dΩ for i=1:ny
)
return sum((Sδ dot(y[i] - interpolated_y[i], y[i] - interpolated_y[i]))dΩ for i = 1:ny)
else # ny = 1
return (Sδ dot(y - interpolated_y[1], y - interpolated_y[1]))dΩ
end
Expand Down
2 changes: 1 addition & 1 deletion src/apinene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function apinene(args...; n = 400, kwargs...)
4.5 63.1 3.8 2.9 25.7
]

objterm = PDEOptimizationProblems.InterpolatedEnergyFETerm(5, 8, zmes, 1, τ, dΩ, 1/n)
objterm = PDEOptimizationProblems.InterpolatedEnergyFETerm(5, 8, zmes, 1, τ, dΩ, 1 / n)
f = (θ, y) -> PDEOptimizationProblems.interpolated_measurement(objterm, y)

ndofs = Gridap.FESpaces.num_free_dofs(Y)
Expand Down
16 changes: 10 additions & 6 deletions src/catmix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function catmix(args...; n::Int = 100, kwargs...)
= Measure(trian, degree)

degree = 1
Γ = BoundaryTriangulation(model, tags=["diri1"])
Γ = BoundaryTriangulation(model, tags = ["diri1"])
= Measure(Γ, degree)

valuetype = Float64
Expand All @@ -46,11 +46,15 @@ function catmix(args...; n::Int = 100, kwargs...)
y1, Y1, y2, Y2 = y
v1, V1, v2, V2 = v
return (
dt(y1, v1) - v1 * u * (10 * y2 - y1) +
dt(y2, v2) + v2 * u * (10 * y2 - y1) + v2 * y2 * (1 - u) +
dt(Y1, V1) +
dt(Y2, V2) )dΩ +
( (Y1 - y1) * V1 )dΓ + ( (Y2 - y2) * V2 )dΓ
dt(y1, v1) - v1 * u * (10 * y2 - y1) +
dt(y2, v2) +
v2 * u * (10 * y2 - y1) +
v2 * y2 * (1 - u) +
dt(Y1, V1) +
dt(Y2, V2),
)dΩ +
((Y1 - y1) * V1)dΓ +
((Y2 - y2) * V2)dΓ
end

nvar_con = Gridap.FESpaces.num_free_dofs(Ycon)
Expand Down
2 changes: 1 addition & 1 deletion src/cellincrease.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function cellincrease(args...; x0 = [0.6, 0.1], n = 10, T = 7, kwargs...)
-p * (kp * pf * (1.0 - cf) - kr * cf * (1.0 - cf - pf)) +
dt(cf, p) +
dt(pf, q) +
q * (kr * cf * (1.0 - cf - pf) * u - kp * pf * pf)
q * (kr * cf * (1.0 - cf - pf) * u - kp * pf * pf),
)dΩ
end

Expand Down
Loading

0 comments on commit b7429f9

Please sign in to comment.