Simple Genetic algorithm

I am trying to write a simple genetic algorithm that automatically generates the string "Hello_World!" I used the code snipit from an earlier post to randomly generate a 12 character string using upper/lower case, exclamation points, and underscores.

Here's the code i have so far:

import java.util.*;

public class test {

private final static char[] chars;

private final static Random random;

static { //

chars = new char[52];

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

chars = (char) (97 + i);

chars[i + 26] = (char) (65 + i);

chars[i + 1] = (char) (95);

chars[i + 2] = (char) (33);

}

random = new Random ();

}

private static String randomString (int length) {

char[] array = new char[length];

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

array = chars[random.nextInt (chars.length)];

}

return new String (array);

}

public static void main (String[] parameters) {

for (int i = 1; i < 10; i ++) {

System.out.println (randomString (12));

}

}

}

Now I need to write the fitness test. The way I plan on going about this is to get the ASCII values of the individual characters in the random string and compare them to the value of the characters in "Hello_World!" however I cannot figure out how to get those values from the random string. Any suggests are greatly appreciated.

[1459 byte] By [psike9a] at [2007-9-23]
# 1

first of all, dont write the ascii values as numbers. e.g. use 'A' instead of 65.

now im not sure what you really want. do you simply want to compare each character and remeber the proper ones?

just iterate over the String.length and compare String.charAt(i) of both strings. (or use String.toCharArray())

TheosTheona at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 2
That's not exacltly what I want the program to do. Basically i'm looking for it to generate a population of random strings and then choose the most fit ones and display the one(s) that are closest to "Hello_World!"
psike9a at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 3

well you can implement various mehtods that return a number representing the quality of the random string compared to a ceratin attribute and in total just add those numbers. the one with the highest score wins...

e.g. double comparePosition(String random, String desired), which returns 1if all numbers fit, and 0 if none does.

TheosTheona at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 4
I think you could treat the chars of the String as numbers, and calculate the differences between the random string's chars and the target string's.
nasch_a at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 5

This is what I would do treat the word you create as a number each character can have different numbers such as 1,2,3 etc. Then each position will be given a value which is contrastingly different from any other position something like 2^0,2^1,2^3 (in reverse order so that it ensures that at least the first few letters are right) etc.

And then compare it against 揾ello_world?for hello world it would be something like starting with the letter d,l,r ...

2^0*4 + 2^1*12+2^3*18+?/b> etc. Its just an idea you might have to test it out.

WirajRa at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 6
give me method how to learn easy
si66hia at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 7

Generate population of Random chromosomes

Evaluate population for fitness

Apply whatever selection criteria you want to get a "matable" population

Apply Crossover (and a little Mutation, if you want) to get your next population

Repeat until done (max iterations reached or fit enough chromosome found)

If you have questions, aske Google.

Good Luck

Lee

tsitha at 2007-7-11 > top of java,Java Essentials,Java Programming...
# 8
Try this: http://silyeek-tech.blogspot.com/2006/03/genetic-algorithm-in-solving-vehicle.html
ahyeeka at 2007-7-11 > top of java,Java Essentials,Java Programming...