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

📄 bfagent.java

📁 ASMUML.zip一个人工股市的建模资料,主要是针对asm2.2的建模,欢迎大家下载
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    {      aForecast = (BFCast)oldActiveList.get(i);      aForecast.setLforecast( aForecast.getForecast());      }  switch (privateParams.condwords) {  case 1:    for( int i=0; i<fcastList.size(); i++ )    {     aForecast = (BFCast)fcastList.get(i);      if ( changeIntToBoolean(aForecast.getConditionsWord(0) & worldvalues.getConditionsWord(0)) )	 { 	   continue ;	 }      activeList.add( aForecast);    }    break;    case 2:      //pj: here is how it used to be in ASM-2.0//      real1 = worldvalues[1];//      for (fptr = fcast; fptr < topfptr; fptr++)//        {//  	if (fptr->conditions[0] & real0) continue;//  	if (fptr->conditions[1] & real1) continue;//  	*nextptr = fptr;//  	nextptr = &fptr->next;//        }    for( int i=0; i<fcastList.size(); i++ )    {     aForecast = (BFCast)fcastList.get(i);      if ( changeIntToBoolean(aForecast.getConditionsWord(0) & worldvalues.getConditionsWord(0)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(1) & worldvalues.getConditionsWord(1)) )	 { 	   continue ;	 }      activeList.add( aForecast);    }      break; case 3:    for( int i=0; i<fcastList.size(); i++ )    {     aForecast = (BFCast)fcastList.get(i);      if ( changeIntToBoolean(aForecast.getConditionsWord(0) & worldvalues.getConditionsWord(0)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(1) & worldvalues.getConditionsWord(1)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(2) & worldvalues.getConditionsWord(2)) )	 { 	   continue ;	 }      activeList.add( aForecast);    }      break;  case 4:    for( int i=0; i<fcastList.size(); i++ )    {     aForecast = (BFCast)fcastList.get(i);      if ( changeIntToBoolean(aForecast.getConditionsWord(0) & worldvalues.getConditionsWord(0)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(1) & worldvalues.getConditionsWord(1)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(2) & worldvalues.getConditionsWord(2)) )	 { 	   continue ;	 }       if ( changeIntToBoolean(aForecast.getConditionsWord(3) & worldvalues.getConditionsWord(3)) )	 { 	   continue ;	 }      activeList.add( aForecast);    }      break;    case 5:    for( int i=0; i<fcastList.size(); i++ )    {     aForecast = (BFCast)fcastList.get(i);      if ( changeIntToBoolean(aForecast.getConditionsWord(0) & worldvalues.getConditionsWord(0)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(1) & worldvalues.getConditionsWord(1)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(2) & worldvalues.getConditionsWord(2)) )	 { 	   continue ;	 }       if ( changeIntToBoolean(aForecast.getConditionsWord(3) & worldvalues.getConditionsWord(3)) )	 { 	   continue ;	 }      if ( changeIntToBoolean(aForecast.getConditionsWord(4) & worldvalues.getConditionsWord(4)) )	 { 	   continue ;	 }      activeList.add( aForecast);    }      break;    }if (privateParams.MAXCONDBITS > 5*16)  System.out.println("error Too many condition bits (MAXCONDBITS)");   //pj??? There ought to be a "default" action here for other cases.  /*This is an alternative implementation of the same as preceeding.It is so much cuter in my view.  I wrote it before I understood thefact that the World gives back 10 for yes and agent has 01 for yes, soyou have to be careful with it. Note the bitmath here, that appearsas it used to throughout the BFagent class.    index=[ fcastList begin: [self getZone]];    for ( aForecast=[index next]; [index getLoc]==Member; aForecast=[index next] )    {      unsigned int flag=0;      unsigned int trit;      unsigned int predictedvalue=999;      trit=-1;      while ( flag == 0 && ++trit < privateParams-> condbits )	    {	      if ( (predictedvalue=[aForecast getConditionsbit: trit]) != 0 )		{		  //if ( predictedvalue == ((worldvalues[WORD(trit)] >> ((trit%16)*2))&3) )		  if (  predictedvalue == [worldvalues getConditionsbit: trit] )		    {		      flag=1;		    }		}	   //       fprintf(stderr,"trit:%d flag: %d  predictionl=%d,  world=%d\n",	      //  			trit, flag, predictedvalue,	      //  			extractvalue(worldvalues,trit) );	         }	    if ( flag!=1 )  [activeList addLast: aForecast];        }    [index drop]; */  return this;}/*"Currently does nothing, used only if their are ANNagents"*/public Object getInputValues(){  return this;}/*"Currently does nothing, used only if their are ANNagents"*/public Object feedForward(){  return this;}public double getDemandAndSlope$forPrice ( double slope ,double trialprice)  /*" Returns the agent's requested bid (if >0) or offer (if <0) using* best (or mean) linear forecast chosen by -prepareForTrading. The* forecast is given by   forecast = pdcoeff*(trialprice+dividend) + offset* where pdcoeff and offset are set by -prepareForTrading.A risk aversion computation gives a target holding, and itsderivative ("slope") with respect to price.  The slope is calculatedas the linear approximated response of a change in price on thetraders' demand at time t, based on the change in the forecastaccording to the currently active linear rule. "*/{  forecast = (trialprice + dividend)*pdcoeff + offset;  if (forecast >= 0.0)    {      demand = -((trialprice*intratep1 - forecast)/divisor + position);      slope = (pdcoeff-intratep1)/divisor;    }  else    {      forecast = 0.0;      demand = - (trialprice*intratep1/divisor + position);      slope = -intratep1/divisor;    }  // Clip bid or offer at "maxbid".  This is done to avoid problems when  // the variance of the forecast becomes very small, thought it's not clear  // that this is the best solution.  if (demand > privateParams.maxbid)    {      demand = privateParams.maxbid;      slope = 0.0;    }  else if (demand < -privateParams.maxbid)    {      demand = -privateParams.maxbid;      slope = 0.0;    }  super.constrainDemand(slope,trialprice);  return demand;}/*"Return agent's forecast"*/public double getRealForecast(){  return forecast;}/*" Now update the variance and strength of all the forecasts that  were active in the previous period, since now we know how they  performed. This method causes an update of price/dividend  information from the world, then it measures how far off each  forecast was and puts the square of that "deviance" measure into the  forecast with the forecast's setVariance: method. Each forecast in  the active list is told to update its forecast.  It also updates the  instance variable variance, which is calculated here as an  exponentially weignted moving average of that forecast's  squared-error (variance).  Inside the code of updatePerformance,  there is a description of the strength formula that is used, and how  the formula now matches the formula used in the original sfsm,  rather than ASM-2.0. "*/public Object updatePerformance(){  //pj: register struct BF_fcast *fptr;  BFCast aForecast;  double deviation, ftarget, tauv, a, b, c, av, bv, maxdev;  // Precompute things for speed  tauv = privateParams.tauv;  a = 1.0/tauv;  b = 1.0-a;  // special rates for variance  // We often want this to be different from tauv  // PARAM:  100. should be a parameter  BL  av = 1.0/(double)100.0;  bv = 1.0-av;    /* fixed variance if tauv at max */  if (tauv == 100000)    {      a = 0.0;      b = 1.0;      av = 0.0;      bv = 1.0;    }  maxdev = privateParams.maxdev;// Update global mean (p+d) and our variance  this.getPriceFromWorld();  ftarget = price + dividend;// Update global mean (p+d) and our variance  realDeviation = deviation = ftarget - lforecast;  if (java.lang.Math.abs(deviation) > maxdev) deviation = maxdev;  global_mean = b*global_mean + a*ftarget;  // Use default for initial variances - for stability at startup  currentTime = Globals.env.getCurrentTime( );  if (currentTime < 1)    variance = privateParams.initvar;  else    variance = bv*variance + av*deviation*deviation; //I cant find anywhere in ASM-2.0's BFagent.m an update of the forecast's //forecast. but it is clearly needed if you look at bfagent from the //objc version. Including the next loop //fixes the strange time series properties too  //printf("active list has %d \n",[activeList getCount] );  for( int i=0; i<activeList.size(); i++ )    {      aForecast = (BFCast)activeList.get(i);      aForecast.updateForecastPrice$Dividend( price , dividend);    }  //pj: Here is the way it was// Update all the forecasters that were activated.//    if (currentTime > 0)//      for (fptr=lactivelist; fptr!=NULL; fptr=fptr->lnext)//        {//          deviation = (ftarget - fptr->lforecast)*(ftarget - fptr->lforecast);//  // 	Benchmark test line - replace true deviation with random one//  //      PARAM: Might be coded as a parameter sometime//  //      deviation = drand();//  //      Only necessary for absolute deviations//  //      if (deviation < 0.0) deviation = -deviation;//  	if (deviation > maxdev) deviation = maxdev;//    	if (fptr->count > tauv)//  	  fptr->variance = b*fptr->variance + a*deviation;//  	else//  	  {//  	    c = 1.0/(1.+fptr->count);//  	    fptr->variance = (1.0 - c)*fptr->variance +//  						c*deviation;//  	  }//          fptr->strength = fptr->specfactor/fptr->variance;//        }  if (currentTime > 0)    {      for( int i=0; i<oldActiveList.size(); i++ )        {          aForecast = (BFCast)oldActiveList.get(i);	  double  lastForecast=aForecast.getLforecast();	  deviation = (ftarget - lastForecast)*(ftarget - lastForecast);	  if (deviation > maxdev) deviation = maxdev;	  if (aForecast.getCnt() > tauv)	    aForecast.setVariance ( b*aForecast.getVariance() + a*deviation);	  else	    {	      c = 1.0/(double) (1.0 + aForecast.getCnt());  //??bfagent had no 1+ here ??	      aForecast.setVariance( (1.0 - c)*aForecast.getVariance() +			 c*deviation);	    }	  aForecast.setStrength( privateParams.maxdev		     - aForecast.getVariance()		     + aForecast.getSpecfactor());	  // ****************************************/	  // pj: The preceeding is based on sfsm's bfagent.m	  //	  // original bfagent has this: rptr->strength = p->maxdev -	  // rptr->variance + rptr->specfactor;	  // BFagent in ASM-2.0 had this: fptr->strength =	  // fptr->specfactor/fptr->variance; I've spoken to Blake	  // LeBaron and we both like the old way and don't know why it	  // was changed.	  // I hasten to say that maxdev is the benchmark for	  // variance, and I wonder if maxdev would not be better	  // renamed maxdev-squared or maxvar.	  //******************************************/	}    }  return this;}/*"Returns the absolute value of realDeviation"*/public double getDeviation(){  return java.lang.Math.abs(realDeviation);}public double getError(){  return divisor/privateParams.lambda;}/*"Currently, does nothing, used only if their are ANNagents"*/public Object updateWeights()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -