List of usage examples for org.apache.commons.math.util ResizableDoubleArray ResizableDoubleArray
public ResizableDoubleArray(ResizableDoubleArray original)
From source file:es.udc.gii.common.eaf.algorithm.population.Individual.java
/** * Sets a double array chromosome to the current individual at position <i>index</i>. * @param index position to set the new chromosome. * @param chromosome a chromosme to be set at position <i>index</i>. *//* ww w . jav a 2 s . co m*/ public void setChromosomeAt(int index, double[] chromosome) { if (this.chromosomes == null) { this.chromosomes = new DoubleArray[1]; } this.chromosomes[index] = new ResizableDoubleArray(chromosome.length); for (int i = 0; i < chromosome.length; i++) { this.chromosomes[index].setElement(i, chromosome[i]); } }
From source file:es.udc.gii.common.eaf.factory.XMLSimpleFactory.java
@Override public Individual createIndividual(FitnessComparator<Individual> comparator) { Configuration conf = EAFConfiguration.getInstance().subset("Population.Individual"); try {//from w w w . ja v a 2s . com Individual ind = (Individual) Class.forName(conf.getString("Class")).newInstance(); List chromosomes = conf.getList("Chromosome[@size]"); if (chromosomes.isEmpty()) { throw new ConfigurationException("No chromosome specified."); } DoubleArray[] chrms = new DoubleArray[chromosomes.size()]; for (int i = 0; i < chromosomes.size(); i++) { int chrom_size = conf.getInt("Chromosome(" + i + ")[@size]"); chrms[i] = new ResizableDoubleArray(chrom_size); for (int j = 0; j < chrom_size; j++) { chrms[i].addElement(0.0); } } ind.setChromosomes(chrms); ind.setComparator(comparator); ind.configure(conf); // ind.generate(); return ind; } catch (Exception ex) { throw new ConfigurationException("Wrong individual configuration for " + conf.getString("Class") + " ?" + " \n Thrown exception: \n" + ex); } }
From source file:es.udc.gii.common.eaf.algorithm.population.Individual.java
/** * Generates the chromosomes of the Individual with values in [-1.0,1.0] *///from ww w . j av a 2 s. com public void generate() { if (this.chromosomes == null) { this.chromosomes = new DoubleArray[1]; this.chromosomes[0] = new ResizableDoubleArray(this.getDimension()); } for (int i = 0; i < this.chromosomes.length; i++) { for (int j = 0; j < this.chromosomes[i].getNumElements(); j++) { this.chromosomes[i].setElement(j, EAFRandom.nextDouble() * 2.0 - 1.0); } } }
From source file:es.udc.gii.common.eaf.algorithm.population.Individual.java
/** * Clones the current individual.//from w w w . j a va2 s . com * @return a new individual which is a copy of the current one. */ @Override public Object clone() { Individual clone = null; DoubleArray[] clone_chromosomes = null; try { clone = (Individual) super.clone(); } catch (CloneNotSupportedException e) { assert false; } //Clono los cromosomas: if (this.chromosomes != null) { clone_chromosomes = new DoubleArray[this.chromosomes.length]; for (int i = 0; i < this.chromosomes.length; i++) { clone_chromosomes[i] = new ResizableDoubleArray(this.chromosomes[i].getNumElements()); for (int j = 0; j < this.chromosomes[i].getNumElements(); j++) { clone_chromosomes[i].setElement(j, this.chromosomes[i].getElement(j)); } } } clone.setChromosomes(clone_chromosomes); clone.setFitness(this.fitness); clone.setSerializeGenotype(serializeGenotype); clone.setSerializeEvalResults(serializeEvalResults); clone.setViolatedConstraints(violatedConstraints); /* Clone objective values */ List<Double> obj = null; if (objectives != null) { obj = new ArrayList<Double>(objectives.size()); for (Double d : objectives) { obj.add(new Double(d.doubleValue())); } } clone.setObjectives(obj); /* Clone constraint values */ List<Double> contr = null; if (constraints != null) { contr = new ArrayList<Double>(constraints.size()); for (Double d : constraints) { contr.add(new Double(d.doubleValue())); } } clone.setConstraints(contr); /* Copy comparator. */ clone.setComparator(getComparator()); return clone; }
From source file:es.udc.gii.common.eaf.algorithm.population.Individual.java
/** * This method is called whenever an instance of this class has to be * de-serialized.<p>/*from w w w.j a v a 2 s .co m*/ * * It sets the values of Individual#getSerializeEvalResults and * Individual#getSerializeGenotype accordingly to the information received * so that subclasses can rely on them to know what kind of information is * to be read.<p> * * Subclasses should override this method if they introduce new attibutes. * Remember to call <code>super.readExternal()</code> in order to * be sure that the state of the parent class is de-serialized and the values * of Individual#getSerializeEvalResults and Individual#getSerializeGenotype * contain the right information. * * @param in - DataInput from which the bytes are read. * @throws java.io.IOException * @throws java.lang.ClassNotFoundException */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { /* Retrieve the type of information comming in */ boolean evals = in.readBoolean(); boolean genotype = in.readBoolean(); this.serializeEvalResults = evals; this.serializeGenotype = genotype; /* Read the evaluation results, if present */ if (evals) { /* Read number of violated constraints */ this.violatedConstraints = in.readInt(); /* Read the fitness value */ this.fitness = in.readDouble(); /* Read the number of objectives (-1 means no objective values) */ int nObjs = in.readInt(); if (nObjs == -1) { this.objectives = null; } else { /* Read all objective values */ this.objectives = new ArrayList<Double>(nObjs); for (int i = 0; i < nObjs; i++) { this.objectives.add(in.readDouble()); } } /* Read the number of constraints (-1 means no constraint values) */ int nConstr = in.readInt(); if (nConstr == -1) { this.constraints = null; } else { /* Read all constraint values */ this.constraints = new ArrayList<Double>(nConstr); for (int i = 0; i < nConstr; i++) { this.constraints.add(in.readDouble()); } } } /* Read the genotype information, if present */ if (genotype) { int nChroms = in.readInt(); if (nChroms == -1) { this.chromosomes = null; } else { /* Read all the chromosomes */ this.chromosomes = new DoubleArray[nChroms]; for (int i = 0; i < nChroms; i++) { int nGenes = in.readInt(); this.chromosomes[i] = new ResizableDoubleArray(nGenes); for (int j = 0; j < nGenes; j++) { this.chromosomes[i].addElement(in.readDouble()); } } } } }