Status channel in Grow()
This commit is contained in:
37
grow/grow.go
37
grow/grow.go
@@ -1,44 +1,47 @@
|
||||
package grow
|
||||
|
||||
import "log"
|
||||
|
||||
import "github.com/firestuff/subcoding/asm"
|
||||
import "github.com/firestuff/subcoding/gen"
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
func (def *Definition) Grow() (*vm.Program, error) {
|
||||
high_score := 0
|
||||
func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
|
||||
status := Status{
|
||||
TargetScore: uint64(len(def.Samples)),
|
||||
}
|
||||
|
||||
log.Print("Starting grow run...")
|
||||
if statusChan != nil {
|
||||
statusChan <- status
|
||||
}
|
||||
|
||||
// TODO: Score should be number of output criteria, not number of Samples
|
||||
for progs := 0; ; progs++ {
|
||||
for {
|
||||
status.Attempts++
|
||||
|
||||
prog := gen.RandProgram(def.GlobalMemorySize, def.FunctionMemorySize, def.InstructionLimit)
|
||||
|
||||
score, err := def.Score(prog)
|
||||
if err != nil {
|
||||
close(statusChan)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if score > high_score {
|
||||
high_score = score
|
||||
if score > status.BestScore {
|
||||
status.BestScore = score
|
||||
status.BestProgram = prog
|
||||
|
||||
src, err := asm.Disassemble(prog)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if statusChan != nil {
|
||||
statusChan <- status
|
||||
}
|
||||
|
||||
log.Printf("New high score %d / %d (after %d attempts):\n%s", high_score, len(def.Samples), progs, src)
|
||||
|
||||
if score == len(def.Samples) {
|
||||
if status.BestScore == status.TargetScore {
|
||||
close(statusChan)
|
||||
return prog, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (def *Definition) Score(prog *vm.Program) (int, error) {
|
||||
score := 0
|
||||
func (def *Definition) Score(prog *vm.Program) (uint64, error) {
|
||||
score := uint64(0)
|
||||
|
||||
for _, sample := range def.Samples {
|
||||
state, err := vm.NewState(prog)
|
||||
|
||||
Reference in New Issue
Block a user