rubikscube

college code for finding optimal rubiks cube solutions in java

git clone https://9o.is/git/rubikscube.git

commit a2ce3fc4d70dd222ba3a8208ae65e6109ab3065c
parent 0117d5af7a98c6bf14b9987fbe087f3c3ce3835e
Author: Jul <jul@9o.is>
Date:   Sun, 21 Oct 2012 16:30:38 -0400

Fixed AttainableRubiksState clas and got rid of Helper. md5 not needed.

Diffstat:
Asrc/AttainableRubiksState.java | 40++++++++++++++++++++++++++++++++++++++++
Dsrc/Helper.java | 28----------------------------
Msrc/KorfHeuristic.java | 34++++++++++++++++++----------------
Msrc/PatternTable.java | 6+++---
Dsrc/SavedRubiksState.java | 31-------------------------------
5 files changed, 61 insertions(+), 78 deletions(-)

diff --git a/src/AttainableRubiksState.java b/src/AttainableRubiksState.java @@ -0,0 +1,40 @@ +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Just a helper class. + */ +public class AttainableRubiksState { + + /* The key is the state of this AttainableRubiksState. */ + private String key; + + /* The heuristic of this AttainableRubiksState. */ + private short heuristic; + + /** + * An AttainableRubiksState must have a state as a key and a heuristic + * @param key the state + * @param heuristic the heuristic (or moves away from the goal state). + */ + public AttainableRubiksState(String key, short heuristic) { + this.key = key; + this.heuristic = heuristic; + } + + /** + * Retrieve state. + * @return The key of AttainableRubiksState. + */ + public String getKey() { + return key; + } + + /** + * Retrieve heuristic. + * @return The heuristic of AttainableRubiksState. + */ + public short getHeuristic() { + return heuristic; + } +} diff --git a/src/Helper.java b/src/Helper.java @@ -1,28 +0,0 @@ -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * Created with IntelliJ IDEA. - * User: julio - * Date: 10/18/12 - * Time: 7:46 AM - * To change this template use File | Settings | File Templates. - */ -public class Helper { - /** Encodes a string with md5. */ - public static String encodeMD5(String string) { - try { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] mdbytes = md.digest(string.getBytes()); - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < mdbytes.length; i++) { - sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); - } - - return sb.toString(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - return ""; - } - } -} diff --git a/src/KorfHeuristic.java b/src/KorfHeuristic.java @@ -7,9 +7,9 @@ import java.util.Queue; */ public class KorfHeuristic { - private SavedRubiksState[] corners = new SavedRubiksState[44089920]; - private SavedRubiksState[] halfEdges = new SavedRubiksState[15360]; - private SavedRubiksState[] half2Edges = new SavedRubiksState[15360]; + private AttainableRubiksState[] corners = new AttainableRubiksState[44089920]; + private AttainableRubiksState[] halfEdges = new AttainableRubiksState[15360]; + private AttainableRubiksState[] half2Edges = new AttainableRubiksState[15360]; private PatternTable cornersTable; private PatternTable halfEdgesTable; @@ -38,7 +38,7 @@ public class KorfHeuristic { public void generateTables() { Queue<RubiksState> travq = new LinkedList<RubiksState>(); travq.add(new RubiksCube().getState()); - int depth = 1; + short depth = 1; RubiksState current; while(!travq.isEmpty()) { current = travq.poll(); @@ -56,7 +56,7 @@ public class KorfHeuristic { // queue a state if its unique or it's not saved already private void queueChildState(Queue<RubiksState> travq, RubiksState state) { for(int i=0; i<corners.length; i++) { - if(Helper.encodeMD5(state.getState()).equals(corners[i].getKey())) + if(state.getState().equals(corners[i].getKey())) return; } travq.add(state); @@ -74,22 +74,24 @@ public class KorfHeuristic { // check if maxLimit depth has been passed } - public void saveState(RubiksState state, int depth) { + public void saveState(RubiksState state, short depth) { //corners - SavedRubiksState saved = - new SavedRubiksState(state.getState(),state.getCorners(), depth); - corners[corners.length-1] = saved; - cornersTable.updateTable(saved); + AttainableRubiksState attainable = + new AttainableRubiksState(state.getCorners(), depth); + corners[corners.length-1] = attainable; + cornersTable.updateTable(attainable); //half edges - saved.setState(state.getHalfEdges()); - halfEdges[halfEdges.length-1] = saved; - halfEdgesTable.updateTable(saved); + AttainableRubiksState attainable1 = + new AttainableRubiksState(state.getHalfEdges(), depth); + halfEdges[halfEdges.length-1] = attainable1; + halfEdgesTable.updateTable(attainable1); //second half edges - saved.setState(state.getHalf2Edges()); - half2Edges[half2Edges.length-1] = saved; - half2EdgesTable.updateTable(saved); + AttainableRubiksState attainable2 = + new AttainableRubiksState(state.getHalf2Edges(), depth); + half2Edges[half2Edges.length-1] = attainable2; + half2EdgesTable.updateTable(attainable2); } public PatternTable getCornersTable() { diff --git a/src/PatternTable.java b/src/PatternTable.java @@ -12,12 +12,12 @@ public class PatternTable { this.file = file; } - public void updateTable(SavedRubiksState saved) { + public void updateTable(AttainableRubiksState attainable) { try { BufferedWriter out = new BufferedWriter(new FileWriter(file,true)); - out.write(saved.getKey()+":"+saved.getState()+":"+ - Integer.toString(saved.getHeuristic())+"\n"); + out.write(attainable.getKey()+":"+ + Short.toString(attainable.getHeuristic())+"\n"); out.close(); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); diff --git a/src/SavedRubiksState.java b/src/SavedRubiksState.java @@ -1,31 +0,0 @@ -/** - * Just a helper class. - */ -public class SavedRubiksState { - - private String key; - private String state; - private int heuristic; - - public SavedRubiksState(String key, String state, int heuristic) { - this.key = Helper.encodeMD5(key); - this.state = state; - this.heuristic = heuristic; - } - - public void setState(String state) { - this.state = state; - } - - public String getKey() { - return key; - } - - public String getState() { - return state; - } - - public int getHeuristic() { - return heuristic; - } -}