コード例 #1
0
 public BotFactory(Maze maze, EngineSettings settings, WallGrid walls, BrainFactory brainFactory, EyeFactory eyeFactory)
 {
     _maze = maze;
     _settings = settings;
     _walls = walls;
     _brainFactory = brainFactory;
     _eyeFactory = eyeFactory;
 }
コード例 #2
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]);
        }
コード例 #3
0
    void Start()
    {
        ball_rigidBody = Ball.GetComponent <Rigidbody2D>();

        var input = new BrainFactoryInput()
        {
            ActivationFunctionInputOutput  = new TanH(),
            ActivationFunctionHiddenLayers = new TanH(), //do not use TanH for the xorbrain it is counter productive.  The xor needs 0 or 1, TanH brings in negative values as well
            Inputs                = 6,
            Outputs               = 1,
            HiddenLayers          = 1,
            NeuronsPerHiddenLayer = 4,
            Alpha = 0.11 //how much impact the training has, sometimes you'll see NaN come back and this dials back the calculations a bit
        };

        pongBrain = BrainFactory.CreateBrain <PongBrain>(input);
    }
コード例 #4
0
ファイル: XORPopulation.cs プロジェクト: danielwayota/Cerebro
        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);
        }
コード例 #5
0
ファイル: XORPopulation.cs プロジェクト: danielwayota/Cerebro
        // ==============================================
        // 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}");
            }
        }
コード例 #6
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);
        }
コード例 #7
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}");
            }
        }
コード例 #8
0
 public override void Initialize(Unit owner)
 {
     base.Initialize(owner); this.Brain = BrainFactory.CreateBrainFromUnit(owner);
 }
コード例 #9
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();
    }