Advertisement
Guest User

pythonVSrust

a guest
Dec 31st, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | Source Code | 0 0
  1. ######### Python Version ##########
  2. def run(nodes, colors, connections, available_colors, entropy, output):
  3. global min_index
  4. global affected_nodes
  5. global FINISHED
  6. global RESTART_FLAG
  7.  
  8. FINISHED = False
  9. RESTART_FLAG = False
  10. affected_nodes = []
  11. min_index = -1
  12.  
  13. def restart_wfc():
  14. global available_colors, entropy, output
  15. available_colors = [[True for _ in range(colors)] for _1 in range(nodes)]
  16. entropy = [colors for _ in range(nodes)]
  17. output = [-1 for _ in range(nodes)]
  18.  
  19.  
  20. #returns the index of the lowest entropy
  21. def find_lowest_entropy():
  22. global colors
  23. global min_index
  24. global FINISHED
  25. global RESTART_FLAG
  26. min_value = colors+1
  27. FINISHED = True
  28. for index, val in enumerate(entropy):
  29. #collapsed nodes
  30. if val == -1:
  31. continue
  32. if val == 0:
  33. RESTART_FLAG = True
  34. restart_wfc()
  35. if(min_value > val):
  36. min_value = val
  37. min_index = index
  38. FINISHED = False
  39.  
  40.  
  41. def observe():
  42. find_lowest_entropy()
  43.  
  44.  
  45. def collapse(index):
  46. global entropy, available_colors, output, affected_nodes
  47. if not(FINISHED):
  48. if(entropy[index] == 0):
  49. raise Exception("Impossible pattern")
  50. entropy[index] = -1
  51. affected_nodes.append(index)
  52. #pick lowest available color
  53. flag = True
  54. color_index = available_colors[index].index(True)
  55. available_colors[index] = [False] * colors
  56. available_colors[index][color_index] = True
  57. output[index] = color_index
  58.  
  59.  
  60. def propogate():
  61. visisted = [False for _ in range(nodes)]
  62. while len(affected_nodes) > 0:
  63. index = affected_nodes.pop()
  64. #find first available color
  65. color_index = available_colors[index].index(True)
  66. #calculate impact
  67. for node_index in range(nodes):
  68. #check if connected and is not collapsed
  69. if connections[index][node_index] and entropy[node_index] != -1:
  70. if(available_colors[node_index][color_index] == True):
  71. available_colors[node_index][color_index] = False
  72. entropy[node_index] -= 1
  73. if entropy[node_index] == 0:
  74. raise Exception("Found error in propogation")
  75. if entropy[node_index] == 1 and not(visisted[node_index]):
  76. visisted[node_index] = True
  77. #collapse
  78. affected_nodes.append(node_index)
  79.  
  80. while not(FINISHED):
  81. RESTART_FLAG = False
  82. observe()
  83. if(min_index == -1):
  84. break
  85. if RESTART_FLAG:
  86. continue
  87. collapse(min_index)
  88. propogate()
  89. return output
  90.  
  91.  
  92. ############# Rust Version #############
  93. #[derive(Debug)]
  94. struct WfcState {
  95. nodes: usize,
  96. colors: usize,
  97. connections: Vec<Vec<bool>>,
  98. available_colors: Vec<Vec<bool>>,
  99. entropy: Vec<usize>,
  100. output: Vec<isize>,
  101. affected_nodes: VecDeque<usize>,
  102. min_index: Option<usize>,
  103. finished: bool,
  104. restart_flag: bool,
  105. }
  106.  
  107. impl WfcState {
  108. fn new(nodes: usize, colors: usize, connections: Vec<Vec<bool>>) -> Self {
  109. Self {
  110. nodes,
  111. colors,
  112. connections,
  113. available_colors: vec![vec![true; colors]; nodes],
  114. entropy: vec![colors; nodes],
  115. output: vec![-1; nodes],
  116. affected_nodes: VecDeque::new(),
  117. min_index: None,
  118. finished: false,
  119. restart_flag: false,
  120. }
  121. }
  122.  
  123. fn restart_wfc(&mut self) {
  124. self.available_colors = vec![vec![true; self.colors]; self.nodes];
  125. self.entropy = vec![self.colors; self.nodes];
  126. self.output = vec![-1; self.nodes];
  127. self.affected_nodes.clear();
  128. self.min_index = None;
  129. self.finished = false;
  130. self.restart_flag = false;
  131. }
  132.  
  133. fn find_lowest_entropy(&mut self) {
  134. let mut min_value = self.colors + 1;
  135. self.finished = true;
  136. self.min_index = None;
  137.  
  138. for (index, &val) in self.entropy.iter().enumerate() {
  139. if val == usize::MAX {
  140. continue; // Collapsed node
  141. }
  142. if val == 0 {
  143. self.restart_flag = true;
  144. self.restart_wfc();
  145. return;
  146. }
  147. if val < min_value {
  148. min_value = val;
  149. self.min_index = Some(index);
  150. self.finished = false;
  151. }
  152. }
  153. }
  154.  
  155. fn collapse(&mut self, index: usize) -> Result<(), String> {
  156. if self.finished {
  157. return Ok(());
  158. }
  159. if self.entropy[index] == 0 {
  160. return Err("Impossible pattern".to_string());
  161. }
  162.  
  163. self.entropy[index] = usize::MAX; // Mark as collapsed
  164. self.affected_nodes.push_back(index);
  165.  
  166. // Pick the lowest available color
  167. let color_index = self
  168. .available_colors[index]
  169. .iter()
  170. .position(|&x| x)
  171. .ok_or_else(|| "No available color".to_string())?;
  172.  
  173. self.available_colors[index] = vec![false; self.colors];
  174. self.available_colors[index][color_index] = true;
  175. self.output[index] = color_index as isize;
  176.  
  177. Ok(())
  178. }
  179.  
  180. fn propagate(&mut self) -> Result<(), String> {
  181. let mut visited = vec![false; self.nodes];
  182.  
  183. while let Some(index) = self.affected_nodes.pop_front() {
  184. let color_index = self
  185. .available_colors[index]
  186. .iter()
  187. .position(|&x| x)
  188. .ok_or_else(|| "No available color during propagation".to_string())?;
  189.  
  190. for node_index in 0..self.nodes {
  191. if self.connections[index][node_index] && self.entropy[node_index] != usize::MAX {
  192. if self.available_colors[node_index][color_index] {
  193. self.available_colors[node_index][color_index] = false;
  194. self.entropy[node_index] -= 1;
  195.  
  196. if self.entropy[node_index] == 0 {
  197. return Err("Propagation error: no valid configuration".to_string());
  198. }
  199. if self.entropy[node_index] == 1 && !visited[node_index] {
  200. visited[node_index] = true;
  201. self.affected_nodes.push_back(node_index);
  202. }
  203. }
  204. }
  205. }
  206. }
  207.  
  208. Ok(())
  209. }
  210.  
  211. fn run(&mut self) -> Result<Vec<isize>, String> {
  212. while !self.finished {
  213. self.restart_flag = false;
  214. self.find_lowest_entropy();
  215.  
  216. if let Some(index) = self.min_index {
  217. if self.restart_flag {
  218. continue;
  219. }
  220. self.collapse(index)?;
  221. self.propagate()?;
  222. } else {
  223. break; // No valid index found
  224. }
  225. }
  226. Ok(self.output.clone())
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement