public Organism doVariation(Generation parents, Generation offspring, Selection sel) { GAParameters params = GAParameters.getParams(); Random rand = params.getRandom(); // pick a parent randomly StructureOrg p = (StructureOrg) (sel.doSelection(parents, 1)[0]); Cell pStruct = p.getCell(); // copy the parent's data into new structures List<Site> pSites = pStruct.getSites(); List<Site> newSites = new LinkedList<Site>(); // perturbing the lattice parameters double[][] strain = new double[3][3]; // initialize the strain matrix to the identity // then add some zero mean Gaussian random variables for (int i = 0; i < strain.length; i++) for (int j = 0; j < strain.length; j++) { strain[i][j] = rand.nextGaussian() * sigmaLattice; // allow strain matrix perturbations to be only between -1 and 1 if (strain[i][j] > 1) strain[i][j] = 1; if (strain[i][j] < -1) strain[i][j] = -1; // add the identity if (i == j) strain[i][j] += 1; } // modify the basis to make newBasis double[][] newVectsD = new double[Constants.numDimensions][Constants.numDimensions]; for (int i = 0; i < Constants.numDimensions; i++) for (int j = 0; j < Constants.numDimensions; j++) newVectsD[i][j] = pStruct.getLatticeVectors().get(i).getCartesianComponents().get(j); // newVects = MatrixMath.SquareMatrixMult(strain, newVects); newVectsD = (new Matrix(strain)).times(new Matrix(newVectsD)).getArray(); List<Vect> newVects = new LinkedList<Vect>(); for (int i = 0; i < Constants.numDimensions; i++) newVects.add(new Vect(newVectsD[i])); // perturb atomic positions for (int i = 0; i < pSites.size(); i++) { // perturb each site's location with probability mutRate List<Double> fracCoords = pSites.get(i).getCoords().getComponentsWRTBasis(pStruct.getLatticeVectors()); if (rand.nextDouble() < mutRate) { // perturb by a Gaussian of stddev mutRadius along each axis for (int k = 0; k < Constants.numDimensions; k++) fracCoords.set( k, GAUtils.renormZeroOne(fracCoords.get(k) + rand.nextGaussian() * sigmaAtoms)); } newSites.add(new Site(pSites.get(i).getElement(), new Vect(fracCoords, newVects))); } // make the new offspring StructureOrg result = new StructureOrg(new Cell(newVects, newSites)); GAOut.out().stdout("StructureMut created new StructureOrg:", GAOut.DEBUG, result.getID()); GAOut.out().stdout(result.toString(), GAOut.DEBUG, result.getID()); return result; }
public BufferedImage generateImageFromDna( List<Gene> dna, GAParameters parameters, double multiplier) { // long timestamp = System.currentTimeMillis(); BufferedImage image = new BufferedImage( (int) (parameters.getTargetImage().getWidth() * multiplier), (int) (parameters.getTargetImage().getHeight() * multiplier), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = image.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED); drawBlackBackground(graphics, parameters, multiplier); for (Gene gene : dna) { int[] x = new int[gene.getPoints().size()]; int[] y = new int[gene.getPoints().size()]; for (int i = 0; i < gene.getPoints().size(); i++) { x[i] = (int) (gene.getPoints().get(i).getX() * multiplier); y[i] = (int) (gene.getPoints().get(i).getY() * multiplier); } Polygon p = new Polygon(x, y, gene.getPoints().size()); graphics.setColor(gene.getColor()); graphics.fillPolygon(p); } // System.out.println("rendering took : " + (System.currentTimeMillis() - timestamp)); return image; }
public StructureMut(List<String> args) { if (args == null || args.size() < 3) GAParameters.usage("Not enough parameters given to StructureMut", true); mutRate = Double.parseDouble(args.get(0)); sigmaAtoms = Double.parseDouble(args.get(1)); sigmaLattice = Double.parseDouble(args.get(2)); }
private void drawBlackBackground( Graphics2D graphics, GAParameters parameters, double multiplier) { graphics.setColor(Color.BLACK); graphics.fillRect( 0, 0, (int) (parameters.getTargetImage().getWidth() * multiplier), (int) (parameters.getTargetImage().getHeight() * multiplier)); }
public Boolean converged(Generation currentGen) { return GAParameters.getParams().getRecord().getGenNum() > maxNumGens; }
public NumGensCC(List<String> args) { if (args == null || args.size() < 1) GAParameters.usage("Not enough parameters given to NumGensCC", true); maxNumGens = Integer.parseInt(args.get(0)); }