Skip to content

Commit 856c46c

Browse files
committed
Add maxpercentage (mp) parameter as methos to stop drawing.
1 parent 4aea3f4 commit 856c46c

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
Mode int
2929
Workers int
3030
Max float64
31+
Maxpct float64
3132
rfs float64
3233
Nth int
3334
Repeat int
@@ -71,11 +72,12 @@ func init() {
7172
flag.Var(&Configs, "n", "number of primitives")
7273
flag.StringVar(&Background, "bg", "", "background color (hex)")
7374
flag.IntVar(&Alpha, "a", 128, "alpha value")
74-
flag.IntVar(&InputSize, "r", 256, "resize large input images to this size")
75+
flag.IntVar(&InputSize, "r", 256, "resize large input images to this size only if larger than specified")
7576
flag.IntVar(&OutputSize, "s", 1024, "output image size")
7677
flag.IntVar(&Mode, "m", 1, "0=combo 1=triangle 2=rect 3=ellipse 4=circle 5=rotatedrect 6=beziers 7=rotatedellipse 8=polygon")
7778
flag.IntVar(&Workers, "j", 0, "number of parallel workers (default uses all cores)")
7879
flag.Float64Var(&Max, "ma", 0, "target score to stop adding primitives (default 0)")
80+
flag.Float64Var(&Maxpct, "mp", 0, "target score in % to stop adding primitives (default 100)")
7981
flag.IntVar(&Nth, "nth", 1, "save every Nth frame (put \"%d\" in path)")
8082
flag.IntVar(&Repeat, "rep", 0, "add N extra shapes per iteration with reduced search")
8183
flag.BoolVar(&V, "v", false, "verbose")
@@ -158,9 +160,13 @@ func main() {
158160
}
159161

160162
// run algorithm
163+
var startScore float64
164+
161165
model := primitive.NewModel(input, bg, OutputSize, Workers)
162166
primitive.Log(1, "%d: t=%.3f, score=%.6f\n", 0, 0.0, model.Score)
163167

168+
startScore = model.Score
169+
164170
start := time.Now()
165171
frame := 0
166172
for j, config := range Configs {
@@ -176,7 +182,10 @@ func main() {
176182
nps := primitive.NumberString(float64(n) / time.Since(t).Seconds())
177183
elapsed := time.Since(start).Seconds()
178184

179-
primitive.Log(1, "%d: t=%.3f, score=%.6f, n=%d, n/s=%s\n", frame, elapsed, model.Score, n, nps)
185+
var pctScore float64
186+
pctScore = 100 - (model.Score / startScore * 100)
187+
188+
primitive.Log(1, "%d: t=%.3f, score=%.6f, n=%d, n/s=%s, percent score=%.3f%%\n", frame, elapsed, model.Score, n, nps, pctScore)
180189

181190
// write output image(s)
182191
for _, output := range Outputs {
@@ -185,7 +194,7 @@ func main() {
185194
saveFrames := percent && ext != ".gif"
186195
saveFrames = saveFrames && frame%Nth == 0
187196
last := j == len(Configs)-1 && i == config.Count-1
188-
if saveFrames || last || model.Score <= Max {
197+
if saveFrames || last || model.Score <= Max || pctScore >= Maxpct {
189198
path := output
190199
if percent {
191200
path = fmt.Sprintf(output, frame)
@@ -204,7 +213,7 @@ func main() {
204213
frames := model.Frames(0.001)
205214
check(primitive.SaveGIFImageMagick(path, frames, 50, 250))
206215
}
207-
if model.Score <= Max {
216+
if model.Score <= Max || pctScore >= Maxpct {
208217
return
209218
}
210219
}

primitive/model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ func (model *Model) Add(shape Shape, alpha int) {
119119
func (model *Model) Step(shapeType ShapeType, alpha, repeat int) int {
120120
//state := model.runWorkers(shapeType, alpha, 1000, 100, 16)
121121
state := model.runWorkers(shapeType, alpha, 500, 50, 8)
122-
state = HillClimb(state, 50).(*State)
122+
//state := model.runWorkers(shapeType, alpha, 250, 25, 4)
123+
//state := model.runWorkers(shapeType, alpha, 100, 200, 32)
124+
//state = HillClimb(state, 20).(*State)
123125
model.Add(state.Shape, state.Alpha)
124126

125127
for i := 0; i < repeat; i++ {
126128
state.Worker.Init(model.Current, model.Score)
127129
a := state.Energy()
128-
state = HillClimb(state, 50).(*State)
130+
state = HillClimb(state, 100).(*State)
129131
//state = HillClimb(state, 100).(*State)
130132
b := state.Energy()
131133
if a == b {

primitive/optimize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func HillClimb(state Annealable, maxAge int) Annealable {
2323
if energy >= bestEnergy {
2424
state.UndoMove(undo)
2525
} else {
26-
// fmt.Printf("step: %d, energy: %.6f\n", step, energy)
26+
//fmt.Printf("step: %d, energy: %.6f\n", step, energy)
2727
bestEnergy = energy
2828
bestState = state.Copy()
2929
age = -1

primitive/quadratic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func (q *Quadratic) Mutate() {
6767
q.X3 = clamp(q.X3+rnd.NormFloat64()*16, -m, float64(w-1+m))
6868
q.Y3 = clamp(q.Y3+rnd.NormFloat64()*16, -m, float64(h-1+m))
6969
case 3:
70-
q.Width = clamp(q.Width+rnd.NormFloat64(), 1, 16)
70+
q.Width = clamp(q.Width+rnd.NormFloat64(), 0.5, 128)
71+
//q.Width = clamp(rnd.NormFloat64(), 0.5, 64)
7172
}
7273
if q.Valid() {
7374
break

0 commit comments

Comments
 (0)