| 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 |
package dr.evolution.wrightfisher; |
| 27 |
|
| 28 |
import dr.math.MathUtils; |
| 29 |
import dr.math.distributions.GammaDistribution; |
| 30 |
|
| 31 |
public class GammaInvFitnessFunction extends FitnessFunction { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public GammaInvFitnessFunction(int genomeLength, double alpha, double beta, double pInv, int stateSize, boolean randomFittest) { |
| 37 |
|
| 38 |
GammaDistribution gamma = new GammaDistribution(beta, alpha); |
| 39 |
|
| 40 |
fitness = new double[genomeLength][stateSize]; |
| 41 |
fittest = new byte[genomeLength]; |
| 42 |
int fitpos = 0; |
| 43 |
for (int i = 0; i < genomeLength; i++) { |
| 44 |
|
| 45 |
if (randomFittest) { |
| 46 |
fitpos = MathUtils.nextInt(stateSize); |
| 47 |
} |
| 48 |
fitness[i][fitpos] = 1.0; |
| 49 |
fittest[i] = (byte) fitpos; |
| 50 |
for (int j = 0; j < stateSize; j++) { |
| 51 |
|
| 52 |
if (j != fitpos) { |
| 53 |
|
| 54 |
if (MathUtils.nextDouble() > pInv) { |
| 55 |
|
| 56 |
double prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0; |
| 57 |
while ((prob <= 0.0) || (prob >= 1.0)) { |
| 58 |
prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0; |
| 59 |
} |
| 60 |
fitness[i][j] = Math.max(0.0, 1.0 - gamma.quantile(prob)); |
| 61 |
} else { |
| 62 |
fitness[i][j] = 0.0; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public final double getFitness(byte[] sequence) { |
| 70 |
|
| 71 |
double totalFitness = 1.0; |
| 72 |
|
| 73 |
for (int i = 0; i < sequence.length; i++) { |
| 74 |
totalFitness *= fitness[i][sequence[i]]; |
| 75 |
} |
| 76 |
return totalFitness; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public double getFitnessFactor(int pos, byte newState, byte oldState) { |
| 83 |
return fitness[pos][newState] / fitness[pos][oldState]; |
| 84 |
} |
| 85 |
|
| 86 |
public final double[][] getFitnessTable() { |
| 87 |
|
| 88 |
for (int j = 0; j < fitness[0].length; j++) { |
| 89 |
for (int i = 0; i < fitness.length; i++) { |
| 90 |
System.out.print((Math.round(fitness[i][j] * 1000.0) / 1000.0) + "\t"); |
| 91 |
} |
| 92 |
System.out.println(); |
| 93 |
} |
| 94 |
|
| 95 |
return fitness; |
| 96 |
} |
| 97 |
|
| 98 |
public void initializeToFittest(byte[] genome) { |
| 99 |
System.arraycopy(fittest, 0, genome, 0, fittest.length); |
| 100 |
} |
| 101 |
|
| 102 |
double[][] fitness; |
| 103 |
byte[] fittest = null; |
| 104 |
} |