Class PolygonChromosome

java.lang.Object
edu.cnu.mdi.sim.ga.triimage.PolygonChromosome
All Implemented Interfaces:
GASolution<PolygonChromosome>

public final class PolygonChromosome extends Object implements GASolution<PolygonChromosome>
A fixed-length chromosome encoding a set of semi-transparent triangles used to approximate a target image.

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 Details

    • DOUBLES_PER_TRIANGLE

      public static final int DOUBLES_PER_TRIANGLE
      Number of double values 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 are 0.0; use ImageApproximationProblem.randomIndividual(Random) to produce a randomly initialized instance.
      Parameters:
      numTriangles - number of triangles (must be > 0)
      Throws:
      IllegalArgumentException - if numTriangles is not positive
  • Method Details

    • copy

      public PolygonChromosome 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:
      copy in interface GASolution<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:
      length in interface GASolution<PolygonChromosome>
      Returns:
      solution length
    • renderInto

      public void renderInto(BufferedImage img, double maxSpanFraction)
      Render this chromosome into an existing BufferedImage.

      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.SRC is used for the background fill so that any alpha channel left from a previous render is fully overwritten. Triangles are painted with AlphaComposite.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 triangles
      maxSpanFraction - maximum allowed span of any triangle as a fraction of the image dimensions (0.0 to 1.0); if > 0,
      Throws:
      NullPointerException - if img is null
    • renderInto

      public void renderInto(BufferedImage img)
    • render

      public BufferedImage render(int width, int height)
      Allocate a new BufferedImage of 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() — use renderInto(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 BufferedImage of type BufferedImage.TYPE_INT_ARGB containing the rendered triangles
      Throws:
      IllegalArgumentException - if width or height is 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