Exercise #2

advertisement
Weizmann 2008
Introduction to Matlab & Data Analysis
Final Project
In-silico evolution
Last day for submission: 01.12.08
Student Name: ________________
Student ID:
________________
HW instructions:

Please submit a hard copy of the HW solution (the code and
running results) and an electronic version to the following e-mail
address: eilon.s@gmail.com
General project description
In the following project you will create a computer simulation of evolution.
First, you will generate a virtual two dimensional world built of squares where each
square can either be empty or occupied by a single organism. Each organism is an
entity that has different characteristics such as a color, age and a gene (in our case the
gene is just going to be a number). At each epoch the organism will multiply and
occupy additional positions in the world (sometimes at the expense of its neighbors).
The evolutionary goal is to solve a simple predefined problem (see
evolutionary goal for details). The gene of each organism is a solution to that
problem. The better the organism solves the problem the higher chance it has to
multiply. Thus, good genes give better solutions to the problem and consequentially
cause their organisms to multiply more rapidly. At each epoch, a gene has a certain
probability of mutating (i.e. changing the problem solution slightly) and
consequentially improving or reducing the organism's multiplication capacity. Finally,
you will build a simple graphical representation of the virtual world that will enable
you to monitor the progression of your creation.
Weizmann 2008
Introduction to Matlab & Data Analysis
Note: the type of strategy used in this project is often called genetic algorithms
and is an effective approach to solving many types of real world problems. The idea is
to couple between "how well an organism solves a given problem" and that
organism's multiplication capacity. Eventually, surviving organisms are likely to
contain a good solution to the problem.
General remarks:
-
Below see a list of functions and tasks. You should implement all of them.
-
You may add additional functions as you please.
-
You can also add additional input and output parameters to the existing
functions (put them after the required input/output parameters).
-
As always, try to avoid using hard coded numbers in the program.
-
Use remarks to document your code.
Evolutionary goal:
The evolutionary goal of an organism in our virtual world is to find the x value for
which the following function is closest to 0.
y = (x.^4 + -18 * x.^3 + 111 * x.^2 - 278 * x + 290) / 100;
Q1. Plot the following function in the range -1 to 10. What are the local and global
solutions to the above problem? (You don't need to hand in the plot).
Generating the world:
function world = createWorld(world_size).
The function will generate a square world_size * world_size
cell array and
initialize its values to 0.
Creating the first organisms in the world:
function world = addOrganismsToWorld(world, n_organisms,
gene_min_val, gene_max_val)
The function receives a world as input and creates n_organisms in it (replacing the
0's with organisms). This is done by randomly choosing n_organisms positions in the
world and invoking the function createOrganism .
function org = createOrganism(world_size, h, w, gene_min_val,
gene_max_val)
Weizmann 2008
Introduction to Matlab & Data Analysis
The function creates an organism structure called org and returns it. The structure
should have the following fields:
h - (organisms row position in the world)
w - (organisms column position in the world)
age - (number of epochs the organism is alive, initialized to 0)
max_age - (maximal number of epochs this organisms will be alive, initialized to 5).
mutation_rate - (probability of mutating after each multiplication – initialized to
0.001);
mutation_max_step - (maximal size of mutation change – initialized to 1)
genes – (a number randomly initialized between gene_min_val, gene_max_val)
color - (specified by 3 numbers between 0 and 1 corresponding to RGB values –
initialized randomly)
Evolving world
function world = evolve(world,
gene_min_val, gene_max_val);
The function receives a world and performs a single epoch of evolution. This is done
by running over all the organisms in the world and:
-
Updating their age (each organism becomes one epoch older).
Killing old organism (organisms surpassing their maximal age die of old age)
Giving each organism the chance to multiply (see multiply function)
Note: the order by which organisms are traversed should be randomly chosen;
otherwise you may get biases that result from the order of scanning. (Hint: take
advantage of the function randperm).
Multiplying organism
Deciding whether an organism should multiply: An organism has a random chance of
multiplying, which is proportional to its distance from the evolutionary goal (the
closer it is to the goal the higher the chance). To check whether an organism should
multiply use its gene as x value for the evolutionary goal. The distance of the y value
from 0 is called the error. The probability of an organism to multiply is
mult_prob = max(1.5 ./ (1 +
0.5 * exp(0.4 * error)), 0.1);
This means that if mult_prob = 0.2 then the organism has a 20% chance of
multiplying. Plot the multiplication probability function for values between 0 and 15
(you do not need to hand this plot).
Q2. Explain what is the rational behind this function.
function new_world = multiply(new_world, org, gene_min_val,
gene_max_val)
-
Check whether the organism should multiply.
Weizmann 2008
-
-
-
Introduction to Matlab & Data Analysis
The new "baby organism" will have the same characteristics as its mother
organism (e.g. same color, gene, etc.) and an age of 0.
Its position is chosen randomly to be one of the adjacent squares of its mother
(one of 8 possible locations if an organism is not on the border of the world).
In case it is on the border make sure the multiplication keeps the organism
inside.
In case the square is already occupied the new organism kills the organism
currently occupying the square and takes its place.
The new organism has a chance to undergo a mutation with probability of
mutation_rate (see createOrganism for details). If a mutation occurs it
changes the gene value to a random value that is in the range:
gene - mutation_max_step to gene + mutation_max_step.
Make sure that after the mutation the gene value does not exceed
gene_min_val, gene_max_val (if it does then reselect a different mutation
step). It also gives the new organism a new random color.
Visualizing the world:
function printWorld(world)
This function should generate/update a graphical representation of the world. Squares
that are not occupied should be colored in white. All occupied squares should be
colored according to their organism's color. Use imagesc for the visualization.
Monitoring evolution progress:
function report(world, i_epoch, gene_min_val, gene_max_val)
First the function plots the evolutionary goal function in the range gene_min_val to
gene_max_val. Second, it superimposes all the different gene solutions that exist in
the world using a '*' mark. For example if the world contains 80 organism (30
organisms with gene = 0.1 and color = [0 1 1.2] and 50 organisms with gene = 5.5 and
color [2.2 1 4.1]) then the function will print two '*' marks with x values of 0.1 and
5.5, and y values 2.6329 and 0.3906 corresponding to the goal outputs. It also uses
the appropriate colors for each mark (i.e. in our example the first mark will have the
color [0 1 1.2] and the second mark [2.2 1 4.1]). The title of the figure is the epoch
index (i_epoch).
Evolution
function evolution ()
The evolution function is the top most function that creates the actual simulation.
First it initializes the following variables:
world_size =
n_epochs
=
n_organisms =
gene_min_val=
gene_max_val=
40;
1000;
3;
-1;
10;
Weizmann 2008
Introduction to Matlab & Data Analysis
Then it performs the following operations:
- Initializes the seed of the random numbers by typing the following command:
rand('seed', 1948); (this is important for the automatic HW checker)
- Creates the world (using createWorld)
- Creates the first organisms in the world (using addOrganismsToWorld)
- Runs n_opochs and in each epcoh:
o Causes the world to evolve (using evolve)
o Prints the world to the screen using(printWorld)
o Reports the world status (using report)
Additional instructions:
- Create an artificial pause of 0.05 seconds between each evolution epoch.
- Plot the world and the report in the same figure (by using subplot) in order to
enable a simultaneous monitoring of both of them.
Final Questions:
Q3. Run the program from different starting points and describe what happens. A
short and concise explanation of several lines is enough (no need for story telling).
Q4. Play with the max_age, mutation_rate, mutation_max_step parameters.
How do different parameter values influence the results? A short and concise
explanation of several lines is enough.
Q5. Hand in the results of two runs starting from the following gene values of the 3
organism:
a. -0.5
6
10
b. 0
3
7
(To do this you need to add an a additional parameter to the function
addOrganismsToWorld(..., init_org_genes)
that enables the user to determine
the initial values of genes instead of selecting them randomly). These values are
manually initialized by the user in the main function evolution. In case
init_org_genes
is the empty (i.e.,
[]) the function initializes the gene values
randomly as before.
For each run hand in colored image snapshots of the world and the corresponding
report at i_epoch = 0, 30 and 1000. Add these images to the hard copy.
Good Luck!
Download