public RubikCube(RubikCube copy, HeuristicSettings settings) { this.settings = settings; Init(); obfuscationCommands = new List <RubikCommand>(copy.obfuscationCommands); solutionCommands = new List <RubikCommand>(copy.solutionCommands); CubeInitialization(copy.Cube); Heuristic = RubikHeuristics.GetHeuristic(settings.heuristicType); H = Heuristic.Heuristic(this, settings); }
public static float[] GetHeuristicMap(RubikCube cube) { float[] heuristicMap = new float[cube.Cube.Length]; for (int i = 0; i < cube.Cube.Length; i++) { int end = RubikCube.CubeNum(cube.Cube[i].num); int curr = RubikCube.CubeNum(i); Vector3 endV = new Vector3(end / 9 - curr / 9, end % 3 - curr % 3, z: (end % 9) / 3 - (curr % 9) / 3); heuristicMap[i] = endV.magnitude / 4.0f; } return(heuristicMap); }
private void StepUpdate(RubikCube cube) { var map = GetErrorMap(cube); for (int i = step + 1; i < map.Length; i++) { if (map[i]) { return; } step++; } }
private bool[] GetErrorMap(RubikCube cube) { var max = settings.Map.Max(); var errorMap = new bool[max + 1]; for (int i = 0; i < cube.Cube.Length; i++) { if (cube.Cube[i].num != i) { errorMap[settings.Map[i]] = true; } } return(errorMap); }
public RubikCube(RubikCube cube) : this() { for (int z = 0; z < N; z++) { for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { var cubik = Cubiks[z, y, x]; Array.Copy(cube.Cubiks[z, y, x].Transform, cubik.Transform, cubik.Transform.Length); cubik.UpdateState(); } } } }
public static void PrintSidesData(RubikCube cube) { var colorNames = Enum.GetNames(typeof(RubikColor)); for (var y = 0; y < 3; y++) { Console.Write(" "); for (var x = 0; x < 3; x++) { PrintColoredSymbol(colorNames[cube.Sides[(int)RSide.Up].Faces[x, y].Color].First()); } Console.WriteLine(); } for (var y = 0; y < 3; y++) { for (var x = 0; x < 3; x++) { PrintColoredSymbol(colorNames[cube.Sides[(int)RSide.Left].Faces[x, y].Color].First()); } for (var x = 0; x < 3; x++) { PrintColoredSymbol(colorNames[cube.Sides[(int)RSide.Front].Faces[x, y].Color].First()); } for (var x = 0; x < 3; x++) { PrintColoredSymbol(colorNames[cube.Sides[(int)RSide.Right].Faces[x, y].Color].First()); } for (var x = 0; x < 3; x++) { PrintColoredSymbol(colorNames[cube.Sides[(int)RSide.Back].Faces[x, y].Color].First()); } Console.WriteLine(); } for (var y = 0; y < 3; y++) { Console.Write(" "); for (var x = 0; x < 3; x++) { PrintColoredSymbol(colorNames[cube.Sides[(int)RSide.Down].Faces[x, y].Color].First()); } Console.WriteLine(); } }
private static void PerformanceTest() { const int testsCount = 1000; long totalTime = 0; var sw = Stopwatch.StartNew(); var rnd = new Random(); var totalTurnsCount = 0; var totalTurnsStep0 = 0; var totalTurnsStep1 = 0; var totalTurnsStep2 = 0; var totalTurnsStep3 = 0; var totalTurnsStep4 = 0; for (var i = 0; i < testsCount; i++) { var r = new RubikCube(); var s = new RubikSolver(r); for (var j = 0; j < 20; j++) { r.RotateSide(((RSide)rnd.Next(0, 6), (RotationType)rnd.Next(0, 3))); } sw.Restart(); s.SolveStep0(); s.SolveStep1(); s.SolveStep2(); s.SolveStep3(); s.SolveStep4(); totalTime += sw.ElapsedMilliseconds; totalTurnsCount += s.TotalRotationsCount; totalTurnsStep0 += s.RotationsCountStep0; totalTurnsStep1 += s.RotationsCountStep1; totalTurnsStep2 += s.RotationsCountStep2; totalTurnsStep3 += s.RotationsCountStep3; totalTurnsStep4 += s.RotationsCountStep3; } sw.Stop(); Console.WriteLine( "\nTests: {0}\nAverage time to solve: {1}ms\nAverage turns to solve: {2}\nAverage turns for:\n" + "\tStep 0: {3}\n\tStep 1: {4}\n\tStep 2: {5}\n\tStep 3: {6}\n\tStep 4: {7}\n", testsCount, totalTime * 1f / testsCount, totalTurnsCount / testsCount, totalTurnsStep0 / testsCount, totalTurnsStep1 / testsCount, totalTurnsStep2 / testsCount, totalTurnsStep3 / testsCount, totalTurnsStep4 / testsCount); }
public float Cost(RubikCube node, RubikCube successor) { return(settings.Cost); }
public float Heuristic(RubikCube cube, HeuristicSettings settings) { this.settings = settings.manhattanSettings; return(GetHeuristicMap(cube).Sum() / cube.Cube.Length); }
private static void HandleControl(ConsoleKeyInfo key, ref RubikCube cube) { var shiftPressed = (key.Modifiers & ConsoleModifiers.Shift) != 0; var solver = new RubikSolver(cube); switch (key.Key) { case ConsoleKey.P: PerformanceTest(); break; case ConsoleKey.Z: solver.SolveStep0(); solver.SolveStep1(); solver.SolveStep2(); solver.SolveStep3(); solver.SolveStep4(); var rotations = solver.GetRotationsArray(); if (rotations.Length > 0) { for (var i = 0; i < rotations.Length - 1; i++) { Console.Write(RubikUtil.RotationCommandToText(rotations[i]) + " "); } Console.Write(RubikUtil.RotationCommandToText(rotations[rotations.Length - 1]) + "\n"); } break; case ConsoleKey.Q: cube = new RubikCube(); var r = new Random(); for (var i = 0; i < 20; i++) { var command = ((RSide)r.Next(0, 6), (RotationType)r.Next(0, 3)); cube.RotateSide(command); Console.Write(RubikUtil.RotationCommandToText(command) + " "); } Console.WriteLine(); break; case ConsoleKey.G: cube.RotateSide(RSide.Left, shiftPressed); break; case ConsoleKey.Y: cube.RotateSide(RSide.Front, shiftPressed); break; case ConsoleKey.J: cube.RotateSide(RSide.Right, shiftPressed); break; case ConsoleKey.N: cube.RotateSide(RSide.Back, shiftPressed); break; case ConsoleKey.T: cube.RotateSide(RSide.Up, shiftPressed); break; case ConsoleKey.M: cube.RotateSide(RSide.Down, shiftPressed); break; } }
private static void Main(string[] args) { try { var cube = new RubikCube(); var solver = new RubikSolver(cube); var keyInfo = new ConsoleKeyInfo(); if (args.Length != 0) { cube.RotateByCommandSequence(args[0]); solver.SolveStep0(); solver.SolveStep1(); solver.SolveStep2(); solver.SolveStep3(); solver.SolveStep4(); var rotations = solver.GetRotationsArray(); if (rotations.Length > 0) { for (var i = 0; i < rotations.Length - 1; i++) { Console.Write(RubikUtil.RotationCommandToText(rotations[i]) + " "); } Console.Write(RubikUtil.RotationCommandToText(rotations[rotations.Length - 1]) + "\n"); } return; } Console.WriteLine("*DIRECT CONTROL MODE*\n"); RubikUtil.PrintSidesData(cube); while (keyInfo.Key != ConsoleKey.Escape) { keyInfo = Console.ReadKey(); Console.Clear(); if (keyInfo.Key != ConsoleKey.Tab) { Console.WriteLine("*DIRECT CONTROL MODE*\n"); HandleControl(keyInfo, ref cube); RubikUtil.PrintSidesData(cube); } else { Console.WriteLine("*COMMAND SEQUENCE INPUT MODE*\n"); RubikUtil.PrintSidesData(cube); cube.RotateByCommandSequence(Console.ReadLine()); Console.WriteLine("\nPress any key...\n"); Console.ReadKey(); Console.Clear(); Console.WriteLine("*DIRECT CONTROL MODE*\n"); RubikUtil.PrintSidesData(cube); } } } catch (Exception e) { Console.WriteLine("Error happened! Error message: " + e); } }
public float Heuristic(RubikCube cube, HeuristicSettings settings) { this.settings = settings.onPlaceSettings; return(HeuristicNotOnPlace(cube)); }
private float HeuristicNotOnPlace(RubikCube cube) { return(cube.Cube.Select((t, i) => (t.num != i ? 1 : 0)).Sum()); }