Skip to content

Commit a62ba71

Browse files
committed
fix: OptimizerBatchNLoptr with numerical gradient approximation saveguard on boundaries
1 parent 9519f95 commit a62ba71

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

R/OptimizerBatchNLoptr.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,15 @@ OptimizerBatchNLoptr = R6Class("OptimizerBatchNLoptr", inherit = OptimizerBatch,
128128
pv$x0 = search_start(inst$search_space, type = pv$start_values)
129129
pv$start_values = NULL
130130

131-
eval_grad_f = if (pv$approximate_eval_grad_f) {
132-
function(x) {
131+
if (pv$approximate_eval_grad_f) {
132+
eval_grad_f = function(x) {
133133
invoke(nloptr::nl.grad, x0 = x, fn = inst$objective_function)
134134
}
135+
saveguard_epsilon = 1e-5
135136
} else {
136-
NULL
137+
eval_grad_f = NULL
138+
saveguard_epsilon = 0
139+
137140
}
138141
pv$eval_grad_f = eval_grad_f
139142
pv$approximate_eval_grad_f = NULL
@@ -149,8 +152,8 @@ OptimizerBatchNLoptr = R6Class("OptimizerBatchNLoptr", inherit = OptimizerBatch,
149152

150153
invoke(nloptr::nloptr,
151154
eval_f = inst$objective_function,
152-
lb = inst$search_space$lower,
153-
ub = inst$search_space$upper,
155+
lb = inst$search_space$lower + saveguard_epsilon, # needed due to numerical issues with NLoptr
156+
ub = inst$search_space$upper - saveguard_epsilon, # needed due to numerical issues with NLoptr
154157
opts = opts,
155158
.args = pv)
156159
}

0 commit comments

Comments
 (0)