欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

遺傳算法Python實(shí)現(xiàn)

系統(tǒng) 1789 0

遺傳算法Python實(shí)現(xiàn)

  • 瞎BB
  • 代碼
    • 導(dǎo)入庫以及參數(shù)設(shè)置
    • 目標(biāo)函數(shù)
    • 生成C行R列的值在0-1的數(shù)組
    • 混沌函數(shù)
    • 二進(jìn)制轉(zhuǎn)十進(jìn)制
    • 個(gè)體按值從大到小排序
    • 交叉變異
    • 適應(yīng)度函數(shù)
    • 主函數(shù)

瞎BB

代碼

導(dǎo)入庫以及參數(shù)設(shè)置

            
              
                import
              
               pandas 
              
                as
              
               pd

              
                import
              
               numpy 
              
                as
              
               np

              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt

              
                import
              
               math

              
                import
              
               random


              
                #range of variable
              
              
bounds 
              
                =
              
               np
              
                .
              
              array
              
                (
              
              
                [
              
              
                -
              
              
                2
              
              
                ,
              
              
                2
              
              
                ]
              
              
                )
              
              
                #begin of variable
              
              
boundsbegin 
              
                =
              
               bounds
              
                [
              
              
                0
              
              
                ]
              
              
                #end of variable
              
              
boundsend 
              
                =
              
               bounds
              
                [
              
              
                1
              
              
                ]
              
              
precision 
              
                =
              
              
                0.0001
              
              
                #calc the BitLength
              
              
BitLength 
              
                =
              
               math
              
                .
              
              ceil
              
                (
              
              np
              
                .
              
              log2
              
                (
              
              
                (
              
              boundsend 
              
                -
              
               boundsbegin
              
                )
              
              
                /
              
               precision
              
                )
              
              
                )
              
              
                #init
              
              
popsize 
              
                =
              
              
                100
              
              
Generationmax 
              
                =
              
              
                100
              
              
pmut 
              
                =
              
              
                0.09
              
            
          

目標(biāo)函數(shù)

            
              
                def
              
              
                targetfun
              
              
                (
              
              x
              
                )
              
              
                :
              
              
    value 
              
                =
              
              
                200
              
              
                *
              
               math
              
                .
              
              exp
              
                (
              
              
                -
              
              
                0.05
              
              
                *
              
               x
              
                )
              
              
                *
              
               math
              
                .
              
              sin
              
                (
              
              x
              
                )
              
              
                return
              
               value

            
          

生成C行R列的值在0-1的數(shù)組

            
              
                def
              
              
                random_random
              
              
                (
              
              C
              
                ,
              
              R
              
                )
              
              
                :
              
              
    rand
              
                =
              
              
                [
              
              
                ]
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              C
              
                *
              
              R
              
                )
              
              
                :
              
              
        rand
              
                .
              
              append
              
                (
              
              random
              
                .
              
              random
              
                (
              
              
                )
              
              
                )
              
              
    rand
              
                =
              
              np
              
                .
              
              array
              
                (
              
              rand
              
                )
              
              
                return
              
               rand
              
                .
              
              reshape
              
                (
              
              C
              
                ,
              
              R
              
                )
              
            
          

混沌函數(shù)

            
              
                def
              
              
                chaos
              
              
                (
              
              size
              
                )
              
              
                :
              
              
    chaos_cro 
              
                =
              
               np
              
                .
              
              zeros
              
                (
              
              size
              
                )
              
              
    chaos_cro
              
                [
              
              
                0
              
              
                ]
              
              
                =
              
               random
              
                .
              
              random
              
                (
              
              
                )
              
              
                for
              
               j 
              
                in
              
              
                range
              
              
                (
              
              
                1
              
              
                ,
              
              size
              
                )
              
              
                :
              
              
            chaos_cro
              
                [
              
              j
              
                ]
              
              
                =
              
              
                4
              
              
                *
              
               chaos_cro
              
                [
              
              j 
              
                -
              
              
                1
              
              
                ]
              
              
                *
              
              
                (
              
              
                1
              
              
                -
              
               chaos_cro
              
                [
              
              j 
              
                -
              
              
                1
              
              
                ]
              
              
                )
              
              
                return
              
               chaos_cro
              
                [
              
              
                -
              
              
                1
              
              
                ]
              
            
          

二進(jìn)制轉(zhuǎn)十進(jìn)制

            
              
                def
              
              
                transform2to10
              
              
                (
              
              sample
              
                )
              
              
                :
              
              
    BitLength 
              
                =
              
              
                len
              
              
                (
              
              sample
              
                )
              
              
    x
              
                =
              
              sample
              
                [
              
              
                -
              
              
                1
              
              
                ]
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                1
              
              
                ,
              
              BitLength
              
                )
              
              
                :
              
              
        x
              
                =
              
              x
              
                +
              
              sample
              
                [
              
              
                -
              
              i
              
                -
              
              
                1
              
              
                ]
              
              
                *
              
              np
              
                .
              
              power
              
                (
              
              
                2
              
              
                ,
              
              i
              
                )
              
              
                return
              
               x

            
          

個(gè)體按值從大到小排序

            
              
                def
              
              
                rank
              
              
                (
              
              population
              
                )
              
              
                :
              
              
    popsize 
              
                =
              
               population
              
                .
              
              shape
              
                [
              
              
                0
              
              
                ]
              
              
    BitLength 
              
                =
              
               population
              
                .
              
              shape
              
                [
              
              
                1
              
              
                ]
              
              
    fitvalue 
              
                =
              
               np
              
                .
              
              zeros
              
                (
              
              popsize
              
                )
              
              
                .
              
              reshape
              
                (
              
              popsize
              
                ,
              
              
                1
              
              
                )
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              popsize
              
                )
              
              
                :
              
              
        x 
              
                =
              
               transform2to10
              
                (
              
              population
              
                [
              
              i
              
                ]
              
              
                )
              
              
                #tansform to range of variable
              
              
        xx
              
                =
              
              boundsbegin 
              
                +
              
               x 
              
                *
              
              
                (
              
              boundsend 
              
                -
              
               boundsbegin
              
                )
              
              
                /
              
              
                (
              
              np
              
                .
              
              power
              
                (
              
              boundsend
              
                ,
              
              BitLength
              
                )
              
              
                -
              
              
                1
              
              
                )
              
              
        fitvalue
              
                [
              
              i
              
                ,
              
              
                0
              
              
                ]
              
              
                =
              
              targetfun
              
                (
              
              xx
              
                )
              
              
                #make fitvalue(j)
                
                  
    res 
                  
                    =
                  
                   np
                  
                    .
                  
                  concatenate
                  
                    (
                  
                  
                    (
                  
                  population
                  
                    ,
                  
                  fitvalue
                  
                    )
                  
                  
                    ,
                  
                  axis 
                  
                    =
                  
                  
                    1
                  
                  
                    )
                  
                  
    res
                  
                    =
                  
                  pd
                  
                    .
                  
                  DataFrame
                  
                    (
                  
                  res
                  
                    )
                  
                  
    population 
                  
                    =
                  
                   res
                  
                    .
                  
                  sort_values
                  
                    (
                  
                  by
                  
                    =
                  
                  BitLength
                  
                    )
                  
                  
    population 
                  
                    =
                  
                   np
                  
                    .
                  
                  array
                  
                    (
                  
                  population
                  
                    )
                  
                  
    population 
                  
                    =
                  
                   population
                  
                    [
                  
                  
                    :
                  
                  
                    ,
                  
                  
                    0
                  
                  
                    :
                  
                  BitLength
                  
                    ]
                  
                  
                    return
                  
                   population

                
              
            
          

