public void Run(Cells cells, CellularAutomataRules rules) { _cells = cells; _ptOffset = CalculateOffset(); Rules = (rules != null) ? rules : _rules; _timer.Start(); }
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(); }
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; }
private void SaveRules(string fileName) { CellularAutomataRules rules = _genAlg.CurrentRules; var settingsCa = _genAlg.CaSettings; var saveData = new RulesSaveData(settingsCa, rules.StateTable); saveData.Save(fileName); }
private void GenerateNewCa() { if (PreviousGeneration == null) { _currentCa = _settingsCa.GetRandomRules(_settingsGa.StateTableDistribution); } else { _currentCa = GenerateChildCa(PreviousGeneration); } }
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); }
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; }
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); }
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; }
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; }
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); }
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); }
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); }
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); }
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); }
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); }
public abstract CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings);
internal void Clear() { Stop(); Rules = null; }
public void Start() { _generations = new List<Generation>(); _currentCa = _settingsCa.GetRandomRules(_settingsGa.StateTableDistribution); }
public CellularAutomataRules(CellularAutomataRules ca) { _stateTable = (int[, ])ca._stateTable.Clone(); }
public bool HasSameDimensions(CellularAutomataRules r) { return NumStates == r.NumStates && Range == r.Range; }
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; }
public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings) { return(ca); }
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 ); } }
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; }
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; }
public void Add(CellularAutomataRules rules) { _ca.Insert(rules, rules.Fitness); }
public CellularAutomataRules(CellularAutomataRules ca) { _stateTable = (int[,]) ca._stateTable.Clone(); }
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 ); } }
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; }
public bool HasSameDimensions(CellularAutomataRules r) { return(NumStates == r.NumStates && Range == r.Range); }
public void Start() { _generations = new List <Generation>(); _currentCa = _settingsCa.GetRandomRules(_settingsGa.StateTableDistribution); }
private void GenerateNewCa() { if (PreviousGeneration == null) _currentCa = _settingsCa.GetRandomRules( _settingsGa.StateTableDistribution ); else _currentCa = GenerateChildCa(PreviousGeneration); }
public override CellularAutomataRules Mutate(CellularAutomataRules ca, GeneticAlgorithmSettings settings) { return ca; }