Problem with evolutionary system

Hi, i wrote such code to implement evolutionary system strategy to compute numericaly for which x following expression is true:

F(x) = 1/sqrt(2*pi) * integral_from_0_to_x_of(exp(0.5)*u^2)du = A

whrere A is 0.53

OK, i think the mathematical side of the problem is not what really matters here. The idea of program is as follows.

I create a population of parents, that is array of of objects of class Chromosom, x and r are initialized randomly within (0,5). Then I evaluate population of parents - check what is the value of the expression for each x and what is the diffrence between A and that value [stored in Chromosom.xDiff]. The I sort that array in terms of increasing xDiff, choose 5 best results and mutate them in population of Children by given equations. Then a population temp of Mutated Children and Parents is created and it is sorted as before by increasing xDiff. 10 first (best) objects are taken from temp and they create new population of Parents. The procedure is repeated until stop condition.

The program COMPILES and RUNS but it crashes the system, I mean it calculates something intensively but soon I have to restat my system becuse everthing is down.

PLEASE HELP MEE !!

Code is as follows:

//////////////////////////////////Chromosom

package LibEvolution;

public class Chromosom implements Comparable<Chromosom> {

public double x;

public double r;

public double xDiff;

public static final int MAX_X = 5;

public static final int MAX_R = 5;

public static final double A = 0.53;

public Chromosom() {

this.x = Math.random()*MAX_X;

this.r = Math.random()*MAX_R;

}

public int compareTo(Chromosom c){

if(this.xDiff > c.xDiff) return 1;

if(this.xDiff == c.xDiff) return 0;

if(this.xDiff < c.xDiff) return -1;

return 0;

}

}

////////////////////////////////////////////////////////////////////////Evo

package evo;

import LibEvolution.Chromosom;

import LibEvolution.Fx;

import java.util.Arrays;

public class Evo {

public static final int N = 10;

public static final int CHOSEN = 5;

public static int t = 1;

public static final double A = 0.53;

public static final double EPS = 0.001;

public static void main(String args[]){

Chromosom[] P = new Chromosom[N]; //Parents

Chromosom[] C = new Chromosom[CHOSEN]; //Children

Chromosom[] temp = new Chromosom[N + CHOSEN]; //Parents + Children

Fx function = new Fx();

double r1 = 1/Math.sqrt(2*N);

double r2 = 1/Math.sqrt(2*Math.sqrt(N));

for(int i = 0; i < N; i++){

P = new Chromosom();

}

// PARENTS INITIALIZED

do{

for(int i = 0; i < N; i++){

P.xDiff = Math.abs(A - function.evaluate(P.x));

}

// PARENTS EVALUATED

Arrays.sort(P);

System.out.println("Parents Set, iteration: " + t);

for(int i = 0; i < N; i++){

System.out.println("xDiff = " + P.xDiff + " x = " + P.x + " r = " + P.r);

}

// PARENTS SORTED BY xDiff and PRINTED

for(int i = 0; i < CHOSEN; i++){

C = P;

}

// CHILDREN REPRODUCTED TO C

System.out.println("Children Set, iteration: " + t);

for(int i = 0; i < CHOSEN; i++){

System.out.println("xDiff = " + C.xDiff + " x = " + C.x + " r = " + C.r);

}

// CHILDREN SORTED BY xDiff and PRINTED

double r01 = r1 * Math.random();

double r02 = r2 * Math.random();

double multiplicator = Math.exp(r01) * Math.exp(r02);

for(int i = 0; i < CHOSEN; i++){

C.r = C.r * multiplicator;

C.x = C.x + C.r;

C.xDiff = Math.abs(A - function.evaluate(C.x));

}

// CHILDREN MUTATED ? ? ? <SOMETHIG WRONG HERE>

for(int i = 0; i < N; i++){

temp = P;

}

for(int i = N; i < (N+CHOSEN); i++){

temp = C[i - N];

}

// CHILDREN AND PARENTS TOGETHER IN Temp[]

Arrays.sort(temp);

System.out.println("Mutated Children + Parents Set, iteration: " + t);

for(int i = 0; i < N+CHOSEN; i++){

System.out.println("xDiff = " + temp.xDiff + " x = " + temp.x + " r = " + temp.r);

}

for(int i = 0; i < N; i++){

P = temp;

}

// NEW PARENTS CREATED FROM BEST OF C AND P

t ++;

}while(Math.abs(A - function.evaluate(P[0].x)) > EPS);

}

}

Additional Class Fx was created to compute value of the F(x) for given x. Calculates the integral by trapezoid method.

/////////////////////////////////////Fx

package LibEvolution;

public class Fx { //using trapezoid method evaluates F(x)

public double e = 0.0001,c1,c2,a = 0,b = 5;

public int i,n,z;

public double xp,xk,h;

public double evaluate(double testX){

a = 0;

b = testX;

n = (int) Math.ceil((b-a)/Math.sqrt(Math.E));

i = 1;

c1 = 0;

c2 = 0;

b = testX;

do

{

c1 = c2;

c2 = this.calkaTrap(a,b,n);

n *= 2;

i++;

}

while (Math.abs(c1-c2) > e);

// System.out.println("Ilosc iteracji: " +i);

// System.out.println("Wartosc calki wynosi metoda trapezow: " + c2);

return c2;

}

public double calkaTrap(double a, double b, int n){

int i = 0;

double c = 0, xp, xk, h;

h = (b-a)/n;

xk = a;

for (i=1; i<=n; i++) {

xp=xk;

xk=xp+h;

c+=(h/2)*(f(xp)+f(xk));

}

return c;

}

private double f(double x){

double param = 1/Math.sqrt(2*Math.PI);

return param*Math.exp(0.5*x*x);

}

public Fx() {

}

}

[5945 byte] By [dstannna] at [2007-10-3 10:08:29]
# 1
Please repost you code using the [code] tags to make it readable. Also, how does 'crashes the system' present?
sabre150a at 2007-7-15 5:28:10 > top of Java-index,Java Essentials,Java Programming...
# 2

I don't know about the entrie program, it's very hard to read without code tags, however this line is probably not right

for(int i = 0; i < N; i++){

P = new Chromosom();

}

I believe that youi don't want to do this, since P refers to an array of Chromosom, not one Chromosom. Do this instead

for(int i = 0; i < N; i++){

P[i] = new Chromosom();

}

All the other for loops seem to suffer from this same problem.

SE*

SomeoneElsea at 2007-7-15 5:28:10 > top of Java-index,Java Essentials,Java Programming...
# 3
> All the other for loops seem to suffer from this same> problem.> The forum software does this by treating [i] as an italic request!
sabre150a at 2007-7-15 5:28:10 > top of Java-index,Java Essentials,Java Programming...
# 4

It looks like the program enters something like infinite loop. Only the first iteration is printed on the screen then program 'thinks' but does not do anything. Also the entire system slows down.

//////////////////////////////////Chromosom

package LibEvolution;

public class Chromosom implements Comparable<Chromosom> {

public double x;

public double r;

public double xDiff;

public static final int MAX_X = 5;

public static final int MAX_R = 5;

public static final double A = 0.53;

public Chromosom() {

this.x = Math.random()*MAX_X;

this.r = Math.random()*MAX_R;

}

public int compareTo(Chromosom c){

if(this.xDiff > c.xDiff) return 1;

if(this.xDiff == c.xDiff) return 0;

if(this.xDiff < c.xDiff) return -1;

return 0;

}

}

////////////////////////////////////////////////////////////////////////Evo

package evo;

import LibEvolution.Chromosom;

import LibEvolution.Fx;

import java.util.Arrays;

public class Evo {

public static final int N = 10;

public static final int CHOSEN = 5;

public static int t = 1;

public static final double A = 0.53;

public static final double EPS = 0.001;

public static void main(String args[]){

Chromosom[] P = new Chromosom[N]; //Parents

Chromosom[] C = new Chromosom[CHOSEN]; //Children

Chromosom[] temp = new Chromosom[N + CHOSEN]; //Parents + Children

Fx function = new Fx();

double r1 = 1/Math.sqrt(2*N);

double r2 = 1/Math.sqrt(2*Math.sqrt(N));

for(int i = 0; i < N; i++){

P[i] = new Chromosom();

}

// PARENTS INITIALIZED

do{

for(int i = 0; i < N; i++){

P[i].xDiff = Math.abs(A - function.evaluate(P[i].x));

}

// PARENTS EVALUATED

Arrays.sort(P);

System.out.println("Parents Set, iteration: " + t);

for(int i = 0; i < N; i++){

System.out.println("xDiff = " + P[i].xDiff + " x = " + P[i].x + " r = " + P[i].r);

}

// PARENTS SORTED BY xDiff and PRINTED

for(int i = 0; i < CHOSEN; i++){

C[i] = P[i];

}

// CHILDREN REPRODUCTED TO C

System.out.println("Children Set, iteration: " + t);

for(int i = 0; i < CHOSEN; i++){

System.out.println("xDiff = " + C[i].xDiff + " x = " + C[i].x + " r = " + C[i].r);

}

// CHILDREN SORTED BY xDiff and PRINTED

double r01 = r1 * Math.random();

double r02 = r2 * Math.random();

double multiplicator = Math.exp(r01) * Math.exp(r02);

for(int i = 0; i < CHOSEN; i++){

C[i].r = C[i].r * multiplicator;

C[i].x = C[i].x + C[i].r;

C[i].xDiff = Math.abs(A - function.evaluate(C[i].x));

}

// CHILDREN MUTATED ? ? ? <SOMETHIG WRONG HERE>

for(int i = 0; i < N; i++){

temp[i] = P[i];

}

for(int i = N; i < (N+CHOSEN); i++){

temp[i] = C[i - N];

}

// CHILDREN AND PARENTS TOGETHER IN Temp[]

Arrays.sort(temp);

System.out.println("Mutated Children + Parents Set, iteration: " + t);

for(int i = 0; i < N+CHOSEN; i++){

System.out.println("xDiff = " + temp[i].xDiff + " x = " + temp[i].x + " r = " + temp[i].r);

}

for(int i = 0; i < N; i++){

P[i] = temp[i];

}

// NEW PARENTS CREATED FROM BEST OF C AND P

t ++;

}while(Math.abs(A - function.evaluate(P[0].x)) > EPS);

}

}

/////////////////////////////////////Fx

package LibEvolution;

public class Fx { //using trapezoid method evaluates F(x)

public double e = 0.0001,c1,c2,a = 0,b = 5;

public int i,n,z;

public double xp,xk,h;

public double evaluate(double testX){

a = 0;

b = testX;

n = (int) Math.ceil((b-a)/Math.sqrt(Math.E));

i = 1;

c1 = 0;

c2 = 0;

b = testX;

do

{

c1 = c2;

c2 = this.calkaTrap(a,b,n);

n *= 2;

i++;

}

while (Math.abs(c1-c2) > e);

// System.out.println("Ilosc iteracji: " +i);

// System.out.println("Wartosc calki wynosi metoda trapezow: " + c2);

return c2;

}

public double calkaTrap(double a, double b, int n){

int i = 0;

double c = 0, xp, xk, h;

h = (b-a)/n;

xk = a;

for (i=1; i<=n; i++) {

xp=xk;

xk=xp+h;

c+=(h/2)*(f(xp)+f(xk));

}

return c;

}

private double f(double x){

double param = 1/Math.sqrt(2*Math.PI);

return param*Math.exp(0.5*x*x);

}

public Fx() {

}

}

Message was edited by:

dstannn

dstannna at 2007-7-15 5:28:10 > top of Java-index,Java Essentials,Java Programming...
# 5
DOH!! I knew that, and forgot!! Another reason for Posters to use the code tags.Anyway, from what I can tell, this may never end, I think the math is wrong somehow. The **** thing just chewed up CPU cycles and RAM until I killed it.
SomeoneElsea at 2007-7-15 5:28:10 > top of Java-index,Java Essentials,Java Programming...