-------------------------------------------Hillstart---------------------------------------------- import java.util.Random; import java.io.*; import java.util.Date; import java.math.*; //HillClimbing mit Zugriff auf HauptBit public class Hillstart { public static void main(String[] args) { float pm = (long)0.015; float pc = (long)0.9; long zeitvor = 0; long zeitnach = 0; int i = 0; String erg = "nofit"; zeitvor = System.nanoTime(); for(i = 1; i < 101;i++) { HauptBit a = new HauptBit(); a.uebergabe(args,pc,pm); a.make_genom(); System.out.println(i +". Durchlauf"); for(;erg == "nofit";) { erg = a.hill(); } erg = "nofit"; } zeitnach = System.nanoTime(); zeitnach = zeitnach-zeitvor; System.out.println("Dauer: " + zeitnach + " NanoSekunden"); System.out.println("entspricht: " + zeitnach/1000000000 + " Sekunden"); } } --------------------------------------HauptBit------------------------------------- import java.util.*; import java.math.*; //KLASSE MIT BITSETS public class HauptBit { String[] auswahl; String[] repvalue = new String[2]; BitSet[] gene, hf; long[] fit; boolean fett; float pc = 0, pm = 0, initrate = 0, replication_scheme = 0, max_generations = 0; int genecnt, genelen, repanz, repproz; long max = 0; long b2dmax = 0; boolean protect_best = true; public void uebergabe(String[] args, float upc, float upm) { for (int i = 0; i < args.length; i++) { auswahl = args[i].split("="); pc = upc; pm = upm; if (args[i].startsWith("initrate")) { initrate = Float.parseFloat(auswahl[1]); } if (args[i].startsWith("genecnt")) { genecnt = Integer.parseInt(auswahl[1]); } if (args[i].startsWith("genelen")) { genelen = Integer.parseInt(auswahl[1]); for(int y = 0;y < genelen;y++) { b2dmax += new Double(Math.pow(2,y)).longValue(); } } if (args[i].startsWith("replication_scheme")) { repvalue = auswahl[1].split("x"); repanz = Integer.parseInt(repvalue[0]); repproz = Integer.parseInt(repvalue[1]); } if (args[i].startsWith("max_generations")) { max_generations = Float.parseFloat(auswahl[1]); } // Werte 1 für ja, 0 für nein if (args[i].startsWith("protect_best")) { if (auswahl[1].equals("1")) { protect_best = true; //System.out.println("PROTECT BEST!!!"); } else { protect_best = false; } } } } public int zufall(int z) { int rw = 0; Random rd = new Random(System.nanoTime()); rw = Math.abs(rd.nextInt(z)); return rw; } public void make_genom() { gene = new BitSet[genecnt]; for (int x = 0; x < genecnt; x++) { BitSet seq = new BitSet(); for (int j = 0; j < ((genelen) * (initrate)); j++) { int zz = zufall(genelen); if (seq.get(zz) == false) { seq.set(zz, true); } else { j--; } } //System.out.println(bitaus(seq)); gene[x] = (BitSet) seq.clone(); } } public void cross(float gen1, float gen2, float pos) { if (protect_best && gen1 == 0 && fit == null) { // ProtectBest } else { BitSet g1 = new BitSet(); BitSet g2 = new BitSet(); int p = (int) pos; g1 = gene[(int) gen1].get(p, genelen - 1); g2 = gene[(int) gen2].get(p, genelen - 1); for (int i = 0; i < genelen; i++) { gene[(int) gen1].set(p + i, g2.get(i)); gene[(int) gen2].set(p + i, g1.get(i)); } } } public void transpos(float gen1, float gen2, float pos1, float pos2) { if (protect_best && (gen2 == 0) && fit == null) { // ProtectBest } else { BitSet g1 = new BitSet(); int p1 = (int)pos1; int p2 = (int)pos2; if(p1 > p2){p2 = (zufall(genelen-p1))+p1;} g1 = gene[(int) gen1].get(p1,p2); for (int i = 0; i < p2-p1; i++) { gene[(int) gen2].set(p1 + i, g1.get(i)); } } } public void mutate() { int zz = 0, zz2 = 0; for (int i = 1; i <= (genelen * genecnt * pm); i++) { zz = zufall(genecnt); zz2 = zufall(genelen); if (protect_best && zz == 0 && fit == null) { i--; } else { gene[zz].flip(zz2); } } } //hillclimbing------------------------ public String hill() { int bestpos = 0; long best = 0, alt = 0; for(int i = 0;i < genecnt;i++) // Gene durchlaufen { boolean neu = false; alt = bin2(gene[i]); for(int j = 0;j < genelen; j++) // Positionen durchlaufen { gene[i].flip(j); // Position mutieren if(alt <= bin2(gene[i])) // Gucken ob Position besser { bestpos = j; // Neue beste Position setzten neu = true; } gene[i].flip(j); } //Wenn alle Positionen eines Gens geprüft wurden if(neu = true) // Wurde die Situation verbessert? { gene[i].flip(bestpos); // Beste Position mutieren best = bin2(gene[i]); if(best >= b2dmax) // Wenn die volle Fitness erreicht wurde { return "fit"; } } //System.out.println(best+" <> "+b2dmax); } return "nofit"; } //hillclimbing-------------ENDE-------- public void fitness() { fit = new long[genecnt]; long c = 0; for (int j = 0; j < genecnt; j++) { c = gene[j].cardinality(); fit[j] = c; bestfit(fit[j]); if (fit[j] == genelen) { fett = true; } } } public void fitness2dbl() { fit = new long[genecnt]; long c = 0; for (int j = 0; j < genecnt; j++) { c = bin2(gene[j]); fit[j] = c; bestfit(fit[j]); if (fit[j] == b2dmax) { fett = true; } } } public void fitsort() { hf = new BitSet[genecnt]; long max = 0; int ind = 0; for (int j = 0; j < genecnt; j++) { max = 0; for (int i = 0; i < genecnt; i++) { if (fit[i] > max) { max = fit[i]; ind = i; } } hf[j] = (BitSet) gene[ind].clone(); fit[ind] = 0; } for (int u = 0; u < genecnt; u++) { gene[u] = (BitSet) hf[u].clone(); } } public long bestfit(long fit) { if (fit > max) { max = fit; } return max; } public void replic_sh() { hf = new BitSet[genecnt]; //Kopieren in Hilfsarray for (int z = 0; z < genecnt; z++) { hf[z] = (BitSet) gene[z].clone(); } int u = 0, r = 0; //repanz für die ersten 20 z.B. if (genecnt < repanz) { System.out.println("Replikation nicht möglich!!! Genanzahl kleiner als Auswahlanzahl!!!"); } else { for (int j = 0; j < repanz; j++) { for (int i = 0; i < (genecnt / repanz); i++) { hf[i + u] = (BitSet) gene[j].clone(); } r++; u = (genecnt / repanz) * r; } for (int z = 0; z < genecnt; z++) { gene[z] = (BitSet) hf[z].clone(); } } } //Ausgabe einer Sequenz als String public String bitaus(BitSet bs) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < genelen; i++) { if (bs.get(i) == true) { sb.append("1"); } else { sb.append("0"); } } System.out.print("long: "+ bin2(bs)+" "); return sb.toString(); } // Umrechnen Bit zu long ein GEN public long bin2(BitSet bs) { long lo = 0; int lauf = genelen; for (int i = 0; i < genelen; i++) { if (bs.get(i) == true) { lo = lo + new Double(Math.pow(2,lauf)).longValue(); } lauf--; } lo = lo/2; return lo; } }