Пример #1
0
        static void TestHashSet()
        {
            ICrossedTree root = FunctionContainer.CreateFunctionContainer_TwoArgs("Сложение");

            ICrossedTree sin = FunctionContainer.CreateFunctionContainer_OneArg("Синус");

            ICrossedTree cos = FunctionContainer.CreateFunctionContainer_OneArg("Косинус");

            ICrossedTree arg1 = FunctionContainer.CreateFunctionContainer_SameArgs(1);

            ICrossedTree arg2 = FunctionContainer.CreateFunctionContainer_SameArgs(2);

            root.AddChild(sin);
            root.AddChild(cos);

            sin.AddChild(arg1);
            cos.AddChild(arg2);

            HashSet <ICrossedTree> crossedTrees = new HashSet <ICrossedTree>();

            crossedTrees.Add(root);
            crossedTrees.Add(root);
            crossedTrees.Add(root);
            crossedTrees.Add(root);

            Console.WriteLine(crossedTrees.Count);

            crossedTrees.Add(root.Clone);
            crossedTrees.Add(root.Clone);
            crossedTrees.Add(root.Clone);

            Console.WriteLine(crossedTrees.Count);
        }
Пример #2
0
        public void UseMutation(IEnumerable <ICrossedTree> trees, ITreeCreator treeCreator, double probability = 0)
        {
            if (probability <= 0)
            {
                return;
            }

            foreach (var tree in trees)
            {
                if (tree.GetChilds().Count() == 0)
                {
                    continue;
                }

                if (Auxiliary.OccurEvent(probability))
                {
                    List <ICrossedTree> fromCandidates = tree.GetChilds().ToList();
                    ICrossedTree        from           = fromCandidates[Auxiliary.SelectCandidateIndex(new int[fromCandidates.Count].Select(d => d + 1).ToArray())];

                    ICrossedTree to = treeCreator.GetInstance(from.SublevelsCount);

                    bool a = tree.ChangeChild(from, to);

                    continue;
                }

                UseMutation(tree.GetChilds().ToList(), treeCreator, probability);
            }
        }
Пример #3
0
        public static ICrossedTree CrossRouletteTrees(ICrossedTree[] trees)
        {
            ICrossedTree resultTree = trees[Auxiliary.SelectCandidateIndex(trees.Select(t => t.Rating).ToArray())].Clone;

            while (resultTree.NeedMoreChilds())
            {
                List <ICrossedTree> childs = new List <ICrossedTree>();

                foreach (var tree in trees)
                {
                    childs.AddRange(tree.GetChilds());
                }

                resultTree.AddChild(CrossRouletteTrees(childs.ToArray()));
            }

            return(resultTree);
        }
Пример #4
0
        public static ICrossedTree CrossOverTrees(ICrossedTree tree1, ICrossedTree tree2)
        {
            int deep = Math.Min(tree1.SublevelsCount, tree2.SublevelsCount);

            int curDeep = Auxiliary.SelectCandidateIndex(new int[deep].Select(d => d + 1).ToArray());

            List <ICrossedTree> fromCandidates = tree1.GetChilds(curDeep).ToList();

            List <ICrossedTree> toCandidates = tree2.GetChilds(curDeep).ToList();

            ICrossedTree from = fromCandidates[Auxiliary.SelectCandidateIndex(new int[fromCandidates.Count].Select(d => d + 1).ToArray())];

            ICrossedTree to = toCandidates[Auxiliary.SelectCandidateIndex(new int[toCandidates.Count].Select(d => d + 1).ToArray())];

            var res = tree1.CloneWithChilds;

            bool a = res.ChangeChild(from, to);

            return(res);
        }