Exemple #1
0
        public void S_OnSearchFinished(object sender, SearchFinishedEventArgs e)
        {
            AbstractSearch s = (AbstractSearch)sender;

            if (e.Found && s.Path.Count > 1)
            {
                Destination = new Path(s.Path);
            }
            Main.thisObj.Components.Remove(s);
        }
Exemple #2
0
        public static ICollection <AbstractSearch> AddCustomSearchCriteria <T>(this ICollection <AbstractSearch> searchCriterias, Expression <Func <T, object> > property)
        {
            Type   propertyType     = null;
            string fullPropertyPath = GetPropertyPath(property, out propertyType);

            AbstractSearch searchCriteria = CreateSearchCriteria(typeof(T), propertyType, fullPropertyPath);

            if (searchCriteria != null)
            {
                searchCriterias.Add(searchCriteria);
            }

            return(searchCriterias);
        }
Exemple #3
0
    public Node[] Expand(Node rootNode, AbstractSearch searchMethod)
    {
        sbyte iDim, jDim;

        byte[]      etc       = Node.FindTileCoords(rootNode.state, 0);
        List <Node> nodesList = new List <Node>();

        for (int x = 0; x < problem.operations.GetLength(0); x++)
        {
            iDim = (sbyte)(etc[0] + problem.operations[x, 0]);
            jDim = (sbyte)(etc[1] + problem.operations[x, 1]);
            if ((iDim < 0) || (iDim > rootNode.state.GetLength(0) - 1))
            {
                continue;
            }
            if ((jDim < 0) || (jDim > rootNode.state.GetLength(1) - 1))
            {
                continue;
            }
            sbyte[] operatorsX = new sbyte[2];
            operatorsX[0] = problem.operations[x, 0];
            operatorsX[1] = problem.operations[x, 1];
            Node childNode = rootNode.AddChild(operatorsX);
            childNode.SetPathCost(searchMethod.PathCostFn(childNode));
            if (rootNode.parentNode == null || !(childNode.state.Cast <byte>().SequenceEqual(rootNode.parentNode.state.Cast <byte>())))
            {
                if (nodesHashSet.Contains(String.Join("", childNode.state.Cast <byte>())) == false)
                {
                    nodesList.Add(childNode);
                    nodesHashSet.Add(String.Join("", childNode.state.Cast <byte>()));
                    nodesQueued++;
                }
            }
        }
        if (nodesList.Count != 0)
        {
            nodesOpened++;
        }
        return(nodesList.ToArray());
    }
        private static AbstractSearch CreateSearchCriterion(Type targetType, Type propertyType, string property)
        {
            AbstractSearch result = null;

            if (propertyType.IsCollectionType())
            {
                propertyType = propertyType.GetGenericArguments().First();
            }

            if (propertyType.IsEnum)
            {
                result = new EnumSearch(propertyType);
            }
            else if (propertyType == typeof(string))
            {
                result = new TextSearch();
            }
            else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
            {
                result = new BooleanSearch();
            }
            else if (propertyType == typeof(byte) || propertyType == typeof(byte?))
            {
                result = new ByteSearch();
            }
            else if (propertyType == typeof(char) || propertyType == typeof(char?))
            {
                result = new CharacterSearch();
            }
            else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
            {
                result = new DateSearch();
            }
            else if (propertyType == typeof(decimal) || propertyType == typeof(decimal?))
            {
                result = new DecimalSearch();
            }
            else if (propertyType == typeof(double) || propertyType == typeof(double?))
            {
                result = new DoubleSearch();
            }
            else if (propertyType == typeof(float) || propertyType == typeof(float?))
            {
                result = new FloatSearch();
            }
            else if (propertyType == typeof(int) || propertyType == typeof(int?))
            {
                result = new IntegerSearch();
            }
            else if (propertyType == typeof(long) || propertyType == typeof(long?))
            {
                result = new LongSearch();
            }
            else if (propertyType == typeof(sbyte) || propertyType == typeof(sbyte?))
            {
                result = new SByteSearch();
            }
            else if (propertyType == typeof(short) || propertyType == typeof(short?))
            {
                result = new ShortSearch();
            }
            else if (propertyType == typeof(uint) || propertyType == typeof(uint?))
            {
                result = new UnsignedIntegerSearch();
            }
            else if (propertyType == typeof(ulong) || propertyType == typeof(ulong?))
            {
                result = new UnsignedLongSearch();
            }
            else if (propertyType == typeof(ushort) || propertyType == typeof(ushort?))
            {
                result = new UnsignedShortSearch();
            }

            if (result != null)
            {
                result.Property       = property;
                result.TargetTypeName = targetType.AssemblyQualifiedName;
            }

            return(result);
        }
Exemple #5
0
    public void Program(AbstractSearch searchMethod)
    {
        int  n           = 0;
        long prevIterSec = 0;

        System.Diagnostics.Stopwatch watch;
        watch = System.Diagnostics.Stopwatch.StartNew();
        if (!LabAIMain.stepsYN)
        {
            Console.Write("\nВыполняется работа алгоритма...");
        }
        MakeQueue(MakeNode(problem.initState));
        while (true)
        {
            if (LabAIMain.stepsYN)
            {
                Console.BackgroundColor = ConsoleColor.Magenta;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\n[ШАГ #{0}]", ++n);
                Console.ResetColor();
            }
            if (IsEmpty())
            {
                Console.WriteLine("\n\nРешений не было найдено.\n");
                return;
            }
            Node newNode = RemoveFront();
            if (problem.GoalTest(newNode.state))
            {
                watch.Stop();
                Console.WriteLine("\n\nРешение было найдено!\n");
                ShowSolution(newNode);
                Console.WriteLine("\nВремя работы алгоритма: {0}.{1:000} секунд(ы)", watch.ElapsedMilliseconds / 1000, watch.ElapsedMilliseconds % 1000);
                Console.WriteLine("Вершин открыто: {0}", nodesOpened);
                Console.WriteLine("Вершин поставлено в очередь: {0}", nodesQueued);
                Console.WriteLine("Максимальная длина очереди: {0}\n", maxQueue < nodesQ.Count ? maxQueue = nodesQ.Count : maxQueue);
                return;
            }
            Node[] nodes = Expand(newNode, searchMethod);
            searchMethod.QueueingFn(nodesQ, nodes);
            if ((watch.ElapsedMilliseconds / 1000 != prevIterSec) && !LabAIMain.stepsYN)
            {
                Console.Write(".");
            }
            prevIterSec = watch.ElapsedMilliseconds / 1000;
            if (LabAIMain.stepsYN)
            {
                Console.WriteLine("depth - {0}, cost - {1}", newNode.depth, newNode.pathCost);
                ShowNode(newNode);
                Console.WriteLine("Перемещение пустой фишки - " + MoveDirection(newNode.action));
                Console.WriteLine("\nРаскрытые вершины:");
                ShowNodesArray(nodes);
                Console.WriteLine("\nОжидают раскрытия:");
                ShowNodesArray(nodesQ.ToArray());
                Console.Write("\n...Любая клавиша - следующий шаг / 'Escape' - выход из пошагового режима...");
                ConsoleKey pressedKey = Console.ReadKey(true).Key;
                Console.SetCursorPosition(0, Console.CursorTop);
                for (int i = 0; i < 80; i++)
                {
                    Console.Write(" ");
                }
                Console.SetCursorPosition(0, Console.CursorTop - 1);
                if (pressedKey == ConsoleKey.Escape)
                {
                    Console.Write("\nВыход из пошагового режима. Выполняется работа алгоритма...");
                    LabAIMain.stepsYN = false;
                }
            }
        }
    }