-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions.java
91 lines (80 loc) · 3.22 KB
/
Functions.java
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
import java.util.Random;
import java.io.*;
class Functions {
static private Random diceRoller = new Random();
//write integer vector to files
static void writeIntVector(String strF, int[] vector){
try{
BufferedWriter fout0 = new BufferedWriter(new FileWriter(strF,true));
for (int aVector : vector) {
fout0.write(aVector + " ");
}
fout0.write("\n");
fout0.close();
}
catch(IOException e){
System.out.println("There was a problem"+e);
}
}
//write float vector to files
static void writeFloatVector(String strF, float[] vector){
try{
BufferedWriter fout0 = new BufferedWriter(new FileWriter(strF,true));
for (float aVector : vector) {
fout0.write(aVector + " ");
}
fout0.write("\n");
fout0.close();
}
catch(IOException e){
System.out.println("There was a problem"+e);
}
}
//sample from a bounded Gaussian distribution
static float gaussSample(float mean, float error, float min, float max){
float gauss = (float) diceRoller.nextGaussian();
float trait = error*gauss+mean;
while(trait <= min|| trait>= max){
gauss = (float) diceRoller.nextGaussian();
trait = error*gauss+mean;
}
return trait;
}
//sample within a range
static int rangeSample(int min, int max){
return diceRoller.nextInt(max-min+1)+min;
}
//inherit from parental phenotype according to trade-off bounds and drift rate
static float[] inheritDriftBounded(float prevD, float prevS, int fam, int drift){
float[] inTraits = {0,0};
float rangeD=Pars.divMax-Pars.divMin;
float rangeS=Pars.spMax-Pars.spMin;
//find drift vales for IMT and speed
float rand = rangeSample(-drift, drift)*Pars.divConv*Pars.epD;
float rand2 = rangeSample(-drift, drift)*Pars.speedConv*Pars.epS;
//add to parental values
float newD = prevD+rand;
float newS = prevS+rand2;
if(fam==0){//open trade-off - just fit within bounds
inTraits[0] = (newD>Pars.divMax) ? Pars.divMax : (newD<Pars.divMin) ? Pars.divMin : newD;
inTraits[1] = (newS>Pars.spMax) ? Pars.spMax : (newS<Pars.spMin) ? Pars.spMin : newS;
}
else{//convex (fam=1) and concave (fam=2) trade-offs, resample if out of bounds
boolean checkFit = (fam==1) ?
1>=(newD-Pars.divMax)*(newD-Pars.divMax)/(rangeD*rangeD)+(newS-Pars.spMin)*(newS-Pars.spMin)/(rangeS*rangeS):
1<=(newD-Pars.divMin)*(newD-Pars.divMin)/(rangeD*rangeD)+(newS-Pars.spMax)*(newS-Pars.spMax)/(rangeS*rangeS);
while(!checkFit){
rand = rangeSample(-drift, drift)*Pars.divConv*Pars.epD;
rand2 = rangeSample(-drift, drift)*Pars.speedConv*Pars.epS;
newD = prevD+rand;
newS = prevS+rand2;
checkFit = (fam==1) ?
1>=(newD-Pars.divMax)*(newD-Pars.divMax)/(rangeD*rangeD)+(newS-Pars.spMin)*(newS-Pars.spMin)/(rangeS*rangeS):
1<=(newD-Pars.divMin)*(newD-Pars.divMin)/(rangeD*rangeD)+(newS-Pars.spMax)*(newS-Pars.spMax)/(rangeS*rangeS);
}
inTraits[0] = (newD>Pars.divMax) ? Pars.divMax : (newD<Pars.divMin) ? Pars.divMin : newD;
inTraits[1] = (newS>Pars.spMax) ? Pars.spMax : (newS<Pars.spMin) ? Pars.spMin : newS;
}
return inTraits;
}
}