欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 欧美激情a∨在线视频播放 中文字幕亚洲图片 | 国产成人综合95精品视频免费 | 91精品国产91久久久久久 | 国产精品中文在线 | 九九久久久| 久久精品一区二区三区四区 | 国产精品中文 | 91一区二区三区在线观看 | 日韩一区精品 | 亚洲一区 中文字幕 | 久久免费看少妇高潮A片JA小说 | 亚洲天堂视频在线观看 | 黄毛片| 日韩爽爽爽视频免费播放 | 深夜做爰性大片中文 | 欧美不卡视频一区发布 | 免费看一区二区三区 | 精品一区二区高清在线观看 | 日本不卡一区 | 美女视频黄色片 | 亚洲最大在线 | 欧美2区 | a在线v | 欧洲男女下面进出的视频 | 99精品丰满人妻无码A片 | 精精国产xxxx视频在线 | www.色黄| 狠狠色丁香婷婷综合久久来 | 在线不卡一区 | 欧美精品久久久久久久久久 | 免费看那种视频 | 免费看黄在线网站 | 亚洲人成亚洲人成在线观看 | 久久精品成人免费国产片桃视频 | www.日日日 | 特级黄视频 | 亚洲无吗在线视频 | 日韩成人一区 | 久久色网 | 欧美福利一区二区三区 | 色呦呦在线免费观看 |