作为学习 Rust 的借口,我正在研究遗传算法的代码,以及稍后的遗传编程。
我声明了突变操作的一个特征:
pub trait Mutator<IndvidualType> {
fn mutate(&self, individual: &IndvidualType) -> IndvidualType;
}
Run Code Online (Sandbox Code Playgroud)
为每个 实现该特征很容易IndividualType,但我想要更通用的东西,这是每个列表(向量)类型基因组所共有的特征,例如:
pub trait HasVectorGenome<IndividualType, BaseType> {
fn new_from_vec(genome: Vec<BaseType>) -> IndvidualType;
fn get_vec(&self) -> Vec<BaseType>;
}
Run Code Online (Sandbox Code Playgroud)
我想要一个通用的变异器,它能够变异每个HasVectorGenome其BaseType实现Rand(以便能够生成新的随机值)。就像是:
struct GeneralMutator;
impl<B, T> Mutator<T> for GeneralMutator
where T: HasVectorGenome<T, B>,
B: Rand
{
fn mutate(&self, individual: &T) -> T {
let genome: Vec<B> = individual.get_vec();
genome[0] = rand::random::<B>();
T::new_from_vec(genome)
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了错误the type parameter `B` is not constrained by the impl trait, self type, or predicates,无法编译。我不知道如何正确表达这一点。
小智 4
我已将这段代码的完整工作版本放在操场上(除了我删除了随机部分)。
首先,我IndividualType从 中删除了参数HasVectorGenome。这只是实现特征的类型,并且您对特征的定义与此不一致(new_from_vec返回IndividualType但get_vec消耗Self)。
其次,我创建了BaseType一个关联类型,这意味着任何单个类型都有一个唯一的基本类型。这在技术上是一个限制,但在大多数情况下,您不需要灵活性,并且它使类型更简单(事实上,这是消除您所看到的错误所需的主要更改)。所以现在的特征是:
pub trait HasVectorGenome {
type BaseType;
fn new_from_vec(genome: Vec<Self::BaseType>) -> Self;
fn get_vec(&self) -> Vec<Self::BaseType>;
}
Run Code Online (Sandbox Code Playgroud)
然后,我调整了实现的where子句GeneralMutator:
impl<T> Mutator<T> for GeneralMutator
where T: HasVectorGenome,
T::BaseType : Rand
Run Code Online (Sandbox Code Playgroud)