交叉變異

            
              
                def
              
              
                cro_mut_improve
              
              
                (
              
              population
              
                ,
              
              rate_mut
              
                )
              
              
                :
              
              
                #seln:two individuals
              
              
    popsize 
              
                =
              
               population
              
                .
              
              shape
              
                [
              
              
                0
              
              
                ]
              
              
    BitLength 
              
                =
              
               population
              
                .
              
              shape
              
                [
              
              
                1
              
              
                ]
              
              
    population 
              
                =
              
               rank
              
                (
              
              population
              
                )
              
              
                #crossover
              
              
    pop
              
                =
              
              popsize
    chaos_cro 
              
                =
              
               np
              
                .
              
              zeros
              
                (
              
              BitLength
              
                )
              
              
                if
              
               popsize 
              
                %
              
              
                2
              
              
                ==
              
              
                1
              
              
                :
              
              
        pop 
              
                =
              
               popsize 
              
                -
              
              
                1
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                0
              
              
                ,
              
              pop
              
                ,
              
              
                2
              
              
                )
              
              
                :
              
              
        chaos_cro 
              
                =
              
               chaos
              
                (
              
              BitLength
              
                )
              
              
        chao 
              
                =
              
               math
              
                .
              
              floor
              
                (
              
              chaos_cro 
              
                *
              
               BitLength
              
                )
              
              
        temp 
              
                =
              
               population
              
                [
              
              i
              
                ,
              
              chao
              
                ]
              
              
        population
              
                [
              
              i
              
                ,
              
              chao
              
                ]
              
              
                =
              
               population
              
                [
              
              i
              
                +
              
              
                1
              
              
                ,
              
              chao
              
                ]
              
              
        population
              
                [
              
              i 
              
                +
              
              
                1
              
              
                ,
              
              chao
              
                ]
              
              
                =
              
               temp
    
    
              
                #mutation
              
              
    by 
              
                =
              
               np
              
                .
              
              array
              
                (
              
              
                [
              
              
                ]
              
              
                )
              
              
    n 
              
                =
              
              
                len
              
              
                (
              
              by
              
                )
              
              
                #generate random individuals to mutate
              
              
                while
              
               n 
              
                ==
              
              
                0
              
              
                :
              
              
        by 
              
                =
              
               random_random
              
                (
              
              
                1
              
              
                ,
              
              popsize
              
                )
              
              
                <
              
              rate_mut
        
              
                for
              
               i 
              
                in
              
               by
              
                [
              
              
                0
              
              
                ]
              
              
                :
              
              
                if
              
               i
              
                :
              
              
                n
              
                +=
              
              
                1
              
              
    num_mut 
              
                =
              
               n
    
              
                for
              
               k 
              
                in
              
              
                range
              
              
                (
              
              popsize
              
                )
              
              
                :
              
              
                if
              
               by
              
                [
              
              
                0
              
              
                ,
              
              k
              
                ]
              
              
                ==
              
              
                True
              
              
                :
              
              
            chaos_mut 
              
                =
              
               np
              
                .
              
              zeros
              
                (
              
              BitLength
              
                )
              
              
            chaos_mut
              
                [
              
              
                0
              
              
                ]
              
              
                =
              
               random
              
                .
              
              random
              
                (
              
              
                )
              
              
                for
              
               t 
              
                in
              
              
                range
              
              
                (
              
              
                1
              
              
                ,
              
              BitLength
              
                )
              
              
                :
              
              
                chaos_mut
              
                [
              
              t
              
                ]
              
              
                =
              
              
                4
              
              
                *
              
               chaos_mut
              
                [
              
              t 
              
                -
              
              
                1
              
              
                ]
              
              
                *
              
              
                (
              
              
                1
              
              
                -
              
               chaos_mut
              
                [
              
              t 
              
                -
              
              
                1
              
              
                ]
              
              
                )
              
              
            position 
              
                =
              
               np
              
                .
              
              floor
              
                (
              
              BitLength 
              
                *
              
               random_random
              
                (
              
              
                1
              
              
                ,
              
              
                2
              
              
                )
              
              
                )
              
              
            position
              
                .
              
              astype
              
                (
              
              np
              
                .
              
              int16
              
                )
              
              
            population
              
                [
              
              k
              
                ,
              
              
                int
              
              
                (
              
              position
              
                [
              
              
                0
              
              
                ,
              
              
                0
              
              
                ]
              
              
                )
              
              
                ]
              
              
                =
              
               np
              
                .
              
              
                round
              
              
                (
              
              chaos
              
                (
              
              BitLength
              
                )
              
              
                )
              
              
            population
              
                [
              
              k
              
                ,
              
              
                int
              
              
                (
              
              position
              
                [
              
              
                0
              
              
                ,
              
              
                1
              
              
                ]
              
              
                )
              
              
                ]
              
              
                =
              
               np
              
                .
              
              
                round
              
              
                (
              
              chaos
              
                (
              
              BitLength
              
                )
              
              
                )
              
              
                return
              
               population

            
          

