-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_part1
More file actions
526 lines (449 loc) · 14.9 KB
/
code_part1
File metadata and controls
526 lines (449 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//Delphi 2007
// optimum is fixed
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, math, TeeProcs, TeEngine, Chart, Series;
const
INI_IND=2000;
NSTER=50;
LAMBDA=3;
MAX_IND=10000000;
NEQUILIBRIUM=1000;
NSIM=2101;
mutationrate_plas=0.001;
mutationrate_disp=0.001;
mutationrate_spec=0;
inioptimum=50; // equals half the width of the climate window
CWS=1.3;
Nreplic=3;
steekproefgrootte=10000;
breedte=100;
max_x=1550;
Type
TForm1 = class(TForm)
CreateLandscapeButton: TButton;
Image1: TImage;
RunButton: TButton;
CloseButton: TButton;
procedure CreateLandscapeButtonClick(Sender: TObject);
procedure RunButtonClick(Sender: TObject);
procedure CloseButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
berekening=record
dens:integer; //total number of individuals
som:double; // sum of plasticity within cell
end;
individual=record
posx,posy:integer;
plas:double;
dispersalsd:double;
spec:double
end;
TPoint=record
x,y:integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses CreateLandscapeUnit;
var
time:integer;
occupied: array[1..max_x, 1..breedte] of boolean; //whether pixel is occupied
population: array [1..MAX_IND] of individual;
young: array [1..MAX_IND] of individual; // important voor event reproduction
totalpop: integer;
eigenschap_cel: array [1..max_x, 1..breedte] of berekening;
survivors:array [1..MAX_IND]of individual; // important for event survival
competitors:array[1..MAX_IND] of individual;// important for event competition
output:textfile;
Topt:double; //position of global optimum within landscape
steekproef: array [1..10000]of individual;
habitat:array[1..max_x,1..breedte]of integer;
factor: integer;
/////**********************************************************************/////
///// Procedure to prepare landscape /////
/////**********************************************************************/////
Procedure preparelandscape;
var
x,y : integer;
begin
for x:=1 to max_x do
for y:= 1 to breedte do
begin
occupied[x,y]:=false;
habitat[x,y]:=1;
// case habitat[x,y] of
// 0: form1.Image1.Canvas.Pixels[x,y]:=clwhite;
// 1: form1.Image1.Canvas.Pixels[x,y]:=clwhite;
end;
end;
/////**********************************************************************/////
///// Procedure to draw landscape /////
/////**********************************************************************/////
Procedure drawlandscape;
var
x,y: integer;
begin
for x:=1 to max_x do
for y:= 1 to breedte do
begin
case habitat[x,y] of
0: form1.Image1.Canvas.Pixels[x,y]:=clwhite;
1: form1.Image1.Canvas.Pixels[x,y]:=clblack;
end;
end;
end;
//****************************************************************************//
// Procedure place optimum //
//****************************************************************************//
Procedure placeoptimum;
begin
Topt:=inioptimum;
end;
//****************************************************************************//
// Procedure move optimum //
//****************************************************************************//
procedure moveoptimum;
begin
if (time>NEQUILIBRIUM) and (Topt<(max_x-inioptimum)) then Topt:=(Topt + cws);
end;
//------------------------------------------------------------------------------
//- function Poisson: function (level 4) called by Reproduction procedure -
//------------------------------------------------------------------------------
function Poisson(lambda:real):integer; // calculates the number of offsprings
// of a single female from a Poisson
// distribution using the variable
// "lambda" (fertility) as mean value
var
a,b :real;
i :integer;
begin // start algorithm from:
if lambda>88 then lambda:=88; // Law, A., Kelton, D., Simulation
a:=exp(- lambda); // Modeling And Analysis, McGraw-Hill
b:=1; // Book Company, New York, 1982,
i:=-1; // p. 258 - 260
repeat
i:=i+1;
b:=b*random;
until (b<a)or (i>199);
poisson:=i;
end;
/////**********************************************************************/////
///// Procedure competition /////
/////**********************************************************************/////
Procedure competition;
var
Nr,b,a,c,x,y,d:integer;
numcompetitors:integer;
chance_of_mort, comp_coef,e:double;
begin
for x := 1 to max_x do
begin
for y := 1 to breedte do
begin
eigenschap_cel[x,y].dens:=0;
eigenschap_cel[x,y].som:=0;
end;
end;
for d := 1 to totalpop do
begin
inc(eigenschap_cel[(population[d].posx),(population[d].posy)].dens);
e:= (eigenschap_cel[(population[d].posx),(population[d].posy)].som);
if ((exp((-1)*
(sqr(population[d].posx-(Topt + population[d].spec))/(100 *population[d].plas))))*10*(exp((-factor)*population[d].plas)))>0 then
eigenschap_cel[(population[d].posx),population[d].posy].som:=(e +((exp((-1)*
(sqr(population[d].posx-(Topt + population[d].spec))/(100 *population[d].plas))))*10*(exp((-factor)*population[d].plas))));
end;
numcompetitors:=0;
for Nr := 1 to totalpop do
begin
comp_coef:= ((exp((-1)*
(sqr(population[Nr].posx-(Topt + population[Nr].spec))
/(100 *population[Nr].plas))))*10*(exp((-factor)*population[Nr].plas)));
if comp_coef<=0 then chance_of_mort:=1 else
begin
chance_of_mort:= (1-(NSTER/(
(eigenschap_cel[(population[Nr].posx),(population[Nr].posy)].som)/comp_coef)));
if eigenschap_cel[(population[Nr].posx),(population[Nr].posy)].som< comp_coef then
writeln (eigenschap_cel[(population[Nr].posx),(population[Nr].posy)].som,
comp_coef, eigenschap_cel[(population[Nr].posx),(population[Nr].posy)].dens:5);
end;
if random> chance_of_mort then
begin
inc (numcompetitors);
competitors[numcompetitors] := population[Nr];
// form1.image1.Canvas.pixels[young[i].posx,young[i].posy]:=clyellow;
end;
end;
for a := 1 to totalpop do
begin
population[a].posx:=0;
population[a].posy:=0;
population[a].dispersalsd:=0;
population[a].plas:=0;
population[a].spec:=0;
end;
totalpop:=numcompetitors;
for b := 1 to totalpop do
begin
population[b]:=competitors[b];
end;
for c := 1 to numcompetitors do
begin
competitors[c].posx:=0;
competitors[c].posy:=0;
competitors[c].dispersalsd:=0;
competitors[c].plas:=0;
competitors[c].spec:=0;
end;
end;
/////**********************************************************************/////
///// Procedure reproduction /////
/////**********************************************************************/////
Procedure reproduction;
var
Nr,n,b:integer;
Newpop:integer;
offspring, populatie_ouders:integer; //offspring: absolute number of offspring per adult
mutation_spec, mutation_plas, mutation_disp:double;
begin
newpop:=0;
offspring:=0;
populatie_ouders:=totalpop;
for Nr := 1 to totalpop do
begin
offspring:= Poisson(LAMBDA);
if offspring>0 then
begin
for n := 1 to offspring do
begin
young[newpop+n]:= population[Nr];
if random<mutationrate_plas then mutation_plas := (random-0.5)else mutation_plas:=0;
if ((young[newpop+n].plas) + mutation_plas) <=0.00000000001 then mutation_plas:= ((-1)*mutation_plas) ;
young[newpop+n].plas:=population[Nr].plas + mutation_plas;
if random<mutationrate_disp then mutation_disp:= (random-0.5)else mutation_disp:=0;
if ((young[newpop+n].dispersalsd) + mutation_disp) <0 then mutation_disp:= ((-1)*mutation_disp);
young[newpop+n].dispersalsd :=population[Nr].dispersalsd + mutation_disp;
if random<mutationrate_spec then mutation_spec:= ((random*10)-5)else mutation_spec:=0;
young[newpop+n].spec:=population[Nr].spec + mutation_spec;
end;
newpop:=newpop+offspring;
end;
end;
totalpop:=newpop;
for b := 1 to populatie_ouders do
begin
population[b].posx:=0;
population[b].posy:=0;
population[b].dispersalsd:=0;
population[b].plas:=0;
population[b].spec:=0;
end;
end;
//continue working with array young//
/////**********************************************************************/////
///// Function for Gaussian dispersal /////
/////**********************************************************************/////
function DisperseGauss(location:TPoint;stratDist:double):TPoint;
var
dx,dy:extended;
begin
dy:=randG(0,stratdist);
dx:=randG(0,stratdist);
location.x:=round(location.x+dx);
location.y:=round(location.y+dy);
DisperseGauss:=location;
end;
/////**********************************************************************/////
///// Procedure to move to other point /////
/////**********************************************************************/////
procedure move;
var
nr:integer;
location,newlocation:TPoint;
a:double;
begin
for Nr := 1 to totalpop do
begin
location.x:=young[Nr].posx;
location.y:=young[Nr].posy;
a:= young[Nr].dispersalsd;
newlocation:=DisperseGauss(location,a);
young[nr].posx:=newlocation.x;
young[nr].posy:=newlocation.y;
if young[nr].posy>breedte then young[nr].posy:= (young[nr].posy - breedte);
if young[nr].posy<=0 then young[nr].posy:= (young[nr].posy + breedte);
end;
end;
/////**********************************************************************/////
///// Procedure to survive /////
/////**********************************************************************/////
procedure survive;
var
a,b,k,l:integer;
numsurvivors, populatie_zaden:integer;
begin
numsurvivors:=0;
populatie_zaden:=totalpop;
for a := 1 to totalpop do
begin
if (habitat[young[a].posx,young[a].posy]=1)
and (round((Topt-inioptimum+1))<=young[a].posx)
and (young[a].posx<=round(Topt+inioptimum))
and (1<=young[a].posy)
and (young[a].posy<=breedte)
then
begin
inc (numsurvivors);
survivors [numsurvivors] := young [a];
// form1.image1.Canvas.pixels[young[i].posx,young[i].posy]:=clyellow;
end;
end;
totalpop:=numsurvivors;
for b := 1 to totalpop do
begin
population[b]:=survivors[b];
end;
for l :=1 to populatie_zaden do
begin
young[l].posx:=0;
young[l].posy:=0;
young[l].dispersalsd:=0;
young[l].plas:=0;
young[l].spec:=0;
end;
for k := 1 to numsurvivors do
begin
survivors[k].posx:=0;
survivors[k].posy:=0;
survivors[k].dispersalsd:=0;
survivors[k].plas:=0;
survivors[k].spec:=0;
end;
end;
/////**********************************************************************/////
///// Procedure to interupt simulation /////
/////**********************************************************************/////
procedure TForm1.CloseButtonClick(Sender: TObject);
begin
halt(0);
close; // exits the program
end;
/////**********************************************************************/////
///// Procedure to initialise /////
/////**********************************************************************/////
procedure initialise;
var x,y: integer;
i: integer;
begin
totalpop:=0;
for i := 1 to INI_IND do
begin
begin
Repeat
begin
x:= round(random(100)+1); // random number is drawn within interval[0,99]
y:= round(random(breedte)+1);
population[i].posx:=x;
population[i].posy:=y;
end;
Until
(habitat[x,y]=1)
and (occupied[population[i].posx,population[i].posy]= false);
end;
occupied[population[i].posx,population[i].posy]:=true;
if (factor=1) or (factor=5) then population[i].plas:=(random*6+0.00000000000001);
if factor=10 then population[i].plas:=(random*2+0.00000000000001);
population[i].dispersalsd:=(5*random);
population[i].spec:=0;//((random*(2*inioptimum))-50);
// form1.image1.Canvas.pixels[population[i].posx,population[i].posy]:=clyellow;
inc(totalpop);
end;
//densloc array aanmaken
end;
/////**********************************************************************/////
///// Procedure to create landscape /////
/////**********************************************************************/////
procedure TForm1.CreateLandscapeButtonClick(Sender: TObject);
begin
CreateLandscapeUnit.createlandscape;
//drawlandscape;
writeln('p habitat = ',p_habitat:3:2);
end;
/////**********************************************************************/////
///// Procedure nemensteekproef /////
/////**********************************************************************/////
Procedure nemensteekproef;
var
i,a:integer;
begin
if totalpop>=100 then
begin
for i := 1 to steekproefgrootte do
begin
repeat a:=round(random*totalpop) until a>0 ;
steekproef[i]:=population[a];
end;
end;
end;
/////**********************************************************************/////
///// Main programme /////
/////**********************************************************************/////
procedure TForm1.RunButtonClick(Sender: TObject);
var
i,N,a:integer;
begin
assignfile(output,'results.txt');
rewrite(output);
for N := 1 to Nreplic do
begin
factor:=N;
if factor=2 then factor:=5;
if factor=3 then factor:=10;
for a := 1 to 5 do
begin
//CreateLandscapeUnit.createlandscape;
preparelandscape;
//drawlandscape;
placeoptimum;
initialise;
for time := 1 to NSIM do
begin
for i := 1 to steekproefgrootte do
begin
steekproef[i].plas:=0;
steekproef[i].posx:=0;
steekproef[i].posy:=0;
steekproef[i].dispersalsd:=0;
steekproef[i].spec:=0;
end;
writeln('generation',time:6);
competition;
if (time = 1000) or (time=2000) or (time=2100) then
begin
nemensteekproef;
for i := 1 to steekproefgrootte do
begin
writeln(output,time:6, factor:6, totalpop:9, steekproef[i].dispersalsd:10:6,steekproef[i].plas:12:8,steekproef[i].posx:6, steekproef[i].spec:8:4);
end;
end;
reproduction;
move;
survive;
moveoptimum;
//writeln(output, time:6, totalpop:6);
application.ProcessMessages;
end;
end;
end;
closefile(output);
writeln('Finished!');
end;
end.