-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
297 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
softwareComponents/voxelReconfig/src/reconfig/metric/potential_fn.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use super::Metric; | ||
use num::integer::Roots; | ||
use num::traits::real::Real; | ||
use num::FromPrimitive; | ||
use std::marker::PhantomData; | ||
|
||
pub trait PotentialFn<TState> { | ||
type Potential: std::cmp::Ord + Default + Copy + std::fmt::Debug = usize; | ||
|
||
fn new(goal: &TState) -> Self | ||
where | ||
Self: Sized; | ||
|
||
fn get_potential(&mut self, state: &TState) -> Self::Potential; | ||
} | ||
|
||
// Uses the `potential + real_cost` for the estimated cost | ||
pub struct Sum<TState, TPotentialFn>(pub TPotentialFn, PhantomData<TState>) | ||
where | ||
TPotentialFn: PotentialFn<TState>; | ||
|
||
impl<TState, TPotentialFn> Metric<TState> for Sum<TState, TPotentialFn> | ||
where | ||
TPotentialFn: PotentialFn<TState>, | ||
TPotentialFn::Potential: FromPrimitive + num::Num, | ||
{ | ||
type Potential = TPotentialFn::Potential; | ||
type EstimatedCost = Self::Potential; | ||
|
||
fn new(goal: &TState) -> Self { | ||
Self(PotentialFn::new(goal), Default::default()) | ||
} | ||
|
||
fn get_potential(&mut self, state: &TState) -> Self::Potential { | ||
self.0.get_potential(state) | ||
} | ||
|
||
fn estimated_cost(cost: super::cost::Cost<Self::Potential>) -> Self::EstimatedCost { | ||
cost.potential | ||
+ FromPrimitive::from_usize(cost.real_cost).expect("Cannot convert real cost") | ||
} | ||
} | ||
|
||
// Uses the `potential + integer_sqrt(real_cost)` for the estimated cost | ||
pub struct ISqrtSum<TState, TPotentialFn>(pub TPotentialFn, PhantomData<TState>) | ||
where | ||
TPotentialFn: PotentialFn<TState>; | ||
|
||
impl<TState, TPotentialFn> Metric<TState> for ISqrtSum<TState, TPotentialFn> | ||
where | ||
TPotentialFn: PotentialFn<TState>, | ||
TPotentialFn::Potential: num::Integer + FromPrimitive, | ||
{ | ||
type Potential = TPotentialFn::Potential; | ||
type EstimatedCost = TPotentialFn::Potential; | ||
|
||
fn new(goal: &TState) -> Self { | ||
Self(PotentialFn::new(goal), Default::default()) | ||
} | ||
|
||
fn get_potential(&mut self, state: &TState) -> Self::Potential { | ||
self.0.get_potential(state) | ||
} | ||
|
||
fn estimated_cost(cost: super::cost::Cost<Self::Potential>) -> Self::EstimatedCost { | ||
cost.potential | ||
+ FromPrimitive::from_usize(cost.real_cost.sqrt()).expect("Cannot convert real cost") | ||
} | ||
} | ||
|
||
// Uses the `potential + sqrt(real_cost)` for the estimated cost | ||
pub struct FSqrtSum<TState, TPotentialFn>(pub TPotentialFn, PhantomData<TState>) | ||
where | ||
TPotentialFn: PotentialFn<TState>; | ||
|
||
impl<TState, TPotentialFn> Metric<TState> for FSqrtSum<TState, TPotentialFn> | ||
where | ||
TPotentialFn: PotentialFn<TState>, | ||
TPotentialFn::Potential: FromPrimitive + Real, | ||
{ | ||
type Potential = TPotentialFn::Potential; | ||
type EstimatedCost = TPotentialFn::Potential; | ||
|
||
fn new(goal: &TState) -> Self { | ||
Self(PotentialFn::new(goal), Default::default()) | ||
} | ||
|
||
fn get_potential(&mut self, state: &TState) -> Self::Potential { | ||
self.0.get_potential(state) | ||
} | ||
|
||
fn estimated_cost(cost: super::cost::Cost<Self::Potential>) -> Self::EstimatedCost { | ||
let real_cost = | ||
Self::EstimatedCost::from_usize(cost.real_cost).expect("Cannot convert real cost"); | ||
cost.potential + real_cost.sqrt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.