99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀優(yōu)化算法—人工蜂群算法(ABC)
優(yōu)化算法—人工蜂群算法(ABC)
2017-03-23
收藏

優(yōu)化算法—人工蜂群算法(ABC)

一、人工蜂群算法的介紹
    人工蜂群算法(Artificial Bee Colony, ABC)是由Karaboga于2005年提出的一種新穎的基于群智能的全局優(yōu)化算法,其直觀背景來源于蜂群的采蜜行為,蜜蜂根據(jù)各自的分工進(jìn)行不同的活動(dòng),并實(shí)現(xiàn)蜂群信息的共享和交流,從而找到問題的最優(yōu)解。人工蜂群算法屬于群智能算法的一種。
二、人工蜂群算法的原理
    1、原理
        標(biāo)準(zhǔn)的ABC算法通過模擬實(shí)際蜜蜂的采蜜機(jī)制將人工蜂群分為3類: 采蜜蜂、觀察蜂和偵察蜂。整個(gè)蜂群的目標(biāo)是尋找花蜜量最大的蜜源。在標(biāo)準(zhǔn)的ABC算法中,采蜜蜂利用先前的蜜源信息尋找新的蜜源并與觀察蜂分享蜜源信息;觀察蜂在蜂房中等待并依據(jù)采蜜蜂分享的信息尋找新的蜜源;偵查蜂的任務(wù)是尋找一個(gè)新的有價(jià)值的蜜源,它們在蜂房附近隨機(jī)地尋找蜜源。
        假設(shè)問題的解空間是維的,采蜜蜂與觀察蜂的個(gè)數(shù)都是,采蜜蜂的個(gè)數(shù)或觀察蜂的個(gè)數(shù)與蜜源的數(shù)量相等。則標(biāo)準(zhǔn)的ABC算法將優(yōu)化問題的求解過程看成是在維搜索空間中進(jìn)行搜索。每個(gè)蜜源的位置代表問題的一個(gè)可能解,蜜源的花蜜量對(duì)應(yīng)于相應(yīng)的解的適應(yīng)度。一個(gè)采蜜蜂與一個(gè)蜜源是相對(duì)應(yīng)的。與第個(gè)蜜源相對(duì)應(yīng)的采蜜蜂依據(jù)如下公式尋找新的蜜源:
其中是區(qū)間上的隨機(jī)數(shù),。標(biāo)準(zhǔn)的ABC算法將新生成的可能解與原來的解作比較,并采用貪婪選擇策略保留較好的解。每一個(gè)觀察蜂依據(jù)概率選擇一個(gè)蜜源,概率公式為

其中,是可能解的適應(yīng)值。對(duì)于被選擇的蜜源,觀察蜂根據(jù)上面概率公式搜尋新的可能解。當(dāng)所有的采蜜蜂和觀察蜂都搜索完整個(gè)搜索空間時(shí),如果一個(gè)蜜源的適應(yīng)值在給定的步驟內(nèi)(定義為控制參數(shù)“l(fā)imit”) 沒有被提高, 則丟棄該蜜源,而與該蜜源相對(duì)應(yīng)的采蜜蜂變成偵查蜂,偵查蜂通過已下公式搜索新的可能解。

其中,r是區(qū)間上的隨機(jī)數(shù),是第d維的下界和上界。
    2、流程
初始化;
重復(fù)以下過程:
將采蜜蜂與蜜源一一對(duì)應(yīng),根據(jù)上面第一個(gè)公式更新蜜源信息,同時(shí)確定蜜源的花蜜量;
觀察蜂根據(jù)采蜜蜂所提供的信息采用一定的選擇策略選擇蜜源,根據(jù)第一個(gè)公式更新蜜源信息,同時(shí)確定蜜源的花蜜量;
確定偵查蜂,并根據(jù)第三個(gè)公式尋找新的蜜源;數(shù)據(jù)分析師培訓(xùn)
記憶迄今為止最好的蜜源;
判斷終止條件是否成立;
三、人工蜂群算法用于求解函數(shù)優(yōu)化問題
    對(duì)于函數(shù)

其中。
代碼:
[cpp] view plain copy 在CODE上查看代碼片派生到我的代碼片
#include<iostream>  
#include<time.h>  
#include<stdlib.h>  
#include<cmath>  
#include<fstream>  
#include<iomanip>  
using namespace std;  
 
const int NP=40;//種群的規(guī)模,采蜜蜂+觀察蜂  
const int FoodNumber=NP/2;//食物的數(shù)量,為采蜜蜂的數(shù)量  
const int limit=20;//限度,超過這個(gè)限度沒有更新采蜜蜂變成偵查蜂  
const int maxCycle=10000;//停止條件  
 
/*****函數(shù)的特定參數(shù)*****/  
const int D=2;//函數(shù)的參數(shù)個(gè)數(shù)  
const double lb=-100;//函數(shù)的下界   
const double ub=100;//函數(shù)的上界  
 
double result[maxCycle]={0};  
 
/*****種群的定義****/  
struct BeeGroup  
{  
    double code[D];//函數(shù)的維數(shù)  
    double trueFit;//記錄真實(shí)的最小值  
    double fitness;  
    double rfitness;//相對(duì)適應(yīng)值比例  
    int trail;//表示實(shí)驗(yàn)的次數(shù),用于與limit作比較  
}Bee[FoodNumber];  
 
BeeGroup NectarSource[FoodNumber];//蜜源,注意:一切的修改都是針對(duì)蜜源而言的  
BeeGroup EmployedBee[FoodNumber];//采蜜蜂  
BeeGroup OnLooker[FoodNumber];//觀察蜂  
BeeGroup BestSource;//記錄最好蜜源  
 
/*****函數(shù)的聲明*****/  
double random(double, double);//產(chǎn)生區(qū)間上的隨機(jī)數(shù)  
void initilize();//初始化參數(shù)  
double calculationTruefit(BeeGroup);//計(jì)算真實(shí)的函數(shù)值  
double calculationFitness(double);//計(jì)算適應(yīng)值  
void CalculateProbabilities();//計(jì)算輪盤賭的概率  
void evalueSource();//評(píng)價(jià)蜜源  
void sendEmployedBees();  
void sendOnlookerBees();  
void sendScoutBees();  
void MemorizeBestSource();  
 
 
/*******主函數(shù)*******/  
int main()  
{  
    ofstream output;  
    output.open("dataABC.txt");  
 
    srand((unsigned)time(NULL));  
    initilize();//初始化  
    MemorizeBestSource();//保存最好的蜜源  
          
    //主要的循環(huán)  
    int gen=0;  
    while(gen<maxCycle)  
    {  
        sendEmployedBees();  
              
        CalculateProbabilities();  
              
        sendOnlookerBees();  
              
        MemorizeBestSource();  
              
        sendScoutBees();  
              
        MemorizeBestSource();  
 
        output<<setprecision(30)<<BestSource.trueFit<<endl;  
              
        gen++;  
    }  
      
    output.close();  
    cout<<"運(yùn)行結(jié)束!!"<<endl;  
    return 0;  
}  
 
/*****函數(shù)的實(shí)現(xiàn)****/  
double random(double start, double end)//隨機(jī)產(chǎn)生區(qū)間內(nèi)的隨機(jī)數(shù)  
{     
    return start+(end-start)*rand()/(RAND_MAX + 1.0);  
}  
 
void initilize()//初始化參數(shù)  
{  
    int i,j;  
    for (i=0;i<FoodNumber;i++)  
    {  
        for (j=0;j<D;j++)  
        {  
            NectarSource[i].code[j]=random(lb,ub);  
            EmployedBee[i].code[j]=NectarSource[i].code[j];  
            OnLooker[i].code[j]=NectarSource[i].code[j];  
            BestSource.code[j]=NectarSource[0].code[j];  
        }  
        /****蜜源的初始化*****/  
        NectarSource[i].trueFit=calculationTruefit(NectarSource[i]);  
        NectarSource[i].fitness=calculationFitness(NectarSource[i].trueFit);  
        NectarSource[i].rfitness=0;  
        NectarSource[i].trail=0;  
        /****采蜜蜂的初始化*****/  
        EmployedBee[i].trueFit=NectarSource[i].trueFit;  
        EmployedBee[i].fitness=NectarSource[i].fitness;  
        EmployedBee[i].rfitness=NectarSource[i].rfitness;  
        EmployedBee[i].trail=NectarSource[i].trail;  
        /****觀察蜂的初始化****/  
        OnLooker[i].trueFit=NectarSource[i].trueFit;  
        OnLooker[i].fitness=NectarSource[i].fitness;  
        OnLooker[i].rfitness=NectarSource[i].rfitness;  
        OnLooker[i].trail=NectarSource[i].trail;  
    }  
    /*****最優(yōu)蜜源的初始化*****/  
    BestSource.trueFit=NectarSource[0].trueFit;  
    BestSource.fitness=NectarSource[0].fitness;  
    BestSource.rfitness=NectarSource[0].rfitness;  
    BestSource.trail=NectarSource[0].trail;  
}  
 
double calculationTruefit(BeeGroup bee)//計(jì)算真實(shí)的函數(shù)值  
{  
    double truefit=0;  
    /******測試函數(shù)1******/  
    truefit=0.5+(sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))-0.5)  
        /((1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*(1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1])));  
 
    return truefit;  
}  
 
double calculationFitness(double truefit)//計(jì)算適應(yīng)值  
{  
    double fitnessResult=0;  
    if (truefit>=0)  
    {  
        fitnessResult=1/(truefit+1);  
    }else  
    {  
        fitnessResult=1+abs(truefit);  
    }  
    return fitnessResult;  
}  
 
void sendEmployedBees()//修改采蜜蜂的函數(shù)  
{  
    int i,j,k;  
    int param2change;//需要改變的維數(shù)  
    double Rij;//[-1,1]之間的隨機(jī)數(shù)  
    for (i=0;i<FoodNumber;i++)  
    {  
          
        param2change=(int)random(0,D);//隨機(jī)選取需要改變的維數(shù)  
 
        /******選取不等于i的k********/  
        while (1)  
        {  
            k=(int)random(0,FoodNumber);  
            if (k!=i)  
            {  
                break;  
            }  
        }  
 
        for (j=0;j<D;j++)  
        {  
            EmployedBee[i].code[j]=NectarSource[i].code[j];  
        }  
 
        /*******采蜜蜂去更新信息*******/  
        Rij=random(-1,1);  
        EmployedBee[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);  
        /*******判斷是否越界********/  
        if (EmployedBee[i].code[param2change]>ub)  
        {  
            EmployedBee[i].code[param2change]=ub;  
        }  
        if (EmployedBee[i].code[param2change]<lb)  
        {  
            EmployedBee[i].code[param2change]=lb;  
        }  
        EmployedBee[i].trueFit=calculationTruefit(EmployedBee[i]);  
        EmployedBee[i].fitness=calculationFitness(EmployedBee[i].trueFit);  
 
        /******貪婪選擇策略*******/  
        if (EmployedBee[i].trueFit<NectarSource[i].trueFit)  
        {  
            for (j=0;j<D;j++)  
            {  
                NectarSource[i].code[j]=EmployedBee[i].code[j];  
            }  
            NectarSource[i].trail=0;  
            NectarSource[i].trueFit=EmployedBee[i].trueFit;  
            NectarSource[i].fitness=EmployedBee[i].fitness;  
        }else  
        {  
            NectarSource[i].trail++;  
        }  
    }  
}  
 
void CalculateProbabilities()//計(jì)算輪盤賭的選擇概率  
{  
    int i;  
    double maxfit;  
    maxfit=NectarSource[0].fitness;  
    for (i=1;i<FoodNumber;i++)  
    {  
        if (NectarSource[i].fitness>maxfit)  
            maxfit=NectarSource[i].fitness;  
    }  
      
    for (i=0;i<FoodNumber;i++)  
    {  
        NectarSource[i].rfitness=(0.9*(NectarSource[i].fitness/maxfit))+0.1;  
    }  
}  
 
void sendOnlookerBees()//采蜜蜂與觀察蜂交流信息,觀察蜂更改信息  
{  
    int i,j,t,k;  
    double R_choosed;//被選中的概率  
    int param2change;//需要被改變的維數(shù)  
    double Rij;//[-1,1]之間的隨機(jī)數(shù)  
    i=0;  
    t=0;  
    while(t<FoodNumber)  
    {  
          
        R_choosed=random(0,1);  
        if(R_choosed<NectarSource[i].rfitness)//根據(jù)被選擇的概率選擇  
        {          
            t++;  
            param2change=(int)random(0,D);  
              
            /******選取不等于i的k********/  
            while (1)  
            {  
                k=(int)random(0,FoodNumber);  
                if (k!=i)  
                {  
                    break;  
                }  
            }  
 
            for(j=0;j<D;j++)  
            {  
                OnLooker[i].code[j]=NectarSource[i].code[j];  
            }  
              
            /****更新******/  
            Rij=random(-1,1);  
            OnLooker[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);  
              
            /*******判斷是否越界*******/  
            if (OnLooker[i].code[param2change]<lb)  
            {  
                OnLooker[i].code[param2change]=lb;  
            }  
            if (OnLooker[i].code[param2change]>ub)  
            {     
                OnLooker[i].code[param2change]=ub;  
            }  
            OnLooker[i].trueFit=calculationTruefit(OnLooker[i]);  
            OnLooker[i].fitness=calculationFitness(OnLooker[i].trueFit);  
              
            /****貪婪選擇策略******/  
            if (OnLooker[i].trueFit<NectarSource[i].trueFit)  
            {  
                for (j=0;j<D;j++)  
                {  
                    NectarSource[i].code[j]=OnLooker[i].code[j];  
                }  
                NectarSource[i].trail=0;  
                NectarSource[i].trueFit=OnLooker[i].trueFit;  
                NectarSource[i].fitness=OnLooker[i].fitness;  
            }else  
            {  
                NectarSource[i].trail++;  
            }  
        }   
        i++;  
        if (i==FoodNumber)  
        {  
            i=0;  
        }  
    }  
}  
 
 
/*******只有一只偵查蜂**********/  
void sendScoutBees()//判斷是否有偵查蜂的出現(xiàn),有則重新生成蜜源  
{  
    int maxtrialindex,i,j;  
    double R;//[0,1]之間的隨機(jī)數(shù)  
    maxtrialindex=0;  
    for (i=1;i<FoodNumber;i++)  
    {  
        if (NectarSource[i].trail>NectarSource[maxtrialindex].trail)  
        {  
            maxtrialindex=i;  
        }  
    }  
    if(NectarSource[maxtrialindex].trail>=limit)  
    {  
        /*******重新初始化*********/  
        for (j=0;j<D;j++)  
        {  
            R=random(0,1);  
            NectarSource[maxtrialindex].code[j]=lb+R*(ub-lb);  
        }  
        NectarSource[maxtrialindex].trail=0;  
        NectarSource[maxtrialindex].trueFit=calculationTruefit(NectarSource[maxtrialindex]);  
        NectarSource[maxtrialindex].fitness=calculationFitness(NectarSource[maxtrialindex].trueFit);  
    }  
}  
 
void MemorizeBestSource()//保存最優(yōu)的蜜源  
{  
    int i,j;  
    for (i=1;i<FoodNumber;i++)  
    {  
        if (NectarSource[i].trueFit<BestSource.trueFit)  
        {  
            for (j=0;j<D;j++)  
            {  
                BestSource.code[j]=NectarSource[i].code[j];  
            }  
            BestSource.trueFit=NectarSource[i].trueFit;  
        }  
    }  
}  

收斂曲線:

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }