How to apply cellular automata to an array of multiple colours?

6 vues (au cours des 30 derniers jours)
Obi Carwood
Obi Carwood le 20 Mai 2019
Commenté : Geoff Hayes le 27 Mai 2019
I have an RGB array of size R x C. I want to apply a cellular automata set of rules that work with these cells of multiple colours.
For example, if I have an empty cell (represented by white); that has 4 green neighbours and 4 blue neighbours, how can I make a red cell "spawn" in that empty cell based on the conditions of the surrounding cells being green and blue.
Another example would be, if an empty cell has a green cell next to it, how can I make it that a cell is never "born" in that cell?
Thanks

Réponse acceptée

Geoff Hayes
Geoff Hayes le 20 Mai 2019
Modifié(e) : Geoff Hayes le 20 Mai 2019
Obi - presumably your matrix is RxCx3 and that red is (255,0,0), green is (0,255,0), blue is (0,0,255), and white is (255,255,255)..or perhaps you have some other colour scheme that will allow you to distinguish red from blue from green, etc. You can define each rule as a function and you can prioritize the rules, applying each function to your cell until one rule is satisfied (presuably once this happens, you don't need to apply any more rules). Each function would have the same signature
function [updatedCellColour] = applyRule1(gameBoard, cellCoordinates)
where the inputs are the RxC array (the current state of the game board on iteration x), and the cell (x,y) coordinates. The output is the colour of the cell - if it is empty, then the rule could not be applied and so you would move on to the next rule.
I suppose your code could look something like
function [gameBoard] = myCellularAutomata(gameBoard)
[R, C, ~] = size(gameBoard);
for k = 1:1000 % iterations of game
tempGameBoard = zeros(size(gameBoard));
updatedCellColour = [];
for r = 1:R
for c = 1:C
[updatedCellColour] = applyRule1(gameBoard, [r c]);
if isempty(updatedCellColour)
[updatedCellColour] = applyRule2(gameBoard, [r c]);
if isempty(updatedCellColour)
% etc.
end
end
% all rules have been applied or we have found one rule that satisfies its conditions
if isempty(updatedCellColour)
tempGameBoard(r,c,:) = updatedCellColour; % or however you plan to do this
end
end
end
gameBoard = tempGameBoard;
end
end
In the above "rough" code, you pass in the game board that has already been initialized with the first set of colours. We iterate for a number of generations/iterations (in this case, 1000) and on each iteartion, we look at each cell and apply the set of rules. if one rule does not satisfy the condition(s) then we try the next rule. However if the rule could not be applied, we move on to the next one. If no rule is satisfied, then there is no change to the cell. If one rule is satisfied, then we save the colour to a temporary game board (which is really the updated game board for that iteration). Once we have iterated over all cells, then we save the temp game board and move to the next iteration.
Out of curiosity, how is your second rule to be interpreted? If a cell has exactly one green cell next to it (what does next mean - can it be any one of the up to eight neighbours?) do we make sure that there is no change to the cell? What if it has two green cells? Or four? i guess you have to be very explicit with how you define each rule...
  5 commentaires
Obi Carwood
Obi Carwood le 26 Mai 2019
Well, I want the automata to work over the entire array simultanously. If its going over each cell individually, won't this take forever to run?
Geoff Hayes
Geoff Hayes le 27 Mai 2019
Probably not "forever" but it could take a while (or not) depending upon the dimensions of your gaming area and how efficient (or not) the code is written. Do you have access to the Parallel Computing Toolbox? (I don't know how you would use this toolbox for your problem but it is perhaps a possibility...)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Biological and Health Sciences dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by