s LearnSqueak.com

SMALLTALK LAB

Golden-Section Search in Squeak: Find a Minimum Without Derivatives

Narrow a single-valley search interval with two probes. Reuse an expensive cost evaluation each round, and stop when you know how far your answer can be from the minimum.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Squeak Workspace. Copy one complete checkpoint at a time and choose Print it. Think of your cost as a function of one number, with a single valley: it strictly falls toward the minimum and strictly rises afterward, or is monotone with a boundary minimum. The examples use floats for the probe locations.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Place two interior probes

Start with an interval that contains the minimum. Put one probe about 38.2% across and another about 61.8% across. On 0 to 8 the probes are about 3.06 and 4.94. The code returns their locations as integers in hundredths so you can check the positions without comparing raw floating-point numbers.

Independent Workspace expression - select all, then Print it
| a b rho x1 x2 |
a := 0. b := 8.
rho := (5 sqrt - 1) / 2.
x1 := b - (rho * (b - a)).
x2 := a + (rho * (b - a)).
{(x1 * 100) rounded. (x2 * 100) rounded}

Expected: #(306 494)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Place two interior probes
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change b to 16. The probes double because the left endpoint is still zero.

Download this checkpoint

02 - Compare costs and discard one end

The cheaper probe is x1. For a strictly single-valley function, the minimum cannot be strictly to the right of x2: if it were, the cost would still be falling there and f1 would be larger than f2. Keep the part from 0 through x2. When x2 is cheaper instead, keep x1 through the old right endpoint. With equal costs on a strictly single-valley function, either of these retained intervals still contains the minimum.

Independent Workspace expression - select all, then Print it
| f a b rho x1 x2 f1 f2 |
f := [:x | (x - 3) squared].
a := 0. b := 8.
rho := (5 sqrt - 1) / 2.
x1 := b - (rho * (b - a)).
x2 := a + (rho * (b - a)).
f1 := f value: x1. f2 := f value: x2.
f1 <= f2 ifTrue: [b := x2] ifFalse: [a := x1].
{f1 < f2. (a * 100) rounded. (b * 100) rounded}

Expected: #(true 0 494)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Compare costs and discard one end
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change the centre from 3 to 6. The result becomes #(false 306 800): the search now cuts off the left end.

Download this checkpoint

03 - See why the golden ratio reuses a probe

After moving the right endpoint to old x2, the previous left probe x1 lands at the new right probe. The reason is that rho squared equals 1 minus rho. The other branch has the mirror property. Keep both the reused location and its cached cost. Recomputing and reevaluating both points would lose the speed advantage.

Independent Workspace expression - select all, then Print it
| a b rho x1 x2 nextRight difference |
a := 0. b := 8.
rho := (5 sqrt - 1) / 2.
x1 := b - (rho * (b - a)).
x2 := a + (rho * (b - a)).
b := x2.
nextRight := a + (rho * (b - a)).
difference := (nextRight - x1) abs.
{((b - a) * 1000) rounded. difference < 0.000001}

Expected: #(4944 true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: See why the golden ratio reuses a probe
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Draw the old and new intervals on paper. The retained interior probe changes from the left slot to the right slot.

Download this checkpoint

04 - Count evaluations across four rounds

Two calls initialize the cache and each of four rounds adds one call. Each round keeps about 61.8% of the previous width, so the width is now about 1.167, and the minimum is still inside. The cost of a comparison is normally small; reusing an expensive function result is what matters here.

Independent Workspace expression - select all, then Print it
| a b rho x1 x2 f1 f2 f calls |
calls := 0.
f := [:x | calls := calls + 1. (x - 3) squared].
a := 0. b := 8.
rho := (5 sqrt - 1) / 2.
x1 := b - (rho * (b - a)).
x2 := a + (rho * (b - a)).
f1 := f value: x1. f2 := f value: x2.
4 timesRepeat: [
 f1 <= f2 ifTrue: [
  b := x2. x2 := x1. f2 := f1.
  x1 := b - (rho * (b - a)). f1 := f value: x1
 ] ifFalse: [
  a := x1. x1 := x2. f1 := f2.
  x2 := a + (rho * (b - a)). f2 := f value: x2]].
{calls. ((b - a) * 1000) rounded. 3 between: a and: b}

Expected: #(6 1167 true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Count evaluations across four rounds
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change 4 timesRepeat: to 5 timesRepeat:. The call count becomes 7 and the bracket shrinks again.

Download this checkpoint

05 - Stop when the bracket is narrow enough

Return the midpoint, both endpoints, the round count, and a flag indicating whether the requested width was reached. The midpoint is an estimate of the minimizer's location, not the minimum cost. If the true minimizer remains inside the final interval, the midpoint's location error is at most half the final width. This demo includes a 120-round limit to prevent an impossibly small tolerance from spinning on floating-point rounding.

Independent Workspace expression - select all, then Print it
| search result |
search := [:f :settings | | a b tol rho x1 x2 f1 f2 rounds |
 a := settings first. b := settings second. tol := settings third.
 (a < b and: [tol > 0]) ifFalse: [Error signal: 'Invalid search interval'].
 rho := (5 sqrt - 1) / 2.
 x1 := b - (rho * (b - a)).
 x2 := a + (rho * (b - a)).
 f1 := f value: x1. f2 := f value: x2. rounds := 0.
 [(b - a > tol) and: [rounds < 120]] whileTrue: [
  f1 <= f2 ifTrue: [
   b := x2. x2 := x1. f2 := f1.
   x1 := b - (rho * (b - a)). f1 := f value: x1
  ] ifFalse: [
   a := x1. x1 := x2. f1 := f2.
   x2 := a + (rho * (b - a)). f2 := f value: x2].
  rounds := rounds + 1].
 {(a + b) / 2. a. b. rounds. b - a <= tol}].
result := search value: [:x | (x - 3) squared] value: #(0 8 0.001).
{(result first * 1000) rounded. result fifth.
 3 between: result second and: result third}

Expected: #(3000 true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Stop when the bracket is narrow enough
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try tolerance 0.1. The report should still indicate success, but the final interval and midpoint error bound will be looser.

Download this checkpoint

06 - Test a corner and a boundary minimum

No derivative is needed, so the corner in the absolute-value example is not a problem. An increasing objective pushes the bracket toward the left endpoint, and a decreasing one pushes toward the right endpoint. The rounded values make the boundaries easy to see, but they do not mean the midpoint hit an endpoint exactly. If exact endpoint answers matter, also compare the original endpoint costs with the candidate's cost.

Independent Workspace expression - select all, then Print it
| search cusp left right |
search := [:f :settings | | a b tol rho x1 x2 f1 f2 rounds |
 a := settings first. b := settings second. tol := settings third.
 (a < b and: [tol > 0]) ifFalse: [Error signal: 'Invalid search interval'].
 rho := (5 sqrt - 1) / 2.
 x1 := b - (rho * (b - a)).
 x2 := a + (rho * (b - a)).
 f1 := f value: x1. f2 := f value: x2. rounds := 0.
 [(b - a > tol) and: [rounds < 120]] whileTrue: [
  f1 <= f2 ifTrue: [
   b := x2. x2 := x1. f2 := f1.
   x1 := b - (rho * (b - a)). f1 := f value: x1
  ] ifFalse: [
   a := x1. x1 := x2. f1 := f2.
   x2 := a + (rho * (b - a)). f2 := f value: x2].
  rounds := rounds + 1].
 {(a + b) / 2. a. b. rounds. b - a <= tol}].
cusp := search value: [:x | (x - 2) abs] value: #(0 8 0.001).
left := search value: [:x | x] value: #(0 8 0.001).
right := search value: [:x | x negated] value: #(0 8 0.001).
{(cusp first * 100) rounded. (left first * 100) rounded.
 (right first * 100) rounded. cusp fifth. left fifth. right fifth}

Expected: #(200 0 800 true true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Test a corner and a boundary minimum
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Move the absolute-value corner to 5. The first entry should become 500.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Golden-Section Search in Squeak: Find a Minimum Without Derivatives native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Bisection for roots asks where a value crosses zero. Golden-section search asks where the value is smallest, even if it never crosses zero. Adding a constant shifts the minimum value but does not move its location.

Several valleys, level stretches away from the minimum, noisy costs, or changing costs may invalidate the discard rule. This routine does not detect those conditions or prove that a given interval contains a global minimum.

Use a positive tolerance in the same units as x and a reasonable interval scale. Float rounding can make nearby probe locations or costs indistinguishable. Treat a false width flag as a failure to meet the requested precision, not as a successful answer.

For mathematical background, see the GNU Scientific Library's One Dimensional Minimization manual. Its API has additional bracketing requirements; this tutorial demonstrates the two-probe idea, not the GSL API.

Three practice challenges

Try a loss that never reaches zero

Add 10 to (x - 3) squared. What happens to the minimum location and the minimum cost?

Show solution

The minimum stays at x = 3 and its cost rises from 0 to 10. Comparing two costs is unaffected by adding the same constant.

Spend fewer calls

After seven shrinking rounds, how many objective calls should the counter show? What would recomputing both probes at every round cost if you also performed the two initialization calls?

Show solution

Nine calls with caching (two initial plus seven new calls). Naively redoing both probes per round after initialization would add fourteen calls, or sixteen total.

Reject a bad assumption

Suppose two separate valleys fit inside the interval. Can a single comparison guarantee that the discarded part has no better answer?

Show solution

No. A cheaper point on one side does not rule out a deeper valley farther away on the other side. Inspect or split the interval using domain knowledge, rather than claiming a global result.

Continue learning

Bisection | Newton Roots | Horner Polynomials

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.