Example #1
0
        public static void Run()
        {
            CerebroML.Cerebro net = BrainFactory.Create()
                                    .WithInput(2)
                                    .WithLayer(3, LayerType.Tanh)
                                    .WithLayer(1, LayerType.Sigmoid)
                                    .Build();

            float[] values = net.Run(new float[] { 1, 0 });

            System.Console.WriteLine(values[0]);
        }
Example #2
0
        public XORPopulation()
        {
            int popMax = 250;
            List <CerebroML.Cerebro> population = new List <CerebroML.Cerebro>();

            BrainFactory factory = BrainFactory.Create()
                                   .WithWeightBiasAmplitude(10f)
                                   .WithInput(2)
                                   .WithLayer(2, LayerType.Tanh)
                                   .WithLayer(1, LayerType.Sigmoid);

            for (int i = 0; i < popMax; i++)
            {
                population.Add(factory.Build());
            }
            this.SetUp(population.ToArray(), 0.05f, 10f);
        }
Example #3
0
        // ==============================================
        // Static runner
        // ==============================================

        public static void Run()
        {
            // Generate first population
            XORPopulation pop = new XORPopulation();

            // The record
            float kingFitness = 0;

            CerebroML.Cerebro king = BrainFactory.Create()
                                     .WithWeightBiasAmplitude(10f)
                                     .WithInput(2)
                                     .WithLayer(2, LayerType.Tanh)
                                     .WithLayer(1, LayerType.Sigmoid)
                                     .Build();

            // Iterate until the king has a good fitness
            while (kingFitness < 0.999) // && generation < GENERATION_LIMT)
            {
                pop.Next();

                kingFitness = pop.bestFitness;
                king.SetGenome(pop.best);

                // Write to the console the current king and the average fitness
                if (pop.generationNumber % 250 == 0)
                {
                    Console.WriteLine($"Gen {pop.generationNumber}");
                    Console.WriteLine($"Fitness: King- {kingFitness:0.00} AVG- {pop.GetAVGFitness():0.00}");
                }
            }

            // Show the final results
            if (king != null)
            {
                Console.WriteLine($"FINAL -  Gen: {pop.generationNumber}");
                Console.WriteLine($"Fitness: King- {kingFitness:0.00}");

                Console.WriteLine($" - (0, 0) = {king.Run(new float[] { 0.0f, 0.0f })[0]:0.00}");
                Console.WriteLine($" - (1, 0) = {king.Run(new float[] { 1.0f, 0.0f })[0]:0.00}");
                Console.WriteLine($" - (0, 1) = {king.Run(new float[] { 0.0f, 1.0f })[0]:0.00}");
                Console.WriteLine($" - (1, 1) = {king.Run(new float[] { 1.0f, 1.0f })[0]:0.00}");
            }
        }
Example #4
0
        //-------------------------------------------------------------------------
        // 生成・準備に関するモノ

        /// <summary>
        /// 対戦に要するオブジェクトの準備、ロケーション情報を持ったオブジェクトを受け取る。
        /// </summary>
        public void Setup(GameObject locations)
        {
            // ロケーション生成
            this.locations.Add(App.Player.P1, new Location("P1", "P2", locations));
            this.locations.Add(App.Player.P2, new Location("P2", "P1", locations));

            // プレイヤーを生成し、脳を設定
            this.p1 = CreatePlayer(App.Player.P1);
            this.p2 = CreatePlayer(App.Player.P2);
            this.p1.SetBrain(BrainFactory.Create(App.Brain.Player, this.p1, this.p2));
            this.p2.SetBrain(BrainFactory.Create(App.Brain.Player, this.p2, this.p1));

            // ガイド生成
            this.guide = new GameObject("Guide").AddComponent <Guide>();
            this.guide.SetParent(CacheTransform);
            this.guide.Init(
                this.locations[App.Player.P1].Center,
                this.locations[App.Player.P2].Center
                );
            this.guide.Setup();

            this.state.SetState(State.Ready);
        }
Example #5
0
        public static void Run()
        {
            // Generate first population
            int popMax = 250;
            List <CerebroML.Cerebro> population = new List <CerebroML.Cerebro>();

            BrainFactory factory = BrainFactory.Create()
                                   .WithWeightBiasAmplitude(10f)
                                   .WithInput(2)
                                   .WithLayer(2, LayerType.Tanh)
                                   .WithLayer(1, LayerType.Sigmoid);

            for (int i = 0; i < popMax; i++)
            {
                population.Add(factory.Build());
            }

            // The record
            float kingFitness = 0;

            CerebroML.Cerebro king = null;

            int generation = 0;

            // Iterate until the king has a good fitness
            while (kingFitness < 0.9) // && generation < GENERATION_LIMT)
            {
                generation++;
                float total = 0;

                // Search for the best one
                for (int e = 0; e < popMax; e++)
                {
                    float f = CalcFitness(population[e]);

                    if (f > kingFitness)
                    {
                        kingFitness = f;
                        king        = population[e];
                    }

                    total += f;
                }

                // Write to the console the current king and the average fitness
                if (generation % 250 == 0)
                {
                    Console.WriteLine($"Gen {generation}");
                    Console.WriteLine($"Fitness: King- {kingFitness:0.00} AVG- {total / popMax:0.00}");

                    Console.WriteLine($" - (0, 0) = {king.Run(new float[] { 0.0f, 0.0f })[0]:0.00}");
                    Console.WriteLine($" - (1, 0) = {king.Run(new float[] { 1.0f, 0.0f })[0]:0.00}");
                    Console.WriteLine($" - (0, 1) = {king.Run(new float[] { 0.0f, 1.0f })[0]:0.00}");
                    Console.WriteLine($" - (1, 1) = {king.Run(new float[] { 1.0f, 1.0f })[0]:0.00}");
                }

                // Make new population
                List <CerebroML.Cerebro> newPop = new List <CerebroML.Cerebro>();
                for (int e = 0; e < popMax; e++)
                {
                    // Find Two parents to make cross over.
                    List <CerebroML.Cerebro> parents = new List <CerebroML.Cerebro>();

                    int    control = 0;
                    Random rnd     = new Random();
                    while (parents.Count < 2 && control < 10000)
                    {
                        control++;
                        int   index = rnd.Next(popMax);
                        float dice  = (float)rnd.NextDouble();

                        if (CalcFitness(population[index]) > dice)
                        {
                            parents.Add(population[index]);
                        }
                    }

                    CerebroML.Cerebro offspring = factory.Build();

                    // Perform cross over and 'inject' the new Genome into the new network
                    if (parents.Count == 2)
                    {
                        Genome e1 = parents[0].GetGenome();
                        Genome e2 = parents[1].GetGenome();

                        Genome child = Genome.Crossover(e1, e2);
                        child.Mutate(0.01f, 10f);

                        offspring.SetGenome(child);
                    }
                    newPop.Add(offspring);
                }
                population = newPop;
            }

            // Show the final results
            if (king != null)
            {
                Console.WriteLine("FINAL");
                Console.WriteLine($"Fitness: King- {kingFitness:0.00}");

                Console.WriteLine($" - (0, 0) = {king.Run(new float[] { 0.0f, 0.0f })[0]:0.00}");
                Console.WriteLine($" - (1, 0) = {king.Run(new float[] { 1.0f, 0.0f })[0]:0.00}");
                Console.WriteLine($" - (0, 1) = {king.Run(new float[] { 0.0f, 1.0f })[0]:0.00}");
                Console.WriteLine($" - (1, 1) = {king.Run(new float[] { 1.0f, 1.0f })[0]:0.00}");
            }
        }
Example #6
0
    // =======================================
    void Awake()
    {
        // Sensor data

        /**
         * x
         * y
         * health_percent
         * Can shoot
         * forward_x
         * forward_y
         *
         * eye_active
         * eye_x
         * eye_y
         *  .
         *  . drive by eyeCount
         *  .
         * hit_from_active
         * hit_from_x
         * hit_from_y
         */
        this.eyes = new Vector3[this.eyeCount];

        this.sensorBuffer = new NameIndexedBuffer();

        this.sensorBuffer.AddIndex("time", 1);
        this.sensorBuffer.AddIndex("position", 2);
        this.sensorBuffer.AddIndex("health", 1);
        this.sensorBuffer.AddIndex("shoot_time", 1);
        this.sensorBuffer.AddIndex("forward", 2);

        for (int i = 0; i < this.eyeCount; i++)
        {
            this.sensorBuffer.AddIndex($"eye_{i}", 3);
        }

        this.sensorBuffer.AddIndex("hit_from", 3);

        // Reaction data

        /**
         * forward
         * backwards
         * turn left
         * turn right
         * shoot
         */
        this.reaction = new float[5];

        this.brain = BrainFactory.Create()
                     .WithInput(this.sensorBuffer.size)
                     .WithLayer(16, LayerType.Tanh)
                     .WithLayer(8, LayerType.Tanh)
                     .WithLayer(this.reaction.Length, LayerType.Sigmoid)
                     .WithWeightBiasAmplitude(10f)
                     .Build();

        this.physics          = GetComponent <Rigidbody>();
        this.positionRecorder = GetComponent <PositionRecorder>();
        this.lookToFriend     = new FloatRecorder();
        this.lookToFoe        = new FloatRecorder();
    }