Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EnqueueTrial: lock trial and support NON_ZERO_DATE mode #92

Merged
merged 7 commits into from Mar 14, 2020

Conversation

c-bata
Copy link
Owner

@c-bata c-bata commented Mar 14, 2020

  • Order trials by trial_id
  • SetTrialState
    • Lock trial by FOR UPDATE
    • Return an error when the state is not changed.
  • Fix an error on MySQL NON_ZERO_DATE mode.

@c-bata c-bata changed the title Lock trial record while updating state Fix EnqueueTrial: lock trial and support NON_ZERO_DATE mode Mar 14, 2020
@c-bata
Copy link
Owner Author

c-bata commented Mar 14, 2020

check scripts

package main

import (
	"context"
	"flag"
	"log"
	"math"

	"github.com/c-bata/goptuna"
	"github.com/c-bata/goptuna/rdb"
	"github.com/c-bata/goptuna/tpe"
	"github.com/jinzhu/gorm"
	"golang.org/x/sync/errgroup"

	_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

func objective(trial goptuna.Trial) (float64, error) {
	x1, _ := trial.SuggestUniform("x1", -10, 10)
	x2, _ := trial.SuggestUniform("x2", -10, 10)
	return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil
}

func main() {
	flag.Parse()
	if len(flag.Args()) == 0 {
		log.Fatal("please pass dialect and dsn")
	}
	dialect := flag.Arg(0)
	dsn := flag.Arg(1)

	db, err := gorm.Open(dialect, dsn)
	if err != nil {
		log.Fatal("failed to open db:", err)
	}

	rdb.RunAutoMigrate(db)
	storage := rdb.NewStorage(db)
	defer db.Close()

	study, err := goptuna.CreateStudy(
		"rdb",
		goptuna.StudyOptionStorage(storage),
		goptuna.StudyOptionSampler(tpe.NewSampler()),
		goptuna.StudyOptionSetDirection(goptuna.StudyDirectionMinimize),
		goptuna.StudyOptionLoadIfExists(true),
	)
	if err != nil {
		log.Fatal("failed to create study", err)
	}

	for i := 0; i < 10; i++ {
		err = study.EnqueueTrial(map[string]float64{
			"x1": float64(i),
		})
		if err != nil {
			log.Fatal("failed to enqueue trial: ", err)
		}
	}

	eg, ctx := errgroup.WithContext(context.Background())
	study.WithContext(ctx)
	for i := 0; i < 10; i++ {
		eg.Go(func() error {
			return study.Optimize(objective, 5)
		})
	}
	if err := eg.Wait(); err != nil {
		log.Fatal("optimize error: ", err)
	}

	v, err := study.GetBestValue()
	if err != nil {
		log.Fatal("failed to get best value", err)
	}
	params, err := study.GetBestParams()
	if err != nil {
		log.Fatal("failed to get best params:", err)
	}
	log.Printf("Best evaluation=%f (x1=%f, x2=%f)",
		v, params["x1"].(float64), params["x2"].(float64))
}
#!/bin/sh

export GO111MODULE=on
DIR=$(cd $(dirname $0); pwd)
REPOSITORY_ROOT=$(cd $(dirname $(dirname $(dirname $0))); pwd)

##################################################################
echo ""
echo "1. Prepare MYSQL 8.0 Server using Docker."
echo ""

docker pull mysql:8.0
docker stop goptuna-mysql

set -e

cd ${DIR}
docker run \
  -d \
  --rm \
  -p 3306:3306 \
  --mount type=volume,src=mysql,dst=/etc/mysql/conf.d \
  -e MYSQL_USER=goptuna \
  -e MYSQL_DATABASE=goptuna \
  -e MYSQL_PASSWORD=password \
  -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
  --name goptuna-mysql \
  mysql:8.0
cd -

echo "Wait ready for mysql"
sleep 20

##################################################################
echo ""
echo "2. Run Goptuna optimizations."
echo ""

go run ${DIR}/main.go mysql "goptuna:password@tcp(localhost:3306)/goptuna?parseTime=true"

##################################################################
echo ""
echo "3. View the optimization results on Optuna's dashboard."
echo ""

if [ -d ./venv ]; then
    source venv/bin/activate
    pip install mysqlclient
else
    python3.7 -m venv venv
    source venv/bin/activate
    pip install optuna bokeh mysqlclient
fi

optuna dashboard --storage mysql+mysqldb://goptuna:password@127.0.0.1:3306/goptuna --study rdb

##################################################################
echo ""
echo "4. Stop MYSQL Server"
echo ""

docker stop goptuna-mysql

Results

(venv) $ _examples/simple_rdb/check_mysql.sh 

1. Prepare MYSQL 8.0 Server using Docker.

8.0: Pulling from library/mysql
Digest: sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2c
Status: Image is up to date for mysql:8.0
docker.io/library/mysql:8.0
Error response from daemon: No such container: goptuna-mysql
518a220798a8e45bb7d5cac71cdf39ce97a59e97f1d7c45b062bf780e665d553
/Users/a14737/go/src/github.com/c-bata/goptuna
Wait ready for mysql

2. Run Goptuna optimizations.

2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=0
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=1
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=2
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=3
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=4
2020/03/14 23:04:52 [INFO] Trial finished: trialID=1 state=Complete evaluation=197.319074
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=5
2020/03/14 23:04:52 [INFO] Trial finished: trialID=2 state=Complete evaluation=1.010140
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=6
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=7
2020/03/14 23:04:52 [INFO] Trial finished: trialID=3 state=Complete evaluation=65.920196
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=8
2020/03/14 23:04:52 [DEBUG] trial is popped from the trial queue.: number=9
2020/03/14 23:04:52 [INFO] Trial finished: trialID=4 state=Complete evaluation=16.312533
2020/03/14 23:04:52 [INFO] Trial finished: trialID=5 state=Complete evaluation=9.530700
2020/03/14 23:04:52 [INFO] Trial finished: trialID=6 state=Complete evaluation=9.623482
2020/03/14 23:04:52 [INFO] Trial finished: trialID=7 state=Complete evaluation=17.325325
2020/03/14 23:04:52 [INFO] Trial finished: trialID=8 state=Complete evaluation=90.717661
2020/03/14 23:04:52 [INFO] Trial finished: trialID=9 state=Complete evaluation=203.531455
2020/03/14 23:04:52 [INFO] Trial finished: trialID=10 state=Complete evaluation=219.357234
2020/03/14 23:04:52 [INFO] Trial finished: trialID=11 state=Complete evaluation=84.481632
2020/03/14 23:04:52 [INFO] Trial finished: trialID=12 state=Complete evaluation=39.563250
2020/03/14 23:04:52 [INFO] Trial finished: trialID=13 state=Complete evaluation=40.890297
2020/03/14 23:04:52 [INFO] Trial finished: trialID=14 state=Complete evaluation=51.778572
2020/03/14 23:04:52 [INFO] Trial finished: trialID=15 state=Complete evaluation=90.378376
2020/03/14 23:04:52 [INFO] Trial finished: trialID=16 state=Complete evaluation=334.332183
2020/03/14 23:04:52 [INFO] Trial finished: trialID=17 state=Complete evaluation=219.518025
2020/03/14 23:04:52 [INFO] Trial finished: trialID=18 state=Complete evaluation=123.555193
2020/03/14 23:04:52 [INFO] Trial finished: trialID=19 state=Complete evaluation=140.738876
2020/03/14 23:04:53 [INFO] Trial finished: trialID=20 state=Complete evaluation=129.619599
2020/03/14 23:04:53 [INFO] Trial finished: trialID=21 state=Complete evaluation=88.410179
2020/03/14 23:04:53 [INFO] Trial finished: trialID=22 state=Complete evaluation=48.504022
2020/03/14 23:04:53 [INFO] Trial finished: trialID=23 state=Complete evaluation=59.914409
2020/03/14 23:04:53 [INFO] Trial finished: trialID=24 state=Complete evaluation=23.948242
2020/03/14 23:04:53 [INFO] Trial finished: trialID=25 state=Complete evaluation=31.228501
2020/03/14 23:04:53 [INFO] Trial finished: trialID=26 state=Complete evaluation=9.817042
2020/03/14 23:04:53 [INFO] Trial finished: trialID=27 state=Complete evaluation=81.965859
2020/03/14 23:04:53 [INFO] Trial finished: trialID=28 state=Complete evaluation=31.320050
2020/03/14 23:04:53 [INFO] Trial finished: trialID=29 state=Complete evaluation=32.049420
2020/03/14 23:04:53 [INFO] Trial finished: trialID=30 state=Complete evaluation=10.340905
2020/03/14 23:04:53 [INFO] Trial finished: trialID=31 state=Complete evaluation=32.421720
2020/03/14 23:04:53 [INFO] Trial finished: trialID=32 state=Complete evaluation=2.461753
2020/03/14 23:04:53 [INFO] Trial finished: trialID=33 state=Complete evaluation=5.154269
2020/03/14 23:04:53 [INFO] Trial finished: trialID=34 state=Complete evaluation=21.231636
2020/03/14 23:04:53 [INFO] Trial finished: trialID=35 state=Complete evaluation=15.082964
2020/03/14 23:04:53 [INFO] Trial finished: trialID=36 state=Complete evaluation=12.300077
2020/03/14 23:04:53 [INFO] Trial finished: trialID=37 state=Complete evaluation=9.043378
2020/03/14 23:04:53 [INFO] Trial finished: trialID=38 state=Complete evaluation=1.711879
2020/03/14 23:04:53 [INFO] Trial finished: trialID=39 state=Complete evaluation=11.332864
2020/03/14 23:04:54 [INFO] Trial finished: trialID=40 state=Complete evaluation=52.605273
2020/03/14 23:04:54 [INFO] Trial finished: trialID=41 state=Complete evaluation=2.702924
2020/03/14 23:04:54 [INFO] Trial finished: trialID=42 state=Complete evaluation=55.047167
2020/03/14 23:04:54 [INFO] Trial finished: trialID=43 state=Complete evaluation=5.519918
2020/03/14 23:04:54 [INFO] Trial finished: trialID=44 state=Complete evaluation=24.198580
2020/03/14 23:04:54 [INFO] Trial finished: trialID=45 state=Complete evaluation=0.732611
2020/03/14 23:04:54 [INFO] Trial finished: trialID=46 state=Complete evaluation=1.090600
2020/03/14 23:04:54 [INFO] Trial finished: trialID=47 state=Complete evaluation=43.223336
2020/03/14 23:04:54 [INFO] Trial finished: trialID=48 state=Complete evaluation=38.155978
2020/03/14 23:04:54 [INFO] Trial finished: trialID=49 state=Complete evaluation=35.895223
2020/03/14 23:04:54 [INFO] Trial finished: trialID=50 state=Complete evaluation=19.599987
2020/03/14 23:04:54 Best evaluation=0.732611 (x1=2.703971, x2=-5.486864)

3. View the optimization results on Optuna's dashboard.

Requirement already satisfied: mysqlclient in ./venv/lib/python3.8/site-packages (1.4.6)
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[W 2020-03-14 23:04:57,178] Optuna dashboard is still highly experimental. Please use with caution!
[I 2020-03-14 23:04:57,187] Starting Bokeh server version 2.0.0 (running on Tornado 6.0.4)
[I 2020-03-14 23:04:57,188] User authentication hooks NOT provided (default user enabled)
[I 2020-03-14 23:04:57,190] Bokeh app running at: http://localhost:5006/dashboard
[I 2020-03-14 23:04:57,190] Starting Bokeh server with process id: 46715
[I 2020-03-14 23:04:57,530] 200 GET /dashboard (::1) 15.85ms
[I 2020-03-14 23:04:58,308] 101 GET /dashboard/ws (::1) 0.71ms
[I 2020-03-14 23:04:58,308] WebSocket connection opened
[I 2020-03-14 23:04:58,309] ServerConnection created
^C
Interrupted, shutting down

4. Stop MYSQL Server

goptuna-mysql

@c-bata c-bata added the bug Something isn't working label Mar 14, 2020
@c-bata c-bata merged commit e277383 into master Mar 14, 2020
@c-bata c-bata deleted the robust-enqueue-trial branch March 14, 2020 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant