Misc.#

class kleinberg_grid_simulator.utils.Result(edt: float, process_time: float, n: int, r: float, p: int, q: int, n_runs: int, julia: bool, numba: bool = True, parallel: bool = False)[source]#

Dataclass to represent the results.

kleinberg_grid_simulator.utils.cache_edt_of_r(compute_edt, n=10000, n_runs=10000, **kwargs)[source]#
Parameters:
  • compute_edt (callable) – Function that computes… EDT!

  • n (int, default=10000) – Grid siDe

  • n_runs (int, default=10000) – Number of routes to compute

  • kwargs (dict) – Other parameters

Returns:

A cached function that computes the edt as a function of r.

Return type:

callable

kleinberg_grid_simulator.utils.get_target(f, a, b, t)[source]#

Solve by dichotomy f(x)=t

Parameters:
  • f (callable) – f is monotonic between a and b, possibly noisy.

  • a (float) – f(a) < t

  • b (float) – f(b) > t

  • t (float) – Target

Returns:

The (possibly approximated) solution of f(x)=t

Return type:

float

Examples

>>> f = cache(lambda x: (x-2)**2)
>>> x = get_target(f, 2., 10., 2.)
>>> f"{x:.4f}"
'3.4142'
kleinberg_grid_simulator.utils.gss(f, a, b, tol=1e-05)[source]#

Find by Golden-section search the minimum of a function f.

Parameters:
  • f (callable) – f, possibly noisy, is convex on [a, b].

  • a (float) – Left guess.

  • b (float) – Right guess.

  • tol (float) – Exit thresold on x.

Returns:

  • float – The (possibly approximated) value that minimizes f over [a, b].

  • float – The (possibly approximated) minimum of f over [a, b].

Examples

>>> f = cache(lambda x: (x-2)**2)
>>> x = gss(f, 1, 5)
>>> f"f({x[0]:.4f}) = {x[1]:.4f}"
'f(2.0000) = 0.0000'