資源簡介
使用蟻群算法實(shí)現(xiàn)了單維的01背包問題,自己寫的,可直接運(yùn)行AcoKP.java文件即可輸出結(jié)果,收斂速度還可以,可根據(jù)自己需要修改參數(shù)

代碼片段和文件信息
package?AcoKP;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.ListIterator;
/**
?*?AcoBP
?*?Author:?wychen
?*?Date:?2017/12/13
?*?Time:?9:08
?*/
public?class?AcoKP?{
????private?int?NC?=?200;//迭代次數(shù)
????private?int?antNum?=?20;//螞蟻數(shù)量
????private?float?Q?=?1.0f;?//用于控制信息素增量到適度范圍
????private?float?rho?=?0.1f;//蒸發(fā)系數(shù)
????private?float?alpha?=?3;??//信息素重要程度
????private?float?beta?=?2;???//啟發(fā)式因子重要程度
????private?float?eta;?//啟發(fā)式因子
????private?ArrayList?ants?=?new?ArrayList();?//蟻群
????//所有物品對象
????ArrayList?resArrayList?=?new?ArrayList();
????/**
?????*?初始化蟻群
?????*
?????*/
????public?void?intAnts(){
????????ants.clear();
????????for(int?i?=?0?;?i?????????????Ant?ant?=?new?Ant(0.0f0.0f);
????????????ants.add(ant);
????????}
????}
????/**
?????*?找出選取物體價值最優(yōu)的螞蟻
?????*
?????*/
????public?Ant?getBestAnt(ArrayList?ants){
????????Ant?bestAnt?=?ants.get(0);
????????for(Ant?ant?:?ants){
????????????if(ant.getResValue()?>?bestAnt.getResValue()){
????????????????bestAnt?=?ant;
????????????}
????????}
????????return?bestAnt;
????}
????/**
?????*?初始化物品重量、價值、信息素濃度
?????*
?????*/
????public?void?init(){
????????for?(int?i?=?0?;?i?????????????Res?res?=?new?Res(Constant.thing[i][1]Constant.thing[i][0]Constant.pheromone);
????????????res.setId(i);
????????????resArrayList.add(res);
????????}
????}
????/**
?????*?賭輪法選擇物品
?????*
?????*?@param?resList
?????*?@return
?????*/
????public?int?getMaxP(Ant?curAnt?ArrayList?resList){
????????ArrayList?canSelect?=?new?ArrayList();//可選擇物品列表
????????float?totalVAP?=?0.0f;
????????for(int?i?=?0?;?i?????????????Res?res?=?resList.get(i);
????????????if(!curAnt.isSelectedRes(i)){
????????????????eta?=?res.getValue()/res.getWeight();?//啟發(fā)信息
????????????????float?vap?=?(float)?(Math.pow(res.getPheromone()alpha)?*?Math.pow(etabeta));
????????????????totalVAP?+=?vap;
????????????????CanSelectRes?canSelectRes?=?new?CanSelectRes(res.getId()vap);
????????????????canSelect.add(canSelectRes);
????????????}
????????}
????????//計算每一個物品被選中的概率
????????ListIterator?iterator?=?canSelect.listIterator();
????????while(iterator.hasNext()){
????????????CanSelectRes?obj?=?iterator.next();
????????????obj.setP(obj.getVAP()/totalVAP);
????????}
????????//賭輪法獲取選取物品對象
????????float?rate?=?(float)Math.random();
????????ListIterator?iter?=?canSelect.listIterator();
????????while(iter.hasNext()){
????????????CanSelectRes?canRes?=?iter.next();
????????????if(rate?<=?canRes.getP()){
????????????????return?canRes.getId();
????????????}?else?{
????????????????rate?=?rate?-?canRes.getP();
????????????}
????????}
????????//人為返回最后一個物品,精度導(dǎo)致
//????????iter?=?canSelect.listIterator();
//????????while(iter.hasNext()){
//????????????CanSelectRes?canSelect1?=?iter.next();
//??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5943??2017-12-17?10:51??AcoKP\AcoKP.java
?????文件???????1261??2017-12-14?11:12??AcoKP\Ant.java
?????文件????????733??2017-12-15?08:40??AcoKP\CanSelectRes.java
?????文件????????826??2017-12-14?19:05??AcoKP\Constant.java
?????文件???????1517??2017-12-14?15:37??AcoKP\Res.java
?????目錄??????????0??2017-12-17?10:52??AcoKP
-----------?---------??----------?-----??----
????????????????10280????????????????????6
評論
共有 條評論