Skip to content

Commit a351aab

Browse files
committed
refactor MutationType::Scaled with ranges to
MutationType::ScaledStep with steps (removes the need for ranges as these are symmetrical by definition, and also removes confusion about range v. edge sampling)
1 parent 4ac4157 commit a351aab

File tree

17 files changed

+196
-246
lines changed

17 files changed

+196
-246
lines changed

benches/genotype.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ pub fn mutation_benchmark(c: &mut Criterion) {
110110
let mut genotype = RangeGenotype::builder()
111111
.with_genes_size(*genes_size)
112112
.with_allele_range(0.0..=1.0)
113-
.with_mutation_type(MutationType::Scaled(vec![
114-
-0.1..=0.1,
115-
-0.01..=0.01,
116-
-0.001..=0.001,
117-
]))
113+
.with_mutation_type(MutationType::ScaledSteps(vec![0.1, 0.01, 0.001]))
118114
.build()
119115
.unwrap();
120116
let mut chromosome = Chromosome::new(genotype.random_genes_factory(&mut rng));

benches/neighbours.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
3939
let mut genotype = RangeGenotype::builder()
4040
.with_genes_size(10)
4141
.with_allele_range(-1.0..=1.0)
42-
.with_mutation_type(MutationType::Scaled(vec![
43-
-0.1..=0.1,
44-
-0.01..=0.01,
45-
-0.001..=0.001,
46-
]))
42+
.with_mutation_type(MutationType::ScaledSteps(vec![0.1, 0.01, 0.001]))
4743
.build()
4844
.unwrap();
4945
genotype.increment_scale_index();

examples/evolve_milp_custom_mutate.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,8 @@ fn main() {
105105
let genotype = MultiRangeGenotype::builder()
106106
.with_allele_ranges(vec![(-10.0..=10.0), (0.0..=10.0)])
107107
.with_mutation_types(vec![
108-
MutationType::Scaled(vec![
109-
-0.1..=0.1,
110-
-0.01..=0.01,
111-
-0.001..=0.001,
112-
-0.0001..=0.0001,
113-
-0.00001..=0.00001,
114-
-0.000001..=0.000001,
115-
]),
116-
MutationType::Scaled(vec![
117-
-0.5..=0.5,
118-
-0.1..=0.1,
119-
-0.05..=0.05,
120-
-0.01..=0.01,
121-
-0.001..=0.001,
122-
-0.0001..=0.0001,
123-
]),
108+
MutationType::ScaledSteps(vec![0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001]),
109+
MutationType::ScaledSteps(vec![0.5, 0.1, 0.05, 0.01, 0.001, 0.0001]),
124110
])
125111
.build()
126112
.unwrap();

examples/evolve_range_float.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ fn main() {
3030
.with_allele_range(0.0..=1.0) // won't converge, with low max_stale_generations, converges just fine with higher max_stale_generations
3131
// .with_mutation_type(MutationType::Relative(-0.1..=0.1)) // converges slowly
3232
.with_mutation_type(MutationType::Transition(1000, 2000, -0.1..=0.1)) // converges slowly
33-
// .with_mutation_type(MutationType::Scaled(vec![
34-
// -0.1..=0.1,
35-
// -0.01..=0.01,
36-
// -0.001..=0.001,
37-
// -0.0001..=0.0001,
38-
// -0.00001..=0.00001,
33+
// .with_mutation_type(MutationType::ScaledSteps(vec![
34+
// 0.1,
35+
// 0.01,
36+
// 0.001,
37+
// 0.0001,
38+
// 0.00001,
3939
// ])) // converges fast, but needs low max_stale_generations to trigger next scale
4040
.build()
4141
.unwrap();

examples/hill_climb_milp.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,8 @@ fn main() {
4949
// MutationType::Relative(-1.0..=1.0)
5050
// ])
5151
.with_mutation_types(vec![
52-
MutationType::Scaled(vec![
53-
-0.1..=0.1,
54-
-0.01..=0.01,
55-
-0.001..=0.001,
56-
-0.0001..=0.0001,
57-
-0.00001..=0.00001,
58-
-0.000001..=0.000001,
59-
]),
60-
MutationType::Scaled(vec![
61-
-0.1..=0.1,
62-
-0.01..=0.01,
63-
-0.001..=0.001,
64-
-0.0001..=0.0001,
65-
-0.00001..=0.00001,
66-
-0.000001..=0.000001,
67-
]),
52+
MutationType::ScaledSteps(vec![0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001]),
53+
MutationType::ScaledSteps(vec![0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001]),
6854
])
6955
.with_genes_hashing(false) // not useful for HillClimb
7056
.build()

examples/hill_climb_range.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ fn main() {
2727
.with_allele_range(0.0..=1.0)
2828
// .with_mutation_type(MutationType::Relative(-0.1..=0.1)) // won't converge for SteepestAscent
2929
// .with_mutation_type(MutationType::Relative(-0.001..=0.001)) // slow converge
30-
.with_mutation_type(MutationType::Scaled(vec![
31-
-0.1..=0.1,
32-
-0.01..=0.01,
33-
-0.001..=0.001,
34-
-0.0001..=0.0001,
35-
-0.00001..=0.00001,
30+
.with_mutation_type(MutationType::ScaledSteps(vec![
31+
0.1, 0.01, 0.001, 0.0001, 0.00001,
3632
]))
3733
.with_genes_hashing(false) // not useful for HillClimb
3834
.build()

examples/permutate_range.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ fn main() {
2525
let genotype = RangeGenotype::builder()
2626
.with_genes_size(4)
2727
.with_allele_range(0.0..=1.0)
28-
.with_mutation_type(MutationType::Scaled(vec![
29-
-0.1..=0.1,
30-
-0.01..=0.01,
31-
-0.001..=0.001,
32-
-0.0001..=0.0001,
33-
-0.00001..=0.00001,
28+
.with_mutation_type(MutationType::ScaledSteps(vec![
29+
0.1, 0.01, 0.001, 0.0001, 0.00001,
3430
]))
3531
.build()
3632
.unwrap();

src/allele.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub trait RangeAllele:
7070
/// used to build a start exclusive range, by adding the increment to the start
7171
fn smallest_increment() -> Self;
7272

73+
/// Returns value 0 for iteration/counting
74+
fn zero() -> Self;
75+
7376
/// Returns value 1 for iteration/counting
7477
fn one() -> Self;
7578

@@ -95,6 +98,9 @@ impl RangeAllele for f32 {
9598
fn smallest_increment() -> Self {
9699
f32::EPSILON
97100
}
101+
fn zero() -> Self {
102+
0.0
103+
}
98104
fn one() -> Self {
99105
1.0
100106
}
@@ -109,6 +115,9 @@ impl RangeAllele for f64 {
109115
fn smallest_increment() -> Self {
110116
f64::EPSILON
111117
}
118+
fn zero() -> Self {
119+
0.0
120+
}
112121
fn one() -> Self {
113122
1.0
114123
}
@@ -123,6 +132,9 @@ impl RangeAllele for i8 {
123132
fn smallest_increment() -> Self {
124133
1
125134
}
135+
fn zero() -> Self {
136+
0
137+
}
126138
fn one() -> Self {
127139
1
128140
}
@@ -137,6 +149,9 @@ impl RangeAllele for i16 {
137149
fn smallest_increment() -> Self {
138150
1
139151
}
152+
fn zero() -> Self {
153+
0
154+
}
140155
fn one() -> Self {
141156
1
142157
}
@@ -151,6 +166,9 @@ impl RangeAllele for i32 {
151166
fn smallest_increment() -> Self {
152167
1
153168
}
169+
fn zero() -> Self {
170+
0
171+
}
154172
fn one() -> Self {
155173
1
156174
}
@@ -165,6 +183,9 @@ impl RangeAllele for u8 {
165183
fn smallest_increment() -> Self {
166184
1
167185
}
186+
fn zero() -> Self {
187+
0
188+
}
168189
fn one() -> Self {
169190
1
170191
}
@@ -179,6 +200,9 @@ impl RangeAllele for u16 {
179200
fn smallest_increment() -> Self {
180201
1
181202
}
203+
fn zero() -> Self {
204+
0
205+
}
182206
fn one() -> Self {
183207
1
184208
}
@@ -193,6 +217,9 @@ impl RangeAllele for u32 {
193217
fn smallest_increment() -> Self {
194218
1
195219
}
220+
fn zero() -> Self {
221+
0
222+
}
196223
fn one() -> Self {
197224
1
198225
}

src/genotype/builder.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ impl<G: Genotype> Builder<G> {
108108
mut self,
109109
allele_mutation_scaled_range: Vec<RangeInclusive<G::Allele>>,
110110
) -> Self {
111-
self.mutation_type = Some(MutationType::Scaled(allele_mutation_scaled_range));
111+
self.mutation_type = Some(MutationType::ScaledSteps(
112+
allele_mutation_scaled_range
113+
.into_iter()
114+
.map(|r| *r.end())
115+
.collect(),
116+
));
112117
self
113118
}
114119

@@ -121,11 +126,11 @@ impl<G: Genotype> Builder<G> {
121126
self.mutation_types = Some(
122127
(0..*genes_size)
123128
.map(|gene_index| {
124-
MutationType::Scaled(
129+
MutationType::ScaledSteps(
125130
allele_mutation_scaled_ranges
126131
.iter()
127132
.map(|gene_ranges_per_scale| {
128-
gene_ranges_per_scale[gene_index].clone()
133+
*gene_ranges_per_scale[gene_index].end()
129134
})
130135
.collect(),
131136
)

0 commit comments

Comments
 (0)