Skip to content

Commit

Permalink
examples/image_matcher - pull elite consumer into another file
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcraven committed Apr 17, 2016
1 parent 622093a commit 6079957
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
45 changes: 45 additions & 0 deletions examples/image_matcher/elite_consumer.go
@@ -0,0 +1,45 @@
package main

import (
ga "github.com/tomcraven/goga"
"os"
_ "image/jpeg"
"image/png"
"image/draw"
"image"
"image/color"
"fmt"
"time"
)

type myEliteConsumer struct {
currentIter int
previousFitness int
}

func (ec *myEliteConsumer) OnElite(g *ga.IGenome) {
bits := (*g).GetBits()
newImage := createImageFromBitset(bits)

// Output elite
outputImageFile, _ := os.Create("elite.png")
png.Encode(outputImageFile, newImage)
outputImageFile.Close()

// Output elite with input image blended over the top
outputImageFileAlphaBlended, _ := os.Create("elite_with_original.png")
draw.DrawMask(newImage, newImage.Bounds(),
inputImage, image.ZP,
&image.Uniform{color.RGBA{0, 0, 0, 255 / 4}}, image.ZP,
draw.Over)
png.Encode(outputImageFileAlphaBlended, newImage)
outputImageFileAlphaBlended.Close()

ec.currentIter++
fitness := (*g).GetFitness()
fmt.Println(ec.currentIter, "\t", fitness, "\t", fitness-ec.previousFitness)

ec.previousFitness = fitness

time.Sleep(10 * time.Millisecond)
}
35 changes: 0 additions & 35 deletions examples/image_matcher/image_matcher.go
Expand Up @@ -3,10 +3,7 @@ package main
import (
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"image/png"
"math/rand"
"os"
"runtime"
Expand Down Expand Up @@ -75,38 +72,6 @@ func (bc *myBitsetCreate) Go() ga.Bitset {
return b
}

type myEliteConsumer struct {
currentIter int
previousFitness int
}

func (ec *myEliteConsumer) OnElite(g *ga.IGenome) {
bits := (*g).GetBits()
newImage := createImageFromBitset(bits)

// Output elite
outputImageFile, _ := os.Create("elite.png")
png.Encode(outputImageFile, newImage)
outputImageFile.Close()

// Output elite with input image blended over the top
outputImageFileAlphaBlended, _ := os.Create("elite_with_original.png")
draw.DrawMask(newImage, newImage.Bounds(),
inputImage, image.ZP,
&image.Uniform{color.RGBA{0, 0, 0, 255 / 4}}, image.ZP,
draw.Over)
png.Encode(outputImageFileAlphaBlended, newImage)
outputImageFileAlphaBlended.Close()

ec.currentIter++
fitness := (*g).GetFitness()
fmt.Println(ec.currentIter, "\t", fitness, "\t", fitness-ec.previousFitness)

ec.previousFitness = fitness

time.Sleep(10 * time.Millisecond)
}

var (
largestShapeBits int
totalBitsPerGenome int
Expand Down

0 comments on commit 6079957

Please sign in to comment.