Advertisement
Guest User

Sokoban_Brute_Solver_2.java

a guest
May 17th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. //  Sokoban brute solver.
  2. //  2021, May, B@R5uk
  3. //  Discussion: https://dxdy.ru/topic144781.html
  4. //          Solver looks for an optimal solution using one of two modes:
  5. //              1) move-primary, push-secondary or
  6. //              2) push-primary, move-secondary.
  7. //          (See second argument of method run() in the StateProcessor class).
  8. //          In the process solver counts a total number of optimal different
  9. //          solutions available with selected search mode and chooses one at
  10. //          random with equal probability (as far as "randomness" of pseudo-random
  11. //          generator allows, see State class, selectRandomState() method) as long
  12. //          as the total number of solutions lies within the Integer.MAX_VALUE limit.
  13. //              A solver's algorithm is essentially the Dijkstra's search algorithm
  14. //          on a graph of Sokoban puzzle states that's using one of the two comparators
  15. //          (see State class, MovePriorityComparator and PushPriorityComparator nested
  16. //          classes) for a composite node distances which is a move and push pair.
  17. //          Solver starts with all the final states and expands them backward to
  18. //          avoid as much problems with dead states as possible without, in fact,
  19. //          implementing deadlocks detection (see State class, expandBack() method).
  20. //          Some preprocessing (MapHandler class, findActiveCells() method) and
  21. //          the network graph representation of a puzzle maze (CellsAdjacency class
  22. //          and MapHandler class, getCellsAdjacency() method) also helps to speed up
  23. //          things.
  24. //              If program runs out of memory than enlarging Java VM Heap Size
  25. //          by adding -Xmx2048m key in Run -> Run Configurations... -> Arguments ->
  26. //          VM Arguments (Eclipse) or in the command line can help.
  27.  
  28. import java.util.*;
  29.  
  30. public class Sokoban_Brute_Solver_2 {
  31.    
  32.     private static final long randomSeed = System .currentTimeMillis ();
  33.     private static final Random rng = new Random (randomSeed);
  34.     //private static final Random rng = null;
  35.     private static final MapAccess mapAccess =
  36.             new MapAccess ( 0, "..\\test.txt");
  37.             //new MapAccess ( 5, "..\\Microban.txt");
  38.             //new MapAccess ( 0, "..\\Original.txt");
  39.             //new MapAccess ( 2, "..\\Yoshio52.txt");
  40.             //new MapAccess (13, "..\\YoshioHM.txt");
  41.             //new MapAccess (17, "..\\Nabokosmos.txt");
  42.             //new MapAccess ( 5, "..\\Nabokosmos.txt");
  43.             //new MapAccess (44, "..\\Sokwhole.txt");
  44.             //new MapAccess ( 3, "..\\Yoshio52.txt");
  45.    
  46.     public static void main (String [] args) {
  47.         String []  mapLines;
  48.         MapLoader  mapLoader     = new MapLoader ();
  49.         MapHandler mapHandler    = new MapHandler ();
  50.         StateProcessor processor = new StateProcessor ();
  51.        
  52.         if (!mapLoader .openFile (mapAccess .fileName)) {
  53.             return;
  54.         }
  55.        
  56.         mapLoader .skip (mapAccess .mapIndex);
  57.         mapLoader .loadNext ();
  58.         mapLoader .closeFile ();
  59.        
  60.         if (!mapLoader .hasLines ()) {
  61.             System .out .println ("No map.");
  62.             return;
  63.         }
  64.        
  65.         mapLines = mapLoader .getMapLines ();
  66.         mapAccess .display ();
  67.         MapLoader .displayLines (mapLines);
  68.        
  69.         if (mapHandler .process (mapLines)) {
  70.             System .out .println (mapHandler .getErrorMessage ());
  71.             return;
  72.         }
  73.        
  74.         System .out .println ();
  75.         System .out .println ("________________________");
  76.         System .out .println ("Move-priority solution:");
  77.         if (processor .run (mapHandler, false, rng)) {
  78.             System .out .println ("Search is incomplete.");
  79.         }
  80.         processor .displaySolution ();
  81.        
  82.         System .out .println ();
  83.         System .out .println ("________________________");
  84.         System .out .println ("Push-priority solution:");
  85.         if (processor .run (mapHandler, true, rng)) {
  86.             System .out .println ("Search is incomplete.");
  87.         }
  88.         processor .displaySolution ();
  89.     }
  90.    
  91.     private static class MapAccess {
  92.         public int    mapIndex;
  93.         public String fileName;
  94.        
  95.         public MapAccess (int mapIndex, String fileName) {
  96.             this .mapIndex = mapIndex;
  97.             this .fileName = fileName;
  98.         }
  99.        
  100.         public void display () {
  101.             System .out .println (fileName + "  #" + (mapIndex + 1));
  102.         }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement