Files
gl01/internal/work/work.go
2025-12-19 21:41:58 +08:00

145 lines
2.5 KiB
Go

package work
import (
"math"
"slices"
"time"
"edgaru089.ink/go/gl01/internal/igwrap"
"edgaru089.ink/go/gl01/internal/io"
"github.com/inkyblackness/imgui-go/v4"
)
var state struct {
inst [2000]*WorkerInstance
buf []*WorkerInstance
steps int
first_step int
iter_disabled bool
}
type iter_info struct {
first_step, last_step int
step_count int
max_score float64
factor float64
}
var stats []iter_info
func Init() {
for i := range state.inst {
inst := RandomInstance()
state.inst[i] = &inst
}
}
func Update(delta time.Duration) {
state.steps++
for i := range state.inst {
state.inst[i].Step(state.steps)
}
slices.SortFunc(state.inst[:], func(x, y *WorkerInstance) int {
if x.score > y.score {
return -1
} else if x.score == y.score && x.pos.Sub(x.target).Len() < y.pos.Sub(y.target).Len() {
return -1
} else {
return 1
}
})
if imgui.Checkbox("Disable Iteration", &state.iter_disabled) {
}
if !state.iter_disabled && (imgui.Button("Iterate") || state.inst[50].score > 0) {
factor := 0.0
for _, f := range io.StepFactors {
factor += f
}
factor /= float64(len(io.StepFactors))
io.StepFactors = io.StepFactors[0:0]
// push stats
stats = append(stats, iter_info{
first_step: state.first_step,
last_step: state.steps,
step_count: state.steps - state.first_step,
max_score: state.inst[0].score,
factor: factor,
})
state.first_step = state.steps
state.buf = state.buf[:0]
for i := range 50 {
state.inst[i].score = 0
state.buf = append(state.buf, state.inst[i])
for range 2000/50 - 1 {
inst := state.inst[i].Offspring()
inst.step0 = state.steps
state.buf = append(state.buf, &inst)
}
}
copy(state.inst[:], state.buf)
}
for i := range 20 {
igwrap.Text("Score: %f\n", state.inst[i].score)
}
if igwrap.Begin("Graph", nil, 0) {
imgui.PushItemWidth(-math.SmallestNonzeroFloat32)
s := make([]float32, len(stats))
if len(stats) < 1 {
goto err
}
for i, st := range stats {
s[i] = float32(st.step_count)
}
if len(s) > 400 {
s = s[len(s)-400:]
}
imgui.PlotLinesV(
"##Step Count",
s,
0,
"",
math.MaxFloat32,
math.MaxFloat32,
imgui.Vec2{0, 200},
)
s = make([]float32, len(stats))
for i, st := range stats {
s[i] = float32(st.factor)
}
if len(s) > 400 {
s = s[len(s)-400:]
}
imgui.PlotLinesV(
"##Factor",
s,
0,
"",
math.MaxFloat32,
math.MaxFloat32,
imgui.Vec2{0, 200},
)
err:
imgui.PopItemWidth()
imgui.End()
}
}