📄 fitness.java
字号:
/************************************************************** * Main Class: Genetic.java * Author: Mark Rich, richm@cs.wisc.edu * Date: 3/30/2000 *************************************************************//** * The Fitness class is used to keep track of a GOAL String * as well as perform most repeated operations in a Genetic * Algorithm. */class Fitness { public final String GOAL; // the word we are searching for /** * Constructor sets up the object by initialiizing GOAL to str * @param str the new GOAL String */ public Fitness(String str) { GOAL = str.toUpperCase(); } /** * To create a random String as long as the GOAL, we use a * StringBuffer object and append on chars. * @return a new random String */ public String getRandomString() { StringBuffer temp = new StringBuffer(""); for (int i = 0; i < GOAL.length(); i++) { temp.append(getRandomChar()); } return temp.toString(); } /** * We use this method to find a random capital character * between A and Z. * @return a random capital char */ public char getRandomChar() { return (char)((Math.random() * 26) + 'A'); } /** * The score method will return the score of a String based * on the distance from the GOAL string. Each character is * compared individually and the score is the result. * @param str a String to be scored * @return the distance of str from the GOAL */ public int score(String str) { int score = 0; for (int i = 0; i < GOAL.length(); i++) { score += Math.abs( (int)(GOAL.charAt(i) - str.charAt(i))); } return score; } /** * Finding the Best Index in an array of Strings is determined * by their score. The array is searched by each element. If * ith element is every greater than the best found so far, make * i the best. * @return the index of the best scoring member of pop */ public int findBestIndex(String[] pop) { int bestIndex = 0; for (int i = 1; i < pop.length; i++) { if (score(pop[i]) < score(pop[bestIndex])) { bestIndex = i; } } return bestIndex; } /** * Selection between two Strings for reproduction is based on * the score of the Strings. Here, the lower score is chosen, * since that will be closer to the GOAL String. * @param parent1 the first parent * @param parent2 the second parent */ public String select(String parent1, String parent2) { if (score(parent1) <= score(parent2)) { return parent1; } else { return parent2; } } /** * To mutate a StringBuffer object, we perform point-mutation. * Select a char at random from the StringBuffer, and replace * it with a new char. * @param mutant the StringBuffer to be mutated */ public void mutate(StringBuffer mutant) { int mutpoint = (int)(Math.random() * GOAL.length()); mutant.setCharAt(mutpoint, getRandomChar()); } /** * Crossover will swap the contents of two StringBuffers. First we * pick a point to make the switch. We need a temporary String * for swapping, then use the replace methods to switch * @param child1 the first child * @param child2 the second child */ public void crossover(StringBuffer child1, StringBuffer child2) { int crossover = (int)(Math.random() * GOAL.length()); String swapchild = child1.toString(); child1.replace(0, crossover, child2.substring(0, crossover)); child2.replace(0, crossover, swapchild.substring(0, crossover)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -