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

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

tsurubee
July 13, 2019

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

tsurubee

July 13, 2019
Tweet

More Decks by tsurubee

Other Decks in Technology

Transcript

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

    ަࠥʢ$SPTTPWFSʣ  ಥવมҟʢ.VUBUJPOʣ ҎԼͷεςοϓʹԊͬͯղઆ͍͖ͯ͠·͢ʂ
  2. 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 }
  3. GoConference’19 4. ަ伹ʢCrossoverʣ 19 બ୒͞Εͨͭͷ਌ݸମΛަࠥ͠ɺࢠݸମΛੜ੒͢Δ Ұ఺ަࠥ    

                ަࠥ఺ ਌ݸମ̍ ਌ݸମ̎                 ࢠݸମ̍ ࢠݸମ̎ ަࠥ఺Λڥʹޓ͍ʹཁૉΛަ׵͢Δૢ࡞Λߦ͏
  4. 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)) } } }                 ɾɾɾɾ ɾɾɾɾ Ұఆͷ֬཰Ͱཁૉͷ஋ΛϥϯμϜʹมߋ͢Δ
  5. GoConference’19 ฒྻԽϞσϧɿϚελʔεϨʔϒํࣜ 25 4MBWF .BTUFS 4MBWF 4MBWF ɾɾɾ ϚελʔεϨʔϒํࣜ w

    ("ͷ࠷΋୯७ͳฒྻԽϞσϧͰ͋ΓɺదԠ౓ܭࢉΛฒྻԽ͢Δ w શମͷ੍ޚ͓ΑͼҨ఻తૢ࡞Λߦ͏ϚελʔͱɺదԠ౓ܭࢉΛߦ͏ෳ਺ͷεϨʔϒ͔ Βߏ੒͞ΕΔ ͜͜ΛฒྻԽ͢Δʂ
  6. 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Λ༻͍ͯɺಠཱͨ͠ෳ਺ͷλεΫΛฏߦͰॲཧͯ͠ɺͦΕΒશͯͷऴྃΛ଴ͪ ߹ΘͤΔॲཧ͕γϯϓϧͳهड़Ͱ࣮૷Ͱ͖Δ