Skip to content

Commit 8f32f28

Browse files
authored
Merge pull request #150 from sintefmath/opt-maximize-arg
Small bugfixes for new optimizer + nicer LBFGS output
2 parents db44116 + 16d3f0d commit 8f32f28

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Jutul"
22
uuid = "2b460a1a-8a2b-45b2-b125-b5c536396eb9"
33
authors = ["Olav Møyner <olav.moyner@gmail.com>"]
4-
version = "0.4.1"
4+
version = "0.4.2"
55

66
[deps]
77
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"

src/DictOptimization/interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function optimize(dopt::DictParameters, objective, setup_fn = dopt.setup_functio
33
obj_change_tol = 1e-6,
44
max_it = 25,
55
opt_fun = missing,
6-
minimize = true,
6+
maximize = false,
77
simulator = missing,
88
config = missing,
99
solution_history = false,
@@ -39,11 +39,11 @@ function optimize(dopt::DictParameters, objective, setup_fn = dopt.setup_functio
3939
end
4040
t_opt = @elapsed if ismissing(opt_fun)
4141
v, x, history = Jutul.LBFGS.box_bfgs(x0, solve_and_differentiate, lb, ub;
42-
maximize = false,
4342
print = Int(dopt.verbose),
4443
max_it = max_it,
4544
grad_tol = grad_tol,
4645
obj_change_tol = obj_change_tol,
46+
maximize = maximize,
4747
kwarg...
4848
)
4949
else

src/LBFGS/constrained_optimizer.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function unit_box_bfgs(
149149
Hi = V' * Hi * V + r * (du * du')
150150
end
151151
else
152-
print_msg("Hessian not updated during iteration $it", :red)
152+
print_msg("Hessian not updated during iteration $it", :yellow)
153153
end
154154
end
155155
# Update history
@@ -560,7 +560,17 @@ function update_history!(hst::Union{OptimizationHistory, Vector}, val, u, pg, al
560560
end
561561

562562
function printInfo(history, it)
563-
return @printf("It: %3d | val: %.3e | ls-its: %d | pgrad: %.3e\n", it, abs(history.val[end]), history.lsit[end], history.pg[end][end])
563+
lsit = history.lsit[end]
564+
if it == 0
565+
println("It. | Objective | Proj. grad | Linesearch-its")
566+
println("-----------------------------------------------")
567+
end
568+
if isnan(lsit)
569+
lsit = "-"
570+
else
571+
lsit = "$(lsit)"
572+
end
573+
return @printf("%4d | %.4e | %.4e | %s\n", it, abs(history.val[end]), history.pg[end][end], lsit)
564574
end
565575

566576
function line_search(
@@ -637,15 +647,15 @@ function line_search(
637647
a = min(a, p2.a - sgf * (p2.a - p1.a))
638648
else
639649
a = (p1.a + p2.a) / 2
640-
print_msg("Cubic interpolation failed, cutting interval in half ...", :red)
650+
print_msg("Cubic interpolation failed, cutting interval in half ...", :yellow)
641651
end
642652
end
643653
end
644654
end
645655
# Check if line search succeeded
646656
if ! line_search_done
647657
flag = -2
648-
print_msg("Line search unable to succeed in $max_it iterations ...", :red)
658+
print_msg("Line search unable to succeed in $max_it iterations ...", :yellow)
649659
# Although line search did not succeed in max_it iterations, we ensure
650660
# to return the greater of p1 and p2's objective value none the less.
651661
if p1.v < p2.v

src/LBFGS/limited_memory_hessian.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function update(H::LimitedMemoryHessian, s, y)
1717
# limit m to number of vars
1818
if H.m > length(s)
1919
H.m = length(s)
20-
Jutul.jutul_message("Warning", "Resetting 'm' to number of parameters: m = $(length(s))", color = :yellow)
20+
Jutul.jutul_message("LBFGS", "Resetting 'm' to number of parameters: m = $(length(s))", color = :yellow)
2121
end
2222
end
2323
return H

src/ad/AdjointsDI/adjoints.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,12 @@ function evaluate_residual_and_jacobian_for_state_pair(x, state, state0, step_in
378378
state0 = case.state0
379379
end
380380
sim = HelperSimulator(case, eltype(x), cache = cache, n_extra = 1)
381+
forces_for_eval = case.forces
382+
if forces_for_eval isa AbstractVector
383+
forces_for_eval = only(forces_for_eval)
384+
end
381385
model_residual(state, state0, sim,
382-
forces = case.forces,
386+
forces = forces_for_eval,
383387
time = step_info[:time],
384388
dt = dt
385389
)
@@ -395,7 +399,7 @@ function evaluate_residual_and_jacobian_for_state_pair(x, state, state0, step_in
395399
else
396400
s = JutulStorage(state)
397401
end
398-
r[end] = G(case.model, s, dt, step_info, case.forces)
402+
r[end] = G(case.model, s, dt, step_info, forces_for_eval)
399403
return copy(r)
400404
end
401405

0 commit comments

Comments
 (0)