public Node SwapGenome(Node from, Node to, int fromIndex, int toIndex)
        {
            _currentExplorationCount = 0;
            InteractingNode          = null;
            Random r = new Random();

            TargetIndex = fromIndex;
            Mode        = TargetMode.Get;
            VisitNode(from);

            Node result = new Analyser().Analyse(new StringTokenizer(to.ToString()));

            Mode        = TargetMode.Set;
            TargetIndex = toIndex;
            VisitNode(result);

            return(result);
        }
Exemplo n.º 2
0
        private void CreateNextGeneration()
        {
            List <Node> nextGeneration = new List <Node>();
            Node        g = _currentGeneration.LastOrDefault();

            for (int i = 0; i < PopulationSize; i += 2)
            {
                int  pidx1 = RouletteSelection();
                int  pidx2 = RouletteSelection();
                var  parent1 = _currentGeneration[pidx1];
                var  parent2 = _currentGeneration[pidx2];
                Node child1, child2;

                if (_random.NextDouble() < CrossoverRate)
                {
                    child1 = CrossOver(parent1, parent2);
                    child2 = CrossOver(parent2, parent1);
                }
                else
                {
                    child1 = parent1;
                    child2 = parent2;
                }

                int  nbTry        = 0;
                int  maxTry       = 10;
                Node child1Backup = new Analyser().Analyse(new StringTokenizer(child1.ToString()));
                Node child2Backup = new Analyser().Analyse(new StringTokenizer(child2.ToString()));
                do
                {
                    child1 = child1Backup;
                    _mutationVisitor.Mutate(ref child1);
                    nbTry++;
                } while (nbTry < maxTry && (!NodeContainsVariable(child1, "A") || !NodeContainsVariable(child1, "B")));
                if (nbTry == maxTry)
                {
                    child1 = child1Backup;
                }

                nbTry = 0;
                do
                {
                    child2 = child2Backup;
                    _mutationVisitor.Mutate(ref child2);
                    nbTry++;
                } while (nbTry < maxTry && (!NodeContainsVariable(child2, "A") || !NodeContainsVariable(child2, "B")));
                if (nbTry == maxTry)
                {
                    child2 = child2Backup;
                }

                nextGeneration.Add(child1);
                nextGeneration.Add(child2);
            }
            if (g != null)
            {
                nextGeneration[0] = g;
            }

            _currentGeneration.Clear();
            _currentGeneration.AddRange(nextGeneration);
        }