Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // aisearch.rs
- use crate::list::{create, ListElem};
- use std::cmp::Ordering;
- pub trait SearchProblem<STATE, ACTION, COST> {
- fn initial_state(&self) -> STATE;
- fn get_actions(&self, state: &STATE) -> Vec<ACTION>;
- fn do_action(&self, state: &STATE, action: &ACTION) -> (STATE, COST);
- fn is_goal(&self, state: &STATE) -> bool;
- fn heurestic(&self, state: &STATE) -> COST;
- }
- #[derive(Eq,Ord)]
- struct SearchElem<'a,C: std::cmp::Ord,S> ((C,C),&'a Vec<S>);
- impl<'a,C : Ord, S> PartialEq for SearchElem<'a,C,S> {
- fn eq(&self, other: &SearchElem<'a,C,S>) -> bool {
- self.0 == other.0
- }
- }
- impl<'a,C : Ord, S> PartialOrd for SearchElem<'a,C,S> {
- fn partial_cmp(&self, other: &SearchElem<'a,C,S>) -> Option<Ordering> {
- self.0.partial_cmp(&other.0)
- }
- }
- struct Searcher<'a, S, A, C:Ord> {
- on_goal: Option<Vec<S>>,
- Problem: &'a dyn SearchProblem<S, A, C>,
- PQueue: ListElem<SearchElem<'a,C,S>> // [((Cost+Heur),Cost),[State0, State1, ..]]
- }
- impl<'a, S: Copy, A, C: Default + PartialOrd + std::ops::Add+Ord> Searcher<'a, S, A, C> {
- fn reset(self) -> Searcher<'a, S, A, C> {
- Searcher {
- PQueue: create( SearchElem (
- (C::default(), C::default()),
- &vec![self.Problem.initial_state()],
- )),
- ..self
- }
- }
- fn next(self) -> Option<Searcher<'a, S,A,C>> {
- let top = match self.PQueue.pop() {
- Err(_) => {return None},
- Ok(x) => x
- };
- match top {
- (mut rest, SearchElem ((costH, cost), path)) => {
- let this_state = &path[0];
- if self.Problem.is_goal(this_state) {
- Some(Searcher {
- on_goal: Some(*path),
- ..self})} else {
- let acts = self.Problem.get_actions(this_state);
- for act in acts.iter() {
- let (state2, costStep) = self.Problem.do_action(this_state, act);
- let rest = rest.add_ord(SearchElem ((C::default(), C::default()), path));
- };
- None
- }
- }
- }
- }
- }
- struct HelloWorldProb {
- state: String,
- goal: &'static str,
- }
- impl SearchProblem<String, String, u16> for HelloWorldProb {
- fn initial_state(&self) -> String {
- String::from("")
- }
- fn get_actions(&self, _state: &String) -> Vec<String> {
- let mut out = Vec::new();
- for x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".chars() {
- out.push(String::from(format!("{}", x)));
- }
- out
- }
- fn do_action(&self, state: &String, action: &String) -> (String, u16) {
- (String::from(state) + action, 1)
- }
- fn is_goal(&self, state: &String) -> bool {
- state == self.goal
- }
- fn heurestic(&self, state: &String) -> u16 {
- let mut x = 0;
- for (a, b) in state.chars().zip(self.goal.chars()) {
- x += if a != b { 1 } else { 0 };
- }
- (x + self.goal.len() - state.len()) as u16
- }
- }
- // sortedvec.rs
- #[derive(Debug)]
- pub struct SortedVec<T> {
- values: Vec<T>,
- }
- pub fn from_vector<T: PartialOrd>(x: Vec<T>) -> SortedVec<T> {
- let mut n = SortedVec { values: Vec::new() };
- n.from_vector(x);
- n
- }
- impl<T: PartialOrd> SortedVec<T> {
- pub fn from_vector(&mut self, mut x: Vec<T>) {
- loop {
- if let Some(v) = x.pop() {
- self.push(v)
- } else {
- break;
- }
- }
- }
- pub fn push(&mut self, val: T) {
- let mut i = 0;
- loop {
- if (i == self.values.len()) || (&self.values[i] < &val) {
- self.values.insert(i, val);
- break;
- }
- i += 1;
- }
- }
- pub fn pop(&mut self) -> Option<T> {
- self.values.pop()
- }
- }
- fn main() {
- println!("Hello, world!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement