move estimated_cost() to member of MorphismType
This commit is contained in:
parent
267b5e009e
commit
39805806be
3 changed files with 50 additions and 54 deletions
|
@ -1,53 +1,51 @@
|
|||
use crate::{MorphismType, TypeTerm};
|
||||
|
||||
pub fn estimated_morphism_cost(ty: &MorphismType) -> u64 {
|
||||
impl MorphismType {
|
||||
pub fn estimated_cost(&self) -> u64 {
|
||||
|
||||
if let Ok((ψ,σ)) = crate::subtype_unify(&ty.src_type, &ty.dst_type) {
|
||||
1
|
||||
} else {
|
||||
match (ty.src_type.clone().normalize(),
|
||||
ty.dst_type.clone().normalize())
|
||||
{
|
||||
(TypeTerm::Ladder(r1),
|
||||
TypeTerm::Ladder(r2)) => {
|
||||
let mut cost = 10;
|
||||
for i in 0..usize::min( r1.len(), r2.len() ) {
|
||||
cost += estimated_morphism_cost(&MorphismType {
|
||||
src_type: r1[i].clone(), dst_type: r2[i].clone() });
|
||||
if let Ok((ψ,σ)) = crate::subtype_unify(&self.src_type, &self.dst_type) {
|
||||
1
|
||||
} else {
|
||||
match (self.src_type.clone().normalize(),
|
||||
self.dst_type.clone().normalize())
|
||||
{
|
||||
(TypeTerm::Ladder(r1),
|
||||
TypeTerm::Ladder(r2)) => {
|
||||
let mut cost = 10;
|
||||
for i in 0..usize::min( r1.len(), r2.len() ) {
|
||||
cost += MorphismType { src_type: r1[i].clone(), dst_type: r2[i].clone() }.estimated_cost();
|
||||
}
|
||||
cost
|
||||
}
|
||||
cost
|
||||
}
|
||||
(TypeTerm::Spec(a1),
|
||||
TypeTerm::Spec(a2)) => {
|
||||
let mut cost = 10;
|
||||
for i in 0..usize::min( a1.len(), a2.len() ) {
|
||||
cost += estimated_morphism_cost(
|
||||
&MorphismType {src_type: a1[i].clone(), dst_type: a2[i].clone() });
|
||||
(TypeTerm::Spec(a1),
|
||||
TypeTerm::Spec(a2)) => {
|
||||
let mut cost = 10;
|
||||
for i in 0..usize::min( a1.len(), a2.len() ) {
|
||||
cost += MorphismType {src_type: a1[i].clone(), dst_type: a2[i].clone() }.estimated_cost();
|
||||
}
|
||||
cost
|
||||
}
|
||||
cost
|
||||
}
|
||||
(TypeTerm::Seq{ seq_repr: sr1, items: items1 },
|
||||
TypeTerm::Seq{ seq_repr: sr2, items: items2 }) => {
|
||||
let mut cost = 10;
|
||||
/*
|
||||
estimated_morphism_cost(
|
||||
&MorphismType { src_type: sr1, dst_type: sr2 }
|
||||
);
|
||||
*/
|
||||
for i in 0..usize::min( items1.len(), items2.len() ) {
|
||||
cost += estimated_morphism_cost(
|
||||
&MorphismType { src_type: items1[i].clone(), dst_type: items2[i].clone() }
|
||||
(TypeTerm::Seq{ seq_repr: sr1, items: items1 },
|
||||
TypeTerm::Seq{ seq_repr: sr2, items: items2 }) => {
|
||||
let mut cost = 10;
|
||||
/*
|
||||
estimated_morphism_cost(
|
||||
&MorphismType { src_type: sr1, dst_type: sr2 }
|
||||
);
|
||||
*/
|
||||
for i in 0..usize::min( items1.len(), items2.len() ) {
|
||||
cost += MorphismType { src_type: items1[i].clone(), dst_type: items2[i].clone() }.estimated_cost();
|
||||
}
|
||||
|
||||
cost
|
||||
}
|
||||
|
||||
cost
|
||||
}
|
||||
|
||||
(a, b) => {
|
||||
if a == b {
|
||||
return 0;
|
||||
} else {
|
||||
return 10;
|
||||
(a, b) => {
|
||||
if a == b {
|
||||
return 0;
|
||||
} else {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ impl<'a, M:Morphism+Clone> ShortestPathProblem<'a, M> {
|
|||
ShortestPathProblem {
|
||||
morphism_base,
|
||||
queue: vec![
|
||||
MorphismPath::<M> { weight: 0, est_remain: estimated_morphism_cost(&ty), cur_type: ty.src_type, morphisms: vec![] }
|
||||
MorphismPath::<M> { weight: 0, est_remain: ty.estimated_cost(), cur_type: ty.src_type, morphisms: vec![] }
|
||||
],
|
||||
goal: ty.dst_type
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ impl<'a, M:Morphism+Clone> ShortestPathProblem<'a, M> {
|
|||
if ! creates_loop {
|
||||
new_path.weight += morph_inst.get_weight();
|
||||
new_path.cur_type = dst_type;
|
||||
new_path.est_remain = estimated_morphism_cost(&MorphismType{ src_type: new_path.cur_type.clone(), dst_type: self.goal.clone() });
|
||||
new_path.est_remain = MorphismType{ src_type: new_path.cur_type.clone(), dst_type: self.goal.clone() }.estimated_cost();
|
||||
|
||||
new_path.morphisms.push(morph_inst);
|
||||
self.queue.push(new_path);
|
||||
|
|
|
@ -5,20 +5,18 @@ fn test_heuristic() {
|
|||
let mut dict = BimapTypeDict::new();
|
||||
|
||||
assert_eq!(
|
||||
estimated_morphism_cost(
|
||||
&MorphismType {
|
||||
src_type: dict.parse("A").expect("parse"),
|
||||
dst_type: dict.parse("A").expect("parse")
|
||||
}),
|
||||
MorphismType {
|
||||
src_type: dict.parse("A").expect("parse"),
|
||||
dst_type: dict.parse("A").expect("parse")
|
||||
}.estimated_cost(),
|
||||
1
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
estimated_morphism_cost(
|
||||
&MorphismType {
|
||||
src_type: dict.parse("<Digit 10> ~ Char ~ Ascii ~ native.UInt8").expect("parse"),
|
||||
dst_type: dict.parse("<Digit 16> ~ native.UInt8").expect("parse")
|
||||
}),
|
||||
MorphismType {
|
||||
src_type: dict.parse("<Digit 10> ~ Char ~ Ascii ~ native.UInt8").expect("parse"),
|
||||
dst_type: dict.parse("<Digit 16> ~ native.UInt8").expect("parse")
|
||||
}.estimated_cost(),
|
||||
41
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue