1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use gut::prelude::*;
#[macro_use]
extern crate lazy_static;
pub mod common {
pub use gut::prelude::*;
macro_rules! local_float_cmp {
($fi:ident, $fj:ident) => {
match ($fi.is_nan(), $fj.is_nan()) {
(true, false) => std::cmp::Ordering::Greater,
(false, true) => std::cmp::Ordering::Less,
(true, true) => std::cmp::Ordering::Equal,
(false, false) => unreachable!(),
}
};
}
pub trait FloatIteratorExt
where
Self: std::marker::Sized,
{
fn fmax(mut self) -> Option<f64> {
todo!()
}
fn fmin(mut self) -> Option<f64> {
todo!()
}
fn imax(mut self) -> Option<(usize, f64)> {
todo!()
}
fn imin(mut self) -> Option<(usize, f64)> {
todo!()
}
}
impl<F, T> FloatIteratorExt for T
where
T: Iterator<Item = F>,
F: std::borrow::Borrow<f64>,
Self: std::marker::Sized,
{
fn fmax(mut self) -> Option<f64> {
if let Some(value) = self.next() {
let f = self.fold(*value.borrow(), |a, b| a.max(*b.borrow()));
Some(f)
} else {
None
}
}
fn fmin(mut self) -> Option<f64> {
if let Some(value) = self.next() {
let f = self.fold(*value.borrow(), |a, b| a.min(*b.borrow()));
Some(f)
} else {
None
}
}
fn imax(mut self) -> Option<(usize, f64)> {
if let Some(value) = self.next() {
let value = *value.borrow();
let value = (1..).zip(self).fold((0, value), |a, b| {
let (ia, fa) = a;
let (ib, fb) = b;
let fb = *fb.borrow();
if fb > fa {
(ib, fb)
} else {
(ia, fa)
}
});
Some(value)
} else {
None
}
}
fn imin(mut self) -> Option<(usize, f64)> {
if let Some(value) = self.next() {
let value = *value.borrow();
let value = (1..).zip(self).fold((0, value), |a, b| {
let (ia, fa) = a;
let (ib, fb) = b;
let fb = *fb.borrow();
if fb < fa {
(ib, fb)
} else {
(ia, fa)
}
});
Some(value)
} else {
None
}
}
}
pub fn float_ordering_maximize(fi: &f64, fj: &f64) -> std::cmp::Ordering {
fj.partial_cmp(&fi).unwrap_or_else(|| local_float_cmp!(fi, fj))
}
pub fn float_ordering_minimize(fi: &f64, fj: &f64) -> std::cmp::Ordering {
fi.partial_cmp(&fj).unwrap_or_else(|| local_float_cmp!(fi, fj))
}
#[test]
fn test_float_ordering() {
let mut values = vec![1.0, -1.0, std::f64::NAN, 0.5, 2.0];
let m = values.iter().fmax();
assert_eq!(m, Some(2.0));
let m = values.iter().fmin();
assert_eq!(m, Some(-1.0));
let m = values.iter().imax();
assert_eq!(m, Some((4, 2.0)));
let m = values.iter().imin();
assert_eq!(m, Some((1, -1.0)));
values.sort_by(|a, b| float_ordering_maximize(&a, &b));
assert_eq!(values[0], 2.0);
assert!(values[4].is_nan());
values.sort_by(|a, b| float_ordering_minimize(&a, &b));
assert_eq!(values[0], -1.0);
assert!(values[4].is_nan());
}
}
#[macro_use]
pub mod random; pub mod encoding;
pub mod engine;
pub mod fitness;
pub mod gears;
pub mod individual;
pub mod operators;
pub mod population;
pub mod termination;
mod annealing;
mod fingerprint;
mod graph6;
mod vars;
pub mod prelude {
pub use crate::engine::Evolve;
pub use crate::fitness::EvaluateFitness;
pub use crate::gears::Breed;
pub use crate::gears::Survive;
pub use crate::individual::EvaluateObjectiveValue;
pub use crate::operators::*;
pub use crate::population::SortMember;
pub use crate::random::*;
pub use crate::encoding::Mutate;
pub use crate::fingerprint::FingerPrintExt;
}
pub use crate::engine::{Engine, EvolutionAlgorithm};
pub use crate::gears::GeneticBreeder;
pub use crate::gears::Survivor;
pub use crate::gears::Valuer;
pub use crate::individual::{Genome, Individual};
pub use crate::population::Population;