Skip to main content

use_optimization/
lib.rs

1#![forbid(unsafe_code)]
2//! Thin facade for the `use-optimization` workspace.
3//!
4//! The crate reexports the focused optimization crates directly so consumers can
5//! opt into one dependency while still using the smaller APIs.
6//!
7//! # Examples
8//!
9//! ```rust
10//! use use_optimization::*;
11//!
12//! let direction = ObjectiveDirection::Maximize;
13//! let result = grid_search_1d(&[0.0, 1.0, 2.0, 3.0], |x| -(x - 2.0) * (x - 2.0), direction)
14//!     .expect("grid search should find a best result");
15//!
16//! assert_eq!(result.best_value, 2.0);
17//! assert_eq!(best_value(&[1.0, 3.0, 2.0], direction), Some(3.0));
18//! assert!(Bounds { min: Some(0.0), max: Some(10.0) }.contains(4.0));
19//! ```
20
21pub use use_grid_search;
22pub use use_grid_search::*;
23pub use use_local_search;
24pub use use_local_search::*;
25pub use use_loss;
26pub use use_loss::*;
27pub use use_objective;
28pub use use_objective::*;
29pub use use_optimization_constraint;
30pub use use_optimization_constraint::*;
31pub use use_score;
32pub use use_score::*;
33pub use use_search_space;
34pub use use_search_space::*;
35
36#[cfg(test)]
37mod tests {
38    use super::{
39        absolute_error, best_value, grid_search_1d, local_search_1d, normalize_min_max, Bounds,
40        LocalSearchConfig, ObjectiveDirection, RangeSpace,
41    };
42
43    #[test]
44    fn facade_reexports_workspace_apis() {
45        assert_eq!(
46            best_value(&[2.0, 6.0, 4.0], ObjectiveDirection::Maximize),
47            Some(6.0)
48        );
49        assert!(Bounds {
50            min: Some(0.0),
51            max: Some(5.0)
52        }
53        .contains(3.0));
54        assert_eq!(absolute_error(3.0, 1.5), 1.5);
55        assert_eq!(
56            normalize_min_max(&[2.0, 4.0, 6.0]),
57            Some(vec![0.0, 0.5, 1.0])
58        );
59        assert_eq!(
60            RangeSpace {
61                start: 0.0,
62                end: 2.0,
63                step: 1.0
64            }
65            .values()
66            .unwrap(),
67            vec![0.0, 1.0, 2.0]
68        );
69
70        let grid = grid_search_1d(
71            &[0.0, 1.0, 2.0, 3.0],
72            |value| -(value - 2.0) * (value - 2.0),
73            ObjectiveDirection::Maximize,
74        )
75        .expect("grid search should succeed");
76        assert_eq!(grid.best_value, 2.0);
77
78        let local = local_search_1d(
79            LocalSearchConfig {
80                initial_value: 0.0,
81                step_size: 1.0,
82                max_iterations: 8,
83                direction: ObjectiveDirection::Maximize,
84            },
85            |value| -(value - 2.0) * (value - 2.0),
86        )
87        .expect("local search should succeed");
88        assert_eq!(local.best_value, 2.0);
89    }
90}