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_boundsandnew_bounds. The value(s) will be interpolated from theirold_boundsto theirnew_bounds.old_bounds – The previous domain (in which
valueis currently). Ifvalueis a scalar,old_boundsmust be a 1D array of size 2, e.g.,[-1, 1]. Otherwise, ifvalueis an array,old_boundsmust 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 thatvaluecontains 3 elements.new_bounds – The new domain (in which the returned value will be). Similarly to
old_bounds, ifvalueis a scalar,new_boundsmust be a 1D array of size 2, e.g.,[0, 1]. Otherwise, ifvalueis an array,new_boundsmust 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 thatvaluecontains 3 elements.
- Returns:
If
valueis a scalar (i.e., has nolen()), a scalar interpolated from the old domain to the new one. Otherwise, ifvalueis an array, a numpy ndarray is returned, in which each element was interpolated from its corresponding old domain to its new one.