Project 2 The Micro-worlds and Genetics Using Chaos Game
Name: Choong Pak Shen & Leonard Lu Tze Jian
Matriculation number: 101904 & 102217

THE MICRO-WORLDS

Abstract
Following a discussion on http://www.umcs.maine.edu/~larry/microworlds/microworld.html, a Micro-world is generated here. Micro-worlds are interesting programs where people can set a couple of rules and try to simulate the scenario of the rules. The rules may not follow the rules from the “Game of Life” and it is possible to be defined arbitrarily by the user. The Micro-worlds are somewhat similar to a “parallel universe” of the real world, but it does not necessarily becomes the real world. Only when all the rules are set according to the real world that the Micro-world may represent the real world. In this Project 2, a Micro-world with a set of programmer-defined rules was generated.

Discussion
For starters, a landscape is generated. The states of the cells are not limited to 0 and 1 in this project. The following states are used in this coding.

0: The dead cell (Transparent)
1: The alive cell (Yellow)
2: A plain field (Brown)
3: A mountain / forest (Green)
4: Water sources (Blue)

It is obvious that the states 2, 3, and 4 are useful only in landscape generator. For this project, an island with a random landscape is generated. The mountain / forest area is generated through majority rule in Project 1, and the water sources surround the island. Since the states are not limited to 1 and 0, the majority rule has to modified a little such that the “testLandscape” has to minus with 18 since the lowest possible value for “testLandscape” is 18 (Consider all the nine cells have a state of 2). An initial condition of 20000 random “3” cells are distributed through a centered sqare area of 170 × 170 so that when the majority rule applies, the mountains or forests will not “over-grow” to the water sources. A total of 40 time steps are taken for the landscape to generate and the result of the majority rule is overlapped to the “Life”.

After the landscape is generated, the rules for the “Life” are defined. From an ideal world consideration, the following rules are set.

(1) The “Life” lives in harmony with nature. “Life”, while obtaining resources from the mountain, forest or water sources, must not change the landscape of the Micro-world.
(2) In plain field, “Life” grows according to the “Game of Life”.
(3) In plain field, “Life” gains resources (food, water) from mountain, forest, or water resources. Without resources, “Life” will die within one time step. With resources, “Life” may live as long as it is allowed by the “Game of Life” rule. A walk radius of 5 is given for the “Life” to gain resources from the surrounding.
(4) “Life” cannot live in water. If it falls into water, it will die at the next time step.
(5) Water is nourishing. If “Life” finds water within the walk radius, it will live and give birth to one of its random neighbours.
(6) Mountain can only support a minority group of “Life”. This condition is set due to rule (1), as the number of “Life” increases in the mountain, certain destruction to the nature may occur.

The programming part of the rules is too redundant to be explained here as it consists of many “If” and “For” commands. One thing should be noted that for every time step, the evaluating cell is not only considering its eight neighbours, but it has to consider the landscape of the Micro-world within the walk radius as well. That is the time-consuming part for this program. Initially, 20000 “Lifes” are scattered throughout the Micro-world and the population pattern is observed. Again, periodic boundary condition applies here.

There are several more conditions which are possible to add in, however due to the processing time consideration, those conditions are not added. The Micro-world can be perfected through the addition of another “Life” tribe so that a war between the tribes can be simulated. The “Life” can be modified to build civilization through a change in the landscape, i.e. ignoring rule (1). A natural disaster such as volcanic explosion and earthquake can be simulated. Weather pattern and climate change are also possible. A final touch for this Micro-world would be migration of the “Life”. Since this Micro-world does not enable the migration functionality, the “Lives” are distributed evenly and see how the population grows instead of starting the “Lives” at one place and see how it distributes to the Micro-world.



The graph above shows the population trend for the Micro-world. It shows a beautiful exponential decrease curve and the population count stabilized at around 50 time steps. From the simulation, it seems like the nature will “limit” the number of “Life” in this Micro-world. This fact is true in some sense. If the “Lives” are not allowed to modify the landscape, there will not be any agriculture, civilization and animal breeding to increase the food resources that are crucial for “Life” to increase. In fact, the resources provided by the landscape are only enough to sustain a limited number of “Lives”. This result agrees with the “thought result” of the initial assumption (ideal world consideration) where the people live in harmony with the nature, without any man-made structures. However, when the population count of the real world is compared, the result is quite astonishing:-



Should a conclusion be made such that human in the real world are not living in harmony with the environment...?

Micro-worlds are capable to do something even bigger. It may read in a real world landscape and simulate how the population distributed, or simulate the pattern of the breakout of a deadly disease. To make the Micro-world approaches the real world situation, more and more rules have to be applied, and eventually Mathematica may not be suitable to simulate the Micro-world. In such a case, other approaches have to be sought, such as C++ or JAVA programming language.

(*Micro-worlds*)
(*Landscape generator*)
(*Constant declaration*)
L=200;(*Size of the simulation box*)
tStepLandscape=40;(*Number of time steps taken in landscape generator*)
Print[ProgressIndicator[Dynamic[nLandscape/tStepLandscape]]];(*Print progress indicator*)

(*Variable initialization*)
For[j=1,j≤L,j++,
For[i=1,i≤L,i++,
oldLandscape[i,j]=2;
newLandscape[i,j]=2;
];
];

(*Water sources defining*)
For[j=1,j≤L,j++,
For[i=1,i≤5,i++,
oldLandscape[i,j]=4;
newLandscape[i,j]=4;
];
];

For[j=1,j≤5,j++,
For[i=1,i≤L,i++,
oldLandscape[i,j]=4;
newLandscape[i,j]=4;
];
];

For[j=1,j≤L,j++,
For[i=195,i≤L,i++,
oldLandscape[i,j]=4;
newLandscape[i,j]=4;
];
];

For[j=195,j≤L,j++,
For[i=1,i≤L,i++,
oldLandscape[i,j]=4;
newLandscape[i,j]=4;
];
];

(*Initial state*)
For[k=1,k≤20000,k++,
oldLandscape[RandomInteger[{15,185}],RandomInteger[{15,185}]]=3;
];

(*Start of time stepping in landscape generator*)
For[nLandscape=1,nLandscape≤tStepLandscape,nLandscape++,
For[j=10,j≤190,j++,
For[i=10,i≤190,i++,
i1=i-1;
i2=i+1;
j1=j-1;
j2=j+1;

(*Test the neighbouring by using the majority rule*)
testLandscape=oldLandscape[i1,j1]+oldLandscape[i1,j]+oldLandscape[i1,j2]+oldLandscape[i,j1]+oldLandscape[i,j]+oldLandscape[i,j2]+oldLandscape[i2,j1]+oldLandscape[i2,j]+oldLandscape[i2,j2]-18;

If[testLandscape≥5,
newLandscape[i,j]=3,
newLandscape[i,j]=2;
];
];
];

(*Equate the old data with the updated data*)
For[j=1,j≤L,j++,
For[i=1,i≤L,i++,
oldLandscape[i,j]=newLandscape[i,j];
];
];
];

(*Tabulate the data for the last time step*)
tLandscape=Table[oldLandscape[i,j],{i,1,L},{j,1,L}];


(*Game of life*)
(*Constant declaration*)
tStep=100;(*Number of time steps taken in the game of life*)
z=-5;(*Walk radius of the life*)
Print[ProgressIndicator[Dynamic[n/tStep]]];(*Print progress indicator*)

(*Variable initialization*)
For[j=1,j≤L,j++,
For[i=1,i≤L,i++,
oldLife[i,j]=0;
newLife[i,j]=0;
];
];

(*Initial state of the game of life*)
For[k=1,k≤20000,k++,
oldLife[RandomInteger[{1,L}],RandomInteger[{1,L}]]=1;
];

(*Tabulate the data in zero time step*)
tLife[0]=Table[oldLife[i,j],{i,1,L},{j,1,L}];

