smartgrid.util.interpolate¶
- smartgrid.util.interpolate(value, old_bounds, new_bounds)[source]¶
Interpolates a value (or array of values) from a domain to a new one.
For example, if the value is
0
, the interpolation from[-1, 1]
to[0, 1]
gives0.5
.This function is particularly useful for manipulating reward ranges, and to convert actions and observations between the
[0, 1]
domain that is typically used in learning algorithm (easier to manipulate) and the actual domain expected from the simulator.It supports interpolating differently for each dimension, when an array of values is passed.
- Parameters:
value – Either a single value (float) or an array of multiple values. It must match
old_bounds
andnew_bounds
. The value(s) will be interpolated from theirold_bounds
to theirnew_bounds
.old_bounds – The previous domain (in which
value
is currently). Ifvalue
is a scalar,old_bounds
must be a 1D array of size 2, e.g.,[-1, 1]
. Otherwise, ifvalue
is an array,old_bounds
must be a 2D array, of the same size asvalue
, each element being an array of size 2, e.g.,[ [-1, 1], [0, 1], [-100, 100] ]
assuming thatvalue
contains 3 elements.new_bounds – The new domain (in which the returned value will be). Similarly to
old_bounds
, ifvalue
is a scalar,new_bounds
must be a 1D array of size 2, e.g.,[0, 1]
. Otherwise, ifvalue
is an array,new_bounds
must be a 2D array, of the same size asvalue
, each element being an array of size 2, e.g.,[ [0, 1], [-10, 10], [-1, 1] ]
, assuming thatvalue
contains 3 elements.
- Returns:
If
value
is a scalar (i.e., has nolen()
), a scalar interpolated from the old domain to the new one. Otherwise, ifvalue
is an array, a numpy ndarray is returned, in which each element was interpolated from its corresponding old domain to its new one.