예제 #1
0
        /// <inheritdoc />
        public void PerformOperation(EncogRandom rnd, IGenome[] parents,
                                     int parentIndex, IGenome[] offspring,
                                     int offspringIndex)
        {
            var parent1 = (EncogProgram)parents[0];
            var parent2 = (EncogProgram)parents[1];

            offspring[0] = null;

            EncogProgramContext context = parent1.Context;
            int size1 = parent1.RootNode.Count;
            int size2 = parent2.RootNode.Count;

            bool done  = false;
            int  tries = 100;

            while (!done)
            {
                int p1Index = rnd.Next(size1);
                int p2Index = rnd.Next(size2);

                var holder1 = new LevelHolder(p1Index);
                var holder2 = new LevelHolder(p2Index);

                IList <EPLValueType> types = new List <EPLValueType>();
                types.Add(context.Result.VariableType);

                FindNode(rnd, parent1.RootNode, types, holder1);
                FindNode(rnd, parent2.RootNode, types, holder2);

                if (LevelHolder.CompatibleTypes(holder1.Types,
                                                holder2.Types))
                {
                    EncogProgram result     = context.CloneProgram(parent1);
                    ProgramNode  resultNode = parent1.FindNode(p1Index);
                    ProgramNode  p2Node     = parent2.FindNode(p2Index);
                    ProgramNode  newInsert  = context.CloneBranch(result,
                                                                  p2Node);
                    result.ReplaceNode(resultNode, newInsert);
                    offspring[0] = result;
                    done         = true;
                }
                else
                {
                    tries--;
                    if (tries < 0)
                    {
                        done = true;
                    }
                }
            }
        }
        /// <summary>
        ///     Generate a random opcode.
        /// </summary>
        /// <param name="rnd">Random number generator.</param>
        /// <param name="opcodes">The opcodes to choose from.</param>
        /// <returns>The selected opcode.</returns>
        public IProgramExtensionTemplate GenerateRandomOpcode(EncogRandom rnd,
                                                              IList <IProgramExtensionTemplate> opcodes)
        {
            int maxOpCode = opcodes.Count;

            if (maxOpCode == 0)
            {
                return(null);
            }

            int tries = 10000;

            IProgramExtensionTemplate result = null;

            while (result == null)
            {
                int opcode = rnd.Next(maxOpCode);
                result = opcodes[opcode];
                tries--;
                if (tries < 0)
                {
                    throw new EACompileError(
                              "Could not generate an opcode.  Make sure you have valid opcodes defined.");
                }
            }
            return(result);
        }
        private void NewCase_Click(object sender, RoutedEventArgs e)
        {
            EncogRandom r = new EncogRandom();

            this.resolution = int.Parse(((ComboBoxItem)Resolution.SelectedValue).Content.ToString());
            this.testCase.InitTestCase(r.Next(3));
            Render();
        }
예제 #4
0
        /// <inheritdoc/>
        public IList <NEATLinkGene> SelectLinks(EncogRandom rnd,
                                                NEATGenome genome)
        {
            IList <NEATLinkGene> result = new List <NEATLinkGene>();
            int cnt = Math.Min(_linkCount, genome.LinksChromosome.Count);

            while (result.Count < cnt)
            {
                int          idx  = rnd.Next(genome.LinksChromosome.Count);
                NEATLinkGene link = genome.LinksChromosome[idx];
                if (!result.Contains(link))
                {
                    result.Add(link);
                }
            }
            return(result);
        }
예제 #5
0
        /// <inheritdoc />
        public void PerformOperation(EncogRandom rnd, IGenome[] parents,
                                     int parentIndex, IGenome[] offspring,
                                     int offspringIndex)
        {
            var program = (EncogProgram)parents[0];
            EncogProgramContext context = program.Context;
            EncogProgram        result  = context.CloneProgram(program);

            IList <EPLValueType> types = new List <EPLValueType>();

            types.Add(context.Result.VariableType);
            var globalIndex = new int[1];

            globalIndex[0] = rnd.Next(result.RootNode.Count);
            FindNode(rnd, result, result.RootNode, types, globalIndex);

            offspring[0] = result;
        }
