Beispiel #1
0
        public IGenome <CNeuroNetWrapper> ProceedNonStructuralMutation()
        {
            NeuroNetGenome copy = new NeuroNetGenome(this);

            foreach (var c in copy._neuroNet.Connections)
            {
                do
                {
                    double ch = GetRandom(copy._nonStructuralMutations);
                    if (Math.Abs(c.Weight + ch) < 2)
                    {
                        c.Weight += ch;
                    }
                    else
                    {
                        if (Math.Abs(c.Weight - ch) < 2)
                        {
                            c.Weight -= ch;
                        }
                    }
                } while (c.Weight > 2 || c.Weight < -2);
            }
            copy._nonStructuralMutations      += 1;
            copy._totalnonStructuralMutations += 1;
            return(copy);
        }
Beispiel #2
0
        public static double TestFunction(NeuroNetGenome gen)
        {
            double       result   = 0.0;
            const int    maxTests = 10;
            const double timestep = 0.02;

            for (int i = 0; i < maxTests; ++i)
            {
                gen.Flush();
                Random r = new Random(123454321 + i);
                double difficultyCoef = i / (maxTests - 1.0);
                Tester t = new Tester(
                    (r.NextDouble() - r.NextDouble()) * (Math.PI / 3) * (difficultyCoef + 0.1),
                    r.NextDouble() * 5 + 1,

                    (r.NextDouble() - r.NextDouble()) * (Math.PI / 4) * (difficultyCoef + 0.1),
                    r.NextDouble() * 5 + 1,

                    10 * (r.NextDouble() + 1) * (1.1 - difficultyCoef),
                    (difficultyCoef + 1) * (r.NextDouble() + 1) * 5);

                do
                {
                    double[] calcs = gen.Calculate(new double[] { t.rotation, t.rotation2, t.position, });

                    double cont = (calcs[0] - 0.5) * 2;

                    t.Update(timestep, cont);
                } while (t.totalTime < 50.0 && Math.Abs(t.rotation) < (Math.PI / 2) && Math.Abs(t.rotation2) < (Math.PI / 2));
                result += t.totalTime;
            }


            return(result);
        }
Beispiel #3
0
 public NeuroNetGenome(NeuroNetGenome copy)
 {
     this._neuroNet = new CNeuroNetWrapper(copy._neuroNet);
     this._nonStructuralMutations      = copy._nonStructuralMutations;
     this._structuralMutations         = copy._structuralMutations;
     this._totalnonStructuralMutations = copy._totalnonStructuralMutations;
     this.LastResult = LastResult;
 }
Beispiel #4
0
        public IGenome <CNeuroNetWrapper> ProceedStructuralMutation()
        {
            NeuroNetGenome copy = new NeuroNetGenome(this);

            CNeuroNetWrapper.Connection con = null;

            int maxCon = copy._neuroNet.Nodes.Count * (copy._neuroNet.Nodes.Count - copy._neuroNet.InputsCount);

            double chance = copy._neuroNet.Connections.Count / (double)maxCon;

            if (GenomeRandom.NextDouble() < chance)
            {
                do
                {
                    foreach (var c in copy._neuroNet.Connections)
                    {
                        if (GenomeRandom.NextDouble() < (1.0 / copy._neuroNet.Connections.Count))
                        {
                            con = c;
                            break;
                        }
                    }
                } while (con == null);

                int from = con.From;
                int to   = con.To;

                copy._neuroNet.Connections.Remove(con);

                int nodeId = copy._neuroNet.Nodes.Count;
                copy._neuroNet.AddNode(nodeId);

                copy._neuroNet.AddConnection(from, nodeId, (GenomeRandom.NextDouble() - GenomeRandom.NextDouble()) * 2);
                copy._neuroNet.AddConnection(nodeId, to, (GenomeRandom.NextDouble() - GenomeRandom.NextDouble()) * 2);
            }
            else
            {
                int from = GenomeRandom.Next(copy._neuroNet.Nodes.Count);
                int to   = GenomeRandom.Next(copy._neuroNet.InputsCount - 1, copy._neuroNet.Nodes.Count);

                copy._neuroNet.AddConnection(from, to, (GenomeRandom.NextDouble() - GenomeRandom.NextDouble()) * 2);
            }

            copy._structuralMutations   += 1;
            copy._nonStructuralMutations = 0;
            copy.ParentResult            = this.LastResult;

            return(copy);
        }