pygx.algo.scalars¶
Step-based scalars used as hyper-parameters for PyGX algorithms.
scalars
¶
Step-based scalars used as hyper-parameters for PyGX algorithms.
A scalar is a deferred numerical expression that produces a number when sampled at a given step. They are useful as hyper-parameters in evolutionary algorithms when a value should change over the course of a search — for example, a mutation rate that anneals from 1.0 down to 0.1 over 1000 steps.
This module provides:
- The
Scalarbase type plusConstantfor trivial cases. - Arithmetic combinators (
Addition,Multiplication,Division,Mod, ...) so scalars compose with+/*/etc. - Elementary functions (
Cosine,Exp,Log, ...). - Stochastic primitives (
Gaussian,LogNormal). make_scalarfor converting a plain Python value into a Scalar when an API accepts either.
STEP
module-attribute
¶
Scalar that returns the step itself: STEP(i) == i.
Use it as the seed for schedule expressions, e.g. STEP * 0.1 + 1.0.
Absolute
¶
Addition
¶
BinaryOp
¶
Ceiling
¶
Constant
¶
Division
¶
Floor
¶
Lambda
¶
Mod
¶
Multiplication
¶
Negation
¶
Power
¶
Scalar
¶
Subtraction
¶
UnaryOp
¶
Cosine
¶
Exp
¶
Log
¶
Bases: Scalar
The logarithm scalar log(x, base) (natural log by default).
Source code in pygx/algo/scalars/_maths.py
Sine
¶
SquareRoot
¶
Gaussian
¶
LogNormal
¶
Bases: _MeanStdRandom
A random float from a log-normal distribution.
mean and std parameterize the underlying normal distribution (the
distribution of the value's logarithm), not the samples themselves.
Source code in pygx/algo/scalars/_randoms.py
Normal
¶
RandomScalar
¶
Bases: Scalar
Base class for random scalars.
A random scalar draws a fresh value from a distribution on every call;
the step is ignored. Subclasses implement next_value.
Triangular
¶
Triangular(
low: int | float = 0.0,
high: int | float = 1.0,
mode: int | float | None = None,
**kwargs
)
Bases: RandomScalar
A random number from a triangular distribution.
The result is truncated to an integer when both bounds are integers.
Source code in pygx/algo/scalars/_randoms.py
Uniform
¶
Bases: RandomScalar
A uniformly distributed random number between low and high.
When both bounds are integers, an integer is drawn (both bounds inclusive); otherwise a float is drawn.
Source code in pygx/algo/scalars/_randoms.py
StepWise
¶
Bases: Scalar
A step-wise schedule that is specified via multiple phases.
Each phase pairs a length with a scheduled value; the value is queried with a step relative to the phase start. The schedule is stateful: it expects to be called with increasing steps (as done during an evolution run), advancing to the next phase after the current phase's last step has been queried. Past the final phase, the last value is returned.
Example:
s = pg.algo.scalars.StepWise([
# Anneal from 1.0 to 0.0 for the first 2 steps.
(2, pg.algo.scalars.linear(2, start=1.0, end=0.0)),
# Then stay at 0.5 for 2 more steps.
(2, 0.5),
])
assert [s(i) for i in range(5)] == [1.0, 0.5, 0.5, 0.5, 0.5]
Source code in pygx/algo/scalars/_step_wise.py
make_scalar
¶
make_scalar(value: Any) -> Scalar
Converts a value into a Scalar.
A Scalar is returned as is; another callable is wrapped into a
Lambda; any other value is wrapped into a
Constant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to convert. |
required |
Returns:
| Type | Description |
|---|---|
Scalar
|
A |
Source code in pygx/algo/scalars/_base.py
scalar_spec
¶
Returns the value spec for a field that accepts a scheduled value.
Use this to declare fields that accept either a plain value or a
step-based schedule for it (a Scalar or any callable taking a step).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_spec
|
ValueSpec
|
The value spec for the value produced at each step. |
required |
Returns:
| Type | Description |
|---|---|
ValueSpec
|
A value spec that accepts either the value itself or a callable that produces such a value from a step (an integer). |
Source code in pygx/algo/scalars/_base.py
scalar_value
¶
Evaluates a scheduled value at a step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
A plain value, or a callable (e.g. a |
required |
step
|
int
|
The step to evaluate the value at. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
|
Source code in pygx/algo/scalars/_base.py
cosine_decay
¶
Returns a scalar that decays from start to end on a cosine curve.
The value is start at step 0 and reaches end at step total_steps.
Source code in pygx/algo/scalars/_maths.py
cyclic
¶
Returns a scalar oscillating between high and low on a cosine wave.
The oscillation has a period of cycle steps and starts at high when
initial_radiant is 0.
Source code in pygx/algo/scalars/_maths.py
exponential_decay
¶
exponential_decay(
decay_rate: float,
decay_interval: int,
start: float = 1.0,
staircase: bool = True,
)
Returns a scalar that multiplies start by decay_rate per interval.
With staircase (the default), the value stays constant within each
decay_interval-step interval and drops at interval boundaries;
otherwise it decays continuously at every step.
Source code in pygx/algo/scalars/_maths.py
linear
¶
Returns a scalar that moves linearly from start to end.
The value is start at step 0 and reaches end at step total_steps.
Source code in pygx/algo/scalars/_maths.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2