Thread safety for status updates, periodically minify
This commit is contained in:
@@ -48,7 +48,7 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
|
||||
for {
|
||||
status.Attempts++
|
||||
|
||||
Mutate(prog)
|
||||
Mutate(def, prog)
|
||||
|
||||
score, err := def.score(prog)
|
||||
if err != nil {
|
||||
@@ -58,7 +58,7 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
|
||||
|
||||
if score > status.BestScore {
|
||||
status.BestScore = score
|
||||
status.BestProgram = prog
|
||||
status.BestProgram = prog.Copy()
|
||||
|
||||
if statusChan != nil {
|
||||
statusChan <- status
|
||||
@@ -73,6 +73,7 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
|
||||
|
||||
return nil, err
|
||||
}
|
||||
status.BestProgram = prog.Copy()
|
||||
|
||||
if statusChan != nil {
|
||||
statusChan <- status
|
||||
@@ -142,6 +143,7 @@ func (def *Definition) minifyFunction(prog *vm.Program, f int) error {
|
||||
|
||||
for i := 0; i < len(prog.Functions[f].Instructions); i++ {
|
||||
origInstructions := prog.Functions[f].Instructions
|
||||
|
||||
tmp := make([]*vm.Instruction, len(prog.Functions[f].Instructions))
|
||||
copy(tmp, prog.Functions[f].Instructions)
|
||||
prog.Functions[f].Instructions = append(tmp[:i], tmp[i+1:]...)
|
||||
|
||||
@@ -8,8 +8,14 @@ import "github.com/firestuff/subcoding/vm"
|
||||
const instructionsPerFunctionMean = 15
|
||||
const instrucitonsPerFunctionStdDev = 10
|
||||
|
||||
func Mutate(prog *vm.Program) {
|
||||
target := int(rand.NormFloat64()*instrucitonsPerFunctionStdDev+instructionsPerFunctionMean)
|
||||
const minifyPeriodMean = 10000
|
||||
|
||||
func Mutate(def *Definition, prog *vm.Program) {
|
||||
if rand.Intn(minifyPeriodMean) == 0 {
|
||||
def.minifyProgram(prog) // ignore error
|
||||
}
|
||||
|
||||
target := int(rand.NormFloat64()*instrucitonsPerFunctionStdDev + instructionsPerFunctionMean)
|
||||
|
||||
if len(prog.Functions[0].Instructions) < target {
|
||||
addInstruction(prog, prog.Functions[0])
|
||||
|
||||
Reference in New Issue
Block a user