First program generation
This commit is contained in:
@@ -23,10 +23,12 @@ func Disassemble(prog *vm.Program) (string, error) {
|
|||||||
|
|
||||||
return fmt.Sprintf(`global_memory_size: %d
|
return fmt.Sprintf(`global_memory_size: %d
|
||||||
function_memory_size: %d
|
function_memory_size: %d
|
||||||
|
instruction_limit: %d
|
||||||
functions:
|
functions:
|
||||||
%s`,
|
%s`,
|
||||||
prog.GlobalMemorySize,
|
prog.GlobalMemorySize,
|
||||||
prog.FunctionMemorySize,
|
prog.FunctionMemorySize,
|
||||||
|
prog.InstructionLimit,
|
||||||
strings.Join(fncs, ""),
|
strings.Join(fncs, ""),
|
||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import "gopkg.in/yaml.v2"
|
|||||||
type program struct {
|
type program struct {
|
||||||
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
||||||
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
||||||
|
InstructionLimit uint64 `yaml:"instruction_limit"`
|
||||||
Functions []function `yaml:"functions"`
|
Functions []function `yaml:"functions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ package gen
|
|||||||
|
|
||||||
import "github.com/firestuff/subcoding/vm"
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
func RandProgram(globalMemorySize, functionMemorySize uint64) *vm.Program {
|
func RandProgram(globalMemorySize, functionMemorySize, instructionLimit uint64) *vm.Program {
|
||||||
prog := &vm.Program{
|
prog := &vm.Program{
|
||||||
GlobalMemorySize: globalMemorySize,
|
GlobalMemorySize: globalMemorySize,
|
||||||
FunctionMemorySize: functionMemorySize,
|
FunctionMemorySize: functionMemorySize,
|
||||||
|
InstructionLimit: instructionLimit,
|
||||||
}
|
}
|
||||||
|
|
||||||
prog.Functions = append(prog.Functions, &vm.Function{
|
prog.Functions = append(prog.Functions, &vm.Function{
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
global_memory_size: 1
|
global_memory_size: 1
|
||||||
function_memory_size: 1
|
function_memory_size: 1
|
||||||
|
instruction_limit: 2
|
||||||
samples:
|
samples:
|
||||||
- global_memory_inputs:
|
- global_memory_inputs:
|
||||||
0: 0
|
0: 0
|
||||||
global_memory_outputs:
|
global_memory_outputs:
|
||||||
0: 1
|
0: 1
|
||||||
|
- global_memory_inputs:
|
||||||
|
0: 1
|
||||||
|
global_memory_outputs:
|
||||||
|
0: 2
|
||||||
|
- global_memory_inputs:
|
||||||
|
0: 100
|
||||||
|
global_memory_outputs:
|
||||||
|
0: 101
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import "gopkg.in/yaml.v2"
|
|||||||
type Definition struct {
|
type Definition struct {
|
||||||
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
||||||
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
||||||
|
InstructionLimit uint64 `yaml:"instruction_limit"`
|
||||||
Samples []*Sample `yaml:"samples"`
|
Samples []*Sample `yaml:"samples"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
grow/grow.go
13
grow/grow.go
@@ -13,16 +13,7 @@ func (def *Definition) Grow() {
|
|||||||
|
|
||||||
// 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 high_score < len(def.Samples) {
|
for high_score < len(def.Samples) {
|
||||||
prog := gen.RandProgram(def.GlobalMemorySize, def.FunctionMemorySize)
|
prog := gen.RandProgram(def.GlobalMemorySize, def.FunctionMemorySize, def.InstructionLimit)
|
||||||
|
|
||||||
{
|
|
||||||
src, err := asm.Disassemble(prog)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Print("Prog:\n%s", src)
|
|
||||||
}
|
|
||||||
|
|
||||||
score, err := def.Score(prog)
|
score, err := def.Score(prog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -37,7 +28,7 @@ func (def *Definition) Grow() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Print("New high score %d / %d:\n%s", high_score, len(def.Samples), src)
|
log.Printf("New high score %d / %d:\n%s", high_score, len(def.Samples), src)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import "github.com/firestuff/subcoding/vm"
|
|||||||
func TestRoundTrip(t *testing.T) {
|
func TestRoundTrip(t *testing.T) {
|
||||||
src := `global_memory_size: 4
|
src := `global_memory_size: 4
|
||||||
function_memory_size: 4
|
function_memory_size: 4
|
||||||
|
instruction_limit: 0
|
||||||
functions:
|
functions:
|
||||||
- - [nop]
|
- - [nop]
|
||||||
- [mov, g0, 1]
|
- [mov, g0, 1]
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import "github.com/firestuff/subcoding/asm"
|
|||||||
|
|
||||||
func TestRandProgram(t *testing.T) {
|
func TestRandProgram(t *testing.T) {
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
prog := gen.RandProgram(4, 4)
|
prog := gen.RandProgram(4, 4, 0)
|
||||||
|
|
||||||
src, err := asm.Disassemble(prog)
|
src, err := asm.Disassemble(prog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ package vm
|
|||||||
type Program struct {
|
type Program struct {
|
||||||
GlobalMemorySize uint64
|
GlobalMemorySize uint64
|
||||||
FunctionMemorySize uint64
|
FunctionMemorySize uint64
|
||||||
|
InstructionLimit uint64
|
||||||
Functions []*Function
|
Functions []*Function
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ type State struct {
|
|||||||
comparisonResult bool
|
comparisonResult bool
|
||||||
globalMemory *Memory
|
globalMemory *Memory
|
||||||
stack []*stackFrame
|
stack []*stackFrame
|
||||||
|
|
||||||
|
instructionCount uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState(prog *Program) (*State, error) {
|
func NewState(prog *Program) (*State, error) {
|
||||||
@@ -70,6 +72,12 @@ func (state *State) processInstruction() {
|
|||||||
instr := fnc.Instructions[state.instructionIndex]
|
instr := fnc.Instructions[state.instructionIndex]
|
||||||
state.instructionIndex += 1
|
state.instructionIndex += 1
|
||||||
instr.opHandler(state, instr)
|
instr.opHandler(state, instr)
|
||||||
|
state.instructionCount += 1
|
||||||
|
|
||||||
|
if state.program.InstructionLimit > 0 && state.instructionCount > state.program.InstructionLimit {
|
||||||
|
state.err = fmt.Errorf("Instruction limit (%d) exceeded", state.program.InstructionLimit)
|
||||||
|
state.running = false
|
||||||
|
}
|
||||||
|
|
||||||
if state.functionIndex < 0 || state.functionIndex >= int64(len(state.program.Functions)) {
|
if state.functionIndex < 0 || state.functionIndex >= int64(len(state.program.Functions)) {
|
||||||
state.ret()
|
state.ret()
|
||||||
|
|||||||
Reference in New Issue
Block a user