Go言語でシミュレーション用のシンプルなフレームワークStageをつくった/Fukuokago Stage

Go言語でシミュレーション用のシンプルなフレームワークStageをつくった/Fukuokago Stage

Fukuoka.go#16 (オンライン開催)
https://fukuokago.connpass.com/event/180414/

Cd3d2cb2dadf5488935fe0ddaea7938a?s=128

monochromegane

July 14, 2020
Tweet

Transcript

  1. ࡾ୐༔հ / Pepabo R&D Institute, GMO Pepabo, Inc. 2020.07.14 Fukuoka.go#16

    GoݴޠͰγϛϡϨʔγϣϯ༻ͷ γϯϓϧͳϑϨʔϜϫʔΫ StageΛͭͬͨ͘
  2. 1SJODJQBMFOHJOFFS :VTVLF.*:",&!NPOPDISPNFHBOF 1FQBCP3%*OTUJUVUF (.01FQBCP *OD IUUQTCMPHNPOPDISPNFHBOFDPN

  3. 1. ͸͡Ίʹ 2. ίϯϐϡʔλγϛϡϨʔγϣϯ ϑϨʔϜϫʔΫStage 3. ·ͱΊ 4. ͓·͚ 3

    ໨࣍
  4. 1. ͸͡Ίʹ

  5. • ঢ়گʹԠͨ͡ΫϦοΫ਺ͷ࠷େԽ΍มԽ఺ݕग़ͷΑ͏ͳɺγεςϜͷదԠతͳ ৼΔ෣͍Λݕূ͢ΔͨΊʹɺ࣌ܥྻʹର͢ΔίϯϐϡʔλγϛϡϨʔγϣϯΛ ߦ͏͜ͱ͕͋Δɻ • → ֬཰తͳৼΔ෣͍΍ɺ࣮؀ڥͰൃੜ͢ΔϥϯμϜͳޡࠩΛදݱ͢Δ৔ ߹ɺཚ਺Λ༻͍ͨγϛϡϨʔγϣϯٕ๏Ͱ͋ΔϞϯςΧϧϩ๏΋ར༻͢Δ 5 ίϯϐϡʔλγϛϡϨʔγϣϯ

  6. 6 ίϯϐϡʔλγϛϡϨʔγϣϯ Arm0 Arm1 Arm2 User(s) System ଴ͪߦྻ ଟ࿹όϯσ Οοτ໰୊

    ౸ணաఔʹϙΞιϯ෼෍ɺαʔϏεաఔʹࢦ਺ ෼෍Λద༻ͨ͠଴ͪߦྻϞσϧͷγϛϡϨʔ γϣϯ ར༻ऀͷᅂ޷͔Β੒Δ঎඼ʹର͢Δ൓Ԡʢใ ुʣΛ͋Δ֬཰෼෍ʹݟཱͯɺظؒதͷྦྷੵใ ुΛ࠷େԽ͢ΔํࡦΛ୳ΔγϛϡϨʔγϣϯ
  7. • γϛϡϨʔγϣϯର৅͸ຖճҟͳΔ͕ɺࣅͨΑ͏ͳॲཧ͸ຖճ࣮૷͍ͯ͠Δ • Ұ࿈ͷॲཧʢධՁର৅ͷɺ͋Δےॻ͖ʹର͢ΔৼΔ෣͍Λෳ਺ճ؍ଌʣ • γϛϡϨʔγϣϯͷฒྻԽ • ਐḿͷ؂ࢹʢϓϩάϨεόʔʣ • ϩάग़ྗ

    • ཚ਺γʔυͷ؅ཧ • →࿦จࠪಡϓϩηεʹ͓͍ͯ࠶࣮ߦͯ͠ಉ݁͡Ռ͕ಘΒΕΔ؀ڥ͸ॏཁ • ݁ՌͷՄࢹԽ 7 ίϯϐϡʔλγϛϡϨʔγϣϯ࣮૷ͷ՝୊
  8. 2. Stage

  9. • ཚ਺Λ༻͍ͨίϯϐϡʔλγϛϡϨʔγϣϯʹ൐͏Ұ࿈͔ͭ൥ࡶͳॲཧΛ։ൃ ऀ͔ΒӅṭ͢ΔγϯϓϧͳϑϨʔϜϫʔΫ • Goݴޠ੡ 9 monochromegane/stage https://github.com/monochromegane/stage

  10. • ཚ਺Λ༻͍ͨίϯϐϡʔλγϛϡϨʔγϣϯʹ൐͏Ұ࿈͔ͭ൥ࡶͳॲཧΛ։ൃ ऀ͔ΒӅṭ͢ΔγϯϓϧͳϑϨʔϜϫʔΫ 10 monochromegane/stage • Ұ࿈ͷॲཧʢධՁର৅ͷɺ͋Δےॻ͖ʹର͢ΔৼΔ෣͍Λෳ਺ճ؍ଌʣ • γϛϡϨʔγϣϯͷฒྻԽ •

    ਐḿͷ؂ࢹʢϓϩάϨεόʔʣ • ϩάग़ྗ • ཚ਺γʔυͷ؅ཧ • ݁ՌͷՄࢹԽ 4UBHF͕ର৅ͱ͢Δൣғ ˎ౎౓ҟͳΔɺධՁର৅ͱےॻ͖ͷΈ Λ࣮૷͢Δ 4UBHF͕ର৅ͱ͠ͳ͍ൣғ ˎγϛϡϨʔγϣϯʹΑͬͯޮՌతͳՄࢹԽ͕ҟͳΔͷͰϩάग़ྗҎ߱͸౎౓࣮૷͕ྑ͍ͱ൑அ
  11. 11 γφϦΦͱΞΫλʔ • StageͰ͸γϛϡϨʔγϣϯΛܶʹྫ͑ͨϝλϑΝʔΛ࠾༻ • Stage = ܶ৔ɺScenario = ୆ຊʢγφϦΦʣɺActor

    = ԋऀ • ։ൃऀ͸ɺҰճͷγϛϡϨʔγϣϯͷ؀ڥͷมԽΛγφϦΦͱɺͦͷ؀ڥʹԠ ͨ͡ԋऀͷৼΔ෣͍ΛίʔυͰ࣮૷͢Δ
  12. 12 StageϑϨʔϜϫʔΫͷϝΠϯॲཧʢٙࣅίʔυʣ γφϦΦ͔ΒηϦϑΛಡΈग़͢ ԋऀ͕ηϦϑʹै͍ԋٕ ԋٕͷ݁ՌΛॻ͖ग़͢

  13. 13 StageϑϨʔϜϫʔΫͷϝΠϯॲཧʢٙࣅίʔυʣ ܶ৔Ͱ͸͕ܶԿ౓΋։࠵͞ΕΔɻ HPSPVUJOFͰͰ͖Δ͚ͩಉ্࣌өʹʂ

  14. • Scenarioͷ࣮૷ 14 StageͰίϯϐϡʔλγϛϡϨʔγϣϯʢ1/3ʣ γφϦΦΛಡΈਐΊΔɻ Χ΢ϯλΛΠϯΫϦϝϯτ͢ΔॲཧͳͲΛ࣮૷ Χ΢ϯλ࣌఺Ͱͷ؀ڥ৘ใΛ-JOFͰฦ͢

  15. • Scenarioͷ࣮૷ 15 StageͰίϯϐϡʔλγϛϡϨʔγϣϯʢ1/3ʣ γφϦΦΛಡΈਐΊΔɻ Χ΢ϯλΛΠϯΫϦϝϯτ͢ΔॲཧͳͲΛ࣮૷ Χ΢ϯλ࣌఺Ͱͷ؀ڥ৘ใΛ-JOFͰฦ͢ ·͋Ͱ΋JOUFSGBDF\^͸Ͳ͏͔ͳ͋ɻ จࣈྻΛѻ͏ͱܾΊͯ4DBOOFS͕ ͦͷ··࢖͑ΔΑ͏ʹͨ͠΄͏͕ศར͔΋

  16. • ActorͱActionͷ࣮૷ 16 StageͰίϯϐϡʔλγϛϡϨʔγϣϯʢ2/3ʣ "DUJPOͷ4USJOH ͕ϩάʹग़ྗ͞ΕΔ -JOFʹର͢ΔৼΔ෣͍Λ࣮૷ɻ ԋٕͷ݁ՌΛ"DUJPOͱͯ͠ฦ͢

  17. • ࣮ߦ 17 StageͰίϯϐϡʔλγϛϡϨʔγϣϯʢ3/3ʣ ࣮ߦճ਺ͱฒྻ਺ɺॳظ ཚ਺γʔυΛࢦఆ ։࢝࣌ࠁ୯ҐͰ࣮ߦ͝ͱͷϩάͱཚ਺γʔυΛه࿥

  18. • ΠςϨʔγϣϯ׬ྃ͝ͱʹݺ͹ΕΔίʔϧόοΫͷ࢓૊ΈΛ༻ҙͯ͠㽂 18 StageͰίϯϐϡʔλγϛϡϨʔγϣϯʢਐḿ؂ࢹʣ

  19. ෳ਺ͷγφϦΦΛ࣋ͭ γϛϡϨʔγϣϯͷྫ

  20. γϛϡϨʔγϣϯͷྫ 20 • มԽݕग़ͷΞϧΰϦζϜʢADWINʣʹର͢ΔγϛϡϨʔγϣϯ • ࣌ܥྻͷมԽͷ౓߹͍ʢٸܹɺΏͬ͘Γʣʹରͯ͠ΞϧΰϦζϜΛධՁ γφϦΦͰهड़ γφϦΦʹର͢Δԋٕ

  21. γϛϡϨʔγϣϯͷྫ 21 Scenario (Abrupt change) Scenario (Gradual change) Actor (ADWIN)

    Action ({x, mu, sum, W}) • มԽݕग़ͷΞϧΰϦζϜʢADWINʣʹର͢ΔγϛϡϨʔγϣϯ • ࣌ܥྻͷมԽͷ౓߹͍ʢٸܹɺΏͬ͘Γʣʹରͯ͠ΞϧΰϦζϜΛධՁ
  22. γϛϡϨʔγϣϯͷྫ 22 Scenario (Abrupt change) Scenario (Gradual change)

  23. γϛϡϨʔγϣϯͷྫʢΞϧΰϦζϜͷ௥Ճʣ 23 Scenario (Abrupt change) Scenario (Gradual change) Actor (ADWIN)

    Actor (ADWIN2) Action ({x, mu, sum, W}) • มԽݕग़ͷΞϧΰϦζϜʢADWINʣʹର͢ΔγϛϡϨʔγϣϯ • ࣌ܥྻͷมԽͷ౓߹͍ʢٸܹɺΏͬ͘Γʣʹରͯ͠ΞϧΰϦζϜΛධՁ
  24. 3. ·ͱΊ

  25. • ࣌ܥྻΛѻ͏ίϯϐϡʔλγϛϡϨʔγϣϯ༻ͷγϯϓϧͳϑϨʔϜϫʔΫ StageΛGoݴޠͰ࣮૷ͨ͠ • ֤γϛϡϨʔγϣϯʹ͓͍ͯγφϦΦͱΞΫλʔͱ͍͏ཁૉͷΈΛҙࣝ͢Ε͹ Α͘ͳΓ࣮૷ͷޮ཰͕֨ஈʹ্͕Γίʔυͷݟ௨͠΋Α͘ͳͬͨ • GoݴޠΛ࢖͏͜ͱͰฒྻԽ͕༰қʹ࣮૷Ͱ͖γϯϓϧͳϑϨʔϜϫʔΫͰ͋ Γͳ͕Βे෼ʹ҆ఆͯ͠ߴ଎ԽΛୡ੒Ͱ͖ͨ •

    ෇Ճతͳར఺ͱͯ͠ɺϩάߏ੒ͳͲ͕౷Ұ͞Εͨ͜ͱͰޙஈͷղੳ΍άϥϑԽ ͷεΫϦϓτ΋ڞ௨Խ͕ਐΜͰ͍Δ 25 ·ͱΊ
  26. 4. ͓·͚

  27. • γφϦΦɺΞΫλʔ͚࣮ͩ૷ͯ͠΋ͦΕΛ੾Γସ͑ΔίϚϯυϥΠϯΦϓγϣ ϯΛॻ͍ͯɺՄࢹԽ༻ͷεΫϦϓτʹΦϓγϣϯ௥Ճͯ͠ɺͦΕΛ·ͱΊΔ γΣϧεΫϦϓτʹ΋Φϓγϣϯ౉ͤΔΑ͏ʹͯ͠… 27 ίϚϯυϥΠϯΦϓγϣϯʹΑΔڍಈมߋͷ࣮૷ ֤ݴޠͰͷίϚϯυϥΠϯύʔαʔͷॻ֮ࣜ͑ͯͳ͍໰୊

  28. • Fukuoka.go#14Ͱ΋঺հͨ͠ • ίϚϯυϥΠϯΦϓγϣϯΛύʔε͢ΔίʔυΛίϚϯυϥΠϯΦϓγϣϯ͔ Βੜ੒͢Δπʔϧ 28 monochromegane/flagen https://github.com/monochromegane/flagen

  29. Example of Go (preset template) 29 $ flagen go --dist

    erlang -e k/l --lambda 1.5 -k 1 -v var ( dist string e string lambda float64 k int v bool ) func init() { flag.StringVar(&dist, "dist", "erlang", "usage of dist") flag.StringVar(&e, "e", "k/l", "usage of e") flag.Float64Var(&lambda, "lambda", 1.5, "usage of lambda") flag.IntVar(&k, "k", 1, "usage of k") flag.BoolVar(&v, "v", false, "usage of v") } ίϚϯυϥΠϯΦϓγϣϯ໊͔Βม਺໊Λੜ੒ ίϚϯυϥΠϯΦϓγϣϯ஋͔ΒܕΛਪଌ ࣮ࡍʹ࢖༻͢ΔίϚϯυϥΠϯΦϓγϣϯ͔Β ίϚϯυϥΠϯͷղੳॲཧΛੜ੒
  30. Example of Python (preset template) 30 $ flagen py --dist

    erlang -e k/l --lambda 1.5 -k 1 -v import argparse parser = argparse.ArgumentParser() parser.add_argument("--dist", default="erlang", help="Help of dist") parser.add_argument("-e", default="k/l", help="Help of e") parser.add_argument("--lambda", type=float, default=1.5, help="Help of lambda") parser.add_argument("-k", type=int, default=1, help="Help of k") parser.add_argument("-v", action="store_false", help="Help of v") args = parser.parse_args() ϋϚΓ͕ͪͳ1ZUIPOͷCPPMͷσϑΥϧτ஋ͷઃఆ΋ ϘΠϥʔςϯϓϨʔτͳͷͰେৎ෉
  31. Example of Ruby (preset template) 31 $ flagen rb --dist

    erlang -e k/l --lambda 1.5 -k 1 -v require 'optparse' opts = { dist: 'erlang', e: 'k/l', lambda: 1.5, k: 1, v: false, } OptionParser.new do |op| op.on('--dist [VALUE]', 'Desc of dist') {|v| opts[:dist] = v } op.on('-e [VALUE]', 'Desc of e') {|v| opts[:e] = v } op.on('--lambda [VALUE]', 'Desc of lambda') {|v| opts[:lambda] = v.to_f } op.on('-k [VALUE]', 'Desc of k') {|v| opts[:k] = v.to_i } op.on('-v', 'Desc of v') {|v| opts[:v] = v } op.parse!(ARGV) end
  32. Example of Shell (preset template) 32 $ flagen sh --dist

    erlang -e k/l --lambda 1.5 -k 1 -v E="k/l" K="1" V="FALSE" while getopts e:k:v OPT do case $OPT in "e" ) E="$OPTARG";; "k" ) K="$OPTARG";; "v" ) V="TRUE";; esac done shift `expr $OPTIND - 1` HFUPQUT͸ҰจࣈͷҾ਺͚ͩѻ͑ΔͷͰ௕͍Φϓγϣϯ͸ແࢹ
  33. • flagenΛΤσΟλͱ࿈ܞ͢Δ • ྫ͑͹VimͰ͸ҎԼʹΑΓɺΧʔιϧҐஔʹ݁ՌΛૠೖ͢Δ͜ͱ͕Ͱ͖Δɻ ʢಈతͳίʔυεχϖοτͱͯ͠ͷར༻ʣ 33 Collaboration :r!flagen YOUR_TEMPLATE YOUR_COMMAND_LINE_OPTIONS...

  34. None