|
Java example source code file (HelloWorldExample.java)
The HelloWorldExample.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.userguide.genetics; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.genetics.AbstractListChromosome; import org.apache.commons.math3.genetics.Chromosome; import org.apache.commons.math3.genetics.ElitisticListPopulation; import org.apache.commons.math3.genetics.GeneticAlgorithm; import org.apache.commons.math3.genetics.InvalidRepresentationException; import org.apache.commons.math3.genetics.MutationPolicy; import org.apache.commons.math3.genetics.OnePointCrossover; import org.apache.commons.math3.genetics.Population; import org.apache.commons.math3.genetics.StoppingCondition; import org.apache.commons.math3.genetics.TournamentSelection; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; public class HelloWorldExample { public static final int POPULATION_SIZE = 1000; public static final double CROSSOVER_RATE = 0.9; public static final double MUTATION_RATE = 0.03; public static final double ELITISM_RATE = 0.1; public static final int TOURNAMENT_ARITY = 2; public static final String TARGET_STRING = "Hello World!"; public static final int DIMENSION = TARGET_STRING.length(); public static void main(String[] args) { long startTime = System.currentTimeMillis(); // initialize a new genetic algorithm GeneticAlgorithm ga = new GeneticAlgorithm(new OnePointCrossover<Character>(), CROSSOVER_RATE, new RandomCharacterMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY)); // initial population Population initial = getInitialPopulation(); // stopping condition StoppingCondition stoppingCondition = new StoppingCondition() { int generation = 0; // @Override public boolean isSatisfied(Population population) { Chromosome fittestChromosome = population.getFittestChromosome(); if (generation == 1 || generation % 10 == 0) { System.out.println("Generation " + generation + ": " + fittestChromosome.toString()); } generation++; double fitness = fittestChromosome.fitness(); if (Precision.equals(fitness, 0.0, 1e-6)) { return true; } else { return false; } } }; System.out.println("Starting evolution ..."); // run the algorithm Population finalPopulation = ga.evolve(initial, stoppingCondition); // Get the end time for the simulation. long endTime = System.currentTimeMillis(); // best chromosome from the final population Chromosome best = finalPopulation.getFittestChromosome(); System.out.println("Generation " + ga.getGenerationsEvolved() + ": " + best.toString()); System.out.println("Total execution time: " + (endTime - startTime) + "ms"); } private static List<Character> randomRepresentation(int length) { return asList(RandomStringUtils.randomAscii(length)); } private static List<Character> asList(String str) { return Arrays.asList(ArrayUtils.toObject(str.toCharArray())); } private static Population getInitialPopulation() { List<Chromosome> popList = new LinkedList Other Java examples (source code examples)Here is a short list of links related to this Java HelloWorldExample.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.