Minify program after hitting score
This commit is contained in:
@@ -65,6 +65,19 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
|
||||
}
|
||||
|
||||
if status.BestScore == status.TargetScore {
|
||||
err = def.minifyProgram(prog)
|
||||
if err != nil {
|
||||
if statusChan != nil {
|
||||
close(statusChan)
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if statusChan != nil {
|
||||
statusChan <- status
|
||||
}
|
||||
|
||||
if statusChan != nil {
|
||||
close(statusChan)
|
||||
}
|
||||
@@ -106,3 +119,42 @@ func (def *Definition) sumOuts() uint64 {
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func (def *Definition) minifyProgram(prog *vm.Program) error {
|
||||
for f := 0; f < len(prog.Functions); f++ {
|
||||
err := def.minifyFunction(prog, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (def *Definition) minifyFunction(prog *vm.Program, f int) error {
|
||||
baseScore, err := def.score(prog)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for loop := true; loop; {
|
||||
loop = false
|
||||
|
||||
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:]...)
|
||||
|
||||
newScore, err := def.score(prog)
|
||||
if err == nil && newScore >= baseScore {
|
||||
loop = true
|
||||
break
|
||||
} else {
|
||||
prog.Functions[f].Instructions = origInstructions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user