pub struct Tree<S>where
S: ModifiableState,{
state: S,
latest: usize,
nodes: Vec<Node<S>>,
cur_node: usize,
}Fields§
§state: S§latest: usize§nodes: Vec<Node<S>>§cur_node: usizeImplementations§
Source§impl<S> Tree<S>where
S: ModifiableState,
impl<S> Tree<S>where
S: ModifiableState,
Sourcepub fn new(state: S, init_op: S::Operation) -> Self
pub fn new(state: S, init_op: S::Operation) -> Self
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 276)
267pub fn beam_search<S>(
268 state: S,
269 init_op: S::Operation,
270 beam_weidth: usize,
271 minimize: bool,
272) -> Option<(S::Score, Vec<S::Operation>)>
273where
274 S: ModifiableState,
275{
276 let mut tree = Tree::new(state, init_op);
277 let mut cands = vec![];
278 let mut set = HashSet::<S::Hash>::default();
279 loop {
280 tree.dfs(&mut cands, &set, true);
281 if let Some(res) = tree.take_best(&cands, minimize) {
282 return Some(res);
283 }
284 if cands.is_empty() {
285 return None;
286 }
287 set.extend(cands.iter().map(|cand| cand.hash.clone()));
288 tree.update(&mut cands, beam_weidth, minimize);
289 }
290}Sourcefn add_node(
&mut self,
op: S::Operation,
parent: usize,
score: S::Score,
hash: S::Hash,
)
fn add_node( &mut self, op: S::Operation, parent: usize, score: S::Score, hash: S::Hash, )
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 189)
171 fn update(&mut self, cands: &mut Vec<Candidate<S>>, beam_weidth: usize, minimize: bool) {
172 if cands.len() > beam_weidth {
173 if minimize {
174 cands.select_nth_unstable_by_key(beam_weidth, |s| s.score.clone());
175 } else {
176 cands.select_nth_unstable_by_key(beam_weidth, |s| Reverse(s.score.clone()));
177 }
178 cands.truncate(beam_weidth);
179 }
180 let len = self.nodes.len();
181 for Candidate {
182 parent,
183 op,
184 score,
185 hash,
186 ..
187 } in cands.drain(..)
188 {
189 self.add_node(op, parent, score, hash);
190 }
191 for i in self.latest..len {
192 if self.nodes[i].child == !0 {
193 self.remove_node(i);
194 }
195 }
196 self.latest = len;
197 }Sourcefn remove_node(&mut self, idx: usize)
fn remove_node(&mut self, idx: usize)
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 193)
171 fn update(&mut self, cands: &mut Vec<Candidate<S>>, beam_weidth: usize, minimize: bool) {
172 if cands.len() > beam_weidth {
173 if minimize {
174 cands.select_nth_unstable_by_key(beam_weidth, |s| s.score.clone());
175 } else {
176 cands.select_nth_unstable_by_key(beam_weidth, |s| Reverse(s.score.clone()));
177 }
178 cands.truncate(beam_weidth);
179 }
180 let len = self.nodes.len();
181 for Candidate {
182 parent,
183 op,
184 score,
185 hash,
186 ..
187 } in cands.drain(..)
188 {
189 self.add_node(op, parent, score, hash);
190 }
191 for i in self.latest..len {
192 if self.nodes[i].child == !0 {
193 self.remove_node(i);
194 }
195 }
196 self.latest = len;
197 }Sourcepub fn operations(&self, idx: usize) -> Vec<S::Operation>
pub fn operations(&self, idx: usize) -> Vec<S::Operation>
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 258)
245 pub fn take_best(
246 &self,
247 cands: &[Candidate<S>],
248 minimize: bool,
249 ) -> Option<(S::Score, Vec<S::Operation>)> {
250 let cands = cands.iter().filter(|cand| cand.accept);
251 if let Some(Candidate {
252 op, parent, score, ..
253 }) = if minimize {
254 cands.min_by_key(|cand| cand.score.clone())
255 } else {
256 cands.max_by_key(|cand| cand.score.clone())
257 } {
258 let mut ret = self.operations(*parent);
259 ret.push(op.clone());
260 Some((score.clone(), ret))
261 } else {
262 None
263 }
264 }Sourcefn update(
&mut self,
cands: &mut Vec<Candidate<S>>,
beam_weidth: usize,
minimize: bool,
)
fn update( &mut self, cands: &mut Vec<Candidate<S>>, beam_weidth: usize, minimize: bool, )
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 288)
267pub fn beam_search<S>(
268 state: S,
269 init_op: S::Operation,
270 beam_weidth: usize,
271 minimize: bool,
272) -> Option<(S::Score, Vec<S::Operation>)>
273where
274 S: ModifiableState,
275{
276 let mut tree = Tree::new(state, init_op);
277 let mut cands = vec![];
278 let mut set = HashSet::<S::Hash>::default();
279 loop {
280 tree.dfs(&mut cands, &set, true);
281 if let Some(res) = tree.take_best(&cands, minimize) {
282 return Some(res);
283 }
284 if cands.is_empty() {
285 return None;
286 }
287 set.extend(cands.iter().map(|cand| cand.hash.clone()));
288 tree.update(&mut cands, beam_weidth, minimize);
289 }
290}Sourcepub fn dfs(
&mut self,
cands: &mut Vec<Candidate<S>>,
set: &HashSet<S::Hash>,
single: bool,
)
pub fn dfs( &mut self, cands: &mut Vec<Candidate<S>>, set: &HashSet<S::Hash>, single: bool, )
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 228)
199 pub fn dfs(&mut self, cands: &mut Vec<Candidate<S>>, set: &HashSet<S::Hash>, single: bool) {
200 let node = &self.nodes[self.cur_node];
201 if node.child == !0 {
202 assert!(node.score == self.state.score());
203 assert!(node.hash == self.state.hash());
204
205 for op in self.state.candidates() {
206 if let Some((score, hash, accept)) =
207 self.state
208 .soft_update(op.clone(), node.score.clone(), node.hash.clone())
209 && !set.contains(&hash)
210 {
211 cands.push(Candidate {
212 parent: self.cur_node,
213 op,
214 score,
215 hash,
216 accept,
217 });
218 };
219 }
220 } else {
221 let node = self.cur_node;
222 let mut child = self.nodes[node].child;
223 let next_single = single & (self.nodes[child].next == !0);
224
225 loop {
226 self.cur_node = child;
227 self.state.update(self.nodes[child].op.clone());
228 self.dfs(cands, set, next_single);
229
230 if !next_single {
231 self.state.revert(self.nodes[child].op.clone());
232 }
233 child = self.nodes[child].next;
234 if child == !0 {
235 break;
236 }
237 }
238
239 if !next_single {
240 self.cur_node = node;
241 }
242 }
243 }
244
245 pub fn take_best(
246 &self,
247 cands: &[Candidate<S>],
248 minimize: bool,
249 ) -> Option<(S::Score, Vec<S::Operation>)> {
250 let cands = cands.iter().filter(|cand| cand.accept);
251 if let Some(Candidate {
252 op, parent, score, ..
253 }) = if minimize {
254 cands.min_by_key(|cand| cand.score.clone())
255 } else {
256 cands.max_by_key(|cand| cand.score.clone())
257 } {
258 let mut ret = self.operations(*parent);
259 ret.push(op.clone());
260 Some((score.clone(), ret))
261 } else {
262 None
263 }
264 }
265}
266
267pub fn beam_search<S>(
268 state: S,
269 init_op: S::Operation,
270 beam_weidth: usize,
271 minimize: bool,
272) -> Option<(S::Score, Vec<S::Operation>)>
273where
274 S: ModifiableState,
275{
276 let mut tree = Tree::new(state, init_op);
277 let mut cands = vec![];
278 let mut set = HashSet::<S::Hash>::default();
279 loop {
280 tree.dfs(&mut cands, &set, true);
281 if let Some(res) = tree.take_best(&cands, minimize) {
282 return Some(res);
283 }
284 if cands.is_empty() {
285 return None;
286 }
287 set.extend(cands.iter().map(|cand| cand.hash.clone()));
288 tree.update(&mut cands, beam_weidth, minimize);
289 }
290}Sourcepub fn take_best(
&self,
cands: &[Candidate<S>],
minimize: bool,
) -> Option<(S::Score, Vec<S::Operation>)>
pub fn take_best( &self, cands: &[Candidate<S>], minimize: bool, ) -> Option<(S::Score, Vec<S::Operation>)>
Examples found in repository?
crates/competitive/src/heuristic/beam_search.rs (line 281)
267pub fn beam_search<S>(
268 state: S,
269 init_op: S::Operation,
270 beam_weidth: usize,
271 minimize: bool,
272) -> Option<(S::Score, Vec<S::Operation>)>
273where
274 S: ModifiableState,
275{
276 let mut tree = Tree::new(state, init_op);
277 let mut cands = vec![];
278 let mut set = HashSet::<S::Hash>::default();
279 loop {
280 tree.dfs(&mut cands, &set, true);
281 if let Some(res) = tree.take_best(&cands, minimize) {
282 return Some(res);
283 }
284 if cands.is_empty() {
285 return None;
286 }
287 set.extend(cands.iter().map(|cand| cand.hash.clone()));
288 tree.update(&mut cands, beam_weidth, minimize);
289 }
290}Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Tree<S>where
S: Freeze,
impl<S> RefUnwindSafe for Tree<S>where
S: RefUnwindSafe,
<S as ModifiableState>::Operation: RefUnwindSafe,
<S as ModifiableState>::Score: RefUnwindSafe,
<S as ModifiableState>::Hash: RefUnwindSafe,
impl<S> Send for Tree<S>where
S: Send,
<S as ModifiableState>::Operation: Send,
<S as ModifiableState>::Score: Send,
<S as ModifiableState>::Hash: Send,
impl<S> Sync for Tree<S>where
S: Sync,
<S as ModifiableState>::Operation: Sync,
<S as ModifiableState>::Score: Sync,
<S as ModifiableState>::Hash: Sync,
impl<S> Unpin for Tree<S>where
S: Unpin,
<S as ModifiableState>::Operation: Unpin,
<S as ModifiableState>::Score: Unpin,
<S as ModifiableState>::Hash: Unpin,
impl<S> UnwindSafe for Tree<S>where
S: UnwindSafe,
<S as ModifiableState>::Operation: UnwindSafe,
<S as ModifiableState>::Score: UnwindSafe,
<S as ModifiableState>::Hash: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more