NREL / routee-compass

An energy-aware routing engine
https://nrel.github.io/routee-compass/
BSD 3-Clause "New" or "Revised" License
10 stars 5 forks source link

n-dimensional interpolation models #135

Open robfitzgerald opened 6 months ago

robfitzgerald commented 6 months ago

We currently support a bilinear interpolation model for providing very fast energy rate predictions by doing a linear interpolation over a structured grid. This could be extended to support N dimensions such that it could support more than two incoming features (currently just speed and grade).

Some things to keep in mind is that we want the interpolate method to be as fast as possible since it will be called for every link traversal, minimizing any memory allocations and ideally just performing simple operations.

This work will eventually support a broader refactor of the PredictionModel trait that supports an arbitrary number of incoming features, building toward this API:

pub trait PredictionModel: Send + Sync {
    fn predict(
        &self,
        state: &[StateVar],
        state_model: &StateModel,
    ) -> Result<EnergyRate, TraversalModelError>;
}
nreinicke commented 6 months ago

@kylecarow - just updated this if you end up having the chance to work on it.

kylecarow commented 3 months ago

WIP in kjc/interpolation

FASTSim has benchmarks for interpolation, see https://github.nrel.gov/MBAP/fastsim/issues/328

kylecarow commented 3 months ago

Compass now uses the new interpolation stuff (in the branch), but should be replumbed to allow generic Feature inputs that track units, I'm thinking something like:

pub enum Feature {
    value: InternalFloat,
    unit: FeatureUnit,
}

pub enum FeatureUnit {
    SpeedUnit,
    GradeUnit,
    // ...
}

And change the arguments of InterpolationSpeedGradeModel::new() (along with corresponding fields) to be close to this:

  pub fn new<P: AsRef<Path>>(
      underlying_model_path: &P,
      underlying_model_type: ModelType,
      underlying_model_name: String,
-     speed_unit: SpeedUnit,
-     speed_bounds: (Speed, Speed),
-     speed_bins: usize,
-     grade_unit: GradeUnit,
-     grade_bounds: (Grade, Grade),
-     grade_bins: usize,
+     features: &[Feature],
+     lower_bounds: &[Feature],
+     upper_bounds: &[Feature],
+     bins: usize,
      energy_rate_unit: EnergyRateUnit,
  ) -> Result<Self, TraversalModelError> {