-
Notifications
You must be signed in to change notification settings - Fork 3
/
tpe-solver.rs
168 lines (157 loc) · 6.03 KB
/
tpe-solver.rs
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
//! A TPE based solver implementation for the [`kurobako`] benchmark.
//!
//! [`kurobako`]: https://github.com/sile/kurobako
use anyhow::anyhow;
use kurobako_core::domain::{self, Variable};
use kurobako_core::epi::channel::{MessageReceiver, MessageSender};
use kurobako_core::epi::solver::SolverMessage;
use kurobako_core::problem::ProblemSpec;
use kurobako_core::rng::ArcRng;
use kurobako_core::solver::{Capability, SolverSpecBuilder};
use kurobako_core::trial::{IdGen, NextTrial, Params, TrialId};
use std::collections::HashMap;
#[derive(Debug)]
struct Solver {
problem: ProblemSpec,
optimizers: Vec<tpe::TpeOptimizer>,
evaluating: HashMap<TrialId, Vec<f64>>,
rng: ArcRng,
}
impl Solver {
fn new(problem: ProblemSpec, seed: u64) -> anyhow::Result<Self> {
let optimizers = problem
.params_domain
.variables()
.iter()
.map(Self::create_optimizer)
.collect::<anyhow::Result<Vec<_>>>()?;
Ok(Self {
problem,
optimizers,
evaluating: HashMap::new(),
rng: ArcRng::new(seed),
})
}
fn create_optimizer(param: &Variable) -> anyhow::Result<tpe::TpeOptimizer> {
match param.range() {
domain::Range::Continuous { low, high } => match param.distribution() {
domain::Distribution::Uniform => Ok(tpe::TpeOptimizer::new(
tpe::parzen_estimator(),
tpe::range(*low, *high)?,
)),
domain::Distribution::LogUniform => Ok(tpe::TpeOptimizer::new(
tpe::parzen_estimator(),
tpe::range(low.ln(), high.ln())?,
)),
},
domain::Range::Discrete { low, high } => Ok(tpe::TpeOptimizer::new(
tpe::parzen_estimator(),
tpe::range(*low as f64, *high as f64)?,
)),
domain::Range::Categorical { choices } => Ok(tpe::TpeOptimizer::new(
tpe::histogram_estimator(),
tpe::categorical_range(choices.len())?,
)),
}
}
fn unwarp(&self, params: &[f64]) -> Vec<f64> {
self.problem
.params_domain
.variables()
.iter()
.zip(params.iter().copied())
.map(|(p, v)| match p.range() {
domain::Range::Continuous { .. } => match p.distribution() {
domain::Distribution::Uniform => v,
domain::Distribution::LogUniform => v.exp(),
},
domain::Range::Discrete { .. } => v.floor(),
domain::Range::Categorical { .. } => v,
})
.collect()
}
fn warp(&self, params: &[f64]) -> Vec<f64> {
self.problem
.params_domain
.variables()
.iter()
.zip(params.iter().copied())
.map(|(p, v)| match p.range() {
domain::Range::Continuous { .. } => match p.distribution() {
domain::Distribution::Uniform => v,
domain::Distribution::LogUniform => v.ln(),
},
domain::Range::Discrete { .. } => v 0.5,
domain::Range::Categorical { .. } => v,
})
.collect()
}
}
fn main() -> anyhow::Result<()> {
let stdout = std::io::stdout();
let stdin = std::io::stdin();
let mut tx = MessageSender::new(stdout.lock());
let mut rx = MessageReceiver::<SolverMessage, _>::new(stdin.lock());
let spec = SolverSpecBuilder::new("TPE")
.capable(Capability::Categorical)
.capable(Capability::Concurrent)
.capable(Capability::LogUniformContinuous)
.capable(Capability::UniformContinuous)
.capable(Capability::UniformDiscrete)
.finish();
tx.send(&SolverMessage::SolverSpecCast { spec })?;
let mut solvers = HashMap::new();
loop {
match rx.recv()? {
SolverMessage::CreateSolverCast {
solver_id,
random_seed,
problem,
} => {
let opt = Solver::new(problem, random_seed)?;
solvers.insert(solver_id, opt);
}
SolverMessage::AskCall {
solver_id,
next_trial_id,
} => {
let solver = solvers
.get_mut(&solver_id)
.ok_or_else(|| anyhow!("unknown solver {:?}", solver_id))?;
let rng = &mut solver.rng;
let params = solver
.optimizers
.iter_mut()
.map(|o| o.ask(rng).map_err(anyhow::Error::from))
.collect::<anyhow::Result<Vec<_>>>()?;
let params = solver.unwarp(¶ms);
let mut idg = IdGen::from_next_id(next_trial_id);
let trial = NextTrial {
id: idg.generate(),
params: Params::new(params.clone()),
next_step: Some(solver.problem.steps.last()),
};
solver.evaluating.insert(trial.id, params);
tx.send(&SolverMessage::AskReply {
next_trial_id: idg.peek_id().get(),
trial,
})?;
}
SolverMessage::TellCall { solver_id, trial } => {
let solver = solvers
.get_mut(&solver_id)
.ok_or_else(|| anyhow!("unknown solver {:?}", solver_id))?;
let params = solver.evaluating.remove(&trial.id).expect("unreachable");
let params = solver.warp(¶ms);
for (o, p) in solver.optimizers.iter_mut().zip(params.into_iter()) {
o.tell(p, trial.values[0])?;
}
tx.send(&SolverMessage::TellReply {})?;
}
SolverMessage::DropSolverCast { solver_id } => {
solvers.remove(&solver_id);
}
other => panic!("unexpected message: {:?}", other),
}
}
}