www.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<num_chromosomes; i++) {
chromosomes[i] = new BitSet(num_genes_per_chromosome);
for (int j=0; j<num_genes_per_chromosome; j++) {
if (Math.random() < 0.5) chromosomes[i].set(j);
}
}
fitness = new float[num_chromosomes];
for (int f=0; f<num_chromosomes; f++) {
fitness[f] = -999;
}
sort();
// define the roulette wheel:
rouletteWheelSize = 0;
for (int i=0; i<numGenesPerChromosome; i++) {
rouletteWheelSize += i + 1;
}
System.out.println("count of slots in roulette wheel=" + rouletteWheelSize);
rouletteWheel = new int[rouletteWheelSize];
int num_trials = numGenesPerChromosome;
int index = 0;
for (int i=0; i<numGenesPerChromosome; i++) {
for (int j=0; j<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<numChromosomes; c++) {
for (int d=(numChromosomes - 2); d>=c; d--) {
if (fitness[d] < 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>=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<numGenesPerChromosome; g++) {
if (g < 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<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<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>3; i--) {
for (int j=0; j<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("calcNormalization(10)=" + g.calcNormalization(10));
System.out.println("calcNormalization(20)=" + g.calcNormalization(20));
System.out.println("calcNormalization(5)=" + g.calcNormalization(5));
for (int i=0; i<501; i++) {
g.evolve();
if ((i%50)==0 || i < 8) {
System.out.println("Generation " + i);
g.print();
}
}
System.out.println("gmin="+RegressionGenetic.gmin+", gmax="+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("Error, number of genes per chromosome must be evenly divisible by 5");
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<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<=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<bitsPerNumber; j++) {
if (getGene(chromosomeIndex, j + numberIndex * bitsPerNumber)) {
x += base;
}
base *= 2;
}
x /= normalization;
x -= 5.0f;
if (gmin > x) gmin = x;
if (gmax < 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<numChromosomes; i++) {
for (int j=0; j<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<numChromosomes; i++) {
for (int j=0; j<5; j++) {
coefficients[j] = geneToFloat(i, j);
}
sum += fitness[i];
if (i < 4 || (i % 100) == 0) {
System.out.print("Fitness for chromosome " + i + " is ");
System.out.print(fitness[i] + " coefficients:");
for (int j=0; j<5; j++) System.out.print(pp(coefficients[j]));
System.out.println();
}
}
sum /= (float)numChromosomes;
System.out.println("Average fitness=" + sum);
}
private String pp(float x) {
x = (int)(1000 * x);
x /= 1000.0f;
return "" + x + " ";
}
}
</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<21; i++) {
g.evolve();
//g.calcFitness();
//if ((i%5)==0) {
System.out.println("Generation " + 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<numGenesPerChromosome; j++) {
if (getGene(geneIndex, j)) {
x += base;
}
base *= 2;
}
x /= 128.0f;
return x;
}
public void calcFitness() {
for (int i=0; i<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<numChromosomes; i++) {
float x = geneToFloat(i);
sum += fitness[i];
if (i < 6) {
System.out.print("Fitness for chromosome ");
System.out.print(i);
System.out.print(" is ");
System.out.println(fitness[i] + ", occurs at x=" + x);
}
}
sum /= (float)numChromosomes;
System.out.println("Average fitness=" + 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<p>
* Description: Simple program to produce a graph for the book<p>
* Copyright: Copyright (c) Mark Watson<p>
* Company: <p>
* @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<size; i++) {
float x = i;
x = xmin + x * (xmax - xmin) / (float)size;
if (i==190) System.out.println("x="+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: <p>
* Description: <p>
* Copyright: Copyright (c) <p>
* Company: <p>
* @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("height="+height);
float min = 99999999.9f;
float max = -min;
int maxindex = 0;
float maxval = 0.0f;
for (int i=0; i<data.length; i++) {
if (min > data[i]) min = data[i];
if (max < data[i]) {
max = data[i];
maxindex = i;
maxval = max;
}
}
System.out.println("min=" + min +", max=" + max +
", max index="+maxindex + ", max val="+maxval);
g.setColor(black);
for (int i=0; i<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("data["+i+"]="+data[i]+", y1="+y1+", y2="+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