public static void loadRotationTreeFromFile(RubikFileReader p_File, RotationTree p_tree) { RotationLinkedList l_rotationLinkedList = new RotationLinkedList(); while (l_rotationLinkedList.readFromFile(p_File)) { p_tree.addRotationLinkedList(l_rotationLinkedList); } }
static RotationTree loadSearchTree() { RotationLinkedList l_rotationLinkedList = new RotationLinkedList(); RotationTree l_tree = new RotationTree(); RubikFileReader l_fileReader = new RubikFileReader("C:\\DevProj\\RubikSolver\\permRubik.txt"); loadRotationTreeFromFile(l_fileReader, l_tree); loadRotationTreeFromStandard(l_tree, l_rotationLinkedList, 7); l_fileReader.close(); return(l_tree); }
private void findBetterSolution(Solution p_solution, RotationTree p_tree, SolutionManager p_solutionManager, int p_floor) { Rubik l_rubik = new Rubik(); Permutation l_permutation = p_solution.getPermutation().getCopy(); int l_minimumValue = l_permutation.getValue(p_floor); // if (l_minimumValue < 8) // l_minimumValue = 8; l_rubik.setPermutation(l_permutation); searchTree(l_minimumValue - 4, p_tree, l_rubik, p_solutionManager, p_solution, p_floor, 0); }
public Solution solve(Rubik p_rubik, RotationTree p_firstTree, RotationTree p_secondTree, RotationTree p_thirdTree) { int l_numberOfCubicleInPlace; Permutation l_permutation = p_rubik.getPermutation(); SolutionManager l_solutionManager = new SolutionManager(); Solution l_solutionToDev; RotationLinkedList l_rotationLinkedList = new RotationLinkedList(); int l_floor = getTargetFloor(l_permutation); l_numberOfCubicleInPlace = l_permutation.getValue(l_floor); l_solutionManager.addSolution(l_rotationLinkedList, l_permutation, null, l_numberOfCubicleInPlace, l_floor); while ((l_solutionToDev = l_solutionManager.getBestUndeveloped()) != null && l_solutionManager.getBestValue() < 40) { int targetFloor = getTargetFloor(l_solutionToDev.getPermutation()); Console.Write("Searching {0}", l_solutionToDev.getPermutation().getValue(targetFloor)); if (l_solutionManager.getBestValue() > l_solutionToDev.getPermutation().getValue(targetFloor) + 14) { Console.WriteLine("Couldn't Find a Solution"); return(l_solutionManager.getBest()); } if (targetFloor == 1) { findBetterSolution(l_solutionToDev, p_firstTree, l_solutionManager, targetFloor); } if (targetFloor == 2) { findBetterSolution(l_solutionToDev, p_secondTree, l_solutionManager, targetFloor); } if (targetFloor == 3) { findBetterSolution(l_solutionToDev, p_thirdTree, l_solutionManager, targetFloor); } l_floor = getTargetFloor(l_solutionManager.getBestValue()); // Console.Write("Floor={0}, Best yet:{1}\n", l_floor, l_solutionManager.getBestValue()); // l_solutionManager.getBest().print(); } var toReturn = l_solutionManager.getBest(); return(toReturn); }
public static void loadRotationTreeFromStandard(RotationTree p_tree, RotationLinkedList p_rotationLinkedList, int p_depth) { if (p_depth == 0) { return; } foreach (Face face in Enum.GetValues(typeof(Face))) { foreach (Direction direction in Enum.GetValues(typeof(Direction))) { Rotation newRotation = new Rotation(face, direction); if (p_rotationLinkedList.isRedundant(newRotation)) { continue; } p_rotationLinkedList.addRotation(newRotation); p_tree.addRotationLinkedList(p_rotationLinkedList); loadRotationTreeFromStandard(p_tree, p_rotationLinkedList, p_depth - 1); p_rotationLinkedList.removeRotation(); } } }
public void searchTree(int p_minimumValue, RotationTree p_tree, Rubik p_rubik, SolutionManager p_solutionManager, Solution p_prevSolution, int p_floor, int depth) { if (p_minimumValue < 2) { p_minimumValue = 2; } Permutation l_permutation = p_rubik.getPermutation().getCopy(); Rubik l_rubik = new Rubik(); for (int i = 0; i < p_tree.getSize(); i++) { RotationLinkedList l_rotationLinkedList = p_tree.getRotationLinkedList(i); if (l_rotationLinkedList != null) { l_rubik.setPermutation(l_permutation); for (int j = 0; j < l_rotationLinkedList.size(); j++) { l_rubik.rotateFace(l_rotationLinkedList.get(j)); } Permutation l_resultPermutation = l_rubik.getPermutation().getCopy(); if (l_resultPermutation.getValue(p_floor) >= p_minimumValue) { p_solutionManager.addSolution(l_rotationLinkedList, l_resultPermutation, p_prevSolution, l_resultPermutation.getValue(p_floor), p_floor); } if (p_floor == 3 && depth == 0) { // Console.WriteLine("Hi"); searchTree(p_minimumValue, p_tree, l_rubik, p_solutionManager, new Solution(l_rotationLinkedList, l_resultPermutation, p_prevSolution), p_floor, 1); } } } }
public static void Main(String[] args) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Rubik myRubik = new Rubik(); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); myRubik.rotateFace(new Rotation(Face.B, Direction.CW)); myRubik.rotateFace(new Rotation(Face.L, Direction.CW)); myRubik.rotateFace(new Rotation(Face.F, Direction.CW)); myRubik.rotateFace(new Rotation(Face.R, Direction.CW)); Solver mySolver = new Solver(); RotationTree firstFloorTree = new RotationTree(); RotationTree secondFloorTree = new RotationTree(); RotationTree thirdFloorTree = new RotationTree(); RubikFileReader readFirstFloor = new RubikFileReader("..\\..\\..\\Resources\\FirstFloor.txt"); RubikFileReader readSecondFloor = new RubikFileReader("..\\..\\..\\Resources\\SecondFloor.txt"); RubikFileReader readThirdFloor = new RubikFileReader("..\\..\\..\\Resources\\ThirdFloor.txt"); RotationTreeLoader.loadRotationTreeFromFile(readFirstFloor, firstFloorTree); RotationTreeLoader.loadRotationTreeFromFile(readSecondFloor, secondFloorTree); RotationTreeLoader.loadRotationTreeFromFile(readThirdFloor, thirdFloorTree); Solution mySolution = mySolver.solve(myRubik, firstFloorTree, secondFloorTree, thirdFloorTree); mySolution.applyToRubik(myRubik); mySolution.print(); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.Write("Elapsed Time={0} seconds", elapsedTime); //27-12-2017: started 11:39 PM, Failed myRubik.getPermutation().print(); }