예제 #6
0
        /// <inheritdoc/>
        public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
                                     IGenome[] offspring, int offspringIndex)
        {
            IntegerArrayGenome mother     = (IntegerArrayGenome)parents[parentIndex];
            IntegerArrayGenome father     = (IntegerArrayGenome)parents[parentIndex + 1];
            IntegerArrayGenome offspring1 = (IntegerArrayGenome)this.owner.Population.GenomeFactory.Factor();
            IntegerArrayGenome offspring2 = (IntegerArrayGenome)this.owner.Population.GenomeFactory.Factor();

            offspring[offspringIndex]     = offspring1;
            offspring[offspringIndex + 1] = offspring2;

            int geneLength = mother.Size;

            // the chromosome must be cut at two positions, determine them
            int cutpoint1 = (int)(rnd.Next(geneLength - this.cutLength));
            int cutpoint2 = cutpoint1 + this.cutLength;

            // keep track of which genes have been taken in each of the two
            // offspring, defaults to false.
            HashSet <int> taken1 = new HashSet <int>();
            HashSet <int> taken2 = new HashSet <int>();

            // handle cut section
            for (int i = 0; i < geneLength; i++)
            {
                if (!((i < cutpoint1) || (i > cutpoint2)))
                {
                    offspring1.Copy(father, i, i);
                    offspring2.Copy(mother, i, i);
                    taken1.Add(father.Data[i]);
                    taken2.Add(mother.Data[i]);
                }
            }

            // handle outer sections
            for (int i = 0; i < geneLength; i++)
            {
                if ((i < cutpoint1) || (i > cutpoint2))
                {
                    offspring1.Data[i] = SpliceNoRepeat.GetNotTaken(mother, taken1);
                    offspring2.Data[i] = SpliceNoRepeat.GetNotTaken(father, taken2);
                }
            }
        }
예제 #7
0
        /// <summary>
        ///     Select a random variable from the defined variables.
        /// </summary>
        /// <param name="rnd">A random number generator.</param>
        /// <param name="desiredTypes">The desired types that the variable can be.</param>
        /// <returns>The index of the defined variable, or -1 if unable to define.</returns>
        public int SelectRandomVariable(EncogRandom rnd,
                                        IList <EPLValueType> desiredTypes)
        {
            IList <VariableMapping> selectionSet = _context
                                                   .FindVariablesByTypes(desiredTypes);

            if (selectionSet.Count == 0 &&
                desiredTypes.Contains(EPLValueType.IntType))
            {
                IList <EPLValueType> floatList = new List <EPLValueType>();
                floatList.Add(EPLValueType.FloatingType);
                selectionSet = _context.FindVariablesByTypes(floatList);
            }

            if (selectionSet.Count == 0)
            {
                return(-1);
            }

            VariableMapping selected = selectionSet[rnd.Next(selectionSet.Count)];

            return(Context.DefinedVariables.IndexOf(selected));
        }
예제 #8
0
파일: Splice.cs 프로젝트: shturm/encog-prim
        /// <inheritdoc/>
        public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
                                     IGenome[] offspring, int offspringIndex)
        {
            IArrayGenome mother     = (IArrayGenome)parents[parentIndex];
            IArrayGenome father     = (IArrayGenome)parents[parentIndex + 1];
            IArrayGenome offspring1 = (IArrayGenome)this.owner.Population.GenomeFactory.Factor();
            IArrayGenome offspring2 = (IArrayGenome)this.owner.Population.GenomeFactory.Factor();

            offspring[offspringIndex]     = offspring1;
            offspring[offspringIndex + 1] = offspring2;

            int geneLength = mother.Size;

            // the chromosome must be cut at two positions, determine them
            int cutpoint1 = (int)(rnd.Next(geneLength - this.cutLength));
            int cutpoint2 = cutpoint1 + this.cutLength;

            // handle cut section
            for (int i = 0; i < geneLength; i++)
            {
                if (!((i < cutpoint1) || (i > cutpoint2)))
                {
                    offspring1.Copy(father, i, i);
                    offspring2.Copy(mother, i, i);
                }
            }

            // handle outer sections
            for (int i = 0; i < geneLength; i++)
            {
                if ((i < cutpoint1) || (i > cutpoint2))
                {
                    offspring1.Copy(mother, i, i);
                    offspring2.Copy(father, i, i);
                }
            }
        }
예제 #9
0
        /// <inheritdoc/>
        public IList <NEATLinkGene> SelectLinks(EncogRandom rnd, NEATGenome genome)
        {
            IList <NEATLinkGene> result = new List <NEATLinkGene>();

            bool mutated = false;

            foreach (var linkGene in genome.LinksChromosome)
            {
                if (rnd.NextDouble() < _proportion)
                {
                    mutated = true;
                    result.Add(linkGene);
                }
            }

            if (!mutated)
            {
                int          idx      = rnd.Next(genome.LinksChromosome.Count);
                NEATLinkGene linkGene = genome.LinksChromosome[idx];
                result.Add(linkGene);
            }

            return(result);
        }
        /// <summary>
        ///     Create a random note according to the specified paramaters.
        /// </summary>
        /// <param name="rnd">A random number generator.</param>
        /// <param name="program">The program to generate for.</param>
        /// <param name="depthRemaining">The depth remaining to generate.</param>
        /// <param name="types">The types to generate.</param>
        /// <param name="includeTerminal">Should we include terminal nodes.</param>
        /// <param name="includeFunction">Should we include function nodes.</param>
        /// <returns>The generated program node.</returns>
        public ProgramNode CreateRandomNode(EncogRandom rnd,
                                            EncogProgram program, int depthRemaining,
                                            IList <EPLValueType> types, bool includeTerminal,
                                            bool includeFunction)
        {
            // if we've hit the max depth, then create a terminal nodes, so it stops
            // here
            if (depthRemaining == 0)
            {
                return(CreateTerminalNode(rnd, program, types));
            }

            // choose which opcode set we might create the node from
            IList <IProgramExtensionTemplate> opcodeSet = Context.Functions.FindOpcodes(types, Context,
                                                                                        includeTerminal, includeFunction);

            // choose a random opcode
            IProgramExtensionTemplate temp = GenerateRandomOpcode(rnd,
                                                                  opcodeSet);

            if (temp == null)
            {
                throw new EACompileError(
                          "Trying to generate a random opcode when no opcodes exist.");
            }

            // create the child nodes
            int childNodeCount = temp.ChildNodeCount;
            var children       = new ProgramNode[childNodeCount];

            if (EncogOpcodeRegistry.IsOperator(temp.NodeType) && children.Length >= 2)
            {
                // for an operator of size 2 or greater make sure all children are
                // the same time
                IList <EPLValueType> childTypes = temp.Params[0]
                                                  .DetermineArgumentTypes(types);
                EPLValueType selectedType = childTypes[rnd
                                                       .Next(childTypes.Count)];
                childTypes.Clear();
                childTypes.Add(selectedType);

                // now create the children of a common type
                for (int i = 0; i < children.Length; i++)
                {
                    children[i] = CreateNode(rnd, program, depthRemaining - 1,
                                             childTypes);
                }
            }
            else
            {
                // otherwise, let the children have their own types
                for (int i = 0; i < children.Length; i++)
                {
                    IList <EPLValueType> childTypes = temp.Params[i]
                                                      .DetermineArgumentTypes(types);
                    children[i] = CreateNode(rnd, program, depthRemaining - 1,
                                             childTypes);
                }
            }

            // now actually create the node
            var result = new ProgramNode(program, temp, children);

            temp.Randomize(rnd, types, result, MinConst, MaxConst);
            return(result);
        }
예제 #11
0
        /// <inheritdoc />
        public override int DetermineMaxDepth(EncogRandom rnd)
        {
            int range = MaxDepth - _minDepth;

            return(rnd.Next(range) + _minDepth);
        }