@@ -560,13 +560,18 @@ local function compute_fgrd (env)
560
560
-- backup states
561
561
backup(var, bak)
562
562
563
- -- last step norm
563
+ -- estimate dh from last step or current point
564
564
local hn = h:norm()
565
+ local dh = 1e-4*hn
566
+ if dh == 0 then dh = 1e-8*x:norm() end
567
+ if dh == 0 then dh = 1e-10 end
568
+
569
+ if trc then printf("nlopt: computing derivatives with dh=%-.5e\n", dh) end
565
570
566
571
-- Broyden's Jacobian update
567
572
local bro, Bc = env.objective.broyden and prv.c ~= nil and hn ~= 0
568
573
if bro then
569
- if trc then printf("nlopt: Broyden's update with |hn|=%-.8e \n", hn) end
574
+ if trc then printf("nlopt: Broyden's update with |hn|=%-.5e \n", hn) end
570
575
Bc = cjac + (c - prv.c - cjac*h)*h:t()/hn^2
571
576
end
572
577
@@ -575,12 +580,13 @@ local function compute_fgrd (env)
575
580
if bro and h[iv] >= 0.8*hn then -- Broyden's rank one update
576
581
cjac:setcol(iv, Bc:getcol(iv))
577
582
if trc then
578
- printf("nlopt: Broyden's update for variable %d (%-.8e )\n", iv, h[iv])
583
+ printf("nlopt: Broyden's update for variable %d (%-.5e )\n", iv, h[iv])
579
584
end
580
585
else -- finite difference required
581
- local ih = x[iv] ~= 0 and x[iv]*xstp[iv] or 1e-12
586
+ local ih = x[iv]*xstp[iv]
587
+ if ih == 0 then ih = dh end
582
588
x[iv] = x[iv]+ih ; var.fval = fval
583
- if x[iv] < xmin[iv] or x[iv] > xmax[iv] then
589
+ if x[iv] < xmin[iv] or x[iv] > xmax[iv] or ih*xslp[iv] < 0 then
584
590
x[iv], ih = x[iv]-2*ih, -ih -- take -ih if bbox are violated
585
591
end
586
592
@@ -814,7 +820,7 @@ local function exec (self)
814
820
local v = variables[i] or {}
815
821
if v.get then var.x0[i] = v.get(env) end
816
822
var.x [i] = var.x0[i]
817
- var.xstp[i] = v.rstp or variables.rstp or 1e-8
823
+ var.xstp[i] = v.rstp or variables.rstp or 0
818
824
var.xslp[i] = v.slope or variables.slope or 0
819
825
var.xtol[i] = v.tol or variables.tol or 0
820
826
var.xmin[i] = v.min or variables.min or -inf
0 commit comments