📄 agent.java
字号:
if((int)(Math.random()*2)==0) newForecast.b=parent1.b; else newForecast.b=parent2.b; if((int)(Math.random()*2)==0) newForecast.c=parent1.c; else newForecast.c=parent2.c; }else{ /* Crossover method 3 -- all from one parent */ //从父母的系数中进行直接拷贝 if ((int)(Math.random()*2)==0){ newForecast.a=parent1.a; newForecast.b=parent1.b; newForecast.c=parent1.c; }else{ newForecast.a=parent2.a ; newForecast.b=parent2.b; newForecast.c=parent2.c; } } { //This is just error checking! String newcond; int specificity=0; newForecast.count=0; // call it new in any case newForecast.updateSpecfactor(); newForecast.strength=0.5*(parent1.strength + parent2.strength); //pj: next steps are purely diagnostic! newcond = newForecast.conditions; for (bit = 0; bit < privateParams.condbits; bit++) if ( newcond.charAt(bit) != '2' ){ specificity++; } } return newForecast;}/*------------------------------------------------------*//* TransferFcasts *//*------------------------------------------------------*/void TransferFcastsFrom(Vector newlist,Vector fcastlist, Vector rejects,int indexes[]){ //用新规则代替旧规则,选择的标准是在pool中找到最接近新规则那个规则并替换掉 Forecast aForecast; Forecast toDieForecast; int size=newlist.size(); for ( int i=0;i<size;i++){ aForecast=(Forecast)newlist.elementAt(i); //找出被替换掉的索引,进行替换 //toDieForecast = GetMort(aForecast, rejects); int k = GetMort(aForecast,rejects,indexes); //toDieForecast = CopyRule(aForecast); fcastlist.removeElementAt(k); fcastlist.addElement(aForecast); }}/*------------------------------------------------------*//* GetMort *//*------------------------------------------------------*/int GetMort(Forecast new1,Vector rejects,int indexes[]) /* GetMort() selects one of the npool weak old fcasts to replace * with a newly generated rule. It pays no attention to strength, * but looks at similarity of the condition bits -- like tournament * selection, we pick two candidates at random and choose the one * with the MORE similar bitstring to be replaced. This maintains * more diversity. */{ //找出rejects列表中与规则new1最相近的一跳 String cond1,cond2,newcond; int numrejects, r1, r2, word, bitmax = 0; int bit, different1, different2, temp1, temp2; //Forecast aReject; int result; numrejects = privateParams.npool; //随机的寻找被替换的两条规则,比较她们哪个与new1最相近 do{ r1 = (int)(Math.random()*numrejects); }while ( rejects.elementAt(r1)== null ); do{ r2 = (int)(Math.random()*numrejects); }while (r1 == r2 || rejects.elementAt(r2) == null); Forecast temp3=(Forecast)rejects.elementAt(r1); Forecast temp4=(Forecast)rejects.elementAt(r2); cond1 = temp3.conditions; cond2 = temp4.conditions;; newcond = new1.conditions; different1 = 0; different2 = 0; bitmax = 16; for(bit=0;bit<privateParams.condbits;bit++){ if(cond1.charAt(bit)!=newcond.charAt(bit)){ different1++; } if(cond2.charAt(bit)!=newcond.charAt(bit)){ different2++; } } /* * This is the big decision whether to push diversity by selecting rules * to leave. Original version is 1 which choses the least different rules * to leave. Version 2 choses at random, and version 3 choses the least * frequently used rule. */ if (different1 < different2){ result=indexes[r1]; }else{ result=indexes[r2]; } return result;//返回被替换的索引}/*------------------------------------------------------*//* Generalize *//*------------------------------------------------------*/void Generalize(Vector list,double avgstrength) /*强迫懒惰的规则,进行不在乎的变异 * Each forecast that hasn't be used for longtime is generalized by * turning a fraction genfrac of the 0/1 bits to don't-cares. */{ Forecast aForecast; int f; int bit, j; boolean changed; currentTime = getCurrentTime(); for (f = 0; f < privateParams.numfcasts; f++){ aForecast = (Forecast)list.elementAt(f); if (currentTime - aForecast.lastactive > privateParams.longtime){ //如果该条规则已经有足够长时间没有被激活了,就进行变异,比例按照aForecast.specificity*privateParams.genfrac计算 changed = false; j = (int)(Math.ceil(aForecast.specificity*privateParams.genfrac)); while(j>0&&aForecast.specificity>0){ bit =(int)(Math.random()*privateParams.condbits); if ( Integer.parseInt(aForecast.getConditionsbit(bit))<2){ aForecast.maskConditionsbit(bit); aForecast.decrSpecificity(); changed = true; j--; } } if (changed){ double varvalue; aForecast.count=0; aForecast.lastactive=currentTime; aForecast.updateSpecfactor(); varvalue = privateParams.maxdev - avgstrength + aForecast.specfactor; if (varvalue >0 ){ aForecast.variance=varvalue; } aForecast.strength=avgstrength; } } }}/*"This is a general utility method for Swarm lists. It removes all objects form the "outputList" and copies the elements from list into it. It does not actually destroy any elements from either list, it just updates references."*/void copyList(Vector list,Vector outputList){ outputList.removeAllElements(); int size=list.size(); for( int i=0;i<size;i++ ){ Forecast aforecast=(Forecast)list.elementAt(i); outputList.addElement(aforecast); }}void GetRule(Vector list,String out[],int index){ Forecast aForecast=(Forecast)(list.elementAt(index)); out[0]=Float.toString((float)(aForecast.forecast)); out[5]=Float.toString((float)(aForecast.variance)); out[1]=Float.toString((float)(aForecast.strength)); out[2]=Float.toString((float)(aForecast.a)); out[3]=Float.toString((float)(aForecast.b)); out[4]=Float.toString((float)(aForecast.c)); out[7]=aForecast.conditions; out[6]=Integer.toString(aForecast.count);}double GetStrength(Vector list,int index){ Forecast aForecast=(Forecast)(list.elementAt(index)); return aForecast.strength;}void getBitList(int bits[]){ int condbits = privateParams.condbits; for(int i=0; i < condbits; i++) bits[i]=i;}void getProbList(double ProbList[]){ int condbits = privateParams.condbits; double tmp=privateParams.newfcastspec;//得到2的比例 for(int i=0; i < condbits; i++) ProbList[i]=Math.random()*tmp;}public Agent(AsmModel lcl) { //该类的初始化函数 local=lcl; fcastList=new Vector(); activeList=new Vector(); oldActiveList=new Vector(); History=new Vector();}int getCurrentTime(){ return local.modelTime;}public void RecordHistory(int currentTime){ //将agent的状态变量进行记录 AgentVariants agv=new AgentVariants(); agv.cash=cash; agv.avspecificity=avspecificity; agv.currentTime=currentTime; agv.demand=demand; agv.divisor=divisor; agv.forecast=forecast; agv.gacount=gacount; agv.global_mean=global_mean; agv.offset=offset; agv.pdcoeff=pdcoeff; agv.position=position; agv.profit=profit; agv.realDeviation=realDeviation; agv.variance=variance; agv.wealth=wealth; History.addElement(agv); if(History.size()>WorldVariants.cycleMax){ History.removeElementAt(0); }}}//预测规则类,分为两部分:即0,1,2组成的if部分的位串和a,b,c系数组成的then部分,//他们都能根据a(p+d)+b*d+c进行p+d的预测class Forecast{ public double forecast; /*预测值" this forecast of return"*/ public double lforecast; /*上一期的预测值" previous forecast"*/ public double variance; /*方差" variance of this forecast"*/ public double strength; /*强度(适应度)"strength=maxdev - variance +specfactor. This was the original sfsm specification, not the ASM-2.0 specification"*/ public double a; /*" (price + dividend) coefficient"*/ public double b; /*" dividend coefficient "*/ public double c; /*" constant term"*/ public double specfactor; /*特异度系数,对总强度做出贡献" specificty=(condbits - nnulls - specificity)* bitcost. "*/ public double bitcost; /*一个权数" cost of using bits in forecasts"*/ public String conditions; /*规则的if部分" a BitVector object that holds records on which conditions in the world are being monitored by this forecast object"*/ public int lastactive; /*上一次被激活的时间" last time period in which this forecast was active"*/ public int specificity; /*" specificity "*/ public int count; /*总共被激活的次数" number of times this forecast has been active"*/ public int condbits; /*if部分的位数"number of bits of information monitored by this forecast"*/ public int nnulls; /*无用位的位数"number of 'unused' bits that remain in the allocated vector after the 'condbits' bits have been used"*/ /*"A BFCast is an object that holds all the forcecasting components of a bit forecast. That means it has a BitVector (a thing called "conditions" in the code that keeps track of which world bits are being monitored) as well as other coefficients that are used to calculate forecasts. It has instance variables that record when the rule was last used, how many times it has been used, how accururate its predictions are, and so forth."*/public Forecast(int cond){ condbits=cond; if (condbits==0 ){return;} forecast= 0.0; count = 0; lastactive=1; specificity = 0; variance = 999999999; conditions=""; if(condbits<=60){ //为了加速 conditions="22222222222222222222222222222222222222222222222222222222222222"; }else{ for(int i=0;i<condbits;i++){ conditions=conditions+"2"; } }}/*"Rather than individually set bits one by one, we might want to set all of them at once. That means we pass in a pointer to an array of words that is the "right size" for all the bits."*/void setConditions(String x){ conditions=x;}/*返回if部分的值*/String getConditions(){ return conditions;}/*"Sets the value of a bit in the conditions"*/void setConditionsbit(int bit,String x){ String temp1=conditions.substring(0,bit); String temp2=conditions.substring(bit+1); conditions=temp1+x+temp2;}/*"Returns 0,1,or 2, for a given bit in the conditions records"*/String getConditionsbit(int bit){ return conditions.substring(bit,bit+1);}void maskConditionsbit(int bit){ //将指定位变为2 setConditionsbit(bit,"2");}/*"Change a YES to a NO, and vice versa"*/void switchConditionsbit(int bit){ //对指定位进行变异 String s=getConditionsbit(bit); if(s=="0")s="1"; else if(s=="1")s="0"; setConditionsbit(bit,s);}/*"Update the spec factor of this forecast. That means calculate:specfactor= (condbits - nnulls - specificity)* bitcost"*/void updateSpecfactor(){ //更新系数specfactor specfactor = (condbits - nnulls - specificity)* bitcost; //follows bfagent.m}/*"Raise the specificity value of this forecast by one unit"*/void incrSpecificity(){ specificity++;}/*"Reduce the specificity value of this forecast by one unit"*/void decrSpecificity(){ specificity--;}/*"Increment this forecast's count variable"*/int incrCount(){ return ++count;}/*计算新的预测值"Calculate new forecast on basis of price and dividend information"*/double updateForecastPrice(double price,double dividend){ lforecast = forecast; forecast = a* (price+dividend) + b*dividend + c; return forecast;}/*"Given an input forecast object, systematically ask it for all its IVARs and replace current settings with them."*/void copyEverythingFrom(Forecast from){ forecast = from.forecast; lforecast = from.lforecast; variance = from.variance; strength = from.strength; a= from.a; b= from.b; c= from.c; specfactor = from.specfactor; lastactive = from.lastactive; specificity = from.specificity; count = from.count; conditions=from.conditions;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -