Example #1
0
        public State FindPath(State root, int maxIteration)
        {
            root.Score = 0;
            open.Insert(evaluator.Evaluate(root), root);
            openHash.Add(root);

            int time = 0;

            while (!open.Empty())
            {
                if (maxIteration > 0)
                    if (time > maxIteration)
                        break;

                var state = open.Pop();
                openHash.Delete(state);

                if (evaluator.IsEndGame(state))
                    return state;

                closed.Add(state);

                var nextStates = generator.Succ(state);

                foreach (var nextState in nextStates)
                {
                    if (!closed.Contains(nextState))
                    {
                        double g = state.Score + evaluator.Cost(state, nextState);
                        double h = evaluator.Evaluate(nextState);

                        if (openHash.Contains(nextState))
                        {
                            int findex = open.ContainsValue(nextState);

                            if (g < open[findex].Item2.Score)
                            {
                                open.RemoveValueAt(findex);
                                open.Insert(g + h, nextState);
                                nextState.Score = g;
                                state.AddChild(nextState);
                            }
                        }
                        else
                        {
                            open.Insert(g + h, nextState);
                            openHash.Add(nextState);
                            nextState.Score = g;
                            state.AddChild(nextState);
                        }
                    }
                }

                time++;
            }

            return null;
        }
        public State FindPath(State root, int maxIteration)
        {
            root.Score = 0;
            open.Insert(evaluator.Evaluate(root), root);
            openHash.Add(root);

            int time = 0;

            while (!open.Empty())
            {
                if (maxIteration > 0)
                    if (time > maxIteration)
                        break;

                var state = open.Pop();
                openHash.Delete(state);

                if (evaluator.IsEndGame(state))
                    return state;

                closed.Add(state);

                double H = evaluator.Evaluate(state);
                double minH = 0;

                List<State> idleStates = new List<State>();

                var nextStates = generator.Succ(state);

                if (nextStates.Count > 0)
                    minH = evaluator.Evaluate(nextStates[0]);

                bool pathFound = false;

                foreach (var nextState in nextStates)
                {
                    if (!closed.Contains(nextState))
                    {
                        double g = state.Score + evaluator.Cost(state, nextState);
                        double h = evaluator.Evaluate(nextState);

                        if (nextState.IsIdle)
                        {
                            idleStates.Add(nextState);
                        }
                        else
                        {
                            minH = Math.Min(minH, h);

                            if (openHash.Contains(nextState))
                            {
                                int findex = open.ContainsValue(nextState);

                                if (g < open[findex].Item2.Score)
                                {
                                    open.RemoveValueAt(findex);
                                    open.Insert(g + h, nextState);
                                    nextState.Score = g;
                                    state.AddChild(nextState);

                                    pathFound = true;
                                }
                            }
                            else
                            {
                                open.Insert(g + h, nextState);
                                openHash.Add(nextState);
                                nextState.Score = g;
                                state.AddChild(nextState);

                                pathFound = true;
                            }
                        }
                    }
                }

                if (minH > H || !pathFound)
                {
                    for (int i = 0; i < idleStates.Count; i++)
                    {
                        var nextState = idleStates[i];

                        double g = state.Score + evaluator.Cost(state, nextState);

                        open.Insert(g + H, nextState);
                        openHash.Add(nextState);
                        nextState.Score = g;
                        state.AddChild(nextState);
                    }
                }

                time++;
            }

            return null;
        }
Example #3
0
        public State Clone()
        {
            State clone = new State(Dimension);

            for (int i = 0; i < Dimension; i++)
                clone[i] = states[i];

            clone.Time = Time;

            return clone;
        }
Example #4
0
        public State AddChild(State child)
        {
            child.Parent = this;

            if (FirstChild == null)
                FirstChild = child;

            if (LastChild != null)
                LastChild.NextSibling = child;

            LastChild = child;

            return child;
        }