(*Start of time stepping*)
For[n=1,n≤tStep,n++,
For[j=1,j≤L,j++,
For[i=1,i≤L,i++,
(*Periodic boundary condition*)
i1=i-1;
i2=i+1;
j1=j-1;
j2=j+1;

If[i1==0,i1=L;];
If[i2==L,i2=1;];
If[j1==0,j1=L;];
If[j2==L,j2=1;];

(*Test the neighbouring by using the game of life rule at the plain field*)
If[oldLandscape[i,j]==2,
testLife=oldLife[i1,j1]+oldLife[i1,j]+oldLife[i1,j2]+oldLife[i,j1]+oldLife[i,j2]+oldLife[i2,j1]+oldLife[i2,j]+oldLife[i2,j2];

If[oldLife[i,j]==1,
If[testLife==2||testLife==3,
newLife[i,j]=1;

(*Set the life span of the "Life"*)
If[newLife[i,j]==1,
newLife[i,j]=0;
];

(*Check the landscape with walk radius*)
For[y=0,y≤10,y++,
For[x=0,x≤10,x++,
(*Periodic boundary condition*)
ix=i+x+z;
jy=j+y+z;
If[ix≤0,ix=L-ix;];
If[ix≥L,ix=ix-L;];
If[jy≤0,jy=L-jy;];
If[jy≥L,jy=jy-L;];

(*Check the landscape with mountain or forest*)
If[oldLandscape[ix,jy]==3&&oldLife[i,j]==1,newLife[i,j]=1;];

(*Check the landscape with water sources*)
If[oldLandscape[ix,jy]==4&&oldLife[i,j]==1,
newLife[i,j]=1;
newLife[i+RandomInteger[{-1,1}],j+RandomInteger[{-1,1}]]=1;
];
];
],
newLife[i,j]=0;
],
If[testLife==3,
newLife[i,j]=1,
newLife[i,j]=0;
];
];
];

(*Test the neighbouring by using the arbitrary rule at the mountain or forest*)
If[oldLandscape[i,j]==3,
testLife=oldLife[i1,j1]+oldLife[i1,j]+oldLife[i1,j2]+oldLife[i,j1]+oldLife[i,j2]+oldLife[i2,j1]+oldLife[i2,j]+oldLife[i2,j2];

If[oldLife[i,j]==1,
If[testLife==1||testLife==2,
newLife[i,j]=1,
newLife[i,j]=0;
],
newlife[i,j]=0;
];
];

(*Test the cell at the water sources*)
If[oldLandscape[i,j]==4,
newLife[i,j]=0;
];
];
];

(*Equate the old data with the updated data*)
For[j=1,j≤L,j++,
For[i=1,i≤L,i++,
oldLife[i,j]=newLife[i,j];
];
];

(*Tabulate the data for all time step*)
tLife[n]=Table[oldLife[i,j],{i,1,L},{j,1,L}];
];

(*Output print out*)
Manipulate[Show[ArrayPlot[tLandscape,ColorRules->{4->Blue,3->Green,2->Brown,1->Yellow,0->White}],ArrayPlot[tLife[n],ColorRules->{4->Blue,3->Green,2->Brown,1->Yellow,0->Transparent}]],{n,0,tStep,1}]

(*Calculate the number of "Lives" in every time step*)
For[n=0,n≤tStep,n++,
population[n]=Count[tLife[n],1,2];
];

(*Population trend print out*)
ListPlot[Table[population[n],{n,0,tStep}],PlotRange->All,AxesOrigin->{0,0},Joined->True,AxesLabel->{"Time step","Population in the game of life"},PlotLabel->"Population count"]

Conclusion
The Micro-worlds system is actually a very simple system which requires minimal programming background and a lot of creativity. By considering all the possibilities, Micro-worlds are such powerful that it will simulate the real world. In this Project 2, only ideal situation is considered and the weaknesses of this program are stated in the discussion. This program is open to any other modifications so that it may simulate some systems better.

CHAOS GAMES AND GENETICS

ABSTRACT:

Chaos Game is applied to genetics so as to offer a visual representation of the genetics code, rather than a series of confusing letters.



Genetic code, or DNA, is made up of 4 strands of Amino Acids, which are A, C, G and T.
When DNA is written down, it looks like this:



And this is just only ONE-THIRD of it. Hence, Chaos Game is used as an alternative for visual representation.

Chaos Game Representation (CGR) is an iterative mapping technique that processes sequence of units, such as nucleotides in a DNA sequence or amino acids in a protein in order to find the coordinates for their position in a continuous space.
Genetics, which is made up of 4 acid amino strands named A, C, G and T, is visualised as a 4-sided square.



When a DNA sequence is introduced, the Chaos Game is played according to the letters.



For DNA sequence, visit <http://www.ncbi.nlm.nih.gov/>.
I have enlisted a few in the <DNA sequence.doc>.

(*Defining initial conditions*)
side=4;(*the sides of the desired shape*)
quadrants=Input["Please enter the number of quadrants:\n(Please enter SQUARED numbers e.g. 4, 9, 16, 25)"];(*the amount of quadrants desired*)
startpoint=0.0;
maxlength=maxheight=1.0;
setpoints={};
total={};

(*Defining the Sides of the Rectangle*)
pointx[1]=pointy[1]=startpoint;
pointx[2]=maxlength;pointy[2]=startpoint;
pointx[3]=startpoint;pointy[3]=maxheight;
pointx[4]=maxlength;pointy[4]=maxheight;

(*Grouping the points of the triangle together and putting them into "setpoints" table *)
For[k=1,k≤side,k++,
point={pointx[k],pointy[k]};
setpoints=Append[setpoints,point];
];

(*Dividing the Rectangle into the desired quadrants*)
l=1;

For[p=1,p≤Sqrt[quadrants],p++,
For[q=1,q≤Sqrt[quadrants],q++,
quadrantx[l]=q*(maxlength/(Sqrt[quadrants]));
quadranty[l]=p*(maxheight/(Sqrt[quadrants]));
quadranttable[l]={};
l=l+1;
];
];

(*Defining the initial position*)
x[0]=maxlength/2;
y[0]=maxheight/2;

(*Defining the position in terms of the quadrant*)
For[j=1,j≤quadrants,j++,
If[x[0]≤quadrantx[j],
If[y[0]≤quadranty[j],quadranttable[j]=Append[quadranttable[j],{x[0],y[0]}];Break[];
];
];
];

(*Begin DNA Sequencing*)

(*Asking for a DNA Sequence input*)
cd=InputString["Please enter the name of your DNA strand:"];
ab=InputString["Please enter the DNA strand:"];

(*Grouping the input data into a String of characters*)
DNA1=Characters[ab];

(*Calculating the length of the input data*)
length=StringLength[ab];

(*Filtering the input data to gain the relevant information*)
DNAlength=1;
DNA={};

For[i=1,i≤length,i++,

If[DNA1[[i]]=="a"||DNA1[[i]]=="A"||DNA1[[i]]=="g"||DNA1[[i]]=="G"||DNA1[[i]]=="c"||DNA1[[i]]=="C"||DNA1[[i]]=="t"||DNA1[[i]]=="T",DNA=Append[DNA,DNA1[[i]]];DNAlength++;
];
];

Print[DNA," ",DNAlength];

Print["timestep progress:  ",ProgressIndicator[Dynamic[i/DNAlength]]];

(*Begin DNA Visualisation using Chaos Game*)
For[i=1,i≤DNAlength-1,i++,

If[DNA[[i]]=="a"||DNA[[i]]=="A",x[i]=(x[i-1]+pointx[1])/2;y[i]=(y[i-1]+pointy[1])/2,
If[DNA[[i]]=="g"||DNA[[i]]=="G",x[i]=(x[i-1]+pointx[4])/2;y[i]=(y[i-1]+pointy[4])/2,
If[DNA[[i]]=="c"||DNA[[i]]=="C",x[i]=(x[i-1]+pointx[3])/2;y[i]=(y[i-1]+pointy[3])/2,
If[DNA[[i]]=="t"||DNA[[i]]=="T",x[i]=(x[i-1]+pointx[2])/2;y[i]=(y[i-1]+pointy[2])/2;
];
];
];
];

(*Grouping the new points in terms of its residing quadrants*)
For[j=1,j≤quadrants,j++,
If[x[i]≤quadrantx[j],
If[y[i]≤quadranty[j],quadranttable[j]=Append[quadranttable[j],{x[i],y[i]}];Break[];
];
];
];
];

(*Grouping all the quadrants together to form a plot*)
For[k=1,k≤quadrants,k++,
If[quadranttable[k]≠{},
total=Append[total,quadranttable[k]];
];
];

ListPlot[total,Axes->False,PlotStyle->Automatic,PlotLabel->cd]
(*game ends*)

DISCUSSION:

Because each DNA is unique (as everyone knows), each visual result is a unique pattern which doctors can distinguish between a lot of others. Besides, the boundary conditions is set the same for each visualisation, making it easier for comparison and differentiation. Higher quadrants allows better visualisation, thus allowing better comparison.
This is an application of Chaos Game, a physics concept, in the biological field which can be very useful.

SUGGESTION:

This code could be modified to incorporate letters of a sentence. If this is done, then a new bar code system could be applied in visualising identities.A circle could be introduced as the poligon which could incorporate any number of alphabets or characters that is needed. Then, the chaos game can be applied to generate patterns of the desired unique sentences.

Besides, chaos game can be used for data encryption because each encryption, be it 20 hexademical or binary, can be encrypted into a pattern using the chaos game. Only people in the know would know what the pattern is trying to tell them.