Start exercising grow code, breaking the VM
This commit is contained in:
@@ -10,12 +10,7 @@ type Definition struct {
|
||||
Samples []*Sample `yaml:"samples"`
|
||||
}
|
||||
|
||||
type Sample struct {
|
||||
GlobalMemoryInputs map[uint64]uint64 `yaml:"global_memory_inputs"`
|
||||
GlobalMemoryOutputs map[uint64]uint64 `yaml:"global_memory_outputs"`
|
||||
}
|
||||
|
||||
func loadDefinition(r io.Reader) (*Definition, error) {
|
||||
func NewDefinition(r io.Reader) (*Definition, error) {
|
||||
dec := yaml.NewDecoder(r)
|
||||
dec.SetStrict(true)
|
||||
|
||||
|
||||
64
grow/grow.go
Normal file
64
grow/grow.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
import "github.com/firestuff/subcoding/asm"
|
||||
import "github.com/firestuff/subcoding/gen"
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
func (def *Definition) Grow() {
|
||||
high_score := 0
|
||||
|
||||
log.Print("Starting grow run...")
|
||||
|
||||
// TODO: Score should be number of output criteria, not number of Samples
|
||||
for high_score < len(def.Samples) {
|
||||
prog := gen.RandProgram(def.GlobalMemorySize, def.FunctionMemorySize)
|
||||
|
||||
{
|
||||
src, err := asm.Disassemble(prog)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Print("Prog:\n%s", src)
|
||||
}
|
||||
|
||||
score, err := def.Score(prog)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if score > high_score {
|
||||
high_score = score
|
||||
|
||||
src, err := asm.Disassemble(prog)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Print("New high score %d / %d:\n%s", high_score, len(def.Samples), src)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (def *Definition) Score(prog *vm.Program) (int, error) {
|
||||
score := 0
|
||||
|
||||
for _, sample := range def.Samples {
|
||||
state, err := vm.NewState(prog)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
sample.SetInputs(state)
|
||||
|
||||
state.Execute() // ignore error
|
||||
|
||||
if sample.OutputsMatch(state) {
|
||||
score += 1
|
||||
}
|
||||
}
|
||||
|
||||
return score, nil
|
||||
}
|
||||
@@ -17,10 +17,10 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
def, err := loadDefinition(defFile)
|
||||
def, err := NewDefinition(defFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Printf("%+v", def)
|
||||
def.Grow()
|
||||
}
|
||||
|
||||
24
grow/sample.go
Normal file
24
grow/sample.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
type Sample struct {
|
||||
GlobalMemoryInputs map[uint64]uint64 `yaml:"global_memory_inputs"`
|
||||
GlobalMemoryOutputs map[uint64]uint64 `yaml:"global_memory_outputs"`
|
||||
}
|
||||
|
||||
func (s *Sample) SetInputs(state *vm.State) {
|
||||
for i, val := range s.GlobalMemoryInputs {
|
||||
state.GlobalMemory().WriteUnsigned(i, val)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sample) OutputsMatch(state *vm.State) bool {
|
||||
for i, val := range s.GlobalMemoryOutputs {
|
||||
if state.GlobalMemory().MustReadUnsigned(i) != val {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user