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
use itertools::{izip, Itertools};
use rand::distributions::uniform::SampleUniform;
use std::{
    convert::TryInto,
    f64, fmt,
    ops::{Add, AddAssign, Div, Mul, Range, Sub},
    sync::{
        atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering},
        Arc, Mutex,
    },
    thread,
    time::Duration,
};

use crate::util::{poll, Polling};

/// Castes all given ranges to `f64` values and calls [`grid_search()`].
/// ```
/// use std::{sync::Arc,time::Duration};
/// use simple_optimization::{grid_search, Polling};
/// fn simple_function(list: &[f64; 3], _: Option<Arc<()>>) -> f64 { list.iter().sum() }
/// let best = grid_search!(
///     (0f64..10f64, 5u32..15u32, 10i16..20i16), // Value ranges.
///     simple_function, // Evaluation function.
///     None, //  No additional evaluation data.
///     // Polling every `10ms`, printing progress (`true`), exiting early if `15.` or less is reached, and not printing thread execution data (`false`).
///     Some(Polling { poll_rate: Duration::from_millis(5), printing: true, early_exit_minimum: Some(15.), thread_execution_reporting: false }),
///     None, // We don't specify the number of threads.
///     // Take `10` samples along range `0` (`0..10`), `11` along range `1` (`5..15`)
///     //  and `12` along range `2` (`10..20`).
///     // In total taking `10*11*12=1320` samples.
///     [10,11,12],
/// );
/// assert_eq!(simple_function(&best, None), 15.);
/// ```
/// Due to specific design the `threads` parameter is excluded for now.
#[macro_export]
macro_rules! grid_search {
    (
        // Generic
        ($($x:expr),*),
        $f: expr,
        $evaluation_data: expr,
        $polling: expr,
        $threads: expr,
        // Specific
        $points: expr,
    ) => {
        {
            use num::ToPrimitive;
            let mut ranges = [
                $(
                    $x.start.to_f64().unwrap()..$x.end.to_f64().unwrap(),
                )*
            ];
            simple_optimization::grid_search(
                ranges,
                $f,
                $evaluation_data,
                $polling,
                $threads,
                $points,
            )
        }
    };
}

/// [Grid search](https://en.wikipedia.org/wiki/Hyperparameter_optimization#Grid_search)
///
/// Evaluate all combinations of values from the 3 values where:
/// - Value 1 covers `10` values at equal intervals from `0..10` (`0,1,2,3,4,5,6,7,8,9`).
/// - Value 2 covers `11` values at equal intervals from `5..15`.
/// - Value 3 covers `12` values at equal intervals from `10..20`.
///
/// Printing progress every `10ms` and exiting early if a value is found which is less than or equal to `15.`.
/// ```
/// use std::{sync::Arc,time::Duration};
/// use simple_optimization::{grid_search, Polling};
/// fn simple_function(list: &[f64; 3], _: Option<Arc<()>>) -> f64 { list.iter().sum() }
/// let best = grid_search(
///     [0f64..10f64, 5f64..15f64, 10f64..20f64], // Value ranges.
///     simple_function, // Evaluation function.
///     None, //  No additional evaluation data.
///     // Polling every `10ms`, printing progress (`true`), exiting early if `15.` or less is reached, and not printing thread execution data (`false`).
///     Some(Polling { poll_rate: Duration::from_millis(5), printing: true, early_exit_minimum: Some(15.), thread_execution_reporting: false }),
///     None, // We don't specify the number of threads.
///     // Take `10` samples along range `0` (`0..10`), `11` along range `1` (`5..15`)
///     //  and `12` along range `2` (`10..20`).
///     // In total taking `10*11*12=1320` samples.
///     [10,11,12],
/// );
/// assert_eq!(simple_function(&best, None), 15.);
/// ```
/// Due to specific design the `threads` parameter is excluded for now.
pub fn grid_search<
    A: 'static + Send + Sync,
    T: 'static
        + Copy
        + Send
        + Sync
        + Default
        + fmt::Debug
        + SampleUniform
        + PartialOrd
        + AddAssign
        + Add<Output = T>
        + Sub<Output = T>
        + Div<Output = T>
        + Mul<Output = T>
        + num::FromPrimitive,
    const N: usize,
