Exemple #1
0
        public static void Draw()
        {
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], GrapherWindowPosition, null, new Color(80, 80, 80, 200), 0f, Vector2.Zero, GrapherWindowSize, SpriteEffects.None, 0f);

            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], GrapherWindowPosition, null, Color.Black, 0f, Vector2.Zero, new Vector2(2, GrapherWindowSize.Y), SpriteEffects.None, 0.001f);
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], GrapherWindowPosition, null, Color.Black, 0f, Vector2.Zero, new Vector2(GrapherWindowSize.X, 2), SpriteEffects.None, 0.001f);
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], GrapherWindowPosition + new Vector2(GrapherWindowSize.X, 0), null, Color.Black, 0f, Vector2.Zero, new Vector2(2, GrapherWindowSize.Y), SpriteEffects.None, 0.001f);
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], GrapherWindowPosition + new Vector2(0, GrapherWindowSize.Y), null, Color.Black, 0f, Vector2.Zero, new Vector2(GrapherWindowSize.X + 2, 2), SpriteEffects.None, 0.001f);

            btn_close.Draw();
            btn_median.Draw();
            btn_average.Draw();
            btn_highest.Draw();
            btn_lowest.Draw();

            List <float> vals = new List <float>();

            List <Generation> gens = new List <Generation>();

            foreach (Generation ge in GeneticAlgorithm.thisEvolution.Generations)
            {
                gens.Add(ge);
            }

            gens.Add(new Generation(GeneticAlgorithm.CurrentGeneration));

            for (int i = 0; i < gens.Count / XScale; i++)
            {
                float val = 0;

                for (int j = 0; j < XScale; j++)
                {
                    switch (graphType)
                    {
                    case Grapher.Average:
                        val += gens[i * XScale + j].AverageFitness;

                        break;

                    case Grapher.Median:
                        val += gens[i * XScale + j].MedianFitness;

                        break;

                    case Grapher.Top:
                        val += gens[i * XScale + j].TopFitness;

                        break;

                    case Grapher.Lowest:
                        val += gens[i * XScale + j].LowestFitness;

                        break;
                    }
                }

                val /= XScale;

                val = MathHelper.Clamp(val, 0, val);

                vals.Add(val);
            }

            if (vals.Count > 0)
            {
                graph.MaxValue = GetHighest(vals.ToArray());
            }

            graph.Draw(vals, Color.Red);

            GameHelper.SpriteBatch.End();
            GameHelper.SpriteBatch.Begin();

            if (MouseWithinWindow())
            {
                if (InputManager.MouseVelocityScreen == Vector2.Zero)
                {
                    timeHovered += (float)GameHelper.GameTime.ElapsedGameTime.TotalSeconds;
                }
                else
                {
                    timeHovered = 0;
                }

                int mouseGeneration = -1;

                float deltaX = (float)graph.Size.X / vals.Count;

                float mouseX = InputManager.MousePositionScreen.X;
                mouseX = mouseX - GrapherWindowPosition.X - 20;

                mouseGeneration = (int)Math.Round(mouseX / deltaX);

                if (mouseGeneration + 1 != vals.Count && mouseGeneration < vals.Count)
                {
                    GameHelper.DrawLine(new Vector2(deltaX * mouseGeneration + GrapherWindowPosition.X + 20, GrapherWindowPosition.Y + 50), new Vector2(deltaX * mouseGeneration + GrapherWindowPosition.X + 20, GrapherWindowPosition.Y + GrapherWindowSize.Y - 20), Color.Red * 0.5f, 1, 0f);
                    DrawToolTip(new Vector2(deltaX * mouseGeneration + GrapherWindowPosition.X + 25, GrapherWindowPosition.Y + 20 + graph.Size.Y - ((vals[mouseGeneration * XScale] / graph.MaxValue) * graph.Size.Y)), "Gen: " + mouseGeneration * XScale + "\n" + graphType.ToString() + ": " + vals[mouseGeneration * XScale].ToString());
                }

                if (timeHovered > timeToHover)
                {
                    // Show tooltip.

                    DrawToolTip(InputManager.MousePositionScreen + new Vector2(30, 0), "Right Click: Simulate\nLeft Shift + Right Click: View");
                }

                if (InputManager.PressedMouseRight() && gen == -1)
                {
                    if (!InputManager.KeyPressing(Microsoft.Xna.Framework.Input.Keys.LeftShift))
                    {
                        // Load generation.

                        ScreenEvo.btn_resumeEvolution.Clickable = true;

                        gen   = GeneticAlgorithm.Generation;
                        saved = new Generation(GeneticAlgorithm.CurrentGeneration).CloneEvolvis();

                        List <Evolvi> loadGen = GeneticAlgorithm.thisEvolution.Generations[mouseGeneration * XScale].CloneEvolvis();

                        GeneticAlgorithm.Generation        = mouseGeneration;
                        GeneticAlgorithm.CurrentGeneration = loadGen;

                        GeneticAlgorithm.GenerationTime        = 0;
                        GeneticAlgorithm.ElapsedGenerationTime = 0;
                    }
                    else
                    {
                        // VIEW INDIVIDUALS OF THAT GENERATION

                        if (GeneticAlgorithm.EvolutionSpeed != 0)
                        {
                            ScreenEvo.btn_pauseEvo.SimulatePress();
                        }

                        MyIndividualViewer.Viewing  = new Generation(GeneticAlgorithm.thisEvolution.Generations[mouseGeneration * XScale].CloneEvolvis());
                        MyIndividualViewer.IsActive = true;
                    }
                }
            }
            else
            {
                timeHovered = 0;
            }
        }
