Example #1
0
        public Form1()
        {
            InitializeComponent();
            manager = new TextureManager(this);
            map     = new Map(32, 32);
            map.AddLayer();

            KeyPreview = true;

            MainDisplay.Enabled = false;//making sure you cant click before you load
            ImageHolder.VerticalScroll.Enabled = true;
            ImageHolder.AutoScroll             = true;
            MainDisplay.AutoSize = true;


            addTexturesToolStripMenuItem.Click += new EventHandler(manager.AddTextureClick);
            tabControl1.SelectedIndexChanged   += new EventHandler(manager.TabChangedHandler);
            g = MainDisplay.CreateGraphics();
        }
Example #2
0
        void RunSwarm()
        {
            Bitmap Background = new Bitmap(10, 10);


            IntPtr mainDC;
            IntPtr memDC;
            IntPtr tempDC;
            IntPtr OffscreenBmp;
            IntPtr oldBmp;
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr prevBmp;

            IntPtr bugPen   = Win32.CreatePen(Win32.PenStyles.PS_SOLID, 1, ColorBug);
            IntPtr bugBrush = Win32.CreateSolidBrush(ColorBug);;

            IntPtr optPen   = Win32.CreatePen(Win32.PenStyles.PS_SOLID, 1, ColorOpt);
            IntPtr optBrush = Win32.CreateSolidBrush(ColorOpt);

            IntPtr oldPen;
            IntPtr oldBrush;

            int   attempts   = 0;
            int   cycleTime  = 0;
            Point displayPos = new Point();

            float[] bugValue = new float[1];


            while (!Shutdown)
            {
                ProcessEvent.WaitOne();

                // draw background to offscreen
                if (Background.Width != MainDisplay.Width / Config.Scaling || Background.Height != MainDisplay.Height / Config.Scaling)
                {
                    RefreshEvent.Set();
                }

                if (Reset)
                {
                    Reset = false;
                    Time  = 0;

                    BestGlobalValue = new float[Objectives.Length];
                    bugValue        = new float[Objectives.Length];

                    for (int i = 0; i < Objectives.Length; i++)
                    {
                        BestGlobalValue[i] = Max[i] ? float.MinValue : float.MaxValue;
                    }

                    BestGlobalCoord = new float[Config.Dimensions];

                    NonDominatingCoords.Clear();
                    NonDominatingValues.Clear();
                }

                if (SetupSwarm)
                {
                    SetupSwarm = false;

                    Bugs.Clear();

                    if (TryGetFeasible(BestGlobalCoord))
                    {
                        for (int i = 0; i < BestGlobalValue.Length; i++)
                        {
                            BestGlobalValue[i] = (float)Objectives[i].Invoke(BestGlobalCoord, Time);
                        }
                    }

                    RefreshEvent.Set();  // ensures that when new setup loaded, it is viewed in right slice
                }

                if (LatestBG != null)
                {
                    Background = LatestBG;
                    LatestBG   = null;

                    if (hBitmap != IntPtr.Zero)
                    {
                        Win32.DeleteObject(hBitmap);
                        hBitmap = IntPtr.Zero;
                    }

                    hBitmap = Background.GetHbitmap();
                }

                // start drawing
                Graphics mainGraphics = MainDisplay.CreateGraphics();
                mainDC = mainGraphics.GetHdc();

                // create memeory DC and select an offscreen bmp into it
                memDC        = Win32.CreateCompatibleDC(mainDC);
                OffscreenBmp = Win32.CreateCompatibleBitmap(mainDC, MainDisplay.Width, MainDisplay.Height);
                oldBmp       = Win32.SelectObject(memDC, OffscreenBmp);


                tempDC  = Win32.CreateCompatibleDC(mainDC);
                prevBmp = Win32.SelectObject(tempDC, hBitmap);
                Win32.StretchBlt(memDC, 0, 0, MainDisplay.Width, MainDisplay.Height, tempDC, 0, 0, Background.Width, Background.Height, (int)Win32.SRCCOPY);
                Win32.SelectObject(tempDC, prevBmp);
                Win32.DeleteDC(tempDC);


                // draw the swarm
                oldPen   = Win32.SelectObject(memDC, bugPen);
                oldBrush = Win32.SelectObject(memDC, bugBrush);


                // add/remove bugs
                while (Bugs.Count > Config.Entities)
                {
                    Bugs.RemoveAt(0);
                }

                if (Config.Entities > Bugs.Count)
                {
                    int add = Bugs.Count; // AddBug can fail so we only interate a set amount
                    for (int i = 0; i < Config.Entities - add; i++)
                    {
                        AddBug();
                    }
                }


                // use cycle time so that we can 'see' particles moving towards new destination, otherwise it looks like random jumps
                foreach (Particle bug in Bugs)
                {
                    if (Play || Step)
                    {
                        if (cycleTime == 0)
                        {
                            // get velocity   THIS IS THE MEAT
                            if (Config.Replusion)
                            {
                                Particle randBug = Bugs[RndGen.Next(0, Bugs.Count - 1)];

                                for (int i = 0; i < Config.Dimensions; i++)
                                {
                                    bug.TestV[i] = Config.Inertia * bug.Velocity[i] +
                                                   Config.PersonalPref * (float)RndGen.NextDouble() * (bug.Best[i] - bug.Postion[i]) +
                                                   -1 * Config.GlobalPref * (float)RndGen.NextDouble() * /* Config.Inertia */ (randBug.Best[i] - bug.Postion[i]) +
                                                   2 * (float)RndGen.NextDouble() * /* Config.Inertia */ (float)RndGen.NextDouble();
                                }
                            }
                            else
                            {
                                // choose a random best global to go towards
                                var bestGlobal = BestGlobalCoord;
                                if (NonDominatingCoords.Count > 0)
                                {
                                    bestGlobal = NonDominatingCoords[RndGen.Next(NonDominatingCoords.Count)];
                                }

                                for (int i = 0; i < Config.Dimensions; i++)
                                {
                                    bug.TestV[i] = Config.Inertia * bug.Velocity[i] +
                                                   Config.PersonalPref * (float)RndGen.NextDouble() * (bug.Best[i] - bug.Postion[i]) +
                                                   Config.GlobalPref * (float)RndGen.NextDouble() * (bestGlobal[i] - bug.Postion[i]);
                                }
                            }

                            bug.MoveNext = false;

                            attempts = 20;
                            while (attempts > 0)
                            {
                                // if new position feasible switch set velocity to the test
                                if (IsFeasible(bug.Postion, bug.TestV))
                                {
                                    float[] velocity = bug.TestV;
                                    bug.TestV    = bug.Velocity;
                                    bug.Velocity = velocity;

                                    bug.MoveNext = true;

                                    break;
                                }
                                // while outside bounds, halve velocity and recalc
                                else
                                {
                                    for (int i = 0; i < Config.Dimensions; i++)
                                    {
                                        bug.TestV[i] = bug.TestV[i] / 2;
                                    }

                                    attempts--;
                                }
                            }
                        }

                        // set next position
                        if (bug.MoveNext)
                        {
                            for (int i = 0; i < Config.Dimensions; i++)
                            {
                                bug.Postion[i] = bug.Postion[i] + bug.Velocity[i] / Config.FlyTime;
                            }
                        }


                        // let the particles fly a bit, take their values every so often
                        if (cycleTime == 0)
                        {
                            // check for personal / global best
                            for (int i = 0; i < Objectives.Length; i++)
                            {
                                bugValue[i] = (float)Objectives[i].Invoke(bug.Postion, Time);
                            }

                            // if time used in objectives, then values at coords need to be re-evaluated
                            if (Config.TimeUsed)
                            {
                                for (int i = 0; i < Objectives.Length; i++)
                                {
                                    BestGlobalValue[i] = (float)Objectives[i].Invoke(BestGlobalCoord, Time);
                                }

                                for (int i = 0; i < NonDominatingCoords.Count; i++)
                                {
                                    for (int x = 0; x < Objectives.Length; x++)
                                    {
                                        NonDominatingValues[i][x] = (float)Objectives[x].Invoke(NonDominatingCoords[i], Time);
                                    }
                                }
                            }

                            // if the position is better or non-dominates it's best
                            if (NonDominatesPoint(bugValue, bug.BestValue))
                            {
                                bugValue.CopyTo(bug.BestValue, 0);

                                bug.Postion.CopyTo(bug.Best, 0);
                            }

                            // if the bug position is not dominated by prev solutions
                            if (NonDominatesSet(bug.Postion, bugValue))
                            {
                                bug.Postion.CopyTo(BestGlobalCoord, 0);

                                // redraw if our view plane changed because a gobal optimal changed
                                if (Config.Dimensions > 2)
                                {
                                    RefreshEvent.Set();
                                }

                                bugValue.CopyTo(BestGlobalValue, 0);

                                // record points in non-dominating set
                                var bestCoord = new float[BestGlobalCoord.Length];
                                var bestValue = new float[bugValue.Length];

                                BestGlobalCoord.CopyTo(bestCoord, 0);
                                bugValue.CopyTo(bestValue, 0);

                                NonDominatingCoords.Add(bestCoord);
                                NonDominatingValues.Add(bestValue);
                                if (NonDominatingCoords.Count > SolutionsKept)
                                {
                                    NonDominatingCoords.RemoveAt(0);
                                    NonDominatingValues.RemoveAt(0);
                                }

                                BeginInvoke(new Voidhandler(StatusBarUpdate));
                            }
                        }
                    }

                    // draw
                    displayPos = GraphtoWindow(bug.Postion);
                    Win32.Ellipse(memDC, displayPos.X - BugSize, displayPos.Y - BugSize, displayPos.X + BugSize, displayPos.Y + BugSize);
                }

                Win32.SelectObject(memDC, oldPen);
                Win32.SelectObject(memDC, oldBrush);


                // draw global optimum
                oldPen   = Win32.SelectObject(memDC, optPen);
                oldBrush = Win32.SelectObject(memDC, optBrush);

                displayPos = GraphtoWindow(BestGlobalCoord);
                Win32.Ellipse(memDC, displayPos.X - BugSize, displayPos.Y - BugSize, displayPos.X + BugSize, displayPos.Y + BugSize);

                foreach (var coord in NonDominatingCoords)
                {
                    displayPos = GraphtoWindow(coord);
                    Win32.Ellipse(memDC, displayPos.X - 1, displayPos.Y - 1, displayPos.X + 1, displayPos.Y + 1);
                }

                Win32.SelectObject(memDC, oldPen);
                Win32.SelectObject(memDC, oldBrush);

                // copy to main screen
                Win32.BitBlt(mainDC, 0, 0, MainDisplay.Width, MainDisplay.Height, memDC, 0, 0, Win32.TernaryRasterOperations.SRCCOPY);


                // release objects
                Win32.SelectObject(memDC, oldBmp);
                Win32.DeleteObject(OffscreenBmp);

                Win32.DeleteDC(memDC);
                mainGraphics.ReleaseHdc(mainDC);


                // finish
                if (Play || Step)
                {
                    cycleTime++;

                    if (cycleTime >= Config.FlyTime)
                    {
                        cycleTime = 0;
                    }

                    if (Config.TimeInc != 0)
                    {
                        Time += Config.TimeInc;
                        RefreshEvent.Set();

                        for (int i = 0; i < BestGlobalValue.Length; i++)
                        {
                            BestGlobalValue[i] = (float)Objectives[i].Invoke(BestGlobalCoord, Time); // value of best position has changed under the new time slice
                        }
                    }
                }

                Step = false;

                //Thread.Sleep(20);

                if (Play)
                {
                    ProcessEvent.Set();
                }
            }

            if (hBitmap != IntPtr.Zero)
            {
                Win32.DeleteObject(hBitmap);
                hBitmap = IntPtr.Zero;
            }
        }