Ejemplo n.º 1
0
        private List <RubickColorMatrix> GenerateNeighbors(RubickColorMatrix _node)
        {
            List <RubickColorMatrix> neighbors = new List <RubickColorMatrix>();

            RubickMovementTypes lastMove = _node.stackSize > 0 ? _node.moveStack[_node.moveStack.Count - 1] : RubickMovementTypes.None;

            if (lastMove != RubickMovementTypes.None)
            {
                lastMove = (RubickMovementTypes)(-((int)lastMove));
            }

            var moves = System.Enum.GetValues(typeof(RubickMovementTypes));

            for (int i = 0; i < moves.Length; i++)
            {
                RubickMovementTypes val = (RubickMovementTypes)moves.GetValue(i);
                if (val != RubickMovementTypes.None && val != lastMove)
                {
                    RubickColorMatrix clone = (RubickColorMatrix)_node.Clone();
                    clone.Transform(val);
                    neighbors.Add(clone);
                }
            }

            return(neighbors);
        }
Ejemplo n.º 2
0
        public ArrayList getSuccessors(object state)
        {
            RubickColorMatrix   node       = (RubickColorMatrix)state;
            ArrayList           successors = new ArrayList();
            RubickMovementTypes lastMove   = node.stackSize > 0 ? node.moveStack[node.moveStack.Count - 1] : RubickMovementTypes.None;

            if (lastMove != RubickMovementTypes.None)
            {
                lastMove = (RubickMovementTypes)(-((int)lastMove));
            }

            var moves = System.Enum.GetValues(typeof(RubickMovementTypes));

            for (int i = 0; i < moves.Length; i++)
            {
                RubickMovementTypes val = (RubickMovementTypes)moves.GetValue(i);
                if (val != RubickMovementTypes.None && val != lastMove)
                {
                    RubickColorMatrix clone = (RubickColorMatrix)node.Clone();
                    clone.Transform(val);
                    successors.Add(new Successor(val.ToString(), clone));
                }
            }

            return(successors);
        }
Ejemplo n.º 3
0
        private List <RubickColorMatrix> GetBestNeighbors(RubickColorMatrix _node)
        {
            List <RubickColorMatrix> neighbors = new List <RubickColorMatrix>();
            int min = int.MaxValue;
            RubickColorMatrix minNeightbor = null;

            var moves = System.Enum.GetValues(typeof(RubickMovementTypes));

            for (int i = 0; i < moves.Length; i++)
            {
                RubickMovementTypes val = (RubickMovementTypes)moves.GetValue(i);
                if (val != RubickMovementTypes.None)
                {
                    RubickColorMatrix clone = (RubickColorMatrix)_node.Clone();
                    clone.Transform(val);
                    int cost = clone.HeuristicCostEstimate();
                    if (cost < min)
                    {
                        min          = cost;
                        minNeightbor = clone;
                    }
                }
            }

            if (min == _node.HeuristicCostEstimate())
            {
                UnityEngine.Debug.Log("Duh");
            }
            neighbors.Add(minNeightbor);
            return(neighbors);
        }
Ejemplo n.º 4
0
        private int GetFScore(RubickColorMatrix _node)
        {
            int val = int.MaxValue;

            fScore.TryGetValue(_node, out val);
            return(val);
        }
Ejemplo n.º 5
0
        private RubickColorMatrix FindLowestFScore()
        {
            RubickColorMatrix elem = null;
            int min = int.MaxValue;

            foreach (var item in openSet)
            {
                int fscore = GetFScore(item);
                if (elem == null || fscore < min)
                {
                    elem = item;
                    min  = fscore;
                }
            }

            return(elem);
        }
Ejemplo n.º 6
0
        public int getHeuristicValue(object state)
        {
            RubickColorMatrix node = (RubickColorMatrix)state;

            return(node.HeuristicCostEstimate());
        }
Ejemplo n.º 7
0
 public RubickCubeResolvedOutputEvent(RubickColorMatrix _cube, int _iterations)
 {
     rubickCube = _cube;
     iterations = _iterations;
     success    = _cube != null && _cube.IsResolved();
 }
Ejemplo n.º 8
0
 public RubickCubeStateOutputEvent(RubickColorMatrix _cube)
 {
     rubickCube = _cube;
 }
Ejemplo n.º 9
0
        public bool isGoalState(object state)
        {
            RubickColorMatrix node = (RubickColorMatrix)state;

            return(node.IsResolved());
        }
Ejemplo n.º 10
0
 public RubickCubeModel() : base()
 {
     rubickCube      = new RubickColorMatrix();
     aStarSearchTree = new AStarTreeColor();
     //UnityEngine.Debug.Log(rubickCube.Print());
 }
Ejemplo n.º 11
0
        private int DistanceBetween(RubickColorMatrix _a, RubickColorMatrix _b)
        {
            int dist = _a.stackSize - _b.stackSize;

            return(System.Math.Abs(dist));
        }
Ejemplo n.º 12
0
 private int HeuristicCostEstimate(RubickColorMatrix _node)
 {
     return(_node.HeuristicCostEstimate());
 }
Ejemplo n.º 13
0
        public void Search(RubickColorMatrix _start)
        {
            result = null;
            closedSet.Clear();
            openSet.Clear();
            openSet.Add(_start);

            gScore.Clear();
            gScore[_start] = 0;

            fScore.Clear();
            fScore[_start] = HeuristicCostEstimate(_start);
            iterations     = 0;
            RubickColorMatrix current = null;
            int attepts = 2000;

            while (openSet.Count > 0 && attepts > 0)
            {
                attepts--;
                iterations++;

                if (attepts == 0)
                {
                    UnityEngine.Debug.Log("Hola");
                }
                current = FindLowestFScore();
                if (current.IsResolved())
                {
                    result = current;
                    return;
                }

                openSet.Remove(current);
                closedSet.Add(current);

                IList <RubickColorMatrix> neighbors = GenerateNeighbors(current);

                foreach (var nb in neighbors)
                {
                    RubickColorMatrix neighbor = nb;
                    if (closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    int tentativeGScore = GetGScore(current) + DistanceBetween(current, neighbor);
                    if (!openSet.Contains(neighbor))
                    {
                        openSet.Add(neighbor);
                    }
                    else
                    {
                        neighbor = openSet.Find(n => n == neighbor);
                        if (tentativeGScore >= GetGScore(neighbor))
                        {
                            continue;
                        }
                    }

                    gScore[neighbor] = tentativeGScore;
                    fScore[neighbor] = gScore[neighbor] + HeuristicCostEstimate(neighbor);
                }
            }
        }