Example #1
0
        /// <summary>
        /// CRAPL license.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            //initialPopulation(organisms);

            //while (organisms.Count > 1)
            //{
            //    // Take two random organisms
            //    Organism[] parents = getTwoParents(organisms);

            //    // Mate them
            //    Organism[] children = SimpleReco.getInstance().recombine(parents[0], parents[1]);

            //    // Profit
            //    for (var i = 0; i < children.Length; ++i)
            //    {
            //        children[i].Died += OrganismDeath;
            //        organisms.Add(children[i]);
            //        children[i].start();
            //    }
            //    Console.WriteLine(organisms.Count);
            //}

            //foreach (Organism o in organisms.ToArray())
            //{
            //    o.thread.Join();
            //    Console.WriteLine(o.ToString());
            //}

            //Console.WriteLine("FIN - Deaths:" + death + " - Organisms left:" + organisms.Count);
            Console.WriteLine("Creating new organisms...");
            Mutation blur = new Mutation();
            blur.addGeneticModifier(new Blur(new Set(new[] { "rotation", "position" }), Set.ALL, 0.1));
            Organism a = new Test(blur);
            Organism b = new Test(blur);

            Console.WriteLine(a);
            Console.WriteLine(b);

            Console.WriteLine("Their child:");
            RecombinationHandler recombination = SimpleReco.getInstance();
            Organism child = recombination.recombine(a, b)[0];
            Console.WriteLine(child);

            Console.WriteLine("Wild mutations appeared !");
            Mutation m = new Mutation();
            m.addGeneticModifier(new Addition(new Set(new[] { "scale" }, Set.Mode.Blacklist), new Set(new[] { "bob" }), 50.50));
            m.addGeneticModifier(new Addition(new Set(new[] { "position", "scale" }), new Set(new[] { "bob" }), -50.50));
            m.addGeneticModifier(new Addition(Set.ALL, new Set(new[] { "Plate" }), 100));
            a.mutate(m);

            Mutation m2 = new Mutation();
            m2.addStructuralModifier(new RemoveExtension(new Set(new[] { "Plate a" })));
            b.mutate(m2);

            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.WriteLine(child);
        }
Example #2
0
        public override Organism[] recombine(Organism a, Organism b)
        {
            IList<Organism> offsprings = new List<Organism>();
            var _50p = new Probability(0.5);
            var _1p = new Probability(0.01);

            for (var i = 0; i < nbrChild; ++i)
            {
                Organism o = a.createEmpty();
                Extension e;

                // 50% chance to inherit the base extension from one of the parents.
                if (_50p.test())
                    e = a.genotype.getRootElement();
                else
                    e = b.genotype.getRootElement();

                o.genotype.setRootElement(e.localClone());

                // The structure recombination is done only for the first level.
                var nExtensions = Math.Min(a.genotype.getRootElement().getNumberOfChildExtensions(), b.genotype.getRootElement().getNumberOfChildExtensions());

                IEnumerator<Extension> t1 = (IEnumerator <Extension>) a.genotype.getRootElement().getChildsEnumerator();
                IEnumerator<Extension> t2 = (IEnumerator <Extension>) b.genotype.getRootElement().getChildsEnumerator();

                while (t1.MoveNext() && t2.MoveNext()){
                    if (_50p.test())
                        o.genotype.getRootElement().addExtension((Extension)t1.Current.deepClone());
                    else
                        o.genotype.getRootElement().addExtension((Extension)t2.Current.deepClone());
                }

                // Rare mutation that extends plate size.
                if (_1p.test())
                {
                    Mutation m = new Mutation();
                    //m.addGeneticModifier(new Multiplication(new Set(new[] { "scale" }), new Set(new[] { "Plate" }), 1.5));
                    o.mutate(m);
                }

                offsprings.Add(o);
            }

            return offsprings.ToArray<Organism>();
        }
Example #3
0
        private static void initialPopulation(List<Organism> organisms)
        {
            const int initialPopulationNumber = 2;

            // Specifies the initial blur mutation to apply.
            Mutation blur = new Mutation();
            blur.addGeneticModifier(new Blur(new Set(new[] { "rotation", "position" }), Set.ALL, 0.1));

            for (var i = 0; i < initialPopulationNumber; ++i)
            {
                Organism o = new Test(blur);
                organisms.Add(o);
                o.Died += OrganismDeath;
                Console.WriteLine(o.ToString());
            }

            foreach (Organism o in organisms.ToArray())
                o.start();
        }