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

📄 world.m

📁 人工智能的matlab程序
💻 M
📖 第 1 页 / 共 2 页
字号:
    divhistory[i] = initdividend;
    }

  for (i = 0; i < NMAS; i++) 
    {
      priceMA[i] = [MovingAverage create: [self getZone]];
      [priceMA[i] initWidth: malength[i] Value: initprice];
      
      divMA[i] = [MovingAverage create: [self getZone]];
      [divMA[i] initWidth: malength[i] Value: initdividend];

      oldpriceMA[i] = [MovingAverage create: [self getZone]];
      [oldpriceMA[i] initWidth: malength[i] Value: initprice];

      olddivMA[i] = [MovingAverage create: [self getZone]];
      [olddivMA[i] initWidth: malength[i] Value: initdividend];
    }

// Initialize bits
  [self makebitvector];

  return self;
}

/*" Sets the market price to "p".  All price changes (besides trial
prices) should use this method.  Also computes profitperunit and
returnratio.  Checks internally for illegal changes of "price", giving us the
effective benefit of encapsulation with the simplicity of use of
a global variable. "*/
- setPrice: (double)p
{
  if (price != savedprice)
    printf("Price was changed illegally");

  oldprice = price;
  price = p;

  profitperunit = price - oldprice + dividend;
  if (oldprice <= 0.0)
    returnratio = profitperunit*1000.0;	/* Arbitrarily large */
  else
    returnratio = profitperunit/oldprice;

  savedprice = price;

  return self;
}

/*"Returns the price, used by many classes."*/
- (double)getPrice
{
  return price;
}

/*"Returns profitperunit, used by Specialist."*/
- (double)getProfitPerUnit
{
  return profitperunit;
}


/*"Sets the global value of "dividend".  All dividend changes should
	use this method.  It checks for illegal changes, as does
	-setPrice:."*/
- setDividend: (double)d
{
  if (dividend != saveddividend)
    printf("Dividend was changed illegally.");
  
  olddividend = dividend;
  dividend = d;

  saveddividend = dividend;
  riskNeutral = dividend/intrate;
    
  return self;
}

/*"Returns the most recent dividend, used by many."*/
- (double)getDividend
{
  return dividend;
}

/*"Returns the risk neutral price.  It is just dividend/intrate."*/
- (double)getRiskNeutral
{
  return riskNeutral;
}



/*" Updates the history records, moving averages, and world bits to
 * reflect the current price and dividend.  Note that this is called
 * in each period after a new dividend has been declared but before
 * the bidding and price adjustment.  The bits seen by the agents thus
 * do NOT reflect the trial price.  The "price" here becomes the
 * "oldprice" by the end of the period. It is called once per period.
 * (This could be done automatically as part of -setDividend:).
 *
 * The dividend used here is at present the latest value, though it
 * could be argued that it should be the one before, to match price.
 * For the p*r/d bits we do use the old one.
 "*/
- updateWorld
{
  register int i;

/* Update the binary up/down indicators for price and dividend */
  updown_top = (updown_top + 1) % UPDOWNLOOKBACK;
  pupdown[updown_top] = price > oldprice;
  dupdown[updown_top] = dividend > olddividend;

/* Update the price and dividend moving averages */
  history_top = history_top + 1 + MAXHISTORY;
  
  //update moving averages of price and dividend

  for (i = 0; i < NMAS; i++) 
    {
      int rago = (history_top-malength[i])%MAXHISTORY;

      [priceMA[i] addValue: price];
      [divMA[i] addValue: dividend];

      [oldpriceMA[i] addValue: pricehistory[rago]];
      [olddivMA[i] addValue: divhistory[rago]];
    }


/* Update the price and dividend histories */
  history_top %= MAXHISTORY;
  pricehistory[history_top] = price;
  divhistory[history_top] = dividend;
    
/* Construct the bit vector for the current state of the world */
  [self makebitvector];

  return self;
}


- makebitvector
/*"  Set all the world bits, based on the current dividend, price,
and  their moving averages and histories.  This moves through the
realworld array, bit by bit, setting the values to 0, 1 or 2,
according to the data that has been observed.  Note the pointer math, such as realworld[i++], that steps the integer i through the array.   Note that "i" increases monotonically throughout this routine, always
being the next bit to assign.  It is crucial that the order here is the
same as in bitnamelist[]. "*/
{
  register int i, j, k, temp;
  double multiple;


  i = 0;
    
  realworld[i++] = 1;
  realworld[i++] = 0;
  realworld[i++] = irand(2);

  /* Dividend went up or down, now and for last few periods */
  temp = updown_top + UPDOWNLOOKBACK;
  for (j = 0; j < UPDOWNLOOKBACK; j++, temp--)
    realworld[i++] = dupdown[temp%UPDOWNLOOKBACK];

  /* Dividend moving averages went up or down */
  for (j = 0; j < NMAS; j++)
    realworld[i++] = (GETMA(divMA,j)) > (GETMA(olddivMA,j));
  

  /* Dividend > MA[j] */
  for (j = 0; j < NMAS; j++)
    realworld[i++] = dividend > ( GETMA(divMA,j));

  /* Dividend MA[j] > dividend MA[k] */
  for (j = 0; j < NMAS-1; j++)
    for (k = j+1; k < NMAS; k++)
      realworld[i++] = (GETMA(divMA,j)) > (GETMA(divMA,k));
      //  realworld[i++] = exponentialMAs ? [divMA[j] getEWMA]:[divMA[j] getMA]  > exponentialMAs ? [divMA[k] getEWMA]:[divMA[k] getMA];
      //realworld[i++] = dmav[j] > dmav[k];

  /* Dividend as multiple of meandividend */
  multiple = dividend/dividendscale;
  for (j = 0; j < NRATIOS; j++)
    realworld[i++] = multiple > ratios[j];

  /* Price as multiple of dividend/intrate.  Here we use olddividend to
   * make a more reasonable comparison with the [old] price. */
  multiple = price*intrate/olddividend;
  for (j = 0; j < NRATIOS; j++)
    realworld[i++] = multiple > ratios[j];

  /* Price went up or down, now and for last few periods */
  temp = updown_top + UPDOWNLOOKBACK;
  for (j = 0; j < UPDOWNLOOKBACK; j++, temp--)
    realworld[i++] = pupdown[temp%UPDOWNLOOKBACK];

  /* Price moving averages went up or down */
  for (j = 0; j < NMAS; j++)
    realworld[i++] = (GETMA(priceMA,j)) > (GETMA(oldpriceMA,j));
    //realworld[i++] =pmav[j] > oldpmav[j];

  /* Price > MA[j] */
  for (j = 0; j < NMAS; j++)
     realworld[i++] = price > (GETMA(priceMA,j));
    //  realworld[i++] = price > exponentialMAs ? [priceMA[j] getEWMA]:[priceMA[j] getMA];
  // realworld[i++] = price > pmav[j];

  /* Price MA[j] > price MA[k] */
  for (j = 0; j < NMAS-1; j++)
    for (k = j+1; k < NMAS; k++)
      realworld[i++] = (GETMA(priceMA,j)) > (GETMA(priceMA,k));
  
  // Check
  if (i != NWORLDBITS)
    printf("Bits calculated != bits defined."); 

/* Now convert these bits using the code:
 *  yes -> 1    (01)
 *  no  -> 2    (10)
 * Then we're able to check rule satisfaction with simple ANDs.
 */
  for (i = 0; i < NWORLDBITS; i++)
    realworld[i] = 2 - realworld[i];

  return self;
}

/*" Returns the real world array of bits.  Used by BFagent to compare
  their worlds to the real world."*/
- getRealWorld: (int *)anArray
{
  memcpy(anArray, realworld, NWORLDBITS*sizeof(int)); 
  return self;
}


- (int)pricetrend: (int)n;
/*"
 * Returns +1, -1, or 0 according to whether the price has risen
 * monotonically, fallen monotonically, or neither, at the last
 * n updates. Causes
 *	an error if nperiods is too large (see UPDOWNLOOKBACK)."
 "*/
{
  int trend, i;

  if (n > UPDOWNLOOKBACK)
    printf("argument %d to -pricetrend: exceeds %d", n, UPDOWNLOOKBACK);
  for (i=0, trend=0; i<n; i++)
    trend |= realworld[i+PUPDOWNBITNUM];
  
  if (trend == 1)
    return 1;
  else if (trend == 2)
    return -1;
  else
    return 0;
}


@end












⌨️ 快捷键说明

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