hoptimizer: manually early stop

User is now able to stop trials by sending system signal (SIGINT & SIGTERM) and see the report earlier at any time.
This commit is contained in:
Raphanus Lo 2022-08-16 12:57:30 +08:00
parent 2e9f554f9e
commit b4e32a9ba7
2 changed files with 17 additions and 6 deletions

View File

@ -6,10 +6,13 @@ import (
"fmt"
"github.com/c9s/bbgo/pkg/optimizer"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
)
@ -90,7 +93,14 @@ var hoptimizeCmd = &cobra.Command{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_ = ctx
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
log.Info("Early stop by manual cancelation.")
cancel()
}()
if len(optSessionName) == 0 {
optSessionName = fmt.Sprintf("bbgo-hpopt-%v", time.Now().UnixMilli())
@ -118,7 +128,8 @@ var hoptimizeCmd = &cobra.Command{
return err
}
report, err := optz.Run(executor, configJson)
report, err := optz.Run(ctx, executor, configJson)
log.Info("All test trial finished.")
if err != nil {
return err
}

View File

@ -229,7 +229,7 @@ func (o *HyperparameterOptimizer) buildObjective(executor Executor, configJson [
}
}
func (o *HyperparameterOptimizer) Run(executor Executor, configJson []byte) (*HyperparameterOptimizeReport, error) {
func (o *HyperparameterOptimizer) Run(ctx context.Context, executor Executor, configJson []byte) (*HyperparameterOptimizeReport, error) {
labelPaths, paramDomains := o.buildParamDomains()
objective := o.buildObjective(executor, configJson, paramDomains)
@ -268,8 +268,8 @@ func (o *HyperparameterOptimizer) Run(executor Executor, configJson []byte) (*Hy
if err != nil {
return nil, err
}
eg, ctx := errgroup.WithContext(context.Background())
study.WithContext(ctx)
eg, studyCtx := errgroup.WithContext(ctx)
study.WithContext(studyCtx)
for i := 0; i < numOfProcesses; i++ {
processEvaluations := maxEvaluationPerProcess
if processEvaluations > maxEvaluation {
@ -280,7 +280,7 @@ func (o *HyperparameterOptimizer) Run(executor Executor, configJson []byte) (*Hy
})
maxEvaluation -= processEvaluations
}
if err := eg.Wait(); err != nil {
if err := eg.Wait(); err != nil && ctx.Err() != context.Canceled {
return nil, err
}
close(trialFinishChan)