lightlab.util.search module

Searching with actuate-measure functions, usually around peaks and monotonic functions

Summary

Exceptions:

SearchRangeError The first argument is direction, the second is a best guess

Functions:

binarySearch Gives the x where evalPointFun(x) == targetY, approximately.
bracketSearch Searches outwards until it finds two X values whose Y values are above and below the targetY.
doesMFbracket
peakSearch Returns the optimal input that gives you the peak, and the peak value
plotAfterPointMeasurement This mutates trackerMF

Reference

exception SearchRangeError[source]

Bases: lightlab.util.io.errors.RangeError

The first argument is direction, the second is a best guess

plotAfterPointMeasurement(trackerMF, yTarget=None)[source]

This mutates trackerMF

Parameters:
  • trackerMF (MeasuredFunction) – function that will be plotted
  • yTarget (float) – plotted as dashed line if not None
peakSearch(evalPointFun, startBounds, nSwarm=3, xTol=0.0, yTol=0.0, livePlot=False)[source]

Returns the optimal input that gives you the peak, and the peak value

You must set either xTol or yTol. Be careful with yTol! It is best used with a big swarm. It does not guarantee that you are that close to peak, just that the swarm is that flat

This algorithm is a modified swarm that is robust to outliers, sometimes.
Each iteration, it takes <nSwarm> measurements and looks at the best (highest). The update is calculated by shrinking the swarm around the index of the best value. It does not compare between iterations: that makes it robust to one-time outliers. It attributes weight only by order of y values in an iteration, not the value between iterations or the magnitude of differences between y’s within an iteration

Not designed to differentiate global vs. local maxima

Parameters:
  • evalPointFun (function) – y=f(x) one argument, one return. The function that we want to find the peak of
  • startBounds (list, ndarray) – minimum and maximum x values that bracket the peak of interest
  • nSwarm (int) – number of evaluations per iteration. Use more if it’s a narrow peak in a big bounding area
  • xTol (float) – if the swarm x’s fall within this range, search returns successfully
  • yTol (float) – if the swarm y’s fall within this range, search returns successfully
  • livePlot (bool) – for notebook plotting
Returns:

best (x,y) point of the peak

Return type:

(float, float)

doesMFbracket(targetY, twoPointMF)[source]
bracketSearch(evalPointFun, targetY, startBounds, xTol, hardConstrain=False, livePlot=False)[source]

Searches outwards until it finds two X values whose Y values are above and below the targetY.

Stop conditions
  • brackets it: returns new bracketing x values
  • step decreases until below xTol: raises RangeError
  • 30 iterations: raises RangeError
Parameters:
  • evalPointFun (function) – y=f(x) one argument, one return. The function that we want to find the target Y value of
  • startBounds (list, ndarray) – x values that usually do not bracket the value of interest
  • xTol (float) – if domain shifts become less than this, raises RangeError
  • hardConstrain (bool, list) – If list, will stay within those
  • livePlot (bool) – for notebook plotting
Returns:

the bracketing range

Return type:

([float, float])

binarySearch(evalPointFun, targetY, startBounds, hardConstrain=False, xTol=0, yTol=0, livePlot=False)[source]

Gives the x where evalPointFun(x) == targetY, approximately. The final call to evalPointFun will be of this value, so no need to call it again, if your goal is to set to the target.

xTol and yTol are OR-ed conditions. If one is satisfied, it will terminate successfully. You must specify at least one.

Assumes that the function is monotonic in any direction It often works when there is a peak inside the startBounds, although not always.

Parameters:
  • evalPointFun (function) – y=f(x) one argument, one return. The function that we want to find the target Y value of
  • startBounds (list, ndarray) – minimum and maximum x values that bracket the peak of interest
  • hardConstrain (bool, list) – if not True, will do a bracketSearch. If list, will stay within those
  • xTol (float) – if domain shifts become less than this, terminates successfully
  • yTol (float) – if range shifts become less than this, terminates successfully
  • livePlot (bool) – for notebook plotting
Returns:

the optimal X value

Return type:

(float)