Debian Codegraph @

src/dr/evolution/wrightfisher/EmpiricalFitnessFunction.java

raw symbols SCIP
1 /*
2 * EmpiricalFitnessFunction.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.wrightfisher;
27
28 import dr.math.MathUtils;
29
30 public class EmpiricalFitnessFunction extends FitnessFunction {
31
32 public EmpiricalFitnessFunction(int genomeLength, double[] mutationFitnesses, int stateSize, boolean isRandom) {
33
34 fitness = new double[genomeLength][stateSize];
35 logFitness = new double[genomeLength][stateSize];
36 int index = 0;
37 for (int i = 0; i < genomeLength; i++) {
38 fitness[i][0] = 1.0;
39 for (int j = 1; j < stateSize; j++) {
40 if (isRandom) {
41 fitness[i][j] = mutationFitnesses[MathUtils.nextInt(mutationFitnesses.length)];
42 } else {
43 fitness[i][j] = mutationFitnesses[index];
44 index = (index + 1) % mutationFitnesses.length;
45 }
46 }
47 }
48
49 // tabulate log fitnesses
50 for (int i = 0; i < stateSize; i++) {
51 for (int j = 0; j < genomeLength; j++) {
52 logFitness[j][i] = Math.log(fitness[j][i]);
53 }
54 }
55 }
56
57 public final double getFitness(byte[] sequence) {
58
59 /*double totalFitness = 0.0;
60
61 for (int i = 0; i < sequence.length; i++) {
62 totalFitness += logFitness[i][sequence[i]];
63 }
64 return Math.exp(totalFitness);
65 */
66 double totalFitness = 1.0;
67
68 for (int i = 0; i < sequence.length; i++) {
69 totalFitness *= fitness[i][sequence[i]];
70 }
71 return totalFitness;
72 }
73
74 /**
75 * @return the relative fitness increase of the new state at given position to the old state.
76 */
77 public double getFitnessFactor(int pos, byte newState, byte oldState) {
78 return fitness[pos][newState] / fitness[pos][oldState];
79 }
80
81 public final double[][] getFitnessTable() {
82 return fitness;
83 }
84
85 public void initializeToFittest(byte[] genome) {
86 for (int i = 0; i < genome.length; i++) {
87 genome[i] = 0;
88 }
89 }
90
91
92 double[][] fitness;
93 double[][] logFitness;
94 }