Exemplo n.º 1
0
        private void FullRun()
        {
            RobbyWorld world = new RobbyWorld(10, 10, 0.5f);

            IList <Reaction> rules = world.GenerateRandomRules();

            int score = world.FullRun(200, rules);

            Debug.Log(world.Log);
            Debug.Log($"Final score = {score}");
        }
Exemplo n.º 2
0
        private void OneTurn()
        {
            (int row, int col)pos1, pos2;
            Tile[] situation = new Tile[TileUtil.NUM_NEIGHBORS];

            RobbyWorld world = new RobbyWorld(10, 10, 0.5f);

            IList <Reaction> rules = world.GenerateRandomRules();

            StringBuilder sb = new StringBuilder();

            int ruleIndex;

            pos1 = world.RobbyPos;
            world.GetSituationAt(pos1, situation);
            ruleIndex = TileUtil.ToDecimal(situation);

            sb.AppendFormat(
                "Robby's position: ({0},{1})\n", pos1.row, pos1.col);
            sb.AppendFormat(
                "Robby's situation: {0} [ {1} ]\n",
                ruleIndex, TileUtil.ToString(situation));
            sb.AppendFormat(
                "Robby's action to take for situation {0}: {1}\n",
                ruleIndex, rules[ruleIndex]);
            sb.AppendFormat("Robby's original score: {0}", world.Score);

            sb.AppendLine("\nActing up...");
            world.NextTurn(rules);

            pos2 = world.RobbyPos;

            sb.AppendFormat(
                "Robby's new position: ({0},{1})\n", pos2.row, pos2.col);
            sb.AppendFormat(
                "Situation in original position: {0}\n",
                world[pos1.row, pos1.col]);
            sb.AppendFormat("Robby's new score: {0}", world.Score);

            Debug.Log(sb.ToString());
        }
Exemplo n.º 3
0
        private void Optimize()
        {
            System.Random random = new System.Random();
            RobbyWorld    world  = new RobbyWorld(10, 10, 0.5f);

            GeneticAlgorithm <Reaction> ga = new GeneticAlgorithm <Reaction>(
                200,
                0.9f,
                0.1f,
                () => new Ind <Reaction>(world.GenerateRandomRules()),
                (new TournamentSelection <Reaction>(random)).Select,
                (new OnePointCrossover <Reaction>(random)).Mate,
                (new FlipEnumMutation <Reaction>(0.1f)).Mutate,
                (ind) => { world.Reset(); ind.Fit = world.FullRun(200, ind.GenesView); },
                random);

            ga.Init();

            Ind <Reaction> best = ga.Run(1000, float.PositiveInfinity);

            Debug.Log($"Best fitness is {best.Fit}");
        }