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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::memory::*;

/// Enum with possible abort strategies.
/// These strategies specify when a running iteration (with the k-means calculation) is aborted.
pub enum AbortStrategy<T: Primitive> {
	/// This strategy aborts the calculation directly after an iteration produced no improvement where `improvement > threshold`
	/// for the first time.
	/// ## Fields:
	/// - **threshold**: Threshold, used to detect an improvement (`improvement > threshold`)
    NoImprovement { threshold: T },
    /// This strategy aborts the calculation, when there have not been any improvements after **x** iterations,
    /// where `improvement > threshold`.
	/// ## Fields:
	/// - **x**: The amount of consecutive without improvement, after which the calculation is aborted
	/// - **threshold**: Threshold, used to detect an improvement (`improvement > threshold`)
	/// - **abort_on_negative**: Specifies whether the strategy instantly aborts when a negative improvement occured (**true**), or if
	/// negative improvements are handled as "no improvements" (**false**).
	NoImprovementForXIterations { x: usize, threshold: T, abort_on_negative: bool }
}
impl<T: Primitive> AbortStrategy<T> {
	pub(crate) fn create_logic(&self) -> Box<dyn AbortStrategyLogic<T>> {
		match *self {
			AbortStrategy::NoImprovementForXIterations{x,threshold,abort_on_negative} => Box::new(NoImprovementForXIterationsLogic {
				x, threshold, abort_on_negative,
				prev_error: T::infinity(),
				no_improvement_counter: 0
			}),
			AbortStrategy::NoImprovement{threshold} => Box::new(NoImprovementLogic {
				threshold,
				prev_error: T::infinity()
			})
		}
	}
}

pub(crate) trait AbortStrategyLogic<T: Primitive> {
	/// Function that has to be called once an iteration of the calculation ended, a new error was calculated.
	/// ## Arguments
	/// - **error**: The new **error (distsum), after an iteration
	/// ## Returns
	/// - **true** if the calculation should continue
	/// - **false** if the calculation should abort
	fn next(&mut self, error: T) -> bool;
}


pub(crate) struct NoImprovementLogic<T: Primitive> {
	threshold: T,
	prev_error: T
}
impl<T: Primitive> AbortStrategyLogic<T> for NoImprovementLogic<T> {
	fn next(&mut self, error: T) -> bool {
		let improvement = self.prev_error - error;
		self.prev_error = error;
		improvement > self.threshold
	}
}


pub(crate) struct NoImprovementForXIterationsLogic<T: Primitive> {
	x: usize,
	threshold: T,
	abort_on_negative: bool,
	prev_error: T,
	no_improvement_counter: usize
}
impl<T: Primitive> AbortStrategyLogic<T> for NoImprovementForXIterationsLogic<T> {
	fn next(&mut self, error: T) -> bool {
		let improvement = self.prev_error - error;
		self.prev_error = error;
		if self.abort_on_negative && improvement < T::zero() { // Negative improvement, and instant abort is requested
			return false;
		}
		if improvement > self.threshold { // positive improvement: reset no-improv-counter
			self.no_improvement_counter = 0;
		} else { // Still no improvement, count 1 up
			self.no_improvement_counter += 1;
		}
		self.no_improvement_counter < self.x
	}
}


#[cfg(test)]
mod tests {
	use super::*;

	#[test] fn test_no_improvement_f32() { test_no_improvement::<f32>(); }
	#[test] fn test_no_improvement_f64() { test_no_improvement::<f64>(); }

	fn test_no_improvement<T: Primitive>() {
		{
			let mut abort_strategy = AbortStrategy::NoImprovement { threshold: T::from(0.0005).unwrap() }.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovement { threshold: T::from(0.0005).unwrap() }.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.99959).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovement { threshold: T::from(0.0005).unwrap() }.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.99935).unwrap() ), true);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovement { threshold: T::from(0.0005).unwrap() }.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.99).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.99999999).unwrap() ), false);
		}
	}


	#[test]
	fn test_no_improvement_for_x_iterations_f32() { test_no_improvement_for_x_iterations::<f32>(); }

	#[test]
	fn test_no_improvement_for_x_iterations_f64() { test_no_improvement_for_x_iterations::<f64>(); }

	fn test_no_improvement_for_x_iterations<T: Primitive>() {
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: false}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: false}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.99959).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: false}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.99935).unwrap() ), true);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: false}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.99).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.99999999).unwrap() ), false);
		}
		// ABORT_ON_NEGATIVE (without negative improvements)
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.99959).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.99935).unwrap() ), true);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 1, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.99).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.99999999).unwrap() ), false);
		}
		// ABORT_ON_NEGATIVE (with negative improvements)
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 2, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(3001.0).unwrap() ), false);
		}
		{ // Should abort on negative improvement, even ifs absolute value < threshold
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 2, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(3000.0004).unwrap() ), false);
		}
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 2, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(3000.0007).unwrap() ), false);
		}

		// X != 1
		{
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 2, threshold: T::from(0.0005).unwrap(), abort_on_negative: false}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), false);
		}
		{ // Same as directly above, but with abort_on_negative = true
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 2, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), false);
		}
		{ // Negative improvement before no_improvement_counter == 2
			let mut abort_strategy = AbortStrategy::NoImprovementForXIterations {
				x: 2, threshold: T::from(0.0005).unwrap(), abort_on_negative: true}.create_logic();
			assert_eq!(abort_strategy.next( T::from(3000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2000.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(1999.0).unwrap() ), true);
			assert_eq!(abort_strategy.next( T::from(2999.0).unwrap() ), false);
		}
	}
}