?? agent.java
字號:
for (f=0; f < privateParams.numfcasts; f++){ Forecast temp1=(Forecast)fcastList.elementAt(f); madv += Math.abs( temp1.variance - meanv); } madv = madv/privateParams.numfcasts; avstrength /= privateParams.numfcasts;// Loop to construct nnew new rules local.localasm.setStatus("正在執行遺傳算法:正在進行遺傳操作"); for (new1 = 0; new1 < privateParams.nnew; new1++){ boolean changed;//因為遺傳操作是按照概率進行的,所以,做一個標志是否進行了遺傳操作 changed = false; // Loop used if we force diversity do{ double varvalue, altvarvalue = 999999999; //制作一個新的規則,并讓他的各個變量取平均值 Forecast aNewForecast=new Forecast(privateParams.condbits); aNewForecast = createNewForecast(); aNewForecast.updateSpecfactor(); aNewForecast.strength=avstrength; //BFagent.m had equivalent of: [aNewForecast setVariance: [aNewForecast getSpecfactor]/[aNewForecast getStrength]]; aNewForecast.lastactive=currentTime; varvalue = privateParams.maxdev-avstrength+aNewForecast.specfactor; //if (varvalue < 0 ) raiseEvent(WarningMessage, "varvalue less than zero"); aNewForecast.variance=varvalue; Forecast temp1=(Forecast)fcastList.elementAt(0); altvarvalue = temp1.variance- madv; if ( varvalue < altvarvalue ){ aNewForecast.variance= altvarvalue; aNewForecast.strength=privateParams.maxdev - altvarvalue + aNewForecast.specfactor; } aNewForecast.lastactive=currentTime; //選擇父親,選擇算法由Tournament進行操作 do parent1 = Tournament(fcastList); while (parent1 == null); // 進行交叉操作 if (Math.random() < privateParams.pcrossover){ //選擇母親 do parent2 = Tournament(fcastList); while (parent2 == parent1 || parent2 == null) ; //開始交叉 aNewForecast=Crossover(aNewForecast,parent1,parent2); changed = true; }else{ //否則把父親規則拷貝給新的規則 aNewForecast=CopyRule(parent1); //進行變異操作 changed = Mutate(aNewForecast,changed); } //將新規則加入列表中 newList.addElement(aNewForecast); //?? were these not initialized in original?// } while (!changed); /* Replace while(0) with while(!changed) to force diversity */ } // Replace nnew of the weakest old rules by the new ones local.localasm.setStatus("正在執行遺傳算法:用新規則淘汰舊規則"); TransferFcastsFrom(newList,fcastList,rejectList,rejectIndex); local.localasm.setStatus("正在執行遺傳算法:強迫沒有被激活的規則產生多樣性"); Generalize(fcastList,avstrength); local.localasm.setStatus("正在執行遺傳算法:重新計算平均值"); // Compute average specificity int specificity = 0; //note here a "raw" for loop around the fcastList. I could create an index //and do the swarm thing, but I leave this here to keep myself humble. for (f = 0; f < privateParams.numfcasts; f++){ parent1 = (Forecast)fcastList.elementAt(f); specificity += parent1.specificity; } avspecificity = ((double) specificity)/(double)privateParams.numfcasts; local.localasm.setStatus("執行遺傳算法結束"); newList.removeAllElements(); rejectList.removeAllElements();}/*"This is a method that copies the instance variables out of one forecast object into another. It copies not only the bitvector of monitored conditions, but also the forecast value, strength, variance, specFactor, specificity, and so forth. The only deviation is that if the return from the original forecast's getCnt method (its count value) is equal to 0, then the strength of the copy is equal to the value of a static variable named minstrength."*/Forecast CopyRule(Forecast from){ Forecast to=new Forecast(privateParams.condbits); to.forecast=from.forecast; to.lforecast=from.lforecast; to.variance=from.variance; to.strength=from.strength; to.a=from.a; to.b=from.b; to.c=from.c; to.specfactor=from.specfactor; to.lastactive=from.lastactive; to.specificity=from.specificity; to.conditions=from.conditions; to.count=from.count; if (from.count==0)to.strength=minstrength; return to;}/*"Given a list of forecasts, find the worst ones and put them into apool of rejects. This method requires 2 inputs, the name of the rejectlist (actually, a Swarm Array) and the Array of forecasts. "*/void MakePool(Vector rejects,int indexes[],Vector list){ //制作一個池子,找出最弱適應度的若干規則 int top; int i,j = 0 ; Forecast aForecast; Forecast aReject=new Forecast(privateParams.condbits); top = -1; //pj: why not just start at 1 so we never worry about putting forecast 0 into the mix? //下面制作這個淘汰值,并且要按照適應度值從小到大進行排列 //找出npool條最弱適應度的規則 for ( i=0; i < privateParams.npool; i++ ){ //從原始列表中順序取出規則 aForecast=(Forecast)list.elementAt(i); for ( j=top; j >= 0 ; j--){//對現在的拒絕列表中的元素進行循環 aReject=(Forecast)(rejects.elementAt(j)); int k=indexes[j]; //如果發現拒絕的規則的強度大于當前順序取出的規則,則把這條拒絕規則往后移一位 //并且更新indexes里面的數值,因為該書組記錄的是被拒絕規則在原始規則列表中的索引值 if(aReject!=null&&aForecast.strength < aReject.strength){ if(rejects.size()>j+1){ rejects.removeElementAt(j+1); } rejects.insertElementAt(aReject,j+1); indexes[j+1]=k; }else{ break; } } //note j decrements at the end of this loop //把拒絕列表中的第一個元素用新的規則替換 if(rejects.size()>j+1){ rejects.removeElementAt(j+1); } //將循環到的新規則加入拒絕列表中 rejects.insertElementAt(aForecast,j+1); indexes[j+1]=i; top++; } //對list列表中剩下的規則進行循環,執行上面過程類似的操作 int t=i; for ( i=t; i < privateParams.numfcasts; i++){ aForecast=(Forecast)list.elementAt(i); Forecast temp1=(Forecast)rejects.elementAt(top); if ( aForecast.strength< temp1.strength) { for ( j = top-1; j >= 0; j--){ aReject=(Forecast)rejects.elementAt(j); int k=indexes[j]; if(aReject!=null&&aForecast.strength < aReject.strength){ if(rejects.size()>j+1){ //indexes.removeElementAt(j+1); rejects.removeElementAt(j+1); } indexes[j+1]=k; rejects.insertElementAt(aReject,j+1); }else{ break; } } } if(rejects.size()>j+1){ //indexes.removeElementAt(j+1); rejects.removeElementAt(j+1); } indexes[j+1]=i; rejects.insertElementAt(aForecast,j+1); //aForecast=(Forecast)rejects.elementAt(j+1); } //pj:note: we are not checking to see if forecast 0 is in here}/*------------------------------------------------------*//* Tournament *//*------------------------------------------------------*/Forecast Tournament(Vector list){ //Tournament選擇算子,隨機的從列表中選擇兩個規則,返回適應度比較大的那個。 int numfcasts=list.size(); Forecast candidate1 = (Forecast)list.elementAt((int)(Math.random()*numfcasts)); Forecast candidate2; do candidate2 = (Forecast)list.elementAt((int)(Math.random()*numfcasts)); while (candidate2 == candidate1); if (candidate1.strength> candidate2.strength) return candidate1; else return candidate2;}/*------------------------------------------------------*//* Mutate *//*------------------------------------------------------*/boolean Mutate(Forecast new1,boolean changed) /*變異操作*/{ int bit; double choice, temp; boolean bitchanged = false; bitchanged =changed; if (privateParams.pmutation > 0){ for (bit = 0; bit < privateParams.condbits; bit++){ if (Math.random() < privateParams.pmutation){ //執行變異操作,如果當前變異位值為0或1,則變異位相反的數或者2 if ( Integer.parseInt(new1.getConditionsbit(bit))<2){ if ((int)(Math.random()*3)> 1){ new1.maskConditionsbit(bit); new1.decrSpecificity(); } else{ new1.switchConditionsbit(bit); bitchanged = changed = true; } }else if ((int)(Math.random()*3)> 0){//否則變異為0或者1 new1.setConditionsbit(bit,String.valueOf((int)(Math.random()*2))); new1.incrSpecificity(); bitchanged = changed = true; } } } } /* mutate p+d coefficient */ //對系數的變異 choice = Math.random(); //如果是大變異,則新規則的a為隨機在區間中取值,否則用參數nhood進行變異,也就是取一個鄰域 if (choice < privateParams.plong){ new1.a=privateParams.a_min + privateParams.a_range*Math.random(); changed = true; }else if (choice < privateParams.plong + privateParams.pshort){ /* short jump = uniform within fraction nhood of range */ temp = new1.a + privateParams.a_range*privateParams.nhood*(Math.random()*2-1); if(temp > privateParams.a_max){ new1.a=privateParams.a_max; }else{ if(temp < privateParams.a_min){ new1.a=privateParams.a_min; }else{ new1.a=temp; } } changed = true; } /* mutate dividend coefficient */ //變異系數b,方法同上 choice = Math.random(); if (choice < privateParams.plong){ /* long jump = uniform distribution between min and max */ new1.b= privateParams.b_min + privateParams.b_range*Math.random(); changed = true; }else if (choice < privateParams.plong + privateParams.pshort){ /* short jump = uniform within fraction nhood of range */ temp = new1.b+privateParams.b_range*privateParams.nhood*(Math.random()*2-1); if(temp > privateParams.b_max){ new1.b=privateParams.b_max; }else{ if(temp < privateParams.b_min){ new1.b=privateParams.b_min; }else{ new1.b=temp; } } changed = true; } /* else leave alone */ /* mutate constant term */ //對c進行變異,方法同上 choice = Math.random(); if (choice < privateParams.plong){ /* long jump = uniform distribution between min and max */ new1.c= privateParams.c_min + privateParams.c_range*Math.random(); changed = true; }else if (choice < privateParams.plong + privateParams.pshort){ /* short jump = uniform within fraction nhood of range */ temp = new1.c + privateParams.c_range*privateParams.nhood*(Math.random()*2-1); if(temp > privateParams.c_max){ new1.c=privateParams.c_max; }else{ if(temp < privateParams.c_min){ new1.c=privateParams.c_min; }else{ new1.c=temp; } } changed = true; } /* else leave alone */ new1.count=0; //更新特定度 if (changed){ new1.updateSpecfactor(); } return(changed);}/*------------------------------------------------------*//* Crossover *//*------------------------------------------------------*/Forecast Crossover(Forecast newForecast,Forecast parent1,Forecast parent2) /*交叉操作,if部分是位串的交叉,then部分是系數的線性或者其他組合 * On the condition bits, Crossover() uses uniform crossover -- each * bit is chosen randomly from one parent or the other. * For the real-valued forecasting parameters, Crossover() does * one of three things: * 1. Choose a linear combination of the parents' parameters, * weighted by strength. * 2. Choose each parameter randomly from each parent. * 3. Choose one of the parents' parameters (all from one or all * from the other). * Method 1 is chosen with probability plinear, method 2 with * probability prandom, method 3 with probability 1-plinear-prandom. */{ /* Uniform crossover of condition bits */ int bit; // unsigned int *cond1, *cond2, *newcond; int word; double weight1, weight2, choice; newForecast.specificity=0; /*for (word = 0; word <privateParams->condwords; word++) newForecast.setConditionsBit(word,0);*/ //將父母的if部分的位串進行隨機混合 for (bit = 0; bit < privateParams.condbits; bit++){ if ((int)(Math.random()*2)== 0){ String value=parent1.getConditionsbit(bit); newForecast.setConditionsbit(bit,value); if (Integer.parseInt(value)<2) newForecast.incrSpecificity(); }else{ String value= parent2.getConditionsbit(bit); newForecast.setConditionsbit(bit,value); if (Integer.parseInt(value)<2) newForecast.incrSpecificity(); } } /* Select one crossover method for the forecasting parameters */ //按照不同的概率選擇交叉系數的方法 choice = Math.random(); if (choice < privateParams.plinear){ /* Crossover method 1 -- linear combination */ //線性加權求和 if(parent1.strength + parent2.strength!=0){ weight1 = parent1.strength / (parent1.strength + parent2.strength); }else{ weight1=0.5; } weight2 = 1.0-weight1; newForecast.a=weight1*parent1.a+weight2*parent2.a; newForecast.b=weight1*parent1.b+weight2*parent2.b; newForecast.c=weight1*parent1.c+weight2*parent2.c; }else if (choice < privateParams.plinear + privateParams.prandom){ /* Crossover method 2 -- randomly from each parent */ //隨機的從父母中選擇系數 if((int)(Math.random()*2)==0) newForecast.a=parent1.a; else newForecast.a=parent2.a;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -