carfield.com.hk Genetic.class 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z Genetic.java 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">// Genetic Algorithm Java classes // // Copyright 1996, Mark Watson. All rights reserved. import java.util.*; abstract public class Genetic { protected int numGenesPerChromosome; // number of genes per chromosome protected int numChromosomes; // number of chromosomes private BitSet chromosomes[]; protected float fitness[]; private float crossoverFraction; private float mutationFraction; private int [] rouletteWheel; private int rouletteWheelSize; public Genetic(int num_genes_per_chromosome, int num_chromosomes) { this(num_genes_per_chromosome, num_chromosomes, 0.8f, 0.01f); } public Genetic(int num_genes_per_chromosome, int num_chromosomes, float crossover_fraction, float mutation_fraction) { numGenesPerChromosome = num_genes_per_chromosome; numChromosomes = num_chromosomes; crossoverFraction = crossover_fraction; mutationFraction = mutation_fraction; chromosomes = new BitSet[num_chromosomes]; for (int i=0; i&lt;num_chromosomes; i++) { chromosomes[i] = new BitSet(num_genes_per_chromosome); for (int j=0; j&lt;num_genes_per_chromosome; j++) { if (Math.random() &lt; 0.5) chromosomes[i].set(j); } } fitness = new float[num_chromosomes]; for (int f=0; f&lt;num_chromosomes; f++) { fitness[f] = -999; } sort(); // define the roulette wheel: rouletteWheelSize = 0; for (int i=0; i&lt;numGenesPerChromosome; i++) { rouletteWheelSize += i + 1; } System.out.println(&quot;count of slots in roulette wheel=&quot; + rouletteWheelSize); rouletteWheel = new int[rouletteWheelSize]; int num_trials = numGenesPerChromosome; int index = 0; for (int i=0; i&lt;numGenesPerChromosome; i++) { for (int j=0; j&lt;num_trials; j++) { rouletteWheel[index++] = i; } num_trials--; } } public boolean getGene(int chromosome, int gene) { return chromosomes[chromosome].get(gene); } public void setGene(int chromosome, int gene, int value) { if (value == 0) chromosomes[chromosome].clear(gene); else chromosomes[chromosome].set(gene); } public void setGene(int chromosome, int gene, boolean value) { if (value) chromosomes[chromosome].set(gene); else chromosomes[chromosome].clear(gene); } public void sort() { BitSet btemp; for (int c=0; c&lt;numChromosomes; c++) { for (int d=(numChromosomes - 2); d&gt;=c; d--) { if (fitness[d] &lt; fitness[d+1]) { btemp = chromosomes[d]; float x = fitness[d]; chromosomes[d] = chromosomes[d+1]; fitness[d] = fitness[d+1]; chromosomes[d+1] = btemp; fitness[d+1] = x; } } } } public void evolve() { calcFitness(); sort(); doCrossovers(); doMutations(); doRemoveDuplicates(); } public void doCrossovers() { int num = (int)(numChromosomes * crossoverFraction); for (int i=num-1; i&gt;=0; i--) { int c1 = (int)(rouletteWheelSize * Math.random() * 0.9999f); int c2 = (int)(rouletteWheelSize * Math.random() * 0.9999f); c1 = rouletteWheel[c1]; c2 = rouletteWheel[c2]; if (c1 != c2) { int locus = 1 + (int)((numGenesPerChromosome - 2) * Math.random()); for (int g=0; g&lt;numGenesPerChromosome; g++) { if (g &lt; locus) { setGene(i, g, getGene(c1, g)); } else { setGene(i, g, getGene(c2, g)); } } } } } // 'to' and 'from' are 'sorted' indices: private void copyGene(int to, int from) { for (int i=0; i&lt;numGenesPerChromosome; i++) if (getGene(from, i)) setGene(to, i, 1); else setGene(to, i, 0); } public void doMutations() { int num = (int)(numChromosomes * mutationFraction); for (int i=0; i&lt;num; i++) { int c = (int)(numChromosomes * Math.random() * 0.99); int g = (int)(numGenesPerChromosome * Math.random() * 0.99); setGene(c, g, !getGene(c, g)); } } public void doRemoveDuplicates() { for (int i=numChromosomes - 1; i&gt;3; i--) { for (int j=0; j&lt;i; j++) { if (chromosomes[i].equals(chromosomes[j])) { int g = (int)(numGenesPerChromosome * Math.random() * 0.99); setGene(i, g, !getGene(i, g)); break; } } } } // Override the following function in sub-classes: abstract public void calcFitness() ; } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z MyGenetic.class 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z RegressionGenetic.class 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z RegressionTest.class 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z RegressionTest.java 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">// File: RegressionTest.java // This file contains a text-mode test program // for class Genetic for doing a polynomial regression test. public class RegressionTest { static RegressionGenetic g; static public void main(String args[]) { // we will use chromosomes with 10 1 bit genes per // floating pouint number (20 bits for 5 numbers) // and a population of 100 chromosomes: g = new RegressionGenetic(30, 5000, 0.98f, 0.15f); System.out.println(&quot;calcNormalization(10)=&quot; + g.calcNormalization(10)); System.out.println(&quot;calcNormalization(20)=&quot; + g.calcNormalization(20)); System.out.println(&quot;calcNormalization(5)=&quot; + g.calcNormalization(5)); for (int i=0; i&lt;501; i++) { g.evolve(); if ((i%50)==0 || i &lt; 8) { System.out.println(&quot;Generation &quot; + i); g.print(); } } System.out.println(&quot;gmin=&quot;+RegressionGenetic.gmin+&quot;, gmax=&quot;+RegressionGenetic.gmax); } } class RegressionGenetic extends Genetic { RegressionGenetic(int num_g, int num_c, float crossover_fraction, float mutation_fraction) { super(num_g, num_c, crossover_fraction, mutation_fraction); if ((num_g / 5)*5 != num_g) { System.out.println(&quot;Error, number of genes per chromosome must be evenly divisible by 5&quot;); System.exit(1); } bitsPerNumber = num_g / 5; normalization = calcNormalization(bitsPerNumber); } public float calcNormalization(int num_bits) { float base = 1.0f; float ret = 0.0f; for (int i=0; i&lt;num_bits - 3; i++) { ret += base; base *= 2; } return base; } private int bitsPerNumber; private float normalization; private float calcFitnessForCoefficients(float [] coef) { float ret = 0.0f; // evaluate function error over a range of values of x: for (float x=0.0f; x&lt;=10.0f; x+= 0.05) { // true value of regression function: float Ftrue = x*x*x*x + x*x*x + x*x + x; // value of generated function at x: float F = coef[0] * x*x*x*x + coef[1] * x*x*x + coef[2] * x*x + coef[3] * x + coef[4]; //ret += (float)Math.sqrt((F - Ftrue) * (F - Ftrue)); ret += (F - Ftrue) * (F - Ftrue); } return 25000.0f - ret; } public static float gmin = 10000000000.0f; public static float gmax = -gmin; float geneToFloat(int chromosomeIndex, int numberIndex) { int base = 1; float x = 0; for (int j=0; j&lt;bitsPerNumber; j++) { if (getGene(chromosomeIndex, j + numberIndex * bitsPerNumber)) { x += base; } base *= 2; } x /= normalization; x -= 5.0f; if (gmin &gt; x) gmin = x; if (gmax &lt; x) gmax = x; return x; } // more efficient to allocate this only once: private float [] coefficients = new float[5]; public void calcFitness() { for (int i=0; i&lt;numChromosomes; i++) { for (int j=0; j&lt;5; j++) { coefficients[j] = geneToFloat(i, j); } fitness[i] = calcFitnessForCoefficients(coefficients); } } public void print() { sort(); int maxindex = 0; float maval = 0.0f; float sum = 0.0f; for (int i=0; i&lt;numChromosomes; i++) { for (int j=0; j&lt;5; j++) { coefficients[j] = geneToFloat(i, j); } sum += fitness[i]; if (i &lt; 4 || (i % 100) == 0) { System.out.print(&quot;Fitness for chromosome &quot; + i + &quot; is &quot;); System.out.print(fitness[i] + &quot; coefficients:&quot;); for (int j=0; j&lt;5; j++) System.out.print(pp(coefficients[j])); System.out.println(); } } sum /= (float)numChromosomes; System.out.println(&quot;Average fitness=&quot; + sum); } private String pp(float x) { x = (int)(1000 * x); x /= 1000.0f; return &quot;&quot; + x + &quot; &quot;; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z TestGenetic.class 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z TestGenetic.java 2000-06-16T16:00:00Z 2000-06-16T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">// File: textGenetic.java // This file contains a text-mode test program // for class Genetic. public class TestGenetic { static MyGenetic g; static public void main(String args[]) { // we will use chromosomes with 10 1 bit genes per // chromosomes, and a population of 12 chromosomes: g = new MyGenetic(10, 30, 0.85f, 0.2); for (int i=0; i&lt;21; i++) { g.evolve(); //g.calcFitness(); //if ((i%5)==0) { System.out.println(&quot;Generation &quot; + i); g.print(); //} } } } class MyGenetic extends Genetic { MyGenetic(int num_g, int num_c, float crossover_fraction, float mutation_fraction) { super(num_g, num_c, crossover_fraction, mutation_fraction); } private float fitness(float x) { return (float)(Math.sin(x) * Math.sin(0.4f * x) * Math.sin(3.0f * x)); } float geneToFloat(int geneIndex) { int base = 1; float x = 0; for (int j=0; j&lt;numGenesPerChromosome; j++) { if (getGene(geneIndex, j)) { x += base; } base *= 2; } x /= 128.0f; return x; } public void calcFitness() { for (int i=0; i&lt;numChromosomes; i++) { float x = geneToFloat(i); fitness[i] = fitness(x); } } public void print() { sort(); int maxindex = 0; float maval = 0.0f; float sum = 0.0f; for (int i=0; i&lt;numChromosomes; i++) { float x = geneToFloat(i); sum += fitness[i]; if (i &lt; 6) { System.out.print(&quot;Fitness for chromosome &quot;); System.out.print(i); System.out.print(&quot; is &quot;); System.out.println(fitness[i] + &quot;, occurs at x=&quot; + x); } } sum /= (float)numChromosomes; System.out.println(&quot;Average fitness=&quot; + sum); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-16T16:00:00Z Graph.class 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z Graph.java 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100"> import javax.swing.*; import java.awt.*; /** * Title: Graph&lt;p&gt; * Description: Simple program to produce a graph for the book&lt;p&gt; * Copyright: Copyright (c) Mark Watson&lt;p&gt; * Company: &lt;p&gt; * @author Mark Watson * @version 1.0 */ public class Graph extends JFrame { GraphPanel jPanel1; float [] data; public Graph() { try { int size = 500; data = new float[size]; float xmin = 0; float xmax = 10; for (int i=0; i&lt;size; i++) { float x = i; x = xmin + x * (xmax - xmin) / (float)size; if (i==190) System.out.println(&quot;x=&quot;+x); float x1 = (float)Math.sin(x); float x2 = (float)Math.sin(x * 0.4f); float x3 = (float)Math.sin(x * 3.0f); data[i] = x1 * x2 * x3; } jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Graph untitled11 = new Graph(); } private void jbInit() throws Exception { jPanel1 = new GraphPanel(data); jPanel1.setBackground(Color.white); this.setDefaultCloseOperation(3); this.getContentPane().add(jPanel1, BorderLayout.CENTER); setSize(550, 500); jPanel1.setVisible(true); setVisible(true); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z GraphPanel.class 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z GraphPanel.java 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import javax.swing.*; import java.awt.*; public class GraphPanel extends java.awt.Canvas { // JPanel { public GraphPanel(float [] data) { super(); this.data = data; } Color black = new Color(0, 0, 0); float [] data; public void paint(Graphics g) { if (data == null) return; int width = this.getWidth(); int height = this.getHeight(); System.out.println(&quot;height=&quot;+height); float min = 99999999.9f; float max = -min; int maxindex = 0; float maxval = 0.0f; for (int i=0; i&lt;data.length; i++) { if (min &gt; data[i]) min = data[i]; if (max &lt; data[i]) { max = data[i]; maxindex = i; maxval = max; } } System.out.println(&quot;min=&quot; + min +&quot;, max=&quot; + max + &quot;, max index=&quot;+maxindex + &quot;, max val=&quot;+maxval); g.setColor(black); for (int i=0; i&lt;data.length - 1; i++) { float y1 = height - 5 - 0.95f *height * ((data[i] - min) / (max - min)); float y2 = height - 5 - 0.95f *height * ((data[i+1] - min) / (max - min)); //System.out.println(&quot;data[&quot;+i+&quot;]=&quot;+data[i]+&quot;, y1=&quot;+y1+&quot;, y2=&quot;+y2); g.drawLine(i+20, (int)y1, i+21, (int)y2); } float yzero = height - 5 - 0.95f *height * ((0.0f - min) / (max - min)); g.drawLine(20, (int)yzero, data.length + 19, (int)yzero); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z UnnamedPackage.dependency 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z dependency cache.dfPackage 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z graph.jpr 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z misc.dfPackage 2000-06-15T16:00:00Z 2000-06-15T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-15T16:00:00Z ga.jpr 2000-04-02T16:00:00Z 2000-04-02T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-04-02T16:00:00Z