Simulates the coevolution of 3 species, one red, one blue, and one green. The trait mediating their interaction is brightness. Diffusive migration occurs across a 2 dimensional plane.
Use the sliders to adjust the strength of interaction, rate of migration and variance of drift.
“Spatial Coevolution” by bob week
https://openprocessing.org/sketch/396443
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Use the sliders to adjust the strength of interaction, rate of migration and variance of drift.
CC Attribution ShareAlike
Spatial Coevolution
week
xxxxxxxxxx
Cell[][] grid, dummy;
int w, h, wid=170, hi=180, old_wid, old_hi, neighborhood = 0, hud = 0, picCount, k=0, e=1, count=0;
float C=0.01, mix=0, loge=0.01, Mratio=2, Msel12, Msel13, Msel23;
float[] m = {.1,.1,.1};
float[] drift = {0,0,0};
float[] G = {1,1,1}, Gamma = {0,0,0};
float[][] M = {{0,0,0},{0,0,0},{0,0,0}}; // matching selection matrix: M[i][j] is selection pressure from species i onto species j
String m_SL="", mig = "", M_SL = "", Mratio_SL = "", A_SL = "", Image = "", Ran = "", Size = "", contrast = "", Mix="", log_e, filename;
char[] number;
//int size = 10;
color c;
boolean pic = false, rando = false, vid = false, rev = false, capture=false, pause = false, pause1=false;
HScrollbar mig1;
HScrollbar mig2;
HScrollbar mig3;
HScrollbar m12;
HScrollbar m13;
HScrollbar m23;
//HScrollbar dft1;
//HScrollbar dft2;
//HScrollbar dft3;
void setup() {
//frameRate(15);
size(800,600);
//colorMode(RGB);
noStroke();
background(0);
mig1 = new HScrollbar(25,50, 120,20,5,120);
mig2 = new HScrollbar(25,100,120,20,5,70);
mig3 = new HScrollbar(25,150,120,20,5,30);
m12 = new HScrollbar(25,200,120,20,5,120);
m13 = new HScrollbar(25,250,120,20,5,20);
m23 = new HScrollbar(25,300,120,20,5,120);
//dft1 = new HScrollbar(25,350,120,20,5,25);
//dft2 = new HScrollbar(25,400,120,20,5,25);
//dft3 = new HScrollbar(25,450,120,20,5,25);
grid = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) grid[i][j] = new Cell(random(0,255),random(0,255),random(0,255),i,j);
}
}
void draw() {
if (pause==false){
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) grid[i][j].render();
}
m[0] = 0.75*(mig1.spos-mig1.sposMin)/(mig1.sposMax-mig1.sposMin);
m[1] = 0.75*(mig2.spos-mig2.sposMin)/(mig2.sposMax-mig2.sposMin);
m[2] = 0.75*(mig3.spos-mig3.sposMin)/(mig3.sposMax-mig3.sposMin);
Mratio = 2;
Msel12 = (0.1)*(m12.spos-m12.sposMin)/(m12.sposMax-m12.sposMin);
Msel13 = (0.1)*(m13.spos-m13.sposMin)/(m13.sposMax-m13.sposMin);
Msel23 = (0.1)*(m23.spos-m23.sposMin)/(m23.sposMax-m23.sposMin);
//drift[0] = 25*(dft1.spos-dft1.sposMin)/(dft1.sposMax-dft1.sposMin);
//drift[1] = 25*(dft2.spos-dft2.sposMin)/(dft2.sposMax-dft2.sposMin);
//drift[2] = 25*(dft3.spos-dft3.sposMin)/(dft3.sposMax-dft3.sposMin);
M[1][0] = Msel12;
M[2][1] = Msel13;
M[0][2] = Msel23;
M[0][1] = -Msel12;
M[1][2] = -Mratio*Msel13;
M[2][0] = -Mratio*Msel23;
dummy = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) dummy[i][j] = new Cell(grid[i][j].species[0],grid[i][j].species[1],grid[i][j].species[2],i,j);
}
for (int i=wid-1; i>=0; i--){
for (int j=hi-1; j>=0; j--) grid[i][j].update(dummy);
}
mig1.update();
mig1.display();
mig2.update();
mig2.display();
mig3.update();
mig3.display();
m12.update();
m12.display();
m13.update();
m13.display();
m23.update();
m23.display();
//dft1.update();
//dft1.display();
//dft2.update();
//dft2.display();
//dft3.update();
//dft3.display();
fill(0);
rect(mig1.xpos-2,mig1.ypos-20,82,12);
rect(mig2.xpos-2,mig2.ypos-20,82,12);
rect(mig3.xpos-2,mig3.ypos-20,82,12);
rect(m12.xpos-2,m12.ypos-20,118,12);
rect(m13.xpos-2,m13.ypos-20,118,12);
rect(m23.xpos-2,m23.ypos-20,118,12);
//rect(dft1.xpos-2,dft1.ypos-20,54,12);
//rect(dft2.xpos-2,dft2.ypos-20,54,12);
//rect(dft3.xpos-2,dft3.ypos-20,54,12);
fill(255);
text("spp1 migration",mig1.xpos,mig1.ypos-10);
text("spp2 migration",mig2.xpos,mig2.ypos-10);
text("spp3 migration",mig3.xpos,mig3.ypos-10);
text("spp 1 & 2 coevolution",m12.xpos,m12.ypos-10);
text("spp 1 & 3 coevolution",m13.xpos,m13.ypos-10);
text("spp 2 & 3 coevolution",m23.xpos,m23.ypos-10);
//text("spp1 drift",dft1.xpos,dft1.ypos-10);
//text("spp2 drift",dft2.xpos,dft2.ypos-10);
//text("spp3 drift",dft3.xpos,dft3.ypos-10);
}
else {
if (pause1==true){
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) grid[i][j].render();
}
m[0] = 0.75*(mig1.spos-mig1.sposMin)/(mig1.sposMax-mig1.sposMin);
m[1] = 0.75*(mig2.spos-mig2.sposMin)/(mig2.sposMax-mig2.sposMin);
m[2] = 0.75*(mig3.spos-mig3.sposMin)/(mig3.sposMax-mig3.sposMin);
Mratio = 2;
Msel12 = (0.1)*(m12.spos-m12.sposMin)/(m12.sposMax-m12.sposMin);
Msel13 = (0.1)*(m13.spos-m13.sposMin)/(m13.sposMax-m13.sposMin);
Msel23 = (0.1)*(m23.spos-m23.sposMin)/(m23.sposMax-m23.sposMin);
//drift[0] = 25*(dft1.spos-dft1.sposMin)/(dft1.sposMax-dft1.sposMin);
//drift[1] = 25*(dft2.spos-dft2.sposMin)/(dft2.sposMax-dft2.sposMin);
//drift[2] = 25*(dft3.spos-dft3.sposMin)/(dft3.sposMax-dft3.sposMin);
M[1][0] = Msel12;
M[2][1] = Msel13;
M[0][2] = Msel23;
M[0][1] = -Msel12;
M[1][2] = -Mratio*Msel13;
M[2][0] = -Mratio*Msel23;
dummy = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) dummy[i][j] = new Cell(grid[i][j].species[0],grid[i][j].species[1],grid[i][j].species[2],i,j);
}
for (int i=wid-1; i>=0; i--){
for (int j=hi-1; j>=0; j--) grid[i][j].update(dummy);
}
mig1.update();
mig1.display();
mig2.update();
mig2.display();
mig3.update();
mig3.display();
m12.update();
m12.display();
m13.update();
m13.display();
m23.update();
m23.display();
//dft1.update();
//dft1.display();
//dft2.update();
//dft2.display();
//dft3.update();
//dft3.display();
fill(0);
rect(mig1.xpos-2,mig1.ypos-20,54,12);
rect(mig2.xpos-2,mig2.ypos-20,54,12);
rect(mig3.xpos-2,mig3.ypos-20,54,12);
rect(m12.xpos-2,m12.ypos-20,118,12);
rect(m13.xpos-2,m13.ypos-20,118,12);
rect(m23.xpos-2,m23.ypos-20,118,12);
//rect(dft1.xpos-2,dft1.ypos-20,54,12);
//rect(dft2.xpos-2,dft2.ypos-20,24,12);
//rect(dft3.xpos-2,dft3.ypos-20,24,12);
fill(255);
text("spp1 migration",mig1.xpos,mig1.ypos-10);
text("spp2 migration",mig2.xpos,mig2.ypos-10);
text("spp3 migration",mig3.xpos,mig3.ypos-10);
text("spp 1 & 2 coevolution",m12.xpos,m12.ypos-10);
text("spp 1 & 3 coevolution",m13.xpos,m13.ypos-10);
text("spp 2 & 3 coevolution",m23.xpos,m23.ypos-10);
//text("spp1 drift",dft1.xpos,dft1.ypos-10);
//text("spp2 drift",dft2.xpos,dft2.ypos-10);
//text("spp3 drift",dft3.xpos,dft3.ypos-10);
}
fill(0);
rect(290,280,210,75);
fill(255);
text("press p to pause/play",300,300);
text("press +/- to incr/decr resolution",300,320);
text("press r to reset with random values",300,340);
pause1 = false;
}
}
void keyReleased() {
switch(key) {
case '+':
dummy = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) dummy[i][j] = new Cell(grid[i][j].species[0],grid[i][j].species[1],grid[i][j].species[2],i,j);
}
old_wid = wid;
old_hi = hi;
hi+=3;
wid+=4;
grid = null;
grid = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) grid[i][j] = new Cell(dummy[i%(old_wid-1)][j%(old_hi-1)].species[0],dummy[i%(old_wid-1)][j%(old_hi-1)].species[1],dummy[i%(old_wid-1)][j%(old_hi-1)].species[2],i,j);
}
dummy = null;
break;
case '-':
dummy = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) dummy[i][j] = new Cell(grid[i][j].species[0],grid[i][j].species[1],grid[i][j].species[2],i,j);
}
old_wid = wid;
old_hi = hi;
hi-=3;
if (hi<3) hi = 3;
wid-=4;
if (wid<4) wid = 4;
grid = null;
grid = new Cell[wid][hi];
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) grid[i][j] = new Cell(dummy[i%(old_wid-1)][j%(old_hi-1)].species[0],dummy[i%(old_wid-1)][j%(old_hi-1)].species[1],dummy[i%(old_wid-1)][j%(old_hi-1)].species[2],i,j);
}
dummy = null;
break;
case 'h':
hud = (1+hud)%3;
break;
case 'p':
pause = !pause;
pause1 = true;
break;
case 'r':
for (int i=0; i<wid; i++){
for (int j=0; j<hi; j++) grid[i][j].set(random(0,255),random(0,255),random(0,255));
}
break;
}
}
class HScrollbar {
int swidth, sheight; // width and height of bar
float xpos, ypos; // x and y position of bar
float spos, newspos; // x position of slider
float sposMin, sposMax; // max and min values of slider
int loose; // how loose/heavy
boolean over; // is the mouse over the slider?
boolean locked;
float ratio;
HScrollbar (float xp, float yp, int sw, int sh, int l, float ispos) {
swidth = sw;
sheight = sh;
int widthtoheight = sw - sh;
ratio = (float)sw / (float)widthtoheight;
xpos = xp;
ypos = yp-sheight/2;
spos = ispos;
newspos = spos;
sposMin = xpos;
sposMax = xpos + swidth - sheight;
loose = l;
}
void update() {
if (overEvent()) {
over = true;
} else {
over = false;
}
if (mousePressed && over) {
locked = true;
}
if (!mousePressed) {
locked = false;
}
if (locked) {
newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
}
if (abs(newspos - spos) > 1) {
spos = spos + (newspos-spos)/loose;
}
}
float constrain(float val, float minv, float maxv) {
return min(max(val, minv), maxv);
}
boolean overEvent() {
if (mouseX > xpos && mouseX < xpos+swidth &&
mouseY > ypos && mouseY < ypos+sheight) {
return true;
} else {
return false;
}
}
void display() {
//noStroke();
fill(204);
rect(xpos, ypos, swidth, sheight);
if (over || locked) {
fill(0, 0, 0);
} else {
fill(102, 102, 102);
}
rect(spos, ypos, sheight, sheight);
}
float getPos() {
// Convert spos to be values between
// 0 and the total width of the scrollbar
return spos * ratio;
}
}
float abiotic_selection(float x,float y,int i){
float value = .1*exp(-(y-height/2)*(y-height/2)/(2*50000))*exp(-(x-width/2)*(x-width/2)/(2*50000));
return value; // returns strength of abiotic selection at location (x,y) for species i
}
float biotic_selection(float x,float y,int i){
float value = .1*exp(-(y-height/2)*(y-height/2)/(2*50000))*exp(-(x-width/2)*(x-width/2)/(2*50000));
return value; // returns strength of abiotic selection at location (x,y) for species i
}
float theta(float x,float y,int i){
float value = 0;
if(i==0) value = 55+200*exp(-(y-2*height/5)*(y-2*height/5)/(2*10000))*exp(-(x-3*width/8)*(x-3*width/8)/(2*10000));
if(i==1) value = 55+200*exp(-(y-2*height/5)*(y-2*height/5)/(2*10000))*exp(-(x-5*width/8)*(x-5*width/8)/(2*10000));
if(i==2) value = 55+200*exp(-(y-3*height/5)*(y-3*height/5)/(2*10000))*exp(-(x-width/2)*(x-width/2)/(2*10000));
return value; // returns phenotypic optimum at location (x,y) for species i
}
float N(float x,float y,int i){
float[] center = {width/2,height/2};
float value = 10+500*exp(-(y-height/2)*(y-height/2)/(2*5000))*exp(-(x-width/2)*(x-width/2)/(2*5000));
return value; // returns effective population size at location (x,y) for species i
}
See More Shortcuts