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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use core::f64 as scalar;

use rand::prelude::*;

use crate::delta_e;
use crate::random::{self, RandomizationStrategy};
use crate::{Color, Lab};

type Scalar = f64;

#[derive(Clone)]
pub struct DistanceResult {
    /// The closest distance between any two colors
    pub min_closest_distance: Scalar,

    /// The average over all nearest-neighbor distances
    pub mean_closest_distance: Scalar,

    /// Indices of the colors that were closest to each other
    pub closest_pair: (usize, usize),

    /// The closest distance and the index of the nearest neighbor
    pub closest_distances: Vec<(Scalar, usize)>,

    pub distance_metric: DistanceMetric,

    /// The number of colors that are fixed and cannot be changed. The actual colors are the first
    /// `num_fixed_colors` elements in the `colors` array.
    pub num_fixed_colors: usize,
}

pub struct IterationStatistics<'a> {
    pub iteration: usize,
    pub temperature: Scalar,
    pub distance_result: &'a DistanceResult,
    pub colors: Vec<Color>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptimizationTarget {
    Mean,
    Min,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptimizationMode {
    Global,
    Local,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DistanceMetric {
    CIE76,
    CIEDE2000,
}

pub struct SimulationParameters {
    pub initial_temperature: Scalar,
    pub cooling_rate: Scalar,
    pub num_iterations: usize,
    pub opt_target: OptimizationTarget,
    pub opt_mode: OptimizationMode,
    pub distance_metric: DistanceMetric,
    pub num_fixed_colors: usize,
}

pub struct SimulatedAnnealing<R: Rng> {
    colors: Vec<(Color, Lab)>,
    temperature: Scalar,
    pub parameters: SimulationParameters,
    rng: R,
}

impl SimulatedAnnealing<ThreadRng> {
    pub fn new(initial_colors: &[Color], parameters: SimulationParameters) -> Self {
        Self::with_rng(initial_colors, parameters, thread_rng())
    }
}

impl<R: Rng> SimulatedAnnealing<R> {
    pub fn with_rng(initial_colors: &[Color], parameters: SimulationParameters, rng: R) -> Self {
        let colors = initial_colors
            .iter()
            .map(|c| (c.clone(), c.to_lab()))
            .collect();

        SimulatedAnnealing {
            colors,
            temperature: parameters.initial_temperature,
            parameters,
            rng,
        }
    }
}

impl<R: Rng> SimulatedAnnealing<R> {
    pub fn get_colors(&self) -> Vec<Color> {
        self.colors.iter().map(|(c, _)| c.clone()).collect()
    }

    fn modify_channel(&mut self, c: &mut u8) {
        if self.rng.gen::<bool>() {
            *c = c.saturating_add(self.rng.gen::<u8>() % 10);
        } else {
            *c = c.saturating_sub(self.rng.gen::<u8>() % 10);
        }
    }

    fn modify_color(&mut self, color: &mut (Color, Lab)) {
        let mut strategy = random::strategies::UniformRGB {};

        match self.parameters.opt_mode {
            OptimizationMode::Local => {
                let mut rgb = color.0.to_rgba();
                self.modify_channel(&mut rgb.r);
                self.modify_channel(&mut rgb.g);
                self.modify_channel(&mut rgb.b);
                color.0 = Color::from_rgb(rgb.r, rgb.g, rgb.b);
            }
            OptimizationMode::Global => {
                color.0 = strategy.generate_with(&mut self.rng);
            }
        }
        color.1 = color.0.to_lab();
    }

    pub fn run(&mut self, callback: &mut dyn FnMut(&IterationStatistics)) -> DistanceResult {
        self.temperature = self.parameters.initial_temperature;

        let mut result = DistanceResult::new(
            &self.colors,
            self.parameters.distance_metric,
            self.parameters.num_fixed_colors,
        );

        if self.parameters.num_fixed_colors == self.colors.len() {
            return result;
        }

        for iter in 0..self.parameters.num_iterations {
            let random_index = if self.parameters.opt_target == OptimizationTarget::Mean {
                self.rng
                    .gen_range(self.parameters.num_fixed_colors, self.colors.len())
            } else {
                // first check if any of the colors cannot change, if that's the case just return
                // the other color. Note that the closest_pair cannot contain only fixed colors.
                #[allow(clippy::if_same_then_else)]
                if result.closest_pair.0 < self.parameters.num_fixed_colors {
                    result.closest_pair.1
                } else if result.closest_pair.1 < self.parameters.num_fixed_colors {
                    result.closest_pair.0
                } else if self.rng.gen() {
                    result.closest_pair.0
                } else {
                    result.closest_pair.1
                }
            };

            debug_assert!(
                random_index >= self.parameters.num_fixed_colors,
                "cannot change fixed color"
            );

            let mut new_colors = self.colors.clone();

            self.modify_color(&mut new_colors[random_index]);

            let new_result = result.update(&new_colors, random_index);

            let (score, new_score) = match self.parameters.opt_target {
                OptimizationTarget::Mean => (
                    result.mean_closest_distance,
                    new_result.mean_closest_distance,
                ),
                OptimizationTarget::Min => {
                    (result.min_closest_distance, new_result.min_closest_distance)
                }
            };

            if new_score > score {
                result = new_result;
                self.colors = new_colors;
            } else {
                let bolzmann = Scalar::exp(-(score - new_score) / self.temperature);
                if self.rng.gen::<Scalar>() <= bolzmann {
                    result = new_result;
                    self.colors = new_colors;
                }
            }

            if iter % 5_000 == 0 {
                let statistics = IterationStatistics {
                    iteration: iter,
                    temperature: self.temperature,
                    distance_result: &result,
                    colors: self.get_colors(),
                };
                callback(&statistics);
            }

            if iter % 1_000 == 0 {
                self.temperature *= self.parameters.cooling_rate;
            }
        }

        result
    }
}

/// Re-arrange the sequence of colors such that the minimal difference between a given color and
/// any of its predecessors is maximized.
///
/// Note: this is only a heuristic and will not yield optimal results (especially at the end of
/// the sequence).
///
/// See: https://en.wikipedia.org/wiki/Farthest-first_traversal
pub fn rearrange_sequence(colors: &mut Vec<Color>, metric: DistanceMetric) {
    let distance = |c1: &Color, c2: &Color| match metric {
        DistanceMetric::CIE76 => c1.distance_delta_e_cie76(c2),
        DistanceMetric::CIEDE2000 => c1.distance_delta_e_ciede2000(c2),
    };

    // vector where the i-th element contains the minimum distance to the colors from 0 to i-1.
    let mut min_distances = vec![i32::max_value(); colors.len()];

    for i in 1..colors.len() {
        let mut max_i = colors.len();
        let mut max_d = i32::min_value();

        for j in i..colors.len() {
            min_distances[j] =
                min_distances[j].min((distance(&colors[j], &colors[i - 1]) * 1000.0) as i32);

            if min_distances[j] > max_d {
                max_i = j;
                max_d = min_distances[j];
            }
        }

        colors.swap(i, max_i);
        min_distances.swap(i, max_i);
    }
}

pub fn distinct_colors(
    count: usize,
    distance_metric: DistanceMetric,
    fixed_colors: Vec<Color>,
    callback: &mut dyn FnMut(&IterationStatistics),
) -> (Vec<Color>, DistanceResult) {
    assert!(count > 1);
    assert!(fixed_colors.len() <= count);

    let num_fixed_colors = fixed_colors.len();
    let mut colors = fixed_colors;

    for _ in num_fixed_colors..count {
        colors.push(random::strategies::UniformRGB.generate());
    }

    let mut annealing = SimulatedAnnealing::new(
        &colors,
        SimulationParameters {
            initial_temperature: 3.0,
            cooling_rate: 0.95,
            num_iterations: 100_000,
            opt_target: OptimizationTarget::Mean,
            opt_mode: OptimizationMode::Global,
            distance_metric,
            num_fixed_colors,
        },
    );

    annealing.run(callback);

    annealing.parameters.initial_temperature = 0.5;
    annealing.parameters.cooling_rate = 0.98;
    annealing.parameters.num_iterations = 200_000;
    annealing.parameters.opt_target = OptimizationTarget::Min;
    annealing.parameters.opt_mode = OptimizationMode::Local;

    let result = annealing.run(callback);

    (annealing.get_colors(), result)
}

impl DistanceResult {
    fn new(
        colors: &[(Color, Lab)],
        distance_metric: DistanceMetric,
        num_fixed_colors: usize,
    ) -> Self {
        let mut result = DistanceResult {
            closest_distances: vec![(scalar::MAX, std::usize::MAX); colors.len()],
            closest_pair: (std::usize::MAX, std::usize::MAX),
            mean_closest_distance: 0.0,
            min_closest_distance: scalar::MAX,
            distance_metric,
            num_fixed_colors,
        };

        for i in 0..colors.len() {
            result.update_distances(colors, i, false);
        }
        result.update_totals();

        result
    }

    fn update(&self, colors: &[(Color, Lab)], changed_color: usize) -> Self {
        let mut result = self.clone();
        result.update_distances(colors, changed_color, true);
        result.update_totals();
        result
    }

    fn update_distances(&mut self, colors: &[(Color, Lab)], color: usize, changed: bool) {
        self.closest_distances[color] = (scalar::MAX, std::usize::MAX);

        // we need to recalculate distances for nodes where the previous min dist was with
        // changed_color but it's not anymore (potentially).
        let mut to_recalc = Vec::with_capacity(colors.len());

        for (i, c) in colors.iter().enumerate() {
            if i == color {
                continue;
            }

            let dist = self.distance(c, &colors[color]);

            if dist < self.closest_distances[i].0 {
                self.closest_distances[i] = (dist, color);
            } else if changed && self.closest_distances[i].1 == color {
                // changed_color was the best before, but unfortunately we cannot say it now for
                // sure because the distance between the two increased. Play it safe and just
                // recalculate its distances.
                to_recalc.push(i);
            }

            if dist < self.closest_distances[color].0 {
                self.closest_distances[color] = (dist, i);
            }
        }

        for i in to_recalc {
            self.update_distances(colors, i, false);
        }
    }

    fn update_totals(&mut self) {
        self.mean_closest_distance = 0.0;
        self.min_closest_distance = scalar::MAX;

        let mut closest_pair_set = false;

        for (i, (dist, closest_i)) in self.closest_distances.iter().enumerate() {
            if i < self.num_fixed_colors && *closest_i < self.num_fixed_colors {
                continue;
            }

            self.mean_closest_distance += *dist;

            // the closest pair must ignore pairs of fixed colors because we cannot change them. On
            // the other hand we can consider pairs with at least one non fixed color because we
            // can change that.
            if (i >= self.num_fixed_colors || *closest_i >= self.num_fixed_colors)
                && (*dist < self.min_closest_distance || !closest_pair_set)
            {
                self.closest_pair = (i, *closest_i);
                closest_pair_set = true;
            }

            self.min_closest_distance = self.min_closest_distance.min(*dist);
        }

        self.mean_closest_distance /=
            (self.closest_distances.len() - self.num_fixed_colors) as Scalar;
    }

    fn distance(&self, a: &(Color, Lab), b: &(Color, Lab)) -> Scalar {
        match self.distance_metric {
            DistanceMetric::CIE76 => delta_e::cie76(&a.1, &b.1),
            DistanceMetric::CIEDE2000 => delta_e::ciede2000(&a.1, &b.1),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        rearrange_sequence, DistanceMetric, OptimizationMode, OptimizationTarget,
        SimulatedAnnealing, SimulationParameters,
    };
    use crate::Color;

    use rand::prelude::*;
    use rand_xoshiro::Xoshiro256StarStar;

    #[test]
    fn test_rearrange_sequence() {
        let mut colors = vec![
            Color::white(),
            Color::graytone(0.25),
            Color::graytone(0.5),
            Color::graytone(0.8),
            Color::black(),
        ];

        rearrange_sequence(&mut colors, DistanceMetric::CIE76);

        assert_eq!(
            colors,
            vec![
                Color::white(),
                Color::black(),
                Color::graytone(0.5),
                Color::graytone(0.25),
                Color::graytone(0.8),
            ]
        );
    }

    #[test]
    fn test_distinct_all_fixed_colors() {
        let colors = [Color::red(), Color::olive(), Color::yellow()];

        let mut sim = SimulatedAnnealing::with_rng(
            &colors,
            SimulationParameters {
                initial_temperature: 3.0,
                cooling_rate: 0.95,
                num_iterations: 100,
                opt_target: OptimizationTarget::Min,
                opt_mode: OptimizationMode::Local,
                distance_metric: DistanceMetric::CIE76,
                num_fixed_colors: 3,
            },
            Xoshiro256StarStar::seed_from_u64(21),
        );
        sim.run(&mut |_| {});

        assert_eq!(
            sim.get_colors(),
            vec![Color::red(), Color::olive(), Color::yellow()]
        );
    }

    #[test]
    fn test_distinct_2_fixed_colors() {
        let colors = [Color::red(), Color::yellow()];

        let mut sim = SimulatedAnnealing::with_rng(
            &colors,
            SimulationParameters {
                initial_temperature: 3.0,
                cooling_rate: 0.95,
                num_iterations: 100,
                opt_target: OptimizationTarget::Min,
                opt_mode: OptimizationMode::Local,
                distance_metric: DistanceMetric::CIE76,
                num_fixed_colors: 1,
            },
            Xoshiro256StarStar::seed_from_u64(42),
        );
        sim.run(&mut |_| {});

        assert_eq!(sim.get_colors()[0], Color::red());
    }
}