Exemple #2
0
        public void DrawNetwork(Network network)
        {
            Network ne = network;

            int   xOffset    = 200;
            int   yOffset    = 30;
            float baseoffset = 300;
            float lastOffset = 0;

            for (int i = 0; i < ne.LayerCount; i++)
            {
                Layer l       = ne.Layers[i];
                float yoffset = l.Neurons.Count / 2f * yOffset;

                for (int j = 0; j < l.NeuronCount; j++)
                {
                    Neuron n = l.Neurons[j];


                    string te = n.ConnectionCount.ToString();

                    if (te.Length > 3)
                    {
                        te = te.Substring(0, 3);
                    }

                    string val = n.Value.ToString();

                    if (n.Value.ToString().Length > 4)
                    {
                        val = n.Value.ToString().Substring(0, 4);
                    }

                    if (n.Bias != 0)
                    {
                        GameHelper.SpriteBatch.DrawString(GameHelper.Font, "B", new Vector2(50 + i * xOffset - (float)((n.Bias * 64f) / 2f), baseoffset - yoffset + j * yOffset - (float)((n.Bias * 64f) / 2f)), Color.Black);
                    }

                    if (n.Bias > 0)
                    {
                        GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], new Vector2(50 + i * xOffset - (float)((n.Bias * 64f) / 2f), baseoffset - yoffset + j * yOffset - (float)((n.Bias * 64f) / 2f)), null, Color.Pink, 0f, Vector2.Zero, (float)Math.Abs(n.Bias * 32f), SpriteEffects.None, 0f);
                    }
                    else
                    {
                        GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], new Vector2(50 + i * xOffset - (float)((n.Bias * 64f) / 2f), baseoffset - yoffset + j * yOffset - (float)((n.Bias * 64f) / 2f)), null, Color.Green, 0f, Vector2.Zero, (float)Math.Abs(n.Bias * 32f), SpriteEffects.None, 0f);
                    }

                    GameHelper.SpriteBatch.DrawString(GameHelper.Font, val, new Vector2(50 + i * xOffset, baseoffset - yoffset + j * yOffset), Color.Black);


                    for (int h = 0; h < n.ConnectionCount; h++)
                    {
                        Connection c = n.Connections[h];

                        Color co = Color.Black;

                        if (c.Weight > 0)
                        {
                            co = Color.White;
                        }

                        Vector2 start = new Vector2(50 + i * xOffset, baseoffset - yoffset + j * yOffset);
                        Vector2 end   = new Vector2(50 - xOffset + i * xOffset, baseoffset - lastOffset + h * yOffset);

                        string we = "";

                        if (c.Weight.ToString().Length > 4)
                        {
                            we = c.Weight.ToString().Substring(0, 4);
                        }

                        //spriteBatch.DrawString(fnt, we, ((start + end) / 2f), Color.Black,  + MathHelper.Pi, Vector2.Zero, 1f, SpriteEffects.None, 0f);

                        if (c.Weight != 0)
                        {
                            GameHelper.DrawLine(start, end, co, MathHelper.Clamp((int)(Math.Abs(c.Weight * 10f)), 1, 10), 1f);
                        }
                    }
                }

                lastOffset = yoffset;
            }
        }
Exemple #3
0
        public static void Draw()
        {
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], ViewerPosition, null, new Color(80, 80, 80, 200), 0f, Vector2.Zero, ViewerSize, SpriteEffects.None, 0f);

            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], ViewerPosition, null, Color.Black, 0f, Vector2.Zero, new Vector2(2, ViewerSize.Y), SpriteEffects.None, 0.001f);
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], ViewerPosition, null, Color.Black, 0f, Vector2.Zero, new Vector2(ViewerSize.X, 2), SpriteEffects.None, 0.001f);
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], ViewerPosition + new Vector2(ViewerSize.X, 0), null, Color.Black, 0f, Vector2.Zero, new Vector2(2, ViewerSize.Y), SpriteEffects.None, 0.001f);
            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], ViewerPosition + new Vector2(0, ViewerSize.Y), null, Color.Black, 0f, Vector2.Zero, new Vector2(ViewerSize.X + 2, 2), SpriteEffects.None, 0.001f);

            btn_close.Draw();

            if (Viewing != null)
            {
                int amount = Viewing.Evolvis.Count();

                int squarRoot = (int)Math.Sqrt(Viewing.Evolvis.Count()) + 1;

                int x = (int)Math.Ceiling((double)amount / squarRoot);

                float xScale = ((ViewerSize.X) / x);
                float yScale = ((ViewerSize.Y - 80) / squarRoot);

                for (int yy = 0; yy < squarRoot; yy++)
                {
                    for (int xx = 0; xx < x; xx++)
                    {
                        int thisEvolvi = (yy * squarRoot) + xx;

                        if (thisEvolvi < amount)
                        {
                            if (Viewing.Evolvis[thisEvolvi] == Viewing.Fittest)
                            {
                                Viewing.Evolvis[thisEvolvi].CustomDraw(ViewerPosition + new Vector2(50, 80) + new Vector2(xScale * xx, yScale * yy), 0f, 2f, Color.Gold);
                            }
                            else
                            {
                                Viewing.Evolvis[thisEvolvi].CustomDraw(ViewerPosition + new Vector2(50, 80) + new Vector2(xScale * xx, yScale * yy), 0f, 2f);
                            }

                            Viewing.Evolvis[thisEvolvi].Movement = new Vector2(2.5f, 0);
                            GameHelper.SpriteBatch.DrawString(GameHelper.Font, (thisEvolvi + 1).ToString(), ViewerPosition + new Vector2(50, 80) + new Vector2(xScale * xx, yScale * yy) + new Vector2(0, yScale / 2f), Color.White);
                        }
                    }
                }

                int mouseGeneration = -1;

                Rectangle viewRec = new Rectangle(ViewerPosition.ToPoint() + new Point(0, 60), new Point((int)(squarRoot * yScale) + 100, (int)(x * xScale) + 160));

                if (InputManager.MouseBoxScreen.Intersects(viewRec))
                {
                    int mX = (int)Math.Round((InputManager.MousePositionScreen - ViewerPosition - new Vector2(50, 80)).X / xScale);
                    int mY = (int)Math.Round((InputManager.MousePositionScreen - ViewerPosition - new Vector2(50, 80)).Y / yScale);


                    mouseGeneration = ((mY * squarRoot) + mX);

                    if (mouseGeneration < amount)
                    {
                        if (InputManager.PressedMouseLeft())
                        {
                            SelectedEvolvi = new Vector3(mX, mY, mouseGeneration);
                        }
                    }

                    if (SelectedEvolvi.Z != -1)
                    {
                        GameHelper.DrawLine(ViewerPosition + new Vector2(50, 80) + new Vector2(xScale * SelectedEvolvi.X, yScale * SelectedEvolvi.Y), InputManager.MousePositionScreen + new Vector2(15, 0), Color.Black, 5, 0f);
                        //DrawToolTip(InputManager.MousePositionScreen + new Vector2(30, 0), mX.ToString() + "\n" + mY.ToString() + "\n" + mouseGeneration.ToString());
                        DrawToolTipIndividual(InputManager.MousePositionScreen + new Vector2(15, 0), Viewing.Evolvis[(int)SelectedEvolvi.Z]);
                    }
                }
            }
        }