View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   import java.util.List;
4   import java.util.Random;
5   import java.util.ArrayList;
6   
7   /**
8    * (C) 2006 - 2008 Thomas Woehlke.
9    * http://thomas-woehlke.de/p/simulated-evolution/
10   * @author Thomas Woehlke
11   * Date: 04.02.2006
12   * Time: 19:55:23
13   */
14  public class CellCore {
15      private List<Integer> dna;
16      private int MAX_VALUE = 16;
17      private int MAX_INITIAL_VALUE = 8;
18      private Random random;
19  
20      public CellCore(Random random) {
21          dna = new ArrayList<Integer>();
22          this.random = random;
23          for (int i = 0; i < Dna.values().length; i++) {
24              int gen = random.nextInt() % MAX_INITIAL_VALUE;
25              dna.add(gen);
26          }
27      }
28  
29      private CellCore(Random random, List<Integer> rna) {
30          this.random = random;
31          dna = new ArrayList<Integer>();
32          dna.addAll(rna);
33      }
34  
35      public CellCore mitosisFactory() {
36          List<Integer> rna = new ArrayList<Integer>();
37          for (Integer dnaBase:dna) {
38              rna.add(dnaBase);
39          }
40          CellCore child = new CellCore(random, rna);
41          int baseIndex = random.nextInt() % Dna.values().length;
42          if (baseIndex < 0) {
43              baseIndex *= -1;
44          }
45          Dna base[] = Dna.values();
46          this.decrease(base[baseIndex]);
47          child.increase(base[baseIndex]);
48          return child;
49      }
50  
51      private void increase(Dna base) {
52          int value = dna.get(base.ordinal());
53          if (value == MAX_VALUE) {
54              for (int i = 0; i < dna.size(); i++) {
55                  Integer val = dna.get(i);
56                  val--;
57                  dna.set(i, val);
58              }
59          }
60          Integer val = dna.get(base.ordinal());
61          val++;
62          dna.set(base.ordinal(), val);
63      }
64  
65      private void decrease(Dna base) {
66          int value = dna.get(base.ordinal());
67          if (value == -MAX_VALUE) {
68              for (int i = 0; i < dna.size(); i++) {
69                  Integer val = dna.get(i);
70                  val++;
71                  dna.set(i, val);
72              }
73              dna.set(base.ordinal(), 0);
74          } else {
75              dna.set(base.ordinal(), value-1);
76          }
77      }
78  
79      public Dna getRandomOrientation() {
80          Dna orientation = Dna.FORWARD;
81          int dnaLength = Dna.values().length;
82          double sumDna = 0.0;
83          for (int i = 0; i < dnaLength; i++) {
84              double val = dna.get(i).longValue() ^ 2;
85              if (val < 0) {
86                  val *= -1;
87              }
88              sumDna += val;
89          }
90          double sum = 0.0;
91          double[] rna = new double[dnaLength];
92          for (int i = 0; i < dnaLength; i++) {
93              double val = dna.get(i).longValue() ^ 2;
94              if (val < 0) {
95                  val *= -1;
96              }
97              sum += val / sumDna;
98              rna[i] = sum;
99          }
100         if (sumDna != 0) {
101             double val = new Double(random.nextInt(MAX_VALUE) ^ 2);
102             if (val < 0) {
103                 val *= -1;
104             }
105             double sumRandom = val / new Double(MAX_VALUE ^ 2);
106             if (sumRandom < 0) {
107                 sumRandom *= -1;
108             }
109             int newInt = 0;
110             for (int i = 0; i < dnaLength; i++) {
111                 if (sumRandom > rna[i]) {
112                     newInt = i;
113                 }
114             }
115             orientation = Dna.values()[newInt];
116         }
117         return orientation;
118     }
119 }