>(
    // Generics
    ranges: [Range<T>; N],
    f: fn(&[T; N], Option<Arc<A>>) -> f64,
    evaluation_data: Option<Arc<A>>,
    polling: Option<Polling>,
    threads: Option<usize>,
    // Specifics
    points: [u64; N],
) -> [T; N] {
    // Gets cpu number
    let cpus = crate::cpus!(threads);
    // 1 cpu is used for polling (this one), so we have -1 cpus for searching.
    let search_cpus = cpus - 1;
    // Computes points per thread
    let mut remainder = [Default::default(); N];
    let mut per = [Default::default(); N];
    for i in 0..N {
        remainder[i] = points[i] % search_cpus as u64;
        per[i] = std::cmp::max(points[i] / search_cpus as u64, 1);
    }

    // println!("remainder: {:?}, per: {:?}",remainder,per);

    // Points ranges for remainder
    // ---------------------------------------
    let remainder_ranges: [Range<u64>; N] = remainder
        .iter()
        .map(|&r| 0..r)
        .collect::<Vec<_>>()
        .try_into()
        .unwrap();
    // Points ranges per thread
    // ---------------------------------------
    // If at any point the threads need to evaluate more than 1 value.
    let some_thread_work = per.iter().any(|&x| x > 1);

    // We effectively fold over our threads for each point range
    let per_ranges_opt: Option<Vec<[Range<u64>; N]>> = some_thread_work.then(|| {
        let mut offset = [Default::default(); N];
        // Initial offset is after remainder
        for i in 0..N {
            offset[i] = remainder_ranges[i].end;
        }

        (0..search_cpus)
            .map(|_| {
                (0..N)
                    .map(|i| {
                        let new = offset[i]..offset[i] + per[i];
                        offset[i] = new.end;
                        new
                    })
                    .collect::<Vec<_>>()
                    .try_into()
                    .unwrap()
            })
            .collect::<Vec<_>>()
    });

    // println!("remainder_ranges: {:?}, per_ranges_opt: {:?}",remainder_ranges,per_ranges_opt);

    // Checks ranges
    // ---------------------------------------
    // Number of evaluations all the threads do.
    let mut iterations = 0;
    // Number of evaluations the remainder does.
    let mut remainder = 0;
    for i in 0..N {
        // Gets points covered by remainder
        let remainder_point_sum = remainder_ranges[i].end - remainder_ranges[i].start;
        remainder += remainder_point_sum;
        // Gets points covered by threads
        let point_sum = per_ranges_opt.as_ref().map_or(0, |per_ranges| {
            per_ranges
                .iter()
                .fold(0, |acc, x| acc + x[i].end - x[i].start)
        });
        iterations += point_sum;
        // Checks sum
        assert_eq!(
            remainder_point_sum + point_sum,
            points[i],
            "remainder: {:?}, threads: {:?}",
            remainder_ranges,
            per_ranges_opt
        );
    }

    // Compute step sizes
    // ---------------------------------------
    let mut steps = [Default::default(); N];
    for (r, k, s) in izip!(ranges.iter(), points.iter(), steps.iter_mut()) {
        *s = (r.end - r.start) / T::from_u64(*k).unwrap();
    }

    // Covers remainder section
    // ---------------------------------------
    let ranges_arc = Arc::new(ranges);
    let (best_value, mut best_params) = search(
        // Generics
        ranges_arc.clone(),
        f,
        evaluation_data.clone(),
        // Since we are doing this on the same thread, we don't need to use these
        Arc::new(AtomicU64::new(Default::default())),
        Arc::new(Mutex::new(Default::default())),
        Arc::new(AtomicBool::new(false)),
        Arc::new(AtomicU8::new(0)),
        Arc::new([]),
        // Specifics
        remainder_ranges,
        steps,
    );

    // println!("completed remainder: {}",best_value);

    // Threads
    // ---------------------------------------

    if let Some(per_ranges) = per_ranges_opt {
        let thread_exit = Arc::new(AtomicBool::new(false));
        let (handles, links): (Vec<_>, Vec<_>) = (0..search_cpus)
            .zip(per_ranges.into_iter())
            .map(|(_, per_ranges)| {
                let ranges_clone = ranges_arc.clone();
                let counter = Arc::new(AtomicU64::new(0));
                let thread_best = Arc::new(Mutex::new(f64::MAX));
                let thread_execution_position = Arc::new(AtomicU8::new(0));
                let thread_execution_time = Arc::new([]);

                let counter_clone = counter.clone();
                let thread_best_clone = thread_best.clone();
                let thread_exit_clone = thread_exit.clone();
                let evaluation_data_clone = evaluation_data.clone();
                let thread_execution_position_clone = thread_execution_position.clone();
                let thread_execution_time_clone = thread_execution_time.clone();
                (
                    thread::spawn(move || {
                        search(
                            // Generics
                            ranges_clone,
                            f,
                            evaluation_data_clone,
                            counter_clone,
                            thread_best_clone,
                            thread_exit_clone,
                            thread_execution_position_clone,
                            thread_execution_time_clone,
                            // Specifics
                            per_ranges,
                            steps,
                        )
                    }),
                    (
                        counter,
                        (
                            thread_best,
                            (thread_execution_position, thread_execution_time),
                        ),
                    ),
                )
            })
            .unzip();
        let (counters, links): (Vec<Arc<AtomicU64>>, Vec<_>) = links.into_iter().unzip();
        let (thread_bests, links): (Vec<Arc<Mutex<f64>>>, Vec<_>) = links.into_iter().unzip();
        let (thread_execution_positions, thread_execution_times) = links.into_iter().unzip();
        if let Some(poll_data) = polling {
            poll(
                poll_data,
                counters,
                remainder,
                iterations,
                thread_bests,
                thread_exit,
                thread_execution_positions,
                thread_execution_times,
            );
        }

        // Joins all handles and folds across extracting the best value and best points.
        let (new_best_value, new_best_params) = handles
            .into_iter()
            .map(|h| h.join().unwrap())
            .fold((best_value, best_params), |(bv, bp), (v, p)| {
                if v < bv {
                    (v, p)
                } else {
                    (bv, bp)
                }
            });
        // If the best value from threads is better than the value from remainder
        if new_best_value < best_value {
            best_params = new_best_params
        }
    }

    return best_params;

    fn search<
        A: 'static + Send + Sync,
        T: 'static
            + Copy
            + Send
            + Sync
            + Default
            + fmt::Debug
            + SampleUniform
            + PartialOrd
            + AddAssign
            + Add<Output = T>
            + Sub<Output = T>
            + Div<Output = T>
            + Mul<Output = T>
            + num::FromPrimitive,
        const N: usize,
    >(
        // Generics
        ranges: Arc<[Range<T>; N]>,
        f: fn(&[T; N], Option<Arc<A>>) -> f64,
        evaluation_data: Option<Arc<A>>,
        counter: Arc<AtomicU64>,
        best: Arc<Mutex<f64>>,
        thread_exit: Arc<AtomicBool>,
        _thread_execution_position: Arc<AtomicU8>,
        _thread_execution_times: Arc<[Mutex<(Duration, u64)>; 0]>,
        // Specifics
        point_ranges: [Range<u64>; N],
        steps: [T; N],
    ) -> (f64, [T; N]) {
        let (mut best_value, mut best_points) = (f64::MAX, [Default::default(); N]);

        let mut start_point = [Default::default(); N];
        for i in 0..N {
            start_point[i] = ranges[i].start;
        }
        // println!("start_point: {:?}",start_point);

        for cartesian_product in point_ranges
            .iter()
            .map(|r| r.clone())
            .multi_cartesian_product()
        {
            // Gets new point
            let mut point = start_point;

            // print!("[");
            for i in 0..N {
                // print!("{:?}*{:?}=",steps[i],T::from_u64(cartesian_product[i]).unwrap());
                point[i] += steps[i] * T::from_u64(cartesian_product[i]).unwrap();
                // print!("{:?},",point[i]);
            }
            // println!("] = {:?}",point);
            let new = f(&point, evaluation_data.clone());
            // println!("{:?} -> {:?}",point,new);
            if new < best_value {
                best_value = new;
                best_points = point;
                *best.lock().unwrap() = best_value;
            }
            counter.fetch_add(1, Ordering::SeqCst);
            // Checks early exit
            if thread_exit.load(Ordering::SeqCst) {
                break;
            }
        }

        (best_value, best_points)
    }
}