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        Bounds, LocalSearchConfig, ObjectiveDirection, RangeSpace, absolute_error, best_value,
40        grid_search_1d, local_search_1d, normalize_min_max,
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!(
50            Bounds {
51                min: Some(0.0),
52                max: Some(5.0)
53            }
54            .contains(3.0)
55        );
56        assert_eq!(absolute_error(3.0, 1.5), 1.5);
57        assert_eq!(
58            normalize_min_max(&[2.0, 4.0, 6.0]),
59            Some(vec![0.0, 0.5, 1.0])
60        );
61        assert_eq!(
62            RangeSpace {
63                start: 0.0,
64                end: 2.0,
65                step: 1.0
66            }
67            .values()
68            .unwrap(),
69            vec![0.0, 1.0, 2.0]
70        );
71
72        let grid = grid_search_1d(
73            &[0.0, 1.0, 2.0, 3.0],
74            |value| -(value - 2.0) * (value - 2.0),
75            ObjectiveDirection::Maximize,
76        )
77        .expect("grid search should succeed");
78        assert_eq!(grid.best_value, 2.0);
79
80        let local = local_search_1d(
81            LocalSearchConfig {
82                initial_value: 0.0,
83                step_size: 1.0,
84                max_iterations: 8,
85                direction: ObjectiveDirection::Maximize,
86            },
87            |value| -(value - 2.0) * (value - 2.0),
88        )
89        .expect("local search should succeed");
90        assert_eq!(local.best_value, 2.0);
91    }
92}