適應(yīng)度函數(shù)

            
              
                def
              
              
                fitness
              
              
                (
              
              population
              
                )
              
              
                :
              
              
                #population=pd.DataFrame(population)
              
              
    popsize 
              
                =
              
               population
              
                .
              
              shape
              
                [
              
              
                0
              
              
                ]
              
              
    BitLength 
              
                =
              
               population
              
                .
              
              shape
              
                [
              
              
                1
              
              
                ]
              
              
    cumsump 
              
                =
              
               np
              
                .
              
              zeros
              
                (
              
              popsize
              
                )
              
              
                .
              
              reshape
              
                (
              
              
                1
              
              
                ,
              
              popsize
              
                )
              
              
    fitvalue 
              
                =
              
               np
              
                .
              
              zeros
              
                (
              
              popsize
              
                )
              
              
                .
              
              reshape
              
                (
              
              
                1
              
              
                ,
              
              popsize
              
                )
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              popsize
              
                )
              
              
                :
              
              
        x 
              
                =
              
               transform2to10
              
                (
              
              population
              
                [
              
              i
              
                ]
              
              
                )
              
              
                #tansform to range of variable
              
              
        xx 
              
                =
              
               boundsbegin 
              
                +
              
               x 
              
                *
              
              
                (
              
              boundsend 
              
                -
              
               boundsbegin
              
                )
              
              
                /
              
              
                (
              
              np
              
                .
              
              power
              
                (
              
              boundsend
              
                ,
              
              BitLength
              
                )
              
              
                -
              
              
                1
              
              
                )
              
              
        fitvalue
              
                [
              
              
                0
              
              
                ,
              
              i
              
                ]
              
              
                =
              
               targetfun
              
                (
              
              xx
              
                )
              
              
                #ensure fitvalue>0
              
              
    fitvalue 
              
                =
              
               fitvalue
              
                +
              
              
                230
              
              
    fsum 
              
                =
              
               fitvalue
              
                .
              
              
                sum
              
              
                (
              
              
                1
              
              
                )
              
              
                [
              
              
                0
              
              
                ]
              
              
    Pperpopulation 
              
                =
              
               fitvalue 
              
                /
              
               fsum
    cumsump
              
                [
              
              
                0
              
              
                ]
              
              
                =
              
               Pperpopulation
              
                [
              
              
                0
              
              
                ]
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                1
              
              
                ,
              
              popsize
              
                )
              
              
                :
              
              
        cumsump
              
                [
              
              
                0
              
              
                ,
              
              i
              
                ]
              
              
                =
              
               cumsump
              
                [
              
              
                0
              
              
                ,
              
              i
              
                -
              
              
                1
              
              
                ]
              
              
                +
              
               Pperpopulation
              
                [
              
              
                0
              
              
                ,
              
              i
              
                ]
              
              
    res 
              
                =
              
               np
              
                .
              
              concatenate
              
                (
              
              
                (
              
              fitvalue
              
                ,
              
              cumsump
              
                )
              
              
                ,
              
              axis 
              
                =
              
              
                0
              
              
                )
              
              
                return
              
               res

            
          

主函數(shù)

            
              ymax
              
                =
              
              np
              
                .
              
              zeros
              
                (
              
              Generationmax
              
                +
              
              
                1
              
              
                )
              
              
ymean
              
                =
              
              np
              
                .
              
              zeros
              
                (
              
              Generationmax
              
                +
              
              
                1
              
              
                )
              
              
xmax
              
                =
              
              np
              
                .
              
              zeros
              
                (
              
              Generationmax
              
                +
              
              
                1
              
              
                )
              
              
                #generate random population
              
              
population 
              
                =
              
               np
              
                .
              
              
                round
              
              
                (
              
              random_random
              
                (
              
              popsize
              
                ,
              
              BitLength
              
                )
              
              
                )
              
              
                #calc fitness return fitvalue and sum of probability
              
              
res
              
                =
              
              fitness
              
                (
              
              population
              
                )
              
              
fitvalue 
              
                =
              
               res
              
                [
              
              
                0
              
              
                ]
              
              
cumsump 
              
                =
              
               res
              
                [
              
              
                1
              
              
                ]
              
              
                #main code
              
              
Generation
              
                =
              
              
                1
              
              
                while
              
               Generation
              
                <
              
              Generationmax
              
                +
              
              
                1
              
              
                :
              
              
    population
              
                =
              
              cro_mut_improve
              
                (
              
              population
              
                ,
              
              pmut
              
                )
              
              
    res
              
                =
              
              fitness
              
                (
              
              population
              
                )
              
              
    fitvalue 
              
                =
              
               res
              
                [
              
              
                0
              
              
                ]
              
              
    cumsump 
              
                =
              
               res
              
                [
              
              
                1
              
              
                ]
              
              
    fmax 
              
                =
              
               np
              
                .
              
              
                max
              
              
                (
              
              fitvalue
              
                )
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              popsize
              
                )
              
              
                :
              
              
                if
              
               fitvalue
              
                [
              
              i
              
                ]
              
              
                ==
              
              fmax
              
                :
              
              
            nmax 
              
                =
              
               i
            
              
                break
              
              
    fmean 
              
                =
              
               np
              
                .
              
              mean
              
                (
              
              fitvalue
              
                ,
              
              axis 
              
                =
              
              
                0
              
              
                )
              
              
    ymax
              
                [
              
              Generation
              
                ]
              
              
                =
              
               fmax
    ymean
              
                [
              
              Generation
              
                ]
              
              
                =
              
               fmean
    x 
              
                =
              
               transform2to10
              
                (
              
              population
              
                [
              
              nmax
              
                ]
              
              
                )
              
              
    xx 
              
                =
              
               boundsbegin 
              
                +
              
               x 
              
                *
              
              
                (
              
              boundsend
              
                -
              
              boundsbegin
              
                )
              
              
                /
              
              
                (
              
              
                pow
              
              
                (
              
              boundsend
              
                ,
              
              BitLength
              
                )
              
              
                -
              
              
                1
              
              
                )
              
              
    xmax
              
                [
              
              Generation
              
                ]
              
              
                =
              
               xx
    Generation 
              
                +=
              
              
                1
              
              
                #Generation=Generation-1;
              
              
Bestpopulation 
              
                =
              
               np
              
                .
              
              
                max
              
              
                (
              
              xmax
              
                )
              
              
BestValue 
              
                =
              
               targetfun
              
                (
              
              Bestpopulation
              
                )
              
              
                print
              
              
                (
              
              Bestpopulation
              
                )
              
              
                print
              
              
                (
              
              BestValue
              
                )
              
              

x
              
                =
              
              
                [
              
              
                ]
              
              
y
              
                =
              
              
                [
              
              
                ]
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                400
              
              
                )
              
              
                :
              
              
    t
              
                =
              
              
                -
              
              
                2
              
              
                +
              
              
                0.01
              
              
                *
              
              i
    x
              
                .
              
              append
              
                (
              
              t
              
                )
              
              
    y
              
                .
              
              append
              
                (
              
              targetfun
              
                (
              
              t
              
                )
              
              
                )
              
              
plt
              
                .
              
              scatter
              
                (
              
              x
              
                ,
              
               y
              
                )
              
            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 夜夜撸夜夜爽 | 99热最新网址 | 日韩黄色网页 | 久热中文字幕在线 | 免费的色网站 | 欧美xxxx性xxxxx高清 | 日本一区二区三区高清不卡 | 精品入口麻豆 | 婷婷视频网 | 日本亚洲精品色婷婷在线影院 | 伊人超碰 | 日本欧美国产精品第一页久久 | 国产一区二区三区福利 | 亚洲综合色网站 | 99久久精品国产一区二区成人 | 国产一级一级毛片 | 日本一区二区不卡 | 天堂av资源| 精品国产精品久久 | 日韩国产精品一区二区三区 | 国产香蕉视频在线观看 | 国产精品91在线播放 | 欧美一级片免费看 | 中文字幕在亚洲第一在线 | 五月婷婷色综合 | 久久国产精品免费一区二区三区 | 久久久久久综合 | 免费观看国产大片资源视频 | 黑人性猛交xxxx乱大交一 | 久久久久久久国产精品电影 | 成人久久久久久久久 | 在线欧美日韩 | 欧美激情久久久 | 日本黄色高清网站 | 免费日本毛片 | 亚洲精品久久久久久一区 | 国产高清在线精品一区二区三区 | 99青青草| 国产美女自卫慰视频福利 | www.久久久 | 一级做一级爱a做片性视频视频 |