Beispiel #1
0
 def find_better_solution(self, p_solution, p_tree, p_solutionManager,
                          p_floor):
     l_rubik = Cube()
     l_permutation = p_solution.get_permutation().get_copy()
     l_minimumValue = Cube.get_value(l_permutation, p_floor)
     l_rubik = Cube(l_permutation)
     self.search_tree(l_minimumValue - 4, p_tree, l_rubik,
                      p_solutionManager, p_solution, p_floor, 0)
Beispiel #2
0
 def get_target_floor_perm(self, p_permutation):
     if Cube.get_value(p_permutation, 1) >= 16:
         if Cube.get_value(p_permutation, 2) < 24:
             return 2
         else:
             return 3
     else:
         return 1
Beispiel #3
0
 def test_overallTest(self):
     myRubik = Cube()
     myRubik2 = Cube()
     myRubik.rotate_face("FRONT", "CW")
     self.assertEqual(8, myRubik.count_all_differences(myRubik2))
     myRubik.rotate_face("TOP", "CW")
     self.assertEqual(13, myRubik.count_all_differences(myRubik2))
Beispiel #4
0
 def add_sequence_to_solution_if_higher_value(self, minimumValueToReach,
                                              solutionManager,
                                              previousSolution,
                                              targetFloorToSortInCube,
                                              rotationSequence,
                                              cubeAfterRotationSequence):
     if Cube.get_value(cubeAfterRotationSequence,
                       targetFloorToSortInCube) >= minimumValueToReach:
         solutionManager.add_solution(
             rotationSequence, cubeAfterRotationSequence, previousSolution,
             Cube.get_value(cubeAfterRotationSequence,
                            targetFloorToSortInCube),
             targetFloorToSortInCube)
 def test_getValue(self):
     myRubik = Cube()
     myRubik.rotate_face("FRONT", "CW")
     myPermutation = Cube(myRubik)
     self.assertEqual(10, Cube.get_value(myPermutation, 1), "first floor")
     self.assertEqual(14, Cube.get_value(
         myPermutation, 2), "second floor")
     self.assertEqual(24, Cube.get_value(myPermutation, 3), "third floor")
Beispiel #6
0
 def get_cube_after_applying_sequence(self, cubeForExperimentation,
                                      rotationSequence):
     for rotationIndex in range(0, rotationSequence.size()):
         cubeForExperimentation.rotate_face(
             rotationSequence.get_rotation(rotationIndex).getFace(),
             rotationSequence.get_rotation(rotationIndex).getDirection())
     cubeAfterRotationSequence = Cube.get_permutation_from_cube(
         cubeForExperimentation).get_copy()
     return cubeAfterRotationSequence
Beispiel #7
0
    def solve(self, p_rubik, p_firstTree, p_secondTree, p_thirdTree):
        l_permutation = Cube(p_rubik)
        l_solutionManager = Solution_manager()
        l_rotationLinkedList = Rotation_sequence()
        l_floor = self.get_target_floor_perm(l_permutation)
        l_numberOfCubicleInPlace = Cube.get_value(l_permutation, l_floor)
        l_solutionManager.add_solution(l_rotationLinkedList, l_permutation,
                                       None, l_numberOfCubicleInPlace, l_floor)
        l_solutionToDev = l_solutionManager.get_best_undeveloped()
        while (l_solutionToDev != None
               and l_solutionManager.get_best_value() < 40):
            targetFloor = self.get_target_floor_perm(
                l_solutionToDev.get_permutation())
            print("Searching " + str(
                Cube.get_value(l_solutionToDev.get_permutation(), targetFloor))
                  + "\n")
            if l_solutionManager.get_best_value() > (Cube.get_value(
                    l_solutionToDev.get_permutation(), targetFloor) + 14):
                print("Couldn't Find a Solution\n")
                return l_solutionManager.get_best()

            if targetFloor == 1:
                self.find_better_solution(l_solutionToDev, p_firstTree,
                                          l_solutionManager, targetFloor)
            if targetFloor == 2:
                self.find_better_solution(l_solutionToDev, p_secondTree,
                                          l_solutionManager, targetFloor)
            if targetFloor == 3:
                self.find_better_solution(l_solutionToDev, p_thirdTree,
                                          l_solutionManager, targetFloor)

            l_floor = self.get_target_floor_value(
                l_solutionManager.get_best_value())

            print("Floor=" + str(l_floor) + " Best yet:" +
                  str(l_solutionManager.get_best_value()) +
                  ", best_undeveloped=" +
                  str(l_solutionManager.get_best_undeveloped() != None) + "\n")
            l_solutionToDev = l_solutionManager.get_best_undeveloped()
        return l_solutionManager.get_best()
Beispiel #8
0
 def search_tree(self, minimumValueToReach, searchTree, cubeToSolve,
                 solutionManager, previousSolution, targetFloorToSortInCube,
                 depth):
     if minimumValueToReach < 2:
         minimumValueToReach = 2
     for rotationSequenceIndex in range(0, searchTree.get_size()):
         rotationSequence = searchTree.get_rotationSequence(
             rotationSequenceIndex)
         if rotationSequence != None:
             cubeAfterRotationSequence = self.get_cube_after_applying_sequence(
                 Cube(cubeToSolve), rotationSequence)
             self.add_sequence_to_solution_if_higher_value(
                 minimumValueToReach, solutionManager, previousSolution,
                 targetFloorToSortInCube, rotationSequence,
                 cubeAfterRotationSequence)
             if targetFloorToSortInCube == 3 and depth == 0:
                 self.search_tree(
                     minimumValueToReach, searchTree,
                     cubeAfterRotationSequence, solutionManager,
                     Solution(rotationSequence, cubeAfterRotationSequence,
                              previousSolution), targetFloorToSortInCube, 1)
 def test_getValueFull(self):
     myRubik = Cube()
     myPermutation = Cube(myRubik)
     self.assertEqual(40, Cube.get_value(myPermutation, 3))
Beispiel #10
0
from production.cube.cube import Cube
from production.solver.solver import Solver
from production.cube.rubik_file_reader import Rubik_file_reader
from production.solver.rotation_tree import Rotation_tree

