CÓDIGO FUENTE CAMBIO MÍNIMO
Enviado por JHONCARBAJAL • 10 de Diciembre de 2013 • Tesis • 1.923 Palabras (8 Páginas) • 412 Visitas
ANEXO I: CÓDIGO FUENTE CAMBIO MÍNIMO
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licensing information please see the file license.txt included with JGAP
* or have a look at the top of class org.jgap.Chromosome which representatively
* includes the JGAP license policy applicable for any file delivered with JGAP.
*/
package ejemplos;
import java.io.File;
import org.jgap.Chromosome;
import org.jgap.Configuration;
import org.jgap.FitnessFunction;
import org.jgap.Gene;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.data.DataTreeBuilder;
import org.jgap.data.IDataCreators;
import org.jgap.impl.DefaultConfiguration;
import org.jgap.impl.IntegerGene;
import org.jgap.xml.XMLDocumentBuilder;
import org.jgap.xml.XMLManager;
import org.w3c.dom.Document;
/**
* En este ejemplo se muestra como resolver un problema clasico de algoritmos
* genéticos utilizando el framework JGAP. El problema consiste en lograr juntar
* el monto de dinero ingresado a la aplicacion por parametro con la menor
* cantidad de monedas posibles. Para resolver el problema nos basamos en la
* moneda de la Republica Argentina. Moneda de 1 Peso ( equivale a 100 centavos
* ) Moneda de 50 Centavos Moneda de 25 Centavos Moneda de 10 Centavos Moneda de
* 5 Centavos Moneda de 1 Centavo
*
* @author Gabriel Veloso
* @author Ruben Arce
* @since 1.0
*/
public class CambioMinimo {
/**
* The total number of times we'll let the population evolve.
*/
private static final int MAX_EVOLUCIONES_PERMITIDAS = 2200;
/**
* Calcula utilizando algoritmos geneticos la solución al problema y la
* imprime por pantalla
*
* @param Monto
* Monto que se desea descomponer en la menor cantidad de monedas
* posibles
* @throws Exception
*
* @author Gabriel Veloso
* @author Ruben Arce
* @since 1.0
*/
public static void calcularCambioMinimo(int Monto)
throws Exception {
// Se crea una configuracion con valores predeterminados.
// -------------------------------------------------------------
Configuration conf = new DefaultConfiguration();
// Se indica en la configuracion que el elemento mas apto siempre pase a
// la proxima generacion
// -------------------------------------------------------------
conf.setPreservFittestIndividual(true);
// Se Crea la funcion de aptitud y se setea en la configuracion
// ---------------------------------------------------------
FitnessFunction myFunc = new CambioMinimoFuncionAptitud(Monto);
conf.setFitnessFunction(myFunc);
// Ahora se debe indicar a la configuracion como seran los cromosoma: en
// este caso tendran 5 genes (uno para cada tipo de moneda) con un valor
// entero (candiad de monedas de ese tipo).
// Se debe crear un cromosoma de ejemplo y cargarlo en la configuracion
// Cada gen tendra un valor maximo y minimo que debe setearse.
// --------------------------------------------------------------
Gene[] sampleGenes = new Gene[6];
sampleGenes[0] = new IntegerGene(conf, 0, Math.round(CambioMinimoFuncionAptitud.MAX_MONTO/100)); // Moneda 1 peso
sampleGenes[1] = new IntegerGene(conf, 0, 10); // Moneda 50 centavos
sampleGenes[2] = new IntegerGene(conf, 0, 10); // Moneda 25 centavos
sampleGenes[3] = new IntegerGene(conf, 0, 10); // Moneda 10 centavos
sampleGenes[4] = new IntegerGene(conf, 0, 10); // Moneda 5 centavos
sampleGenes[5] = new IntegerGene(conf, 0, 10); // Moneda 1 centavo
IChromosome sampleChromosome = new Chromosome(conf, sampleGenes);
conf.setSampleChromosome(sampleChromosome);
// Por ultimo se debe indicar
...