예제 #1
0
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();
            for (int y = 0; y < G.GetSize(); y++)
            {
                for (int x = 0; x < G.GetSize(); x++)
                {
                    int   v  = G.GetAtom(x, y);
                    Color pc = ColorMap[v];
                    _spriteBatch.Draw(OnePx, new Rectangle(x * CellWidth, y * CellHeight, CellWidth, CellHeight), pc);
                }
            }
            _spriteBatch.End();

            {
                // Do some simulated annealing.
                // Putting this in Update method ruins the animation due to large computation time.
                for (int i = 0; i < G.GetSize() * G.GetSize() * 10; i++)
                {
                    SA.AnnealStep();
                }

                // Slowly reduce temperature but don't allow it to go too low.
                if (SA.T > 0.1)
                {
                    SA.T *= 0.99f;
                }
            }


            base.Draw(gameTime);
        }
예제 #2
0
        protected override void Initialize()
        {
            OnePx = new Texture2D(_graphics.GraphicsDevice, 1, 1);
            OnePx.SetData <Color>(new Color[1] {
                Color.White
            });

            // NOTE: Too large a grid takes a long time per frame.
            G  = new AtomGrid(14, 4);
            SA = new SimulatedAnnealing(G);
            //SA.Anneal();

            _graphics.PreferredBackBufferWidth  = G.GetSize() * CellWidth;
            _graphics.PreferredBackBufferHeight = G.GetSize() * CellHeight;
            _graphics.ApplyChanges();
            base.Initialize();
        }
        public void Anneal()
        {
            Debug.WriteLine("Starting energy: {0}", G.GetSystemEnergy());
            int iters = 0;

            while (T > 0 && iters++ < 35)
            {
                // Technically we should loop until we reach a thermal equilibrium at this temperature.
                // Just assume many iterations reach this point.
                int totalReduction = 0;
                for (int i = 0; i < G.GetSize() * G.GetSize() * 10; i++)
                {
                    int dE = AnnealStep();
                    totalReduction += dE;
                }

                // If we are still reducing E then continue, else give-in
                // Currently we just do a fixed num of steps in here.
                T = T * 0.9f; // Reduce temperature
            }
            Debug.WriteLine("Finished annealing state, final energy = {0}, T={1}", G.GetSystemEnergy(), T);
        }