01: package jaimoves.immun;
02:
03: import java.util.BitSet;
04: import java.util.Random;
05: import java.lang.Comparable;
06:
07: public class AntiBody implements Comparable { // pojedyncze rozwiazanie
08: private Random rnd = new Random();
09:
10: private BitSet kod2;
11: private double fitting;
12:
13: public AntiBody(int length) {
14: kod2 = new BitSet(length);
15: for (int i = 0; i < kod2.size(); i++) {
16: if (rnd.nextBoolean())
17: kod2.set(i);
18: // getKod().add(new Boolean(rnd.nextBoolean()));
19: }
20: }
21:
22: public AntiBody(BitSet bits) {
23: kod2 = bits;
24: }
25:
26: public AntiBody cloneOperation() {
27: BitSet kod2Clone = new BitSet();
28: kod2Clone = (BitSet) kod2.clone();
29: AntiBody ret = new AntiBody(kod2.length());
30: ret.setKod(kod2Clone);
31: return ret;
32: }
33:
34: public void hipermutate(int number, int var) {
35: // System.out.println(kod2.length());
36: switch (var) {
37: case 1:
38: for (int i = 0; i < number; i++) {
39: int a = rnd.nextInt(kod2.size());
40: int b = rnd.nextInt(kod2.size()); // TUTAJ JEST CHYBA BLAD
41: boolean AA = kod2.get(a);
42: kod2.set(a, kod2.get(b));
43: kod2.set(b, AA);
44: }
45: return;
46: case 2:
47: for (int i = 0; i < number; i++) {
48: int a = rnd.nextInt(kod2.size());
49: kod2.flip(a);
50: }
51: return;
52: }
53: }
54:
55: public int hammingDist(AntiBody a) {
56: BitSet temp = (BitSet) kod2.clone();
57: temp.xor(a.kod2);
58: // System.out.print(" " + a.getKod().cardinality() + " ");
59: return a.getKod().cardinality();
60: }
61:
62: public BitSet getKod() {
63: return kod2;
64: }
65:
66: public void setKod(BitSet kod) {
67: this .kod2 = kod;
68: }
69:
70: public double getFitting() {
71: return fitting;
72: }
73:
74: public void setFitting(double fitting) {
75: this .fitting = fitting;
76: }
77:
78: public int compareTo(Object o) {
79: AntiBody anti = (AntiBody) o;
80: if (anti.getFitting() > getFitting())
81: return +1;
82: else if (anti.getFitting() == getFitting())
83: return 0;
84: else
85: return -1;
86: }
87: }
|