Esempio n. 1
0
        public static void BreedingSelf(GraphicsDevice g, GameWindow w)
        {
            const int TEST_SIZE = 500;
            Random    r         = new Random();
            var       results   = new List <float>(1024);

            Console.WriteLine("start opt test");
            for (int i = 0; i < TEST_SIZE; i++)
            {
                Console.WriteLine(i);
                var treeA = AptNode.GenerateTree(r.Next(1, 20), r, true);

                var childTree = treeA.BreedWith(treeA, r, true);
                var machine   = new StackMachine(childTree);
                var stack     = new float[machine.nodeCount];

                for (float y = -1.0f; y <= 1.0f; y += .005f)
                {
                    for (float x = -1.0f; x <= 1.0f; x += .005f)
                    {
                        results.Add(machine.Execute(x, y, stack));
                    }
                }
                Console.WriteLine("result[2]" + results[2]);
                results.Clear();
            }
            Console.WriteLine("done breed test");
        }
Esempio n. 2
0
        public static void Optimizing(GraphicsDevice g, GameWindow w)
        {
            const int TEST_SIZE = 10000;
            Random    r         = new Random();

            Console.WriteLine("start opt test");
            for (int i = 0; i < TEST_SIZE; i++)
            {
                Console.WriteLine(i);
                var tree    = AptNode.GenerateTree(r.Next(1, 20), r, true);
                var machine = new StackMachine(tree);

                var optTree    = AptNode.ConstantFolding(tree);
                var optMachine = new StackMachine(optTree);
                var stack      = new float[machine.nodeCount];
                var optStack   = new float[optMachine.nodeCount];

                for (float y = -1.0f; y <= 1.0f; y += .01f)
                {
                    for (float x = -1.0f; x <= 1.0f; x += .01f)
                    {
                        float result    = machine.Execute(x, y, stack);
                        float optResult = optMachine.Execute(x, y, optStack);

                        if (Math.Abs(optResult - result) > .01f)
                        {
                            Console.WriteLine("result:" + result);
                            Console.WriteLine("optResult:" + optResult);
                            Console.WriteLine("x:" + x + "y:" + y);
                            Console.WriteLine("--- tree --");
                            Console.WriteLine(tree.ToLisp());
                            Console.WriteLine("--- optTree --");
                            Console.WriteLine(optTree.ToLisp());
                            throw new Exception("opt fail");
                        }
                    }
                }
            }
            Console.WriteLine("done opt test");
        }
Esempio n. 3
0
        public Pic(PicType type, Random rand, int min, int max, GraphicsDevice g, GameWindow w, bool video, GameState state)
        {
            this.video = video;
            SharedConstructor(type, g, w, state);

            for (int i = 0; i < Trees.Length; i++)
            {
                Trees[i]    = AptNode.GenerateTree(rand.Next(min, max), rand, video);
                Machines[i] = new StackMachine(Trees[i]);
            }


            if (type == PicType.GRADIENT)
            {
                var     enum_size = Enum.GetNames(typeof(GradientType)).Length;
                var     gradType  = (GradientType)rand.Next(0, enum_size);
                float[] hues;
                switch (gradType)
                {
                case GradientType.ANALOGOUS:
                {
                    hues = new float[3];
                    (hues[0], hues[1], hues[2]) = GetAnalogousHues((float)rand.NextDouble());
                    break;
                }

                case GradientType.COMPLEMENTARY:
                {
                    hues = new float[2];
                    (hues[0], hues[1]) = GetComplementaryHues((float)rand.NextDouble());
                    break;
                }

                case GradientType.SPLIT_COMPLEMENTARY:
                {
                    hues = new float[3];
                    (hues[0], hues[1], hues[2]) = GetSplitComplementaryHues((float)rand.NextDouble());

                    break;
                }

                case GradientType.SQUARE:
                {
                    hues = new float[4];
                    (hues[0], hues[1], hues[2], hues[3]) = GetSquareHues((float)rand.NextDouble());
                    break;
                }

                case GradientType.TETRADIC:
                {
                    hues = new float[4];
                    (hues[0], hues[1], hues[2], hues[3]) = GetTetradicHues((float)rand.NextDouble());
                    break;
                }

                case GradientType.TRIADIC:
                {
                    hues = new float[3];
                    (hues[0], hues[1], hues[2]) = GetTriadicHues((float)rand.NextDouble());
                    break;
                }

                case GradientType.RANDOM:
                {
                    hues = new float[rand.Next(Settings.MIN_GRADIENTS, Settings.MAX_GRADIENTS)];
                    for (int i = 0; i < hues.Length; i++)
                    {
                        hues[i] = (float)rand.NextDouble();
                    }
                    break;
                }

                case GradientType.DOUBLE_COMPLEMENT:
                {
                    hues = new float[4];
                    (hues[0], hues[1], hues[2], hues[3]) = GetTetradicHues((float)rand.NextDouble());
                    break;
                }

                case GradientType.DIAD:
                {
                    hues = new float[2];
                    (hues[0], hues[1]) = GetComplementaryHues((float)rand.NextDouble());
                    break;
                }

                default:
                    throw new Exception("hues broke");
                }

                colors = hues.SelectF(h =>
                {
                    float s = (float)rand.NextDouble();
                    float v = (float)rand.NextDouble();
                    var(red, green, blue) = HSV2RGB(h, s, v);
                    return(new Color(red, green, blue));
                });

                pos = new float[colors.Length];
                int chance = Settings.STOP_GRADIENT_CHANCE * pos.Length;
                for (int i = 0; i < colors.Length; i++)
                {
                    if (i > 0 && rand.Next(0, chance) == 0)
                    {
                        pos[i] = pos[i - 1];
                    }
                    else
                    {
                        pos[i] = (float)(rand.NextDouble() * 2.0 - 1.0);
                    }
                }
                Array.Sort(pos);
            }
            SetupTextbox();
        }