⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agent.java

📁 在这个电脑中的虚拟市场中
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
  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 + -