Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goでつくる進化計算パッケージ

5381bd68abe2b91239ca1600db2a890d?s=47 tsurubee
July 13, 2019

 Goでつくる進化計算パッケージ

5381bd68abe2b91239ca1600db2a890d?s=128

tsurubee

July 13, 2019
Tweet

Transcript

  1. GoConference’19 GoͰͭ͘ΔਐԽܭࢉύοέʔδ @tsurubee 2019.07.13 Go Conference '19 Summer in Fukuoka

  2. GoConference’19 ௽ా തจ (@tsurubee) GMOϖύϘגࣜձࣾ ϗεςΟϯάࣄۀ෦
 ΠϯϑϥνʔϜ 2

  3. GoConference’19 ࠓ೔࿩͢͜ͱ wਐԽܭࢉͱ͸Ҩ఻తΞϧΰϦζϜͱ͸ wҨ఻తΞϧΰϦζϜͷ࢓૊Έ w(PݴޠʹΑΔҨ఻తΞϧΰϦζϜͷ࣮૷ wࠓ೔ͷ͓୊ʢ࠷దԽ໰୊ʣΛղ͍ͯΈΔ 3

  4. GoConference’19 ਐԽܭࢉͱ͸ wੜ෺ͷਐԽ΍ৼΔ෣͍Λ໛฿ͨ͠࠷దԽख๏ͷ૯শ wϝλώϡʔϦεςΟοΫͳख๏Ͱ͋Γɺෳࡶͳ໰୊΍໰୊ߏ଄ͷಉఆ͕ ࠔ೉ͳ໰୊ʹ΋༗ޮͳ࠷దԽख๏ͱͯ͠ظ଴͞Ε͍ͯΔ 4

  5. GoConference’19 ਐԽܭࢉͱ͸ wੜ෺ͷਐԽ΍ৼΔ෣͍Λ໛฿ͨ͠࠷దԽख๏ͷ૯শ wϝλώϡʔϦεςΟοΫͳख๏Ͱ͋Γɺෳࡶͳ໰୊΍໰୊ߏ଄ͷಉఆ͕ ࠔ೉ͳ໰୊ʹ΋༗ޮͳ࠷దԽख๏ͱͯ͠ظ଴͞Ε͍ͯΔ 5 NFUBIFVSJTUJDBMHPSJUIN ಛఆͷ໰୊ͷΈΛର৅ͱ͢ΔͷͰ͸ͳ͘ɺ ༷ʑͳ໰୊ʹରͯ͠ɺൺֱత୹࣌ؒͰ
 ۙࣅղΛޮ཰Α͘ٻΊΔղ๏

  6. GoConference’19 ਐԽܭࢉύοέʔδ eago wཻࢠ܈࠷దԽ wҨ఻తΞϧΰϦζϜ 6 IUUQTHJUIVCDPNUTVSVCFFFBHP &WPMVUJPOBSZ"MHPSJUINJNQMFNFOUFEJO(P ࣮૷ΞϧΰϦζϜ

  7. GoConference’19 ਐԽܭࢉύοέʔδ eago wཻࢠ܈࠷దԽ wҨ఻తΞϧΰϦζϜ 7 IUUQTHJUIVCDPNUTVSVCFFFBHP &WPMVUJPOBSZ"MHPSJUINJNQMFNFOUFEJO(P ࣮૷ΞϧΰϦζϜ ࠓճ͸ͪ͜Βʹ͍ͭͯ঺հʂ

  8. GoConference’19 Ҩ఻తΞϧΰϦζϜ(GA)ͱ͸ w ूஂͷதͰ؀ڥʹదԠͨ͠ݸମ͕ΑΓߴ͍֬཰Ͱ
 ੜ͖࢒Γɺ࣍ੈ୅ʹࢠΛ࢒͢ʢࣗવ౫ଡઆʣ࢓૊Έ
 Λιϑτ΢ΣΞతʹ໛͢͜ͱͰෳࡶͳ໰୊ʹର͢Δ
 ࠷దղΛ୳ࡧ͢Δख๏ w ໰୊ͷղΛੜ෺ͷݸମʹݟཱͯͯɺͦͷݸମͷूஂʹ ରͯ͠ɺબ୒ʢ4FMFDUJPOʣަࠥʢ$SPTTPWFSʣಥવ


    มҟʢ.VUBUJPOʣΛద༻͢Δ 8 ೔ܦY5&$)৽ܕ৽װઢʮ/ܥʯͷlإz ΛੜΜͩʮҨ఻తΞϧΰϦζϜʯͷൿີ IUUQTXXXTMJEFTIBSFOFUL[PLNHFOFUJDBMHPSJUIN IUUQTUFDIOJLLFJCQDPKQJUBSUJDMF$0-6./
  9. GoConference’19 9 GAͷྲྀΕ

  10. GoConference’19 10 GAͷྲྀΕ ූ߸Խ

  11. GoConference’19 11 దԠ౓ GAͷྲྀΕ ූ߸Խ

  12. GoConference’19 12 GAͷྲྀΕ దԠ౓ બ୒ ූ߸Խ

  13. GoConference’19 13 దԠ౓ બ୒ ަࠥ ಥવมҟ GAͷྲྀΕ ූ߸Խ

  14. GoConference’19 ࠓ೔ͷ͓୊ 14

  15. GoConference’19 Gopherͷը૾Λੜ੒ͯ͠ΈΑ͏ʂ 15  ූ߸Խʢ&ODPEJOHʣ  దԠ౓ʢ'JUOFTTʣ  બ୒ʢ4FMFDUJPOʣ 

    ަࠥʢ$SPTTPWFSʣ  ಥવมҟʢ.VUBUJPOʣ ҎԼͷεςοϓʹԊͬͯղઆ͍͖ͯ͠·͢ʂ
  16. GoConference’19 1. ූ߸ԽʢEncodingʣ 16 ໰୊ʹର͢ΔղΛݸମͷછ৭ମɺղͷߏ੒ཁૉΛҨ఻ࢠͱͯ͠දݱ͢Δ છ৭ମ͸ɺʷʷ ࣍ݩϕΫτϧ 'MBUUFOJOH ɾɾɾɾ ʷ

    <>VJOUܕ 3(#"ը૾
  17. GoConference’19 ໰୊ʹର͢Δղʢݸମʣͷྑ͞ΛධՁ 2. దԠ౓ʢFitnessʣ 17 ը૾ؒڑ཭ʢྨࣅ౓ʣϐΫηϧؒڑ཭ͷ૯࿨ͷฏํࠜ ը૾ؒڑ཭͕θϩʹ͍ۙ΄Ͳɺ༏ΕͨղͰ͋Δ ը૾ؒڑ཭ ϐΫηϧؒڑ཭ ϢʔΫϦουڑ཭

  18. GoConference’19 3. બ୒ʢSelectionʣ ؀ڥʹదԠͰ͖Δݸମ΄Ͳ࣍ੈ୅ʹࣗ෼ͷҨ఻ࢠΛ࢒ͤΔΑ͏ʹ͢ΔͨΊʹɺ ਌ݸମͷదԠ౓ʹԠͯ͡ੜ͖࢒ΔݸମΛબ୒͢Δ 18 τʔφϝϯτબ୒ʢUPVSOBNFOUTFMFDUJPOʣ ϥϯμϜʹநग़ͨ͠4ݸͷݸମͷ͏ͪɺ
 ࠷΋దԠ౓͕ߴ͍ݸମΛબ୒ "

    # $ % దԠ౓     $ func Select(indi Individuals) (Individuals, error) { if len(indi) < t.NContestants { return nil, errors.New("NContestants is too large") } selected := make(Individuals, len(indi)) for i := range selected { winner := indi[rand.Intn(len(indi))] for j := 0; j < t.NContestants; j++ { next := indi[rand.Intn(len(indi))] if winner.Fitness > next.Fitness { winner = next } } selected[i] = winner } return selected, nil }
  19. GoConference’19 4. ަ伹ʢCrossoverʣ 19 બ୒͞Εͨͭͷ਌ݸମΛަࠥ͠ɺࢠݸମΛੜ੒͢Δ Ұ఺ަࠥ    

                ަࠥ఺ ਌ݸମ̍ ਌ݸମ̎                 ࢠݸମ̍ ࢠݸମ̎ ަࠥ఺Λڥʹޓ͍ʹཁૉΛަ׵͢Δૢ࡞Λߦ͏
  20. GoConference’19 5. ಥવมҟʢMutationʣ 20 ͍ͣΕͷ਌΋͍࣋ͬͯͳ͍Ҩ఻ࢠΛࢠʹ࣋ͨͤΔ͜ͱͰଟ༷ੑͷҡ࣋͢Δ func (I Image) Mutation() {

    mutationRate := 0.0005 rand.Seed(time.Now().UnixNano()) for i := 0; i < len(I.Pix); i++ { if rand.Float64() < mutationRate { I.Pix[i] = uint8(rand.Intn(255)) } } }                 ɾɾɾɾ ɾɾɾɾ Ұఆͷ֬཰Ͱཁૉͷ஋ΛϥϯμϜʹมߋ͢Δ
  21. GoConference’19 Ҏ্ͷεςοϓΛ܁Γฦͯ͠
 ਐԽΛॏͶ͍͖ͯ·͢ʂ 21

  22. GoConference’19 ͳͥGoݴޠͰॻ͍ͨͷ͔ʁ 22

  23. GoConference’19 GAͷ՝୊ɿܭࢉ࣌ؒͷ୹ॖ w ("͸ɺ๲େͳܭࢉྔΛཁ͢ΔͨΊɺ࣮༻࣌ؒ಺Ͱྑ޷ͳղΛಘΔͨΊʹ
 ܭࢉॲཧΛߴ଎Խ͢Δ͜ͱ͕๬·Ε͍ͯΔ 23 w ("͸ɺෳ਺ͷղީิΛ༻͍ͨଟ఺୳ࡧͷΞϧΰϦζϜͰ͋ΔͨΊɺຊ࣭త ʹฒྻԽͱ਌࿨ੑ͕ߴ͍ w

    ܭࢉ࣌ؒ୹ॖΛ໨తͱͯ͠ɺ༷ʑͳ("ͷฒྻԽख๏͕ఏҊ͞Ε͍ͯΔ
  24. GoConference’19 24 GA͸ॲཧΛฒྻԽ͠΍͍͢ Goݴޠ͸ฒྻԽॲཧ͕ॻ͖΍͍͢ GoݴޠΛ࢖͏ͱ༷ʑͳGAͷฒྻԽϞσϧ ΛαΫοͱ࣮૷Ͱ͖ͯ࠷ߴͳͷͰ͸ʁ

  25. GoConference’19 ฒྻԽϞσϧɿϚελʔεϨʔϒํࣜ 25 4MBWF .BTUFS 4MBWF 4MBWF ɾɾɾ ϚελʔεϨʔϒํࣜ w

    ("ͷ࠷΋୯७ͳฒྻԽϞσϧͰ͋ΓɺదԠ౓ܭࢉΛฒྻԽ͢Δ w શମͷ੍ޚ͓ΑͼҨ఻తૢ࡞Λߦ͏ϚελʔͱɺదԠ౓ܭࢉΛߦ͏ෳ਺ͷεϨʔϒ͔ Βߏ੒͞ΕΔ ͜͜ΛฒྻԽ͢Δʂ
  26. GoConference’19 goroutineʹΑΔGAͷฒྻԽ w ͦΕͧΕͷݸମͷదԠ౓ܭࢉ͸ಠཱͯ͠ߦ͑ΔͨΊɺదԠ౓ܭࢉΛ͢ΔͨΊͷεϨʔϒͱͯ͠ HPSPVUJOFΛ্ཱͪ͛ͯɺฒྻͰܭࢉΛߦ͏ 26 func (indis Individuals) Evaluate(parallel

    bool) { var wg sync.WaitGroup for i := range indis { wg.Add(1) go func(i int) { defer wg.Done() indis[i].Evaluate() //దԠ౓ܭࢉॲཧ }(i) } wg.Wait() } w TZOD8BJU(SPVQΛ༻͍ͯɺಠཱͨ͠ෳ਺ͷλεΫΛฏߦͰॲཧͯ͠ɺͦΕΒશͯͷऴྃΛ଴ͪ ߹ΘͤΔॲཧ͕γϯϓϧͳهड़Ͱ࣮૷Ͱ͖Δ
  27. GoConference’19 Demo 27

  28. GoConference’19 GAʹΑΔը૾ੜ੒ 28 (PQIFSͷ͕࢟ʜʂ ʻ৚݅ʼ 1PQVMBUJPO4J[F
 $SPTTPWFS3BUF
 .VUBUJPO3BUF
 5PVSOBNFOU4J[F ੈ୅໨

    ੈ୅໨ ໨ඪ ੈ୅໨
  29. GoConference’19 ฒྻԽͷ༗ޮੑ 29 దԠ౓ܭࢉͷฒྻԽ͕ॲཧ࣌ؒͷ୹ॖʹد༩͍ͯ͠Δ Ϛγϯɿ.BD#PPL1SP
 ϓϩηοαɿ()[*OUFM$PSFJ
 ϝϞϦɿ(#

  30. GoConference’19 ·ͱΊ w("Λ࢝Ίͱͨ͠ਐԽܭࢉ͸ɺෳࡶͳ໰୊΍໰୊ߏ଄ͷಉఆ͕ࠔ೉ͳ໰୊ ʹ΋༗ޮͳ࠷దԽख๏ͱͯ͠ظ଴͞Ε͍ͯΔ w("͸ଟ఺୳ࡧΞϧΰϦζϜͰ͋ΔͨΊɺฒྻԽͱ਌࿨ੑ͕ߴ͍ wHPSPVUJOFΛ༻͍ΔͱαΫοͱ("ͷฒྻԽॲཧ͕ॻ͚Δ w("ͰԿͱ͔(PQIFSͷ໘Ө͕ݟ͑Δը૾͕࡞Εͨ 30

  31. GoConference’19 ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ 31