Skip to content

Instantly share code, notes, and snippets.

View gidili's full-sized avatar
🐢
Probably eating pizza 🍕

Giovanni Idili gidili

🐢
Probably eating pizza 🍕
View GitHub Profile
@gidili
gidili / gist:94600
Created April 13, 2009 18:21
TypingMonkey to generate random strings
/// <summary>
/// The Typing monkey generates random strings - can't be static 'cause it's a monkey.
/// </summary>
/// <remarks>
/// If you wait long enough it will eventually produce Shakespeare.
/// </remarks>
class TypingMonkey
{
private const string legalCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.";
% the size of our array of cells
latticeSize=149;
% number of time steps
max=320;
a=zeros(1,149);
newa=zeros(1,149);
% the ruleset data object
ruleSet = CARuleset;
% the size of our array of cells
latticeSize=149;
% neighborhood radius
radius = 1;
% rule size
ruleSize=3;
% number of time steps
max=320;
a=zeros(1,latticeSize);
function [a] = circularSubarray(A, m, n)
if isempty(A),
error('circularSubarray: isempty(A)','A is empty') ;
elseif m==n,
error('circularSubarray: m==n','first and second index are the same') ;
end
N = size(A);
N = N(2);
% the size of our array of cells
latticeSize=149;
% neighborhood radius
radius = 3;
% rule size
ruleSize= radius*2 +1;
% number of time steps
max=200;
a=zeros(1,latticeSize);
% patterns
ruleSet.patterns = flipud(combn([0 1],ruleSize));
% declare transformation rules array - 148 bits (2^7) initialized to zero
gkl = zeros(1,2^ruleSize);
% GKL rule:
% If ci(t) = 0, then ci(t + 1) = majority [ci(t), ci-1(t) ci-3(t)];
% If ci(t) = 1, then ci(t + 1) = majority [ci(t), ci+1(t) ci+3(t)];
for j=1:2^ruleSize,
pattern = ruleSet.patterns(j,:);
% clear memory so that we can compare timespans
clear
tic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CONSTANTS DECLARATION
% the size of our array of cells
latticeSize=149;
% neighborhood radius
radius = 3;
function [C] = generateBinaryInitialConfigurations(total, sizeOfEach)
% will generate [total] initial configuration each of size [sizeOfEach]
howManyOfEach = total/2;
blackCounter = 0;
whiteCounter = 0;
tot = 0;
while((blackCounter < howManyOfEach || whiteCounter < howManyOfEach) && tot<total)
a = randint(1, sizeOfEach, [0 1]);
if(sum(a)>sizeOfEach/2 && blackCounter < howManyOfEach)
function [ fitness ] = majorityClassificationFitness( rule , varargin )
%CALCULATEFITNESS
% this function calculates performance fitness of given rule - returns a
% scalar calculated as successfulRuns/TotalRuns. Takes a 128 bits vector
% representing a CA rule for a neighborhood of radius 3.
% Note: A lot of (if not all) the costants declared here could be passed down as
% parameters - but since this is going to be used as the fitness function
% in a Genetic Algorithm very specific to the problem at hand to be called
% from the matlab Genetic Algorithm toolkit, it is more practical to pass
% down as input param only the transformation rule.
function [ fitness ] = majorityClassificationFitness( rule , varargin )
%CALCULATEFITNESS
% this function calculates performance fitness of given rule - returns a
% scalar calculated as successfulRuns/TotalRuns. Takes a 128 bits vector
% representing a CA rule for a neighborhood of radius 3.
% Note: A lot of (if not all) the costants declared here could be passed down as
% parameters - but since this is going to be used as the fitness function
% in a Genetic Algorithm very specific to the problem at hand to be called
% from the matlab Genetic Algorithm toolkit, it is more practical to pass
% down as input param only the transformation rule.