Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Comparator;
  8. import java.util.Collections;
  9. import java.util.Set;
  10. import java.util.HashSet;
  11.  
  12. class SortbyMht implements Comparator<State>
  13. {
  14.     // Used for sorting in ascending order of
  15.     // roll number
  16.     public int compare(State a, State b)
  17.     {
  18.         return a.mhtDistance - b.mhtDistance;
  19.     }
  20. }
  21.  
  22. class State {
  23.     class Coord {
  24.         public int x;
  25.         public int y;
  26.         Coord(int x,int y) {
  27.             this.x = x;
  28.             this.y = y;
  29.         }
  30.     }
  31.  
  32.     int[] plates = new int[9];
  33.     int mhtDistance;
  34.     Coord[] coords = new Coord[] {
  35.         new Coord(0, 0),
  36.         new Coord(0, 1),
  37.         new Coord(0, 2),
  38.         new Coord(1, 0),
  39.         new Coord(1, 1),
  40.         new Coord(1, 2),
  41.         new Coord(2, 0),
  42.         new Coord(2, 1),
  43.         new Coord(2, 2),
  44.     };
  45.     State(int[] plates) {
  46.         for(int i=0; i < plates.length; i++) {
  47.             this.plates[i] = plates[i];
  48.         }
  49.         this.mhtDistance = calculateMhtDist();
  50.     }
  51.  
  52.     private int calculateMhtDist() {
  53.         int sum = 0;
  54.         for (int i = 0; i < this.plates.length; i++) {
  55.             int x1 = coords[this.plates[i]].x;
  56.             int y1 = coords[this.plates[i]].y;
  57.             int x0 = coords[i].x;
  58.             int y0 = coords[i].y;
  59.             sum += Math.abs(x1-x0) + Math.abs(y1-y0);
  60.         }
  61.         return sum;
  62.     }
  63.  
  64.     public void print() {
  65.         for(int i : this.plates) {
  66.             System.out.print(i + " ");
  67.         }
  68.         System.out.println(this.mhtDistance);
  69.     }
  70. }
  71.  
  72. public class Genetic8Puzzle {
  73.  
  74.     public static int randInt(int min, int max) {
  75.  
  76.         // NOTE: This will (intentionally) not run as written so that folks
  77.         // copy-pasting have to think about how to initialize their
  78.         // Random instance.  Initialization of the Random instance is outside
  79.         // the main scope of the question, but some decent options are to have
  80.         // a field that is initialized once and then re-used as needed or to
  81.         // use ThreadLocalRandom (if using at least Java 1.7).
  82.         //
  83.         // In particular, do NOT do 'Random rand = new Random()' here or you
  84.         // will get not very good / not very random results.
  85.         Random rand = new Random();
  86.    
  87.         // nextInt is normally exclusive of the top value,
  88.         // so add 1 to make it inclusive
  89.         int randomNum = rand.nextInt((max - min) + 1) + min;
  90.    
  91.         return randomNum;
  92.     }
  93.  
  94.     static List<State> population = new ArrayList<>();
  95.     private static int[] goalState = new int[]{0, 1 , 2, 3, 4, 5 ,6 ,7 ,8};
  96.  
  97.     public static void init() {
  98.         try {
  99.             BufferedReader reader = new BufferedReader(new FileReader(
  100.                         "test.txt"));
  101.             String line = reader.readLine();
  102.             int[] plates = new int[9];
  103.             while (line != null) {
  104.                 String[] parts = line.split(" ");
  105.                 for(int i=0; i < parts.length; i++) {
  106.                     plates[i] = Integer.parseInt(parts[i]);
  107.                 }
  108.                 population.add(new State(plates));
  109.                 line = reader.readLine();
  110.             }
  111.             reader.close();
  112.         } catch (IOException e) {
  113.                 e.printStackTrace();
  114.         }
  115.     }
  116.     static State reproduce(State x, State y) {
  117.         int splitter = randInt(0, 8);
  118.         int[] newPlates = new int[9];
  119.         for (int i = 0; i < splitter; i++) {
  120.             newPlates[i] = x.plates[i];
  121.         }
  122.         for (int i = splitter; i < 9; i++) {
  123.             newPlates[i] = y.plates[i];
  124.         }
  125.         return new State(newPlates);
  126.     }
  127.     static State mutate(State child) {
  128.         int i = randInt(0, child.plates.length - 1);
  129.         int j = randInt(0, child.plates.length - 1);
  130.         int temp = child.plates[i];
  131.         child.plates[i] = child.plates[j];
  132.         child.plates[j] = temp;
  133.         return child;
  134.     }
  135.  
  136.     public static State randomSelection(List<State> population) {
  137.         int randNum = randInt(0, Math.min(population.size(), 6));
  138.         return population.get(randNum);
  139.     }
  140.     public static boolean  distinctValues(int[] plates){
  141.         Set<Integer> foundPlates = new HashSet<>();
  142.         for(Integer plate : plates){
  143.             if(foundPlates.contains(plate)){
  144.                 return false;
  145.             }
  146.             foundPlates.add(plate);
  147.         }
  148.         return true;
  149.     }
  150.  
  151.     public static void main(String[] args) {
  152.         init();
  153.         int i = 0;
  154.         while (true) {
  155.             i++;
  156.                 System.out.println("in");
  157.                 // random select x
  158.                 State x = population.get(randInt(0, population.size() - 1));
  159.                 // random select y
  160.                 State y = population.get(randInt(0, population.size() - 1));
  161.                 // create child
  162.                 State child = reproduce(x, y);
  163.                
  164.                 child.print();
  165.                 // check if goal
  166.                 if (child.mhtDistance == 0) {
  167.                     // success
  168.                     break;
  169.                 }
  170.                 //mutate if necessary
  171.                 int randomNumber = randInt(0, 9);
  172.                 if (randomNumber == 1) {
  173.                     child = mutate(child);
  174.                 }
  175.                 //if (distinctValues(child.plates)) {
  176.                     population.add(child);
  177.                     // sort
  178.                     Collections.sort(population, new SortbyMht());
  179.                 //}
  180.            
  181.         }
  182.  
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement