コード例 #1
0
    private void generateCell(Genom genom, bool mutationOnly = false)
    {
        GameObject newCell = Instantiate(cellPrefab);

        if (genom != null)
        {
            Cell nc = newCell.GetComponent <Cell>();

            Genom g = genom.clone();
            if (mutationOnly || Random.value > 0.5)
            {
                nc.getGenom().mutate();
            }
            else if (leaderBoard.Count > 1)
            {
                nc.getGenom().crossWith(leaderBoard[Random.Range(0, leaderBoard.Count)]);
            }

            nc.setGenom(g);
        }

        Vector3 newPosition = new Vector3(Random.value * worldSize - worldSize / 2, 1, Random.value * worldSize - worldSize / 2);

        newCell.transform.SetPositionAndRotation(newPosition, Quaternion.identity);
    }
コード例 #2
0
    protected override void ApplyGenom(Genom genom)
    {
        motorTorque = genom.GetGen("motor_torque").value;

        maxSpeed = genom.GetGen("max_speed").value;

        motorWheelRight.motorTorque = -motorTorque;
        motorWheelLeft.motorTorque  = -motorTorque;

        maxSteerAngle = genom.GetGen("max_steer_angle").value;

        minDistance = genom.GetGen("min_distance").value;

        viewRadius = genom.GetGen("view_radius").value;

        wheelRotatingSpeed = genom.GetGen("wheel_rotating_speed").value;

        float bublesHue = genom.GetGen("bubles_color").value;

        bubleColor = Color.HSVToRGB(bublesHue, 1, 1);

        //set color to particle system
        var main = bubles.main;

        main.startColor = bubleColor;

        //set color to lights
        neonLights.SetLightsColor(bubleColor);

        //
        lastpos = transform.position;
    }
コード例 #3
0
ファイル: NEAT.cs プロジェクト: Johbja/NEAT
 public NEAT(int inputs, int outputs)
 {
     genom         = new Genom(inputs, outputs);
     inputLength   = inputs;
     outputsLength = outputs;
     species       = false;
 }
コード例 #4
0
ファイル: NEAT.cs プロジェクト: Johbja/NEAT
 public NEAT(NEAT network)
 {
     genom         = new Genom(network.genom);
     inputLength   = network.inputLength;
     outputsLength = network.outputsLength;
     species       = network.species;
 }
コード例 #5
0
    public Genom clone()
    {
        Genom newGenom = new Genom();

        newGenom.neuralNetwork = this.neuralNetwork.clone();
        newGenom.skills        = (int[])skills.Clone();

        return(newGenom);
    }
コード例 #6
0
    public override void CrossOverDna(Genom <T> a, Genom <T> b, Genom <T> child)
    {
        int crossPoint = Random.Range(0, a.Length);
        var childDna   = new T[a.Length];

        for (var i = 0; i < childDna.Length; i++)
        {
            childDna[i] = i < crossPoint ? a.Dna[i] : b.Dna[i];
        }
        child.Dna = childDna;
    }
コード例 #7
0
ファイル: GA.cs プロジェクト: nagyistoce/Neuroflow
 private Genom CreateChild(int index, Genom g1, Genom g2)
 {
     var genes = new double[GenomLength];
     var coPoints = CreateCrossoverPoints();
     bool takeFirst = true;
     for (int i = 0; i < genes.Length; i++)
     {
         if (coPoints.Contains(i)) takeFirst = !takeFirst;
         genes[i] = Mutate(takeFirst ? g1.Genes[i] : g2.Genes[i]);
     }
     return new Genom(genes);
 }
コード例 #8
0
    public void crossWith(Genom g)
    {
        neuralNetwork.crossWith(g.neuralNetwork);
        for (int i = 0; i < skillsNumber; i++)
        {
            if (Random.value > .5)
            {
                skills[i] = g.skills[i];
            }
        }

        normalizeSkills();
    }
コード例 #9
0
    protected override float ProcessStep(Genom genom)
    {
        //
        forcePointMat.SetColor("_EmissionColor", Color.cyan);

        float degreeX;

        if (transform.rotation.eulerAngles.x <= 180)
        {
            degreeX = transform.rotation.eulerAngles.x;
        }
        else
        {
            degreeX = transform.rotation.eulerAngles.x - 360;
        }


        Vector3 localForward = transform.worldToLocalMatrix.MultiplyVector(transform.forward);
        Vector3 backForward  = transform.worldToLocalMatrix.MultiplyVector(-transform.forward);


        if (-maxDegreeXLeft > degreeX && degreeX < 0)
        {
            rig.AddForceAtPosition(localForward * forcePowerLeft, forcePoint.position);

            forcePointMat.SetColor("_EmissionColor", Color.magenta);

            //Debug.LogError("a");
        }
        if (maxDegreeXRight < degreeX && degreeX > 0)
        {
            rig.AddForceAtPosition(backForward * forcePowerRight, forcePoint.position);

            forcePointMat.SetColor("_EmissionColor", Color.yellow);

            //Debug.LogError("b");
        }

        // define score
        float meters = lastPos.z - transform.position.z;

        float scoreForStep = meters / Time.deltaTime;

        lastPos = transform.position;


        return(scoreForStep);
    }
コード例 #10
0
ファイル: Game.cs プロジェクト: AlexYLD/NeatAI
        public Game(Genom brain, MainWindow window)
        {
            int fieldSide = 30;

            Window = window;
            if (Window != null)
            {
                _field = Window.field;
            }
            else
            {
                _field = new Dot[fieldSide, fieldSide];
                for (int i = 0; i < fieldSide; i++)
                {
                    for (int j = 0; j < fieldSide; j++)
                    {
                        Dot dot = new Dot(j, i, null);
                        _field[j, i] = dot;
                    }
                }
            }

            Brain = brain;


            for (int i = 0; i < 4; i++)
            {
                _snake.AddLast(_field[fieldSide / 2 + i, fieldSide / 2]);

                ChangeDotStatus(_snake.Last.Value, DotStatus.Snake);
            }

            while (_apple == null || _snake.Contains(_apple))
            {
                int rndX = rnd.Next(fieldSide);
                int rndY = rnd.Next(fieldSide);
                _apple = _field[rndX, rndY];
            }


            ChangeDotStatus(_apple, DotStatus.Apple);
        }
コード例 #11
0
    public override Genom InitGenom()
    {
        Genom genom = new Genom();

        genom.AddGen(new Gen("motor_torque", 1000, 1));

        genom.AddGen(new Gen("max_speed", 6, 1));

        genom.AddGen(new Gen("max_steer_angle", 60, 1));

        genom.AddGen(new Gen("min_distance", 10, 1));

        genom.AddGen(new Gen("view_radius", 2, 0.6f));

        genom.AddGen(new Gen("wheel_rotating_speed", 2, 0.6f));

        genom.AddGen(new Gen("bubles_color", 1, 0)); //Hue value

        return(genom);
    }
コード例 #12
0
    protected override float ProcessStep(Genom genom)
    {
        float scoreForStep = Time.deltaTime;

        forcePointMat.SetColor("_EmissionColor", Color.cyan);

        //Debug.LogError(genes.valuableDegree + " " + info.rotationX);

        //Debug.LogError(info.rotationX);

        float degreeX;

        if (transform.rotation.eulerAngles.x <= 180)
        {
            degreeX = transform.rotation.eulerAngles.x;
        }
        else
        {
            degreeX = transform.rotation.eulerAngles.x - 360;
        }


        if (-maxDegreeX > degreeX && degreeX < 0)
        {
            rig.AddForceAtPosition(Vector3.forward * forcePowerLeft, forcePoint.position);

            forcePointMat.SetColor("_EmissionColor", Color.magenta);

            //Debug.LogError("a");
        }
        if (maxDegreeX < degreeX && degreeX > 0)
        {
            rig.AddForceAtPosition(Vector3.back * forcePowerRight, forcePoint.position);

            forcePointMat.SetColor("_EmissionColor", Color.yellow);

            //Debug.LogError("b");
        }

        return(scoreForStep);
    }
コード例 #13
0
ファイル: NEAT.cs プロジェクト: Johbja/NEAT
    public Genom(Genom genom)
    {
        nodeGenes = new List <Node>();
        foreach (Node n in genom.nodeGenes)
        {
            Node newNode = new Node(n.nodeID, n.order, n.nodeType);
            nodeGenes.Add(newNode);
        }

        connectionGenes = new List <Connection>();
        foreach (Connection c in genom.connectionGenes)
        {
            Connection newC = new Connection(c.inNode, c.outNode, c.weight, c.enabled, c.innovation);
            nodeGenes.Find(n => n.nodeID == newC.inNode).AddConnection(newC);
            connectionGenes.Add(newC);
        }

        inputLength       = genom.inputLength;
        currentInnovation = genom.currentInnovation;
        genomFitness      = genom.genomFitness;
        counter           = genom.counter;
    }
コード例 #14
0
        public CrossingResult Cross(Individual other)
        {
            var first = this.Genom.Take(Genom.Length / 2).ToList();

            for (int i = 0; i < Genom.Length - (Genom.Length / 2); i++)
            {
                first.Add(other.Genom.First(x => !first.Contains(x)));
            }

            var second = other.Genom.Take(other.Genom.Length / 2).ToList();

            for (int i = 0; i < other.Genom.Length - (other.Genom.Length / 2); i++)
            {
                second.Add(Genom.First(x => !second.Contains(x)));
            }

            return(new CrossingResult()
            {
                First = new TspIndividual(_fitnessFunc, first.ToArray(), _randomProvider),
                Second = new TspIndividual(_fitnessFunc, second.ToArray(), _randomProvider)
            });
        }
コード例 #15
0
ファイル: Form1.cs プロジェクト: GhoulLord/codersstrikeback
        private void FillWeightsWithGenom(NeuralNetwork.NeuralNetwork nn, Genom genom)
        {
            int genIndex = 0;

            foreach (var intputNeuron in nn.InputLayer.Neurons.Cast <InputNeuron>())
            {
                intputNeuron.Bias = genom.Gens[genIndex];
                genIndex++;
            }

            foreach (var hiddenLayer in nn.HiddenLayers)
            {
                foreach (var hiddenNeuron in hiddenLayer.Neurons.Cast <HiddenNeuron>())
                {
                    hiddenNeuron.Bias = genom.Gens[genIndex];
                    genIndex++;

                    foreach (var axon in hiddenNeuron.Inputs)
                    {
                        axon.Weight = genom.Gens[genIndex];
                        genIndex++;
                    }
                }
            }

            foreach (var outputNeuron in nn.OutputLayer.Neurons.Cast <OutputNeuron>())
            {
                outputNeuron.Bias = genom.Gens[genIndex];
                genIndex++;

                foreach (var axon in outputNeuron.Inputs)
                {
                    axon.Weight = genom.Gens[genIndex];
                    genIndex++;
                }
            }
        }
コード例 #16
0
ファイル: Form1.cs プロジェクト: GhoulLord/codersstrikeback
        private void FillGenomWithWeights(Genom genom, NeuralNetwork.NeuralNetwork nn)
        {
            int genIndex = 0;

            foreach (var intputNeuron in nn.InputLayer.Neurons.Cast <InputNeuron>())
            {
                genom.Gens[genIndex] = intputNeuron.Bias;
                genIndex++;
            }

            foreach (var hiddenLayer in nn.HiddenLayers)
            {
                foreach (var hiddenNeuron in hiddenLayer.Neurons.Cast <HiddenNeuron>())
                {
                    genom.Gens[genIndex] = hiddenNeuron.Bias;
                    genIndex++;

                    foreach (var axon in hiddenNeuron.Inputs)
                    {
                        genom.Gens[genIndex] = axon.Weight;
                        genIndex++;
                    }
                }
            }

            foreach (var outputNeuron in nn.OutputLayer.Neurons.Cast <OutputNeuron>())
            {
                genom.Gens[genIndex] = outputNeuron.Bias;
                genIndex++;

                foreach (var axon in outputNeuron.Inputs)
                {
                    genom.Gens[genIndex] = axon.Weight;
                    genIndex++;
                }
            }
        }
コード例 #17
0
ファイル: MutationRule.cs プロジェクト: Futahei/GACubes
 public abstract void Mutate(Genom <T> genom);
コード例 #18
0
ファイル: MainWindow.xaml.cs プロジェクト: AlexYLD/NeatAI
        private void RunNewPopulation(object sender)
        {
            _newPop = true;
            ((Button)sender).IsEnabled = false;
            new Thread(() =>
            {
                _mainRun?.Join();
                _newPop  = false;
                _mainRun = new Thread(() =>
                {
                    int gen               = 0;
                    double bestScore      = 0;
                    Population population = new Population(7, 3, true);
                    _best = population.AllNets[0];
                    while (!_newPop)
                    {
                        foreach (Genom brain in population.AllNets)
                        {
                            brain.Fitness = 0;
                            Game game     = new Game(brain, brain.Equals(_best) && _toShow ? this : null);

                            while (game.move())
                            {
                                if (_newPop)
                                {
                                    game.clearField();
                                    return;
                                }

                                if (brain.Equals(_best) && _toShow)
                                {
                                    Thread.Sleep(_timeout);
                                }
                            }

                            if (brain.Equals(_best))
                            {
                                game.clearField();
                            }
                        }

                        _best = population.GetBest(population.AllNets);
                        if (_best.Fitness > 100000 && _best.Fitness > bestScore)
                        {
                            SaveCurrentGenome(@"C:\Users\ayarygi\RiderProjects\NeatAI\NeatNet\bin\Debug\best\" + _best.Fitness + "_" + gen);
                        }

                        Console.WriteLine("gen " + gen + " Count " + population.AllNets.Count + " spe: " + population._species.Count + " curBest: " + _best.Fitness + " Best " + bestScore);
                        if (bestScore < _best.Fitness)
                        {
                            bestScore = _best.Fitness;
                            if (bestScore > 10000)
                            {
                                //toShow = true;
                            }
                        }

                        gen++;

                        if (gen >= 1000)
                        {
                            Dispatcher.Invoke(() => RunNewPopulation(sender));
                        }

                        population.NextGen();
                    }
                });
                _mainRun.Start();
                Dispatcher.Invoke(() => ((Button)sender).IsEnabled = true);
            }).Start();
        }
コード例 #19
0
 private int[] Randomize(int[] genom)
 {
     return(Genom.OrderBy(x => _randomProvider.GetRandomValue(Int32.MaxValue)).ToArray());
 }
コード例 #20
0
ファイル: Program.cs プロジェクト: nagyistoce/Neuroflow
        private static void Show(string title, Genom genom, GeneBasedColorMixer trainingMixer, GeneBasedColorMixer validationMixer, bool store = false)
        {
            var trainingResult = trainingMixer.CreateMixedColors(genom);
            var validationResult = validationMixer.CreateMixedColors(genom);

            Console.WriteLine("\n-- {0} --", title);

            Console.WriteLine("Training Errors - Max: {0}, Avg: {1}, Min: {2}", trainingResult.MaxError.ToString("0.0000"), trainingResult.AvgError.ToString("0.0000"), trainingResult.MinError.ToString("0.0000"));
            Console.WriteLine("Training Worst Mix: {0}", trainingResult.WorstMix);
            Console.WriteLine("Training Best Mix: {0}", trainingResult.BestMix);
            Console.WriteLine();
            Console.WriteLine("Validation Errors - Max: {0}, Avg: {1}, Min: {2}", validationResult.MaxError.ToString("0.0000"), validationResult.AvgError.ToString("0.0000"), validationResult.MinError.ToString("0.0000"));
            Console.WriteLine("Validation Worst Mix: {0}", validationResult.WorstMix);
            Console.WriteLine("Validation Best Mix: {0}", validationResult.BestMix);

            if (store)
            {
                ColorFilteringPars fp;
                ColorMixingPars mp;
                GeneBasedColorMixer.ToColorMixerPars(genom, out fp, out mp);
                foreach (var bestFiles in Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "best*.*")) File.Delete(bestFiles);
                string fileName = "best-t-" + trainingResult.WorstMix.Error.ToString("0.0000") + "-v-" + validationResult.WorstMix.Error.ToString("0.0000");
                using (var ffs = File.CreateText(fileName + ".fp.xml"))
                using (var fw = XmlWriter.Create(ffs, new XmlWriterSettings { Indent = true }))
                using (var mfs = File.CreateText(fileName + ".mp.xml"))
                using (var mw = XmlWriter.Create(mfs, new XmlWriterSettings { Indent = true }))
                {
                    //new DataContractSerializer(typeof(ColorFilteringPars)).WriteObject(fw, fp);
                    //fw.Flush();
                    //new DataContractSerializer(typeof(ColorMixingPars)).WriteObject(mw, mp);
                    //mw.Flush();
                }
            }
        }
コード例 #21
0
ファイル: VectorMutationRule.cs プロジェクト: Futahei/GACubes
    public override void Mutate(Genom <Vector2> genom)
    {
        int index = Random.Range(0, genom.Length);

        genom.Dna[index] = MutateDna(genom.Dna[index]);
    }
コード例 #22
0
 public void setGenom(Genom g)
 {
     genome = g;
     gameObject.GetComponent <Renderer>().material.color = genome.color;
 }
コード例 #23
0
 public Genom(Genom <T> baseGenom)
 {
     Length = baseGenom.Length;
     Dna    = baseGenom.Dna;
 }
コード例 #24
0
ファイル: Program.cs プロジェクト: theoremoon/GA_Action
        static void GeneticAlgorithm(string outFileName, Field field, int screenWidth, Random random, uint genomLength = 1000, uint groupSize = 10)
        {
            // ほげる
            if (groupSize % 2 != 0)
            {
                throw new Exception("AAN");
            }
            List <KeyValuePair <int, bool> > maxScores = new List <KeyValuePair <int, bool> >();
            // 初期化
            List <Genom> genoms = new List <Genom>();

            for (int i = 0; i < groupSize; i++)
            {
                genoms.Add(Genom.Init(genomLength, random));
            }

            List <double> scoreAvgs = new List <double>();

            for (int c = 0; c < 3000; c++)
            {
                List <int> scores = new List <int>();
                // 評価
                for (int i = 0; i < groupSize; i++)
                {
                    Genom     genom     = genoms.ElementAt(i);
                    Simulator simulator = new Simulator(field, screenWidth);
                    while (!simulator.End && !genom.End)
                    {
                        //simulator.Draw();
                        //System.Threading.Thread.Sleep(100);
                        Action nextAction = genom.Next();
                        simulator.Update(nextAction);
                    }
                    genom.IsClear = simulator.IsClear;
                    genom.Score   = simulator.Score;
                    //Console.Clear();
                    //Console.SetCursorPosition(0, 20);
                    //Console.WriteLine("GENERATION:{0}", c);
                    //Console.WriteLine("SCORE:{0}", genom.Score);

                    scores.Add(genom.Score);
                    if (maxScores.Count < 10)
                    {
                        maxScores.Add(new KeyValuePair <int, bool>(genom.Score, genom.IsClear));
                        maxScores.Sort((a, b) => a.Key.CompareTo(b.Key));
                    }
                    else if (maxScores[0].Key < genom.Score)
                    {
                        maxScores.Add(new KeyValuePair <int, bool>(genom.Score, genom.IsClear));
                        maxScores = maxScores.OrderByDescending(a => a.Key).Take(10).OrderBy(a => a.Key).ToList();
                    }
                }
                scoreAvgs.Add(scores.Average());
                // 次世代
                genoms = NextGeneration(genoms);
            }
            using (StreamWriter sw = new StreamWriter(outFileName))
            {
                maxScores.Reverse();
                sw.WriteLine("{0} / {1} was goal", maxScores.OrderByDescending(a => a.Key).Take(10).Where(a => a.Value).Count(), 10);
                foreach (var scoreAvg in scoreAvgs)
                {
                    sw.WriteLine("{0}", scoreAvg);
                }
            }
        }
コード例 #25
0
    protected override float ProcessStep(Genom genom)
    {
        Vector3 posDelta = lastpos - transform.position;

        //Debug.LogError(posDelta.magnitude);

        if (posDelta.magnitude > maxSpeed)
        {
            motorWheelRight.motorTorque = 0;
            motorWheelLeft.motorTorque  = 0;
        }
        else
        {
            motorWheelRight.motorTorque = -motorTorque;
            motorWheelLeft.motorTorque  = -motorTorque;
        }

        RaycastHit hit;

        dirLeft  = -transform.TransformDirection(Vector3.forward) * minDistance + transform.TransformDirection(Vector3.left) * minDistance * viewRadius;
        dirRight = -transform.TransformDirection(Vector3.forward) * minDistance + transform.TransformDirection(Vector3.right) * minDistance * viewRadius;

        // Does the ray intersect any objects
        // left
        if (Physics.Raycast(transform.position, dirLeft.normalized, out hit, dirLeft.magnitude))
        {
            Debug.DrawRay(transform.position, dirLeft.normalized * hit.distance, bubleColor);

            if (-maxSteerAngle < currSteerAngle)
            {
                currSteerAngle -= wheelRotatingSpeed; //* (1 / (dirLeft.magnitude / hit.distance));
            }
            else
            {
                currSteerAngle = -maxSteerAngle;
            }

            //motorWheelRight.motorTorque = 0;
            //motorWheelLeft.motorTorque = 0;
        }
        // right
        if (Physics.Raycast(transform.position, dirRight.normalized, out hit, dirRight.magnitude))
        {
            Debug.DrawRay(transform.position, dirRight.normalized * hit.distance, bubleColor);

            if (maxSteerAngle > currSteerAngle)
            {
                currSteerAngle += wheelRotatingSpeed; //* (1 / (dirRight.magnitude / hit.distance));
            }
            else
            {
                currSteerAngle = maxSteerAngle;
            }

            //motorWheelRight.motorTorque = 0;
            //motorWheelLeft.motorTorque = 0;
        }
        // no hit
        if (hit.distance <= 0)
        {
            float value      = currSteerAngle - wheelRotatingSpeed;
            float normalazed = Mathf.Abs(value) / value; // [-1, 1]

            if (Mathf.Abs(value) > wheelRotatingSpeed)
            {
                currSteerAngle -= normalazed;
            }
            else
            {
                currSteerAngle = 0;
            }
        }

        steerWheelRight.steerAngle = currSteerAngle;
        steerWheelLeft.steerAngle  = currSteerAngle;

        // count scores
        if (posDelta.magnitude < 0.08f)
        {
            stayTime += Time.deltaTime;
        }
        else
        {
            stayTime = 0;
        }

        float score = posDelta.magnitude;

        lastpos = transform.position;

        return(score);
    }
コード例 #26
0
ファイル: CrossOverRule.cs プロジェクト: Futahei/GACubes
 public abstract void CrossOverDna(Genom <T> a, Genom <T> b, Genom <T> child);