myRubik = Cube()
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("LEFT", "CW")
myRubik.rotate_face("FRONT", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("LEFT", "CW")
myRubik.rotate_face("FRONT", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("LEFT", "CW")
myRubik.rotate_face("FRONT", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("RIGHT", "CW")
myRubik.rotate_face("BACK", "CW")
myRubik.rotate_face("LEFT", "CW")
myRubik.rotate_face("FRONT", "CW")
myRubik.rotate_face("RIGHT", "CW")
Beispiel #11
0
    def test_complexSolver(self):
        myRubik = Cube()
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        myRubik.rotate_face("BACK", "CW")
        myRubik.rotate_face("LEFT", "CW")
        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("RIGHT", "CW")
        mySolver = Solver()

        readFirstFloor = Rubik_file_reader(
            "c:\\Users\\yk700h\\dev\\rubikCubeSolver\\python\\Resources\\FirstFloor.txt"
        )
        readSecondFloor = Rubik_file_reader(
            "c:\\Users\\yk700h\\dev\\rubikCubeSolver\\python\\Resources\\SecondFloor.txt"
        )
        readThirdFloor = Rubik_file_reader(
            "c:\\Users\\yk700h\\dev\\rubikCubeSolver\\python\\Resources\\ThirdFloor.txt"
        )

        firstFloorTree = Rotation_tree.get_rotation_tree_from_file(
            readFirstFloor)
        readFirstFloor.close()
        secondFloorTree = Rotation_tree.get_rotation_tree_from_file(
            readSecondFloor)
        readSecondFloor.close()
        thirdFloorTree = Rotation_tree.get_rotation_tree_from_file(
            readThirdFloor)
        readThirdFloor.close()
        #System.out.format("****************")
        mySolution = mySolver.solve(myRubik, firstFloorTree, secondFloorTree,
                                    thirdFloorTree)
        #System.out.format("****************")
        mySolution.apply_to_rubik(myRubik)
        mySolution.print()
        # long endTime = System.n nanoTime()
        #Console.WriteLine("Elapsed Time=%d seconds", ((endTime - beginningTime) / 1000000000))
        #27-12-2017: started 11:39 PM, Failed
        #myRubik.print()
        self.assertTrue(myRubik.equals(Cube()))
Beispiel #12
0
 def test_rotateBackClockwise(self):
     #Colors for : Front, Back, Right, Left, Top and Bottom  faces
     myRubik = Cube()
     myRubik.rotate_face("BACK", "CW")
     self.assertEqual("RIGHTCOLOR", myRubik.get_color("TOP", "LIF_TOP"))
     self.assertEqual("RIGHTCOLOR",
                      myRubik.get_color("TOP", "LIF_TOPRIGHT"))
     self.assertEqual("RIGHTCOLOR", myRubik.get_color("TOP", "LIF_TOPLEFT"))
     self.assertEqual("BACKCOLOR",
                      myRubik.get_color("BACK", "LIF_BOTTOMLEFT"))
     myRubik.rotate_face("BACK", "CW")
     myRubik.rotate_face("BACK", "CW")
     myRubik.rotate_face("BACK", "CW")
     self.assertEqual("TOPCOLOR", myRubik.get_color("TOP", "LIF_TOP"))
     self.assertEqual("TOPCOLOR", myRubik.get_color("TOP", "LIF_TOPRIGHT"))
     self.assertEqual("TOPCOLOR", myRubik.get_color("TOP", "LIF_TOPLEFT"))
     self.assertEqual("BACKCOLOR",
                      myRubik.get_color("BACK", "LIF_BOTTOMLEFT"))
Beispiel #13
0
    def test_simpleRotations(self):
        myRubik = Cube()
        for i in range(0, 20):
            myRubik.rotate_face("TOP", "CW")
            myRubik.rotate_face("RIGHT", "CW")
            myRubik.rotate_face("LEFT", "CW")
            myRubik.rotate_face("BOTTOM", "CW")
            myRubik.rotate_face("RIGHT", "CW")
            myRubik.rotate_face("TOP", "CW")
            myRubik.rotate_face("RIGHT", "CW")
            myRubik.rotate_face("BACK", "CW")
            myRubik.rotate_face("LEFT", "CW")
            myRubik.rotate_face("FRONT", "CW")

        for i in range(0, 20):
            myRubik.rotate_face("FRONT", "CCW")
            myRubik.rotate_face("LEFT", "CCW")
            myRubik.rotate_face("BACK", "CCW")
            myRubik.rotate_face("RIGHT", "CCW")
            myRubik.rotate_face("TOP", "CCW")
            myRubik.rotate_face("RIGHT", "CCW")
            myRubik.rotate_face("BOTTOM", "CCW")
            myRubik.rotate_face("LEFT", "CCW")
            myRubik.rotate_face("RIGHT", "CCW")
            myRubik.rotate_face("TOP", "CCW")

        myRubik.rotate_face("FRONT", "CW")
        myRubik.rotate_face("FRONT", "CCW")
        self.assertTrue(myRubik.equals(Cube()))
Beispiel #14
0
 def test_rotateBottomCounterClockwise(self):
     #Colors for : Front, Back, Right, Left, Top and Bottom  faces
     myRubik = Cube()
     myRubik.rotate_face("BOTTOM", "CCW")
     self.assertEqual("RIGHTCOLOR",
                      myRubik.get_color("FRONT", "LIF_BOTTOM"))
     self.assertEqual("RIGHTCOLOR",
                      myRubik.get_color("FRONT", "LIF_BOTTOMRIGHT"))
     self.assertEqual("RIGHTCOLOR",
                      myRubik.get_color("FRONT", "LIF_BOTTOMLEFT"))
     self.assertEqual("BOTTOMCOLOR",
                      myRubik.get_color("BOTTOM", "LIF_BOTTOMLEFT"))
     myRubik.rotate_face("BOTTOM", "CCW")
     myRubik.rotate_face("BOTTOM", "CCW")
     myRubik.rotate_face("BOTTOM", "CCW")
     self.assertEqual("FRONTCOLOR",
                      myRubik.get_color("FRONT", "LIF_BOTTOM"))
     self.assertEqual("FRONTCOLOR",
                      myRubik.get_color("FRONT", "LIF_BOTTOMRIGHT"))
     self.assertEqual("FRONTCOLOR",
                      myRubik.get_color("FRONT", "LIF_BOTTOMLEFT"))
     self.assertEqual("BOTTOMCOLOR",
                      myRubik.get_color("BOTTOM", "LIF_BOTTOMLEFT"))
Beispiel #15
0
 def test_compareFirstFloor(self):
     myRubik = Cube()
     myRubik2 = Cube()
     myRubik.set_color("FRONT", "LIF_BOTTOMLEFT", "BACKCOLOR")
     myRubik.set_color("BACK", "LIF_BOTTOM", "FRONTCOLOR")
     myRubik.set_color("FRONT", "LIF_TOPLEFT", "BACKCOLOR")
     self.assertEqual(2, myRubik.count_difference_first_floor(myRubik2))
     myRubik.set_color("LEFT", "LIF_BOTTOMLEFT", "BACKCOLOR")
     self.assertEqual(3, myRubik.count_difference_first_floor(myRubik2))