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
|
||||
function_memory_size: %d
|
||||
instruction_limit: %d
|
||||
functions:
|
||||
%s`,
|
||||
prog.GlobalMemorySize,
|
||||
prog.FunctionMemorySize,
|
||||
prog.InstructionLimit,
|
||||
strings.Join(fncs, ""),
|
||||
), nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import "gopkg.in/yaml.v2"
|
||||
type program struct {
|
||||
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
||||
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
||||
InstructionLimit uint64 `yaml:"instruction_limit"`
|
||||
Functions []function `yaml:"functions"`
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,11 @@ package gen
|
||||
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
func RandProgram(globalMemorySize, functionMemorySize uint64) *vm.Program {
|
||||
func RandProgram(globalMemorySize, functionMemorySize, instructionLimit uint64) *vm.Program {
|
||||
prog := &vm.Program{
|
||||
GlobalMemorySize: globalMemorySize,
|
||||
FunctionMemorySize: functionMemorySize,
|
||||
InstructionLimit: instructionLimit,
|
||||
}
|
||||
|
||||
prog.Functions = append(prog.Functions, &vm.Function{
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
global_memory_size: 1
|
||||
function_memory_size: 1
|
||||
instruction_limit: 2
|
||||
samples:
|
||||
- global_memory_inputs:
|
||||
0: 0
|
||||
global_memory_outputs:
|
||||
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 {
|
||||
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
||||
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
||||
InstructionLimit uint64 `yaml:"instruction_limit"`
|
||||
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
|
||||
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)
|
||||
}
|
||||
prog := gen.RandProgram(def.GlobalMemorySize, def.FunctionMemorySize, def.InstructionLimit)
|
||||
|
||||
score, err := def.Score(prog)
|
||||
if err != nil {
|
||||
@@ -37,7 +28,7 @@ func (def *Definition) Grow() {
|
||||
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) {
|
||||
src := `global_memory_size: 4
|
||||
function_memory_size: 4
|
||||
instruction_limit: 0
|
||||
functions:
|
||||
- - [nop]
|
||||
- [mov, g0, 1]
|
||||
|
||||
@@ -7,7 +7,7 @@ import "github.com/firestuff/subcoding/asm"
|
||||
|
||||
func TestRandProgram(t *testing.T) {
|
||||
for i := 0; i < 100; i++ {
|
||||
prog := gen.RandProgram(4, 4)
|
||||
prog := gen.RandProgram(4, 4, 0)
|
||||
|
||||
src, err := asm.Disassemble(prog)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,5 +3,6 @@ package vm
|
||||
type Program struct {
|
||||
GlobalMemorySize uint64
|
||||
FunctionMemorySize uint64
|
||||
InstructionLimit uint64
|
||||
Functions []*Function
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ type State struct {
|
||||
comparisonResult bool
|
||||
globalMemory *Memory
|
||||
stack []*stackFrame
|
||||
|
||||
instructionCount uint64
|
||||
}
|
||||
|
||||
func NewState(prog *Program) (*State, error) {
|
||||
@@ -70,6 +72,12 @@ func (state *State) processInstruction() {
|
||||
instr := fnc.Instructions[state.instructionIndex]
|
||||
state.instructionIndex += 1
|
||||
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)) {
|
||||
state.ret()
|
||||
|
||||
Reference in New Issue
Block a user