Пример #1
0
 public void Run(Cells cells, CellularAutomataRules rules)
 {
     _cells    = cells;
     _ptOffset = CalculateOffset();
     Rules     = (rules != null) ? rules : _rules;
     _timer.Start();
 }
Пример #2
0
        private void ExportAsAnimatedGifWorker(string outputFilePath, ProgressBox box,
                                               int numFramesPreamble, int numFrames)
        {
            Cells cells = _genAlg.CaSettings.GetInitialCells(_genAlg.GaSettings.InitialStateDistribution);
            CellularAutomataRules rules    = ctrlCellularAutomata.Rules;
            NeighborhoodFunction  function = _genAlg.CaSettings.NeighborhoodFunction;
            CellPainter           painter  = _genAlg.CaSettings.CellStructure.Painter;

            for (int i = 0; i < numFramesPreamble; i++)
            {
                cells = cells.ApplyRules(rules, function);
                box.Invoke(
                    new Action(
                        () => box.Increment()
                        )
                    );
            }

            using (FileStream fs = new FileStream(outputFilePath, FileMode.Create))
                using (GifEncoder encoder = new GifEncoder(fs, cells.Columns * 2, cells.Rows * 2))
                {
                    Bitmap bmp    = new Bitmap(cells.Columns * 2, cells.Rows * 2);
                    Point  offset = new Point(0, 0);
                    for (int i = 0; i < numFrames; i++)
                    {
                        painter.PaintBitmap(bmp, cells, offset, ctrlCellularAutomata.Colors);
                        encoder.AddFrame(Image.FromHbitmap(bmp.GetHbitmap()), 0, 0, new TimeSpan(0));
                        cells = cells.ApplyRules(rules, function);

                        box.Increment();
                    }
                }

            box.Finish();
        }
Пример #3
0
 public Cells ApplyRules(CellularAutomataRules rules, NeighborhoodFunction func)
 {
     Cells cells = (Cells) this.Clone();
     for (int row = 0; row < Rows; row++)
         for (int col = 0; col < Columns; col++)
             cells[col, row] = rules.GetSuccessorState(this[col, row], func.Map(this.Neighbors(col, row), rules.NumStates));
     return cells;
 }
Пример #4
0
        private void SaveRules(string fileName)
        {
            CellularAutomataRules rules = _genAlg.CurrentRules;
            var settingsCa = _genAlg.CaSettings;
            var saveData   = new RulesSaveData(settingsCa, rules.StateTable);

            saveData.Save(fileName);
        }
Пример #5
0
 private void GenerateNewCa()
 {
     if (PreviousGeneration == null)
     {
         _currentCa = _settingsCa.GetRandomRules(_settingsGa.StateTableDistribution);
     }
     else
     {
         _currentCa = GenerateChildCa(PreviousGeneration);
     }
 }
Пример #6
0
        public Cells ApplyRules(CellularAutomataRules rules, NeighborhoodFunction func)
        {
            Cells cells = (Cells)this.Clone();

            for (int row = 0; row < Rows; row++)
            {
                for (int col = 0; col < Columns; col++)
                {
                    cells[col, row] = rules.GetSuccessorState(this[col, row], func.Map(this.Neighbors(col, row), rules.NumStates));
                }
            }
            return(cells);
        }
Пример #7
0
 public override CellularAutomataRules Generate(List<CellularAutomataRules> parents)
 {
     if (parents.Count() != NumParents || !parents[0].HasSameDimensions(parents[1]))
         throw new ArgumentException();
     var child = new CellularAutomataRules(parents[0].Range, parents[0].NumStates);
     var rndParent = RulesDistribution(parents);
     for (int row = 0; row < child.NumStates; row++)
     {
         CellularAutomataRules parent = rndParent.Next();
         for (int col = 0; col < child.Range; col++)
             child[col, row] = parent[col, row];
     }
     return child;
 }
Пример #8
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n      = Util.RndUniformInt((int)(ca.Range * ca.NumStates * settings.MutationTemperature)) + 1;

            for (int i = 0; i < n; i++)
            {
                int row = Util.RndUniformInt(ca.NumStates);
                int col = Util.RndUniformInt(ca.Range);

                mutant[col, row] = settings.StateTableDistribution.Next();
            }
            return(mutant);
        }
Пример #9
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n = Util.RndUniformInt((int)(ca.NumStates * settings.MutationTemperature)) + 1;
            for (int i = 0; i < n; i++)
            {
                int rowSrc = Util.RndUniformInt(ca.NumStates);
                int rowDest = rowSrc;
                while (rowDest == rowSrc)
                    rowDest = Util.RndUniformInt(ca.NumStates);

                for (int column = 0; column < ca.Range; column++)
                    mutant[column, rowDest] = mutant[column, rowSrc];
            }
            return mutant;
        }
Пример #10
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n = Util.RndUniformInt((int)(ca.Range * settings.MutationTemperature)) + 1;
            for (int i = 0; i < n; i++)
            {
                int colSrc = Util.RndUniformInt(ca.Range);
                int colDest = colSrc;
                while (colDest == colSrc)
                    colDest = Util.RndUniformInt(ca.Range);

                for (int row = 0; row < ca.NumStates; row++)
                    mutant[colDest, row] = mutant[colSrc, row];
            }
            return mutant;
        }
Пример #11
0
        public override CellularAutomataRules Generate(List <CellularAutomataRules> parents)
        {
            if (parents.Count() != NumParents || !parents[0].HasSameDimensions(parents[1]))
            {
                throw new ArgumentException();
            }
            var child = new CellularAutomataRules(parents[0].Range, parents[0].NumStates);

            for (int col = 0; col < child.Range; col++)
            {
                for (int row = 0; row < child.NumStates; row++)
                {
                    child[col, row] = (parents[0][col, row] + parents[1][col, row]) % child.NumStates;
                }
            }
            return(child);
        }
Пример #12
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n      = Util.RndUniformInt((int)(10.0 * settings.MutationTemperature)) + 1;

            for (int i = 0; i < n; i++)
            {
                int row, col;
                int count = 0;
                do
                {
                    row = Util.RndUniformInt(ca.NumStates);
                    col = Util.RndUniformInt(ca.Range);
                } while (mutant[col, row] == 0 && count++ < 20);
                mutant[col, row] = 0;
            }
            return(mutant);
        }
Пример #13
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n      = Util.RndUniformInt((int)(ca.Range * settings.MutationTemperature)) + 1;

            for (int i = 0; i < n; i++)
            {
                int colSrc  = Util.RndUniformInt(ca.Range);
                int colDest = colSrc;
                while (colDest == colSrc)
                {
                    colDest = Util.RndUniformInt(ca.Range);
                }

                for (int row = 0; row < ca.NumStates; row++)
                {
                    mutant[colDest, row] = mutant[colSrc, row];
                }
            }
            return(mutant);
        }
Пример #14
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n      = Util.RndUniformInt((int)(ca.NumStates * settings.MutationTemperature)) + 1;

            for (int i = 0; i < n; i++)
            {
                int rowSrc  = Util.RndUniformInt(ca.NumStates);
                int rowDest = rowSrc;
                while (rowDest == rowSrc)
                {
                    rowDest = Util.RndUniformInt(ca.NumStates);
                }

                for (int column = 0; column < ca.Range; column++)
                {
                    mutant[column, rowDest] = mutant[column, rowSrc];
                }
            }
            return(mutant);
        }
Пример #15
0
        public override CellularAutomataRules Generate(List <CellularAutomataRules> parents)
        {
            if (parents.Count() != NumParents)
            {
                throw new ArgumentException();
            }
            var child  = new CellularAutomataRules(parents[0].Range, parents[0].NumStates);
            var parent = parents[0];

            int columns  = parents[0].Range;
            int halfCols = columns / 2;

            for (int row = 0; row < child.NumStates; row++)
            {
                for (int i = 0; i < halfCols; i++)
                {
                    child[i, row] = parent[i, row];
                    child[columns - i - 1, row] = parent[i, row];
                }
            }
            return(child);
        }
Пример #16
0
        public override CellularAutomataRules Generate(List <CellularAutomataRules> parents)
        {
            if (parents.Count() != NumParents)
            {
                throw new ArgumentException();
            }
            var child  = new CellularAutomataRules(parents[0].Range, parents[0].NumStates);
            var parent = parents[0];

            int rows     = parents[0].NumStates;
            int halfRows = rows / 2;

            for (int i = 0; i < halfRows; i++)
            {
                for (int col = 0; col < child.Range; col++)
                {
                    child[col, i]            = parent[col, i];
                    child[col, rows - i - 1] = parent[col, i];
                }
            }
            return(child);
        }
Пример #17
0
 public abstract CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings);
Пример #18
0
 public void Run(Cells cells, CellularAutomataRules rules)
 {
     _cells = cells;
     _ptOffset = CalculateOffset();
     Rules = (rules != null) ? rules : _rules;
     _timer.Start();
 }
Пример #19
0
 internal void Clear()
 {
     Stop();
     Rules = null;
 }
Пример #20
0
 public void Start()
 {
     _generations = new List<Generation>();
     _currentCa = _settingsCa.GetRandomRules(_settingsGa.StateTableDistribution);
 }
Пример #21
0
 public CellularAutomataRules(CellularAutomataRules ca)
 {
     _stateTable = (int[, ])ca._stateTable.Clone();
 }
Пример #22
0
 public bool HasSameDimensions(CellularAutomataRules r)
 {
     return NumStates == r.NumStates && Range == r.Range;
 }
Пример #23
0
 public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
 {
     var mutant = new CellularAutomataRules(ca);
     int n = Util.RndUniformInt((int)(10.0 * settings.MutationTemperature)) + 1;
     for (int i = 0; i < n; i++)
     {
         int row, col;
         int count = 0;
         do
         {
             row = Util.RndUniformInt(ca.NumStates);
             col = Util.RndUniformInt(ca.Range);
         } while (mutant[col, row] == 0 && count++ < 20);
         mutant[col, row] = 0;
     }
     return mutant;
 }
Пример #24
0
 public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
 {
     return(ca);
 }
Пример #25
0
 public abstract CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings);
Пример #26
0
        public void LoadRules()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter      = "Lifelike JSON|*.json";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            DialogResult result = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (result != DialogResult.OK)
            {
                return;
            }

            RulesSaveData data = RulesSaveData.Load(openFileDialog1.FileName);

            if (data == null)
            {
                return;
            }

            CellularAutomataRules rules = new CellularAutomataRules(data.StateTable);

            try
            {
                if (!_genAlg.IsInProgess)
                {
                    ctrlCellularAutomataSettings.Set(data.CellStructure, rules.NumStates, data.NeighborhoodFunction);
                    ctrlGeneticAlgorithmSettings.SetNumStates(rules.NumStates);
                }
                else
                {
                    if (_genAlg.CaSettings.NeighborhoodFunction.Name != data.NeighborhoodFunction ||
                        _genAlg.CaSettings.NumStates != rules.NumStates ||
                        _genAlg.CaSettings.CellStructure.Name != data.CellStructure)
                    {
                        MessageBox.Show(
                            "Neighbor function, cell structure, and/or number states do not match the currently running genetic algorithm settings",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                        return;
                    }
                    _genAlg.CurrentRules = rules;
                }

                if (data.CustomColors != null)
                {
                    Colors = data.CustomColors.Select(
                        ary => Color.FromArgb(ary[0], ary[1], ary[2])
                        ).ToList();
                }

                Cells cells = _genAlg.CaSettings.GetInitialCells(_genAlg.GaSettings.InitialStateDistribution);
                ctrlCellularAutomata.Run(cells, rules);
            }
            catch (Exception)
            {
                MessageBox.Show(
                    "Invalid CA rules file.",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }
Пример #27
0
        public override CellularAutomataRules Generate(List<CellularAutomataRules> parents)
        {
            if (parents.Count() != NumParents)
                throw new ArgumentException();
            var child = new CellularAutomataRules(parents[0].Range, parents[0].NumStates);
            var parent = parents[0];

            int rows = parents[0].NumStates;
            int halfRows = rows / 2;
            for (int i = 0; i < halfRows; i++ )
                for (int col = 0; col < child.Range; col++)
                {
                    child[col, i] = parent[col, i];
                    child[col, rows - i - 1] = parent[col, i];
                }
            return child;
        }
Пример #28
0
        public override CellularAutomataRules Generate(List<CellularAutomataRules> parents)
        {
            if (parents.Count() != NumParents)
                throw new ArgumentException();
            var child = new CellularAutomataRules(parents[0].Range, parents[0].NumStates);
            var parent = parents[0];

            int columns = parents[0].Range;
            int halfCols = columns / 2;
            for (int row = 0; row < child.NumStates; row++)
                for (int i = 0; i < halfCols; i++)
                {
                    child[i, row] = parent[i, row];
                    child[columns - i - 1, row] = parent[i, row];
                }
            return child;
        }
Пример #29
0
 public void Add(CellularAutomataRules rules)
 {
     _ca.Insert(rules, rules.Fitness);
 }
Пример #30
0
 public CellularAutomataRules(CellularAutomataRules ca)
 {
     _stateTable = (int[,]) ca._stateTable.Clone();
 }
Пример #31
0
 public void Add(CellularAutomataRules rules)
 {
     _ca.Insert(rules, rules.Fitness);
 }
Пример #32
0
        public void LoadRules()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Lifelike JSON|*.json";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            DialogResult result = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (result != DialogResult.OK)
                return;

            RulesSaveData data = RulesSaveData.Load(openFileDialog1.FileName);
            if (data == null)
                return;

            CellularAutomataRules rules = new CellularAutomataRules(data.StateTable);
            try
            {
                if (!_genAlg.IsInProgess)
                {
                    ctrlCellularAutomataSettings.Set(data.CellStructure, rules.NumStates, data.NeighborhoodFunction);
                    ctrlGeneticAlgorithmSettings.SetNumStates(rules.NumStates);
                }
                else
                {
                    if (_genAlg.CaSettings.NeighborhoodFunction.Name != data.NeighborhoodFunction ||
                            _genAlg.CaSettings.NumStates != rules.NumStates ||
                            _genAlg.CaSettings.CellStructure.Name != data.CellStructure)
                    {
                        MessageBox.Show(
                            "Neighbor function, cell structure, and/or number states do not match the currently running genetic algorithm settings",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                        );
                        return;
                    }
                    _genAlg.CurrentRules = rules;
                }

                if (data.CustomColors != null)
                    Colors = data.CustomColors.Select(
                        ary => Color.FromArgb(ary[0], ary[1], ary[2])
                    ).ToList();

                Cells cells = _genAlg.CaSettings.GetInitialCells(_genAlg.GaSettings.InitialStateDistribution);
                ctrlCellularAutomata.Run(cells, rules);
            }
            catch (Exception)
            {
                MessageBox.Show(
                    "Invalid CA rules file.",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                );
            }
        }
Пример #33
0
        public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
        {
            var mutant = new CellularAutomataRules(ca);
            int n = Util.RndUniformInt((int)(ca.Range * ca.NumStates * settings.MutationTemperature))+1;
            for (int i = 0; i < n; i++)
            {
                int row = Util.RndUniformInt(ca.NumStates);
                int col = Util.RndUniformInt(ca.Range);

                mutant[col, row] = settings.StateTableDistribution.Next();
            }
            return mutant;
        }
Пример #34
0
 public bool HasSameDimensions(CellularAutomataRules r)
 {
     return(NumStates == r.NumStates && Range == r.Range);
 }
Пример #35
0
 public void Start()
 {
     _generations = new List <Generation>();
     _currentCa   = _settingsCa.GetRandomRules(_settingsGa.StateTableDistribution);
 }
Пример #36
0
 private void GenerateNewCa()
 {
     if (PreviousGeneration == null)
         _currentCa = _settingsCa.GetRandomRules( _settingsGa.StateTableDistribution );
     else
         _currentCa = GenerateChildCa(PreviousGeneration);
 }
Пример #37
0
 public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings)
 {
     return ca;
 }