Tree

Struct Tree 

Source
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: usize

Implementations§

Source§

impl<S> Tree<S>
where S: ModifiableState,

Source

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}
Source

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    }
Source

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    }
Source

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    }
Source

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}
Source

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}
Source

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§

Source§

impl<S> Debug for Tree<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.