Status channel in Grow()
This commit is contained in:
@@ -6,6 +6,7 @@ import "math/rand"
|
|||||||
import "os"
|
import "os"
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
import "github.com/firestuff/subcoding/asm"
|
||||||
import "github.com/firestuff/subcoding/grow"
|
import "github.com/firestuff/subcoding/grow"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -28,8 +29,31 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = def.Grow()
|
statusChan := make(chan grow.Status)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_, err = def.Grow(statusChan)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
status, ok := <-statusChan
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if status.BestProgram == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := asm.Disassemble(status.BestProgram)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("New best score %d / %d (after %d attempts):\n%s", status.BestScore, status.TargetScore, status.Attempts, src)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
grow/grow.go
37
grow/grow.go
@@ -1,44 +1,47 @@
|
|||||||
package grow
|
package grow
|
||||||
|
|
||||||
import "log"
|
|
||||||
|
|
||||||
import "github.com/firestuff/subcoding/asm"
|
|
||||||
import "github.com/firestuff/subcoding/gen"
|
import "github.com/firestuff/subcoding/gen"
|
||||||
import "github.com/firestuff/subcoding/vm"
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
func (def *Definition) Grow() (*vm.Program, error) {
|
func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
|
||||||
high_score := 0
|
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
|
// 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)
|
prog := gen.RandProgram(def.GlobalMemorySize, def.FunctionMemorySize, def.InstructionLimit)
|
||||||
|
|
||||||
score, err := def.Score(prog)
|
score, err := def.Score(prog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
close(statusChan)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if score > high_score {
|
if score > status.BestScore {
|
||||||
high_score = score
|
status.BestScore = score
|
||||||
|
status.BestProgram = prog
|
||||||
|
|
||||||
src, err := asm.Disassemble(prog)
|
if statusChan != nil {
|
||||||
if err != nil {
|
statusChan <- status
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("New high score %d / %d (after %d attempts):\n%s", high_score, len(def.Samples), progs, src)
|
if status.BestScore == status.TargetScore {
|
||||||
|
close(statusChan)
|
||||||
if score == len(def.Samples) {
|
|
||||||
return prog, nil
|
return prog, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (def *Definition) Score(prog *vm.Program) (int, error) {
|
func (def *Definition) Score(prog *vm.Program) (uint64, error) {
|
||||||
score := 0
|
score := uint64(0)
|
||||||
|
|
||||||
for _, sample := range def.Samples {
|
for _, sample := range def.Samples {
|
||||||
state, err := vm.NewState(prog)
|
state, err := vm.NewState(prog)
|
||||||
|
|||||||
12
grow/status.go
Normal file
12
grow/status.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package grow
|
||||||
|
|
||||||
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
TargetScore uint64
|
||||||
|
|
||||||
|
Attempts uint64
|
||||||
|
|
||||||
|
BestScore uint64
|
||||||
|
BestProgram *vm.Program
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user