Class PolygonChromosome
- All Implemented Interfaces:
GASolution<PolygonChromosome>
Encoding
Each chromosome also carries a fixed opaque background color. Each triangle
occupies 10 consecutive doubles in a flat
double[] array:
index 0 1 2 3 4 5 6 7 8 9
x1 y1 x2 y2 x3 y3 r g b a
All values are normalized to [0, 1]. Position values are mapped to
pixel coordinates by multiplying by the target image dimensions at render
time, so the same chromosome renders correctly at any resolution.
Rendering
Two render paths are provided:
renderInto(BufferedImage)— renders into a caller-supplied (reusable) image. Used by fitness evaluation to avoid per-call allocation.render(int, int)— allocates a fresh image and renders into it. Used by the view for display-quality output.
Thread safety
Instances are not thread-safe. Each simulation thread and each display thread
should operate on separate instances. copy() produces a fully
independent deep copy.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intNumber ofdoublevalues encoding a single triangle: three (x, y) vertex pairs plus (r, g, b, a) color components. -
Constructor Summary
ConstructorsConstructorDescriptionPolygonChromosome(int numTriangles) Construct a zero-initialized chromosome for the given number of triangles. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Create an independent copy of this solution.double[]Return a defensive copy of the raw gene array.intReturn the number of triangles encoded by this chromosome.intlength()Return the number of encoded elements in this solution.render(int width, int height) Allocate a newBufferedImageof the given dimensions, render this chromosome into it, and return it.voidrenderInto(BufferedImage img) voidrenderInto(BufferedImage img, double maxSpanFraction) Render this chromosome into an existingBufferedImage.
-
Field Details
-
DOUBLES_PER_TRIANGLE
public static final int DOUBLES_PER_TRIANGLENumber ofdoublevalues encoding a single triangle: three (x, y) vertex pairs plus (r, g, b, a) color components.- See Also:
-
-
Constructor Details
-
PolygonChromosome
public PolygonChromosome(int numTriangles) Construct a zero-initialized chromosome for the given number of triangles. All gene values are0.0; useImageApproximationProblem.randomIndividual(Random)to produce a randomly initialized instance.- Parameters:
numTriangles- number of triangles (must be > 0)- Throws:
IllegalArgumentException- ifnumTrianglesis not positive
-
-
Method Details
-
copy
Create an independent copy of this solution.Returns an independent deep copy of this chromosome. The copy shares no state with the original and is safe to mutate independently.
- Specified by:
copyin interfaceGASolution<PolygonChromosome>- Returns:
- a deep copy whose runtime type matches this solution
-
length
public int length()Return the number of encoded elements in this solution.Returns the total number of gene values:
numTriangles ×DOUBLES_PER_TRIANGLE.- Specified by:
lengthin interfaceGASolution<PolygonChromosome>- Returns:
- solution length
-
renderInto
Render this chromosome into an existingBufferedImage.This method is the preferred path for fitness evaluation: the caller supplies a reusable image (e.g. from a
ThreadLocal) so no allocation occurs per call. The image is completely overwritten — no blending with previous content occurs.The background is filled with opaque black before any triangles are painted.
AlphaComposite.SRCis used for the background fill so that any alpha channel left from a previous render is fully overwritten. Triangles are painted withAlphaComposite.SRC_OVER(the default), allowing semi-transparent layering.- Parameters:
img- the image to render into (non-null); its dimensions determine the pixel-space coordinates used for all trianglesmaxSpanFraction- maximum allowed span of any triangle as a fraction of the image dimensions (0.0 to 1.0); if > 0,- Throws:
NullPointerException- ifimgis null
-
renderInto
-
render
Allocate a newBufferedImageof the given dimensions, render this chromosome into it, and return it.This method is intended for display-quality rendering by the view. It allocates a fresh image on every call and should not be called from
fitness()— userenderInto(BufferedImage)with a reusable buffer instead.- Parameters:
width- image width in pixels (must be > 0)height- image height in pixels (must be > 0)- Returns:
- a newly allocated
BufferedImageof typeBufferedImage.TYPE_INT_ARGBcontaining the rendered triangles - Throws:
IllegalArgumentException- ifwidthorheightis not positive
-
getNumTriangles
public int getNumTriangles()Return the number of triangles encoded by this chromosome.- Returns:
- number of triangles
-
getGenesCopy
public double[] getGenesCopy()Return a defensive copy of the raw gene array.Prefer direct field access (
genes[i]) within the package for performance-sensitive paths such as crossover and mutation.- Returns:
- copy of the gene array
-