⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geneticalgorithm.java

📁 VHDL制作的ann的code
💻 JAVA
字号:
/* * Encog Neural Network and Bot Library for Java v1.x * http://www.heatonresearch.com/encog/ * http://code.google.com/p/encog-java/ *  * Copyright 2008, Heaton Research Inc., and individual contributors. * See the copyright.txt in the distribution for a full listing of  * individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */package org.encog.solve.genetic;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.TimeUnit;/** * GeneticAlgorithm: Implements a genetic algorithm. This is an abstract class. * Other classes are provided in this book that use this base class to train * neural networks or provide an answer to the traveling salesman problem. *  * The genetic algorithm is also capable of using a thread pool to speed * execution. */abstract public class GeneticAlgorithm<GENE_TYPE> {	/**	 * Threadpool timeout.	 */	public static final int TIMEOUT = 120;		/**	 * How many chromosomes should be created.	 */	private int populationSize;	/**	 * The percent that should mutate.	 */	private double mutationPercent;	/**	 * What percent should be chosen to mate. They will choose partners from the	 * entire mating population.	 */	private double percentToMate;	/**	 * Percent of the population that the mating population chooses partners.	 * from.	 */	private double matingPopulation;	/**	 * Should the same gene be prevented from repeating.	 */	private boolean preventRepeat;	/**	 * How much genetic material should be cut when mating.	 */	private int cutLength;	/**	 * An optional thread pool to use.	 */	private ExecutorService pool;	/**	 * The population.	 */	private Chromosome<GENE_TYPE>[] chromosomes;	/**	 * Get a specific chromosome.	 * 	 * @param i	 *            The chromosome to return, 0 for the first one.	 * @return A chromosome.	 */	public Chromosome<GENE_TYPE> getChromosome(final int i) {		return this.chromosomes[i];	}	/**	 * Return the entire population.	 * 	 * @return the chromosomes	 */	public Chromosome<GENE_TYPE>[] getChromosomes() {		return this.chromosomes;	}	/**	 * Get the cut length.	 * 	 * @return The cut length.	 */	public int getCutLength() {		return this.cutLength;	}	/**	 * Get the mating population.	 * 	 * @return The mating population percent.	 */	public double getMatingPopulation() {		return this.matingPopulation;	}	/**	 * Get the mutation percent.	 * 	 * @return The mutation percent.	 */	public double getMutationPercent() {		return this.mutationPercent;	}	/**	 * Get the percent to mate.	 * 	 * @return The percent to mate.	 */	public double getPercentToMate() {		return this.percentToMate;	}	/**	 * Get the optional threadpool.	 * 	 * @return the pool	 */	public ExecutorService getPool() {		return this.pool;	}	/**	 * Get the population size.	 * 	 * @return The population size.	 */	public int getPopulationSize() {		return this.populationSize;	}	/**	 * Should repeating genes be prevented.	 * 	 * @return True if repeating genes should be prevented.	 */	public boolean isPreventRepeat() {		return this.preventRepeat;	}	/**	 * Modify the weight matrix and thresholds based on the last call to	 * calcError.	 * 	 * @throws NeuralNetworkException	 */	public void iteration() {		final int countToMate = (int) (getPopulationSize() 				* getPercentToMate());		final int offspringCount = countToMate * 2;		int offspringIndex = getPopulationSize() - offspringCount;		final int matingPopulationSize = (int) (getPopulationSize() 				* getMatingPopulation());		final Collection<Callable<Integer>> tasks 		  = new ArrayList<Callable<Integer>>();		// mate and form the next generation		for (int i = 0; i < countToMate; i++) {			final Chromosome<GENE_TYPE> mother = this.chromosomes[i];			final int fatherInt = (int) (Math.random() * matingPopulationSize);			final Chromosome<GENE_TYPE> father = this.chromosomes[fatherInt];			final Chromosome<GENE_TYPE> child1 = 				this.chromosomes[offspringIndex];			final Chromosome<GENE_TYPE> child2 = 				this.chromosomes[offspringIndex + 1];			final MateWorker<GENE_TYPE> worker 			  = new MateWorker<GENE_TYPE>(					mother, father, child1, child2);			try {				if (this.pool != null) {					tasks.add(worker);				} else {					worker.call();				}			} catch (final Exception e) {				e.printStackTrace();			}						offspringIndex += 2;		}		if (this.pool != null) {			try {				this.pool.invokeAll(tasks, TIMEOUT, TimeUnit.SECONDS);			} catch (final InterruptedException e) {				e.printStackTrace();			}		}		// sort the next generation		sortChromosomes();	}	/**	 * Set the specified chromosome.	 * 	 * @param i	 *            The chromosome to set.	 * @param value	 *            The value for the specified chromosome.	 */	public void setChromosome(final int i, 			final Chromosome<GENE_TYPE> value) {		this.chromosomes[i] = value;	}	/**	 * Set the entire population.	 * 	 * @param chromosomes	 *            the chromosomes to set	 */	public void setChromosomes(final 			Chromosome<GENE_TYPE>[] chromosomes) {		this.chromosomes = chromosomes;	}	/**	 * Set the cut length.	 * 	 * @param cutLength	 *            The cut length.	 */	public void setCutLength(final int cutLength) {		this.cutLength = cutLength;	}	/**	 * Set the mating population percent.	 * 	 * @param matingPopulation	 *            The mating population percent.	 */	public void setMatingPopulation(final double matingPopulation) {		this.matingPopulation = matingPopulation;	}	/**	 * Set the mutation percent.	 * 	 * @param mutationPercent The percent to mutate.	 */	public void setMutationPercent(final double mutationPercent) {		this.mutationPercent = mutationPercent;	}	/**	 * Set the percent to mate.	 * 	 * @param percentToMate The percent to mate.	 */	public void setPercentToMate(final double percentToMate) {		this.percentToMate = percentToMate;	}	/**	 * Set the optional thread pool.	 * 	 * @param pool	 *            the pool to set	 */	public void setPool(final ExecutorService pool) {		this.pool = pool;	}	/**	 * Set the population size.	 * 	 * @param populationSize	 *            The population size.	 */	public void setPopulationSize(final int populationSize) {		this.populationSize = populationSize;	}	/**	 * Set the gene.	 * 	 * @param preventRepeat Should repeats be prevented.	 */	public void setPreventRepeat(final boolean preventRepeat) {		this.preventRepeat = preventRepeat;	}	/**	 * Sort the chromosomes.	 */	public void sortChromosomes() {		Arrays.sort(this.chromosomes);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -