Example #1
0
        public void MutateTest()
        {
            var parent = new List <INode> {
                new Mow(), new Mow(), new Mow(), new Mow()
            };
            var geneSet = new FnCreateGeneDelegate[] { () => new Turn() };

            Tuple <Field, Mower, Program> FnEvaluate(IReadOnlyList <INode> instructions)
            {
                var field    = new ToroidField(10, 10, FieldContents.Grass);
                var location = new Location(5, 5);
                var dir      = Directions.North;
                var mower    = new Mower(location, dir);
                var program  = new Program(instructions);

                program.Evaluate(mower, field, 0);
                return(new Tuple <Field, Mower, Program>(field, mower, program));
            }

            Fitness FnFitness(IReadOnlyList <INode> genes) =>
            GetFitness(genes, FnEvaluate);

            var copy = parent.ToList();

            Mutate(parent, geneSet, 2, 5, FnFitness, 1);
            CollectionAssert.AreNotEqual(copy, parent);
            Assert.IsTrue(parent.Any(c => c.GetType() != typeof(Mow)));
        }
 public void Evaluate(Mower mower, Field field, int count)
 {
     foreach (var instruction in Main)
     {
         instruction.Execute(mower, field, count + 1);
     }
 }
        public void TurnLeftTest()
        {
            var location = new Location(5, 5);
            var dir      = Directions.North;
            var mower    = new Mower(location, dir);

            mower.TurnLeft();
            Assert.AreSame(Directions.West, mower.Direction);
            Assert.AreEqual(location, mower.Location);
            Assert.AreEqual(1, mower.StepCount);
        }
        public void Execute(Mower mower, Field field, int count)
        {
            if (count > 10)
            {
                throw new Exception("Too many calls.");
            }

            foreach (var op in Ops)
            {
                op.Execute(mower, field, count + 1);
            }
        }
        public void JumpFailedTest()
        {
            var location = new Location(3, 2);
            var dir      = Directions.East;
            var mower    = new Mower(location, dir);
            var field    = new ValidatingField(4, 4, FieldContents.Grass);

            field.Display(mower);

            mower.Jump(field, 2, 1);
            field.Display(mower);
            Assert.AreEqual(new Location(3, 2), mower.Location);
        }
        public void Execute(Mower mower, Field field, int count)
        {
            if (count > 10)
            {
                throw new Exception("Too many calls.");
            }

            var funcId = FuncId ?? 0;

            if (Funcs.Count > funcId)
            {
                Funcs[funcId].Execute(mower, field, count + 1);
            }
        }
Example #7
0
        private static void RunWith(FnCreateGeneDelegate[] geneSet, int width, int height, int minGenes, int maxGenes,
                                    int expectedNumberOfInstructions, int maxMutationRounds, FnCreateFieldDelegate fnCreateField,
                                    int expectedNumberOfSteps)
        {
            var mowerStartLocation  = new Location(width / 2, height / 2);
            var mowerStartDirection = Directions.South;

            List <INode> FnCreate() =>
            Create(geneSet, 1, height);

            Tuple <Field, Mower, Program> FnEvaluate(IReadOnlyList <INode> instructions)
            {
                var program = new Program(instructions);
                var mower   = new Mower(mowerStartLocation, mowerStartDirection);
                var field   = fnCreateField();

                try
                {
                    program.Evaluate(mower, field, 0);
                }
                catch (Exception)
                {
                    // pass
                }
                return(new Tuple <Field, Mower, Program>(field, mower, program));
            }

            Fitness FnGetFitness(IReadOnlyList <INode> genes) =>
            GetFitness(genes, FnEvaluate);

            var watch = Stopwatch.StartNew();

            void FnDisplay(Chromosome <INode, Fitness> candidate) =>
            Display(candidate, watch, FnEvaluate);

            void FnMutate(List <INode> child) =>
            Mutate(child, geneSet, minGenes, maxGenes, FnGetFitness, maxMutationRounds);

            var optimalFitness = new Fitness(width * height, expectedNumberOfInstructions, expectedNumberOfSteps);

            var best = Genetic <INode, Fitness> .GetBest(FnGetFitness, 0, optimalFitness, null, FnDisplay, FnMutate,
                                                         FnCreate, null, 10, Crossover);

            Assert.IsTrue(optimalFitness.CompareTo(best.Fitness) <= 0);
        }
        public void MowTest()
        {
            var field = new ValidatingField(7, 3, FieldContents.Grass);
            var mower = new Mower(new Location(1, 1), Directions.East);

            field.Display(mower);

            for (var i = 0; i < 4; i++)
            {
                mower.Mow(field);
                field.Display(mower);
            }

            for (var i = 1; i < 5; i++)
            {
                Assert.AreEqual(i.ToString().PadLeft(2), field[i + 1, 1]);
            }
        }
 public void Execute(Mower mower, Field field, int count)
 {
     mower.Jump(field, Forward, Right);
 }
 public void Execute(Mower mower, Field field, int count)
 {
     mower.TurnLeft();
 }
 public void Execute(Mower mower, Field field, int count)
 {
     mower.Mow(field);
 }