Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. // aisearch.rs
  2.  
  3. use crate::list::{create, ListElem};
  4. use std::cmp::Ordering;
  5.  
  6. pub trait SearchProblem<STATE, ACTION, COST> {
  7. fn initial_state(&self) -> STATE;
  8. fn get_actions(&self, state: &STATE) -> Vec<ACTION>;
  9. fn do_action(&self, state: &STATE, action: &ACTION) -> (STATE, COST);
  10. fn is_goal(&self, state: &STATE) -> bool;
  11. fn heurestic(&self, state: &STATE) -> COST;
  12. }
  13.  
  14. #[derive(Eq,Ord)]
  15. struct SearchElem<'a,C: std::cmp::Ord,S> ((C,C),&'a Vec<S>);
  16.  
  17. impl<'a,C : Ord, S> PartialEq for SearchElem<'a,C,S> {
  18. fn eq(&self, other: &SearchElem<'a,C,S>) -> bool {
  19. self.0 == other.0
  20. }
  21. }
  22.  
  23. impl<'a,C : Ord, S> PartialOrd for SearchElem<'a,C,S> {
  24. fn partial_cmp(&self, other: &SearchElem<'a,C,S>) -> Option<Ordering> {
  25. self.0.partial_cmp(&other.0)
  26. }
  27. }
  28.  
  29. struct Searcher<'a, S, A, C:Ord> {
  30. on_goal: Option<Vec<S>>,
  31. Problem: &'a dyn SearchProblem<S, A, C>,
  32. PQueue: ListElem<SearchElem<'a,C,S>> // [((Cost+Heur),Cost),[State0, State1, ..]]
  33. }
  34.  
  35. impl<'a, S: Copy, A, C: Default + PartialOrd + std::ops::Add+Ord> Searcher<'a, S, A, C> {
  36. fn reset(self) -> Searcher<'a, S, A, C> {
  37. Searcher {
  38. PQueue: create( SearchElem (
  39. (C::default(), C::default()),
  40. &vec![self.Problem.initial_state()],
  41. )),
  42. ..self
  43. }
  44. }
  45. fn next(self) -> Option<Searcher<'a, S,A,C>> {
  46. let top = match self.PQueue.pop() {
  47. Err(_) => {return None},
  48. Ok(x) => x
  49. };
  50. match top {
  51. (mut rest, SearchElem ((costH, cost), path)) => {
  52. let this_state = &path[0];
  53. if self.Problem.is_goal(this_state) {
  54. Some(Searcher {
  55. on_goal: Some(*path),
  56. ..self})} else {
  57. let acts = self.Problem.get_actions(this_state);
  58. for act in acts.iter() {
  59. let (state2, costStep) = self.Problem.do_action(this_state, act);
  60. let rest = rest.add_ord(SearchElem ((C::default(), C::default()), path));
  61.  
  62. };
  63. None
  64. }
  65. }
  66. }
  67. }
  68. }
  69.  
  70. struct HelloWorldProb {
  71. state: String,
  72. goal: &'static str,
  73. }
  74.  
  75. impl SearchProblem<String, String, u16> for HelloWorldProb {
  76. fn initial_state(&self) -> String {
  77. String::from("")
  78. }
  79. fn get_actions(&self, _state: &String) -> Vec<String> {
  80. let mut out = Vec::new();
  81. for x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".chars() {
  82. out.push(String::from(format!("{}", x)));
  83. }
  84. out
  85. }
  86. fn do_action(&self, state: &String, action: &String) -> (String, u16) {
  87. (String::from(state) + action, 1)
  88. }
  89. fn is_goal(&self, state: &String) -> bool {
  90. state == self.goal
  91. }
  92. fn heurestic(&self, state: &String) -> u16 {
  93. let mut x = 0;
  94. for (a, b) in state.chars().zip(self.goal.chars()) {
  95. x += if a != b { 1 } else { 0 };
  96. }
  97. (x + self.goal.len() - state.len()) as u16
  98. }
  99. }
  100.  
  101. // sortedvec.rs
  102.  
  103. #[derive(Debug)]
  104. pub struct SortedVec<T> {
  105. values: Vec<T>,
  106. }
  107.  
  108. pub fn from_vector<T: PartialOrd>(x: Vec<T>) -> SortedVec<T> {
  109. let mut n = SortedVec { values: Vec::new() };
  110. n.from_vector(x);
  111. n
  112. }
  113.  
  114. impl<T: PartialOrd> SortedVec<T> {
  115. pub fn from_vector(&mut self, mut x: Vec<T>) {
  116. loop {
  117. if let Some(v) = x.pop() {
  118. self.push(v)
  119. } else {
  120. break;
  121. }
  122. }
  123. }
  124.  
  125. pub fn push(&mut self, val: T) {
  126. let mut i = 0;
  127. loop {
  128. if (i == self.values.len()) || (&self.values[i] < &val) {
  129. self.values.insert(i, val);
  130. break;
  131. }
  132. i += 1;
  133. }
  134. }
  135. pub fn pop(&mut self) -> Option<T> {
  136. self.values.pop()
  137. }
  138. }
  139.  
  140. fn main() {
  141. println!("Hello, world!");
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement