1pub use hyperopt_core::{
28 Direction, Distribution, HyperoptError, ObjectiveError, ObjectiveResult, ParamRecord, Pruner,
29 Sampler, Storage, StorageError, Study, StudyMetadata, StudyState, Trial, TrialContext,
30 TrialState, Value,
31};
32pub use hyperopt_pruners::{MedianPruner, NopPruner, SuccessiveHalvingPruner};
33pub use hyperopt_samplers::{BoundHandling, CmaEsSampler, GridSampler, RandomSampler, TpeSampler};
34pub use hyperopt_storage::InMemoryStorage;
35#[cfg(feature = "sqlite")]
36pub use hyperopt_storage::SqliteStorage;
37
38pub struct StudyBuilder {
43 name: String,
44 direction: Direction,
45 sampler: Option<Box<dyn Sampler>>,
46 pruner: Option<Box<dyn Pruner>>,
47 storage: Option<Box<dyn Storage>>,
48}
49
50impl StudyBuilder {
51 pub fn new(name: impl Into<String>) -> Self {
53 StudyBuilder {
54 name: name.into(),
55 direction: Direction::Minimize,
56 sampler: None,
57 pruner: None,
58 storage: None,
59 }
60 }
61
62 pub fn direction(mut self, direction: Direction) -> Self {
64 self.direction = direction;
65 self
66 }
67
68 pub fn sampler(mut self, sampler: impl Sampler + 'static) -> Self {
70 self.sampler = Some(Box::new(sampler));
71 self
72 }
73
74 pub fn pruner(mut self, pruner: impl Pruner + 'static) -> Self {
76 self.pruner = Some(Box::new(pruner));
77 self
78 }
79
80 pub fn storage(mut self, storage: impl Storage + 'static) -> Self {
82 self.storage = Some(Box::new(storage));
83 self
84 }
85
86 pub fn build(self) -> Result<Study, HyperoptError> {
88 let sampler = self
89 .sampler
90 .unwrap_or_else(|| Box::new(RandomSampler::new()));
91 let pruner = self.pruner.unwrap_or_else(|| Box::new(NopPruner::new()));
92 let storage = self
93 .storage
94 .unwrap_or_else(|| Box::new(InMemoryStorage::new()));
95 Study::new(self.name, self.direction, sampler, pruner, storage)
96 }
97}
98
99pub mod prelude {
102 pub use crate::StudyBuilder;
103 pub use hyperopt_core::{
104 Direction, Distribution, HyperoptError, ObjectiveError, ObjectiveResult, Pruner, Sampler,
105 Storage, Study, Trial, TrialContext, TrialState, Value,
106 };
107 pub use hyperopt_pruners::{MedianPruner, NopPruner, SuccessiveHalvingPruner};
108 pub use hyperopt_samplers::{
109 BoundHandling, CmaEsSampler, GridSampler, RandomSampler, TpeSampler,
110 };
111 pub use hyperopt_storage::InMemoryStorage;
112 #[cfg(feature = "sqlite")]
113 pub use hyperopt_storage::SqliteStorage;
114}