refine optimizer executor config structure

This commit is contained in:
c9s 2022-06-21 12:31:42 +08:00
parent 16682596df
commit 9b82de596b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 39 additions and 7 deletions

View File

@ -3,7 +3,11 @@
# go run ./cmd/bbgo optimize --config bollmaker_ethusdt.yaml --optimizer-config optimizer.yaml --debug # go run ./cmd/bbgo optimize --config bollmaker_ethusdt.yaml --optimizer-config optimizer.yaml --debug
# #
--- ---
maxThread: 10 executor:
type: local
local:
maxNumberOfProcesses: 10
matrix: matrix:
- type: iterate - type: iterate
label: interval label: interval

View File

@ -80,6 +80,7 @@ var optimizeCmd = &cobra.Command{
} }
executor := &optimizer.LocalProcessExecutor{ executor := &optimizer.LocalProcessExecutor{
Config: optConfig.Executor.LocalExecutorConfig,
Bin: os.Args[0], Bin: os.Args[0],
WorkDir: ".", WorkDir: ".",
ConfigDir: configDir, ConfigDir: configDir,

View File

@ -18,11 +18,30 @@ type SelectorConfig struct {
Step fixedpoint.Value `json:"step,omitempty" yaml:"step,omitempty"` Step fixedpoint.Value `json:"step,omitempty" yaml:"step,omitempty"`
} }
type LocalExecutorConfig struct {
MaxNumberOfProcesses int `json:"maxNumberOfProcesses" yaml:"maxNumberOfProcesses"`
}
type ExecutorConfig struct {
Type string `json:"type" yaml:"type"`
LocalExecutorConfig *LocalExecutorConfig `json:"local" yaml:"local"`
}
type Config struct { type Config struct {
Executor *ExecutorConfig `json:"executor" yaml:"executor"`
MaxThread int `yaml:"maxThread,omitempty"` MaxThread int `yaml:"maxThread,omitempty"`
Matrix []SelectorConfig `yaml:"matrix"` Matrix []SelectorConfig `yaml:"matrix"`
} }
var defaultExecutorConfig = &ExecutorConfig{
Type: "local",
LocalExecutorConfig: defaultLocalExecutorConfig,
}
var defaultLocalExecutorConfig = &LocalExecutorConfig{
MaxNumberOfProcesses: 10,
}
func LoadConfig(yamlConfigFileName string) (*Config, error) { func LoadConfig(yamlConfigFileName string) (*Config, error) {
configYaml, err := ioutil.ReadFile(yamlConfigFileName) configYaml, err := ioutil.ReadFile(yamlConfigFileName)
if err != nil { if err != nil {
@ -34,9 +53,16 @@ func LoadConfig(yamlConfigFileName string) (*Config, error) {
return nil, err return nil, err
} }
// The default of MaxThread is 5 if optConfig.Executor == nil {
if optConfig.MaxThread <= 0 { optConfig.Executor = defaultExecutorConfig
optConfig.MaxThread = 5 }
if optConfig.Executor.Type == "" {
optConfig.Executor.Type = "local"
}
if optConfig.Executor.Type == "local" && optConfig.Executor.LocalExecutorConfig == nil {
optConfig.Executor.LocalExecutorConfig = defaultLocalExecutorConfig
} }
return &optConfig, nil return &optConfig, nil

View File

@ -199,7 +199,7 @@ func (o *GridOptimizer) Run(executor Executor, configJson []byte) (map[string][]
} }
} }
ctx := context.WithValue(context.Background(), "MaxThread", o.Config.MaxThread) ctx := context.Background()
resultsC, err := executor.Run(ctx, taskC) resultsC, err := executor.Run(ctx, taskC)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -36,6 +36,7 @@ type AsyncHandle struct {
} }
type LocalProcessExecutor struct { type LocalProcessExecutor struct {
Config *LocalExecutorConfig
Bin string Bin string
WorkDir string WorkDir string
ConfigDir string ConfigDir string
@ -73,7 +74,7 @@ func (e *LocalProcessExecutor) readReport(output []byte) (*backtest.SummaryRepor
} }
func (e *LocalProcessExecutor) Run(ctx context.Context, taskC chan BacktestTask) (chan BacktestTask, error) { func (e *LocalProcessExecutor) Run(ctx context.Context, taskC chan BacktestTask) (chan BacktestTask, error) {
var maxNumOfProcess = ctx.Value("MaxThread").(int) var maxNumOfProcess = e.Config.MaxNumberOfProcesses
var resultsC = make(chan BacktestTask, maxNumOfProcess*2) var resultsC = make(chan BacktestTask, maxNumOfProcess*2)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}