Exemplo n.º 1
0
void printStackAndPc(int s[], int bp, int sp, int p[], int pc) {
  printf("[ ");
  int i;
  for (i=0; i<=sp; i++)
    if (IsInt(s[i]))
      printf("%d ", Untag(s[i]));
    else
      printf("#%d ", s[i]);      
  printf("]");
  printf("{%d:", pc); printInstruction(p, pc); printf("}\n"); 
}
Exemplo n.º 2
0
Actor::~Actor()
{
	StringSet::iterator it = _tags.begin();
	while (it != _tags.end())
	{
		String tag = *it;
		it++;
		Untag(tag);
	}

	Actor::_nameList.erase(_name);
}
Exemplo n.º 3
0
static void Gradient(This *t, ccount nfree, ccount *ifree,
  cBounds *b, real *x, creal y, real *grad)
{
  count i;

  for( i = 0; i < nfree; ++i ) {
    ccount dim = Untag(ifree[i]);
    creal xd = x[dim];
    creal delta = (b[dim].upper - xd < DELTA) ? -DELTA : DELTA;
    x[dim] += delta;
    grad[i] = (Sample(t, x) - y)/delta;
    x[dim] = xd;
  }
}
Exemplo n.º 4
0
Experience::Experience(Vector2 pos)
{
	_setUp = false;
	_speed = 10.0f;
	SetIsSensor(true); // make not collidable
	pos.X += (MathUtil::RandomFloat(1.0) - 0.5);
	pos.Y += (MathUtil::RandomFloat(1.0) - 0.5);
	SetSprite("Resources/Images/exp1.png");
	SetPosition(pos);
	SetDrawShape(ADS_Square);
	SetSize(MathUtil::RandomFloat(0.4) + 0.2);
	//SetColor(0, 0.8, 0);
	Untag("Enemy");
	Tag("Experience");
	InitPhysics();
	theWorld.Add(this);
}
Exemplo n.º 5
0
Actor::~Actor()
{
	StringSet::iterator it = _tags.begin();
	while (it != _tags.end())
	{
		String tag = *it;
		it++;
		Untag(tag);
	}

	Actor::_nameList.erase(_name);
	
	StringSet subs = theSwitchboard.GetSubscriptionsFor(this);
	it = subs.begin();
	while (it != subs.end())
	{
		theSwitchboard.UnsubscribeFrom(this, *it);
		++it;
	}
}
Exemplo n.º 6
0
static real Sample(This *t, creal *x0)
{
  Vector(real, xtmp, 2*NDIM);
  Vector(real, ftmp, 2*NCOMP);
  real *xlast = xtmp, f;
  real dist = 0;
  count dim, comp;
  number n = 1;

  for( dim = 0; dim < t->ndim; ++dim ) {
    creal x1 = *xlast++ = Min(Max(*x0++, 0.), 1.);
    real dx;
    if( (dx = x1 - t->border.lower) < 0 ||
        (dx = x1 - t->border.upper) > 0 ) dist += Sq(dx);
  }

  if( dist > 0 ) {
    dist = sqrt(dist)/EXTRAPOLATE_EPS;
    for( dim = 0; dim < t->ndim; ++dim ) {
      real x2 = xtmp[dim], dx, b;
      if( (dx = x2 - (b = t->border.lower)) < 0 ||
          (dx = x2 - (b = t->border.upper)) > 0 ) {
        xtmp[dim] = b;
        x2 = b - dx/dist;
      }
      *xlast++ = x2;
    }
    n = 2;
  }

  DoSample(t, n, xtmp, ftmp);

#define fin(x) Min(Max(x, -.5*INFTY), .5*INFTY) 

  comp = Untag(t->selectedcomp);
  f = fin(ftmp[comp]);
  if( n > 1 ) f += dist*(f - fin(ftmp[comp + t->ncomp]));

  return Sign(t->selectedcomp)*f;
}
Exemplo n.º 7
0
int execcode(int p[], int s[], int iargs[], int iargc, int /* boolean */ trace) {
  int bp = -999;        // Base pointer, for local variable access 
  int sp = -1;          // Stack top pointer
  int pc = 0;           // Program counter: next instruction
  for (;;) {
    if (trace) 
      printStackAndPc(s, bp, sp, p, pc);
    switch (p[pc++]) {
    case CSTI:
      s[sp+1] = Tag(p[pc++]); sp++; break;
    case ADD: 
      s[sp-1] = Tag(Untag(s[sp-1]) + Untag(s[sp])); sp--; break;
    case SUB: 
      s[sp-1] = Tag(Untag(s[sp-1]) - Untag(s[sp])); sp--; break;
    case MUL: 
      s[sp-1] = Tag(Untag(s[sp-1]) * Untag(s[sp])); sp--; break;
    case DIV: 
      s[sp-1] = Tag(Untag(s[sp-1]) / Untag(s[sp])); sp--; break;
    case MOD: 
      s[sp-1] = Tag(Untag(s[sp-1]) % Untag(s[sp])); sp--; break;
    case EQ: 
      s[sp-1] = Tag(s[sp-1] == s[sp] ? 1 : 0); sp--; break;
    case LT: 
      s[sp-1] = Tag(s[sp-1] < s[sp] ? 1 : 0); sp--; break;
    case NOT: {
      int v = s[sp];
      s[sp] = Tag((IsInt(v) ? Untag(v) == 0 : v == 0) ? 1 : 0);
    } break;
    case DUP: 
      s[sp+1] = s[sp]; sp++; break;
    case SWAP: 
      { int tmp = s[sp];  s[sp] = s[sp-1];  s[sp-1] = tmp; } break; 
    case LDI:                 // load indirect
      s[sp] = s[Untag(s[sp])]; break;
    case STI:                 // store indirect, keep value on top
      s[Untag(s[sp-1])] = s[sp]; s[sp-1] = s[sp]; sp--; break;
    case GETBP:
      s[sp+1] = Tag(bp); sp++; break;
    case GETSP:
      s[sp+1] = Tag(sp); sp++; break;
    case INCSP:
      sp = sp+p[pc++]; break;
    case GOTO:
      pc = p[pc]; break;
    case IFZERO: { 
      int v = s[sp--];
      pc = (IsInt(v) ? Untag(v) == 0 : v == 0) ? p[pc] : pc+1; 
    } break;
    case IFNZRO: { 
      int v = s[sp--];
      pc = (IsInt(v) ? Untag(v) != 0 : v != 0) ? p[pc] : pc+1; 
    } break;
    case CALL: { 
      int argc = p[pc++];
      int i;
      for (i=0; i<argc; i++)               // Make room for return address
        s[sp-i+2] = s[sp-i];               // and old base pointer
      s[sp-argc+1] = Tag(pc+1); sp++; 
      s[sp-argc+1] = Tag(bp);   sp++; 
      bp = sp+1-argc;
      pc = p[pc]; 
    } break; 
    case TCALL: { 
      int argc = p[pc++];                  // Number of new arguments
      int pop  = p[pc++];                  // Number of variables to discard
      int i;
      for (i=argc-1; i>=0; i--)    // Discard variables
        s[sp-i-pop] = s[sp-i];
      sp = sp - pop; pc = p[pc]; 
    } break; 
    case RET: { 
      int res = s[sp]; 
      sp = sp-p[pc]; bp = Untag(s[--sp]); pc = Untag(s[--sp]); 
      s[sp] = res; 
    } break; 
    case PRINTI:
      printf("%d ", IsInt(s[sp]) ? Untag(s[sp]) : s[sp]); break; 
    case PRINTC:
      printf("%c", Untag(s[sp])); break; 
    case LDARGS: {
      int i;
      for (i=0; i<iargc; i++) // Push commandline arguments
        s[++sp] = Tag(iargs[i]);
    } break;
    case STOP:
      return 0;
    case NIL:    
      s[sp+1] = 0; sp++; break;
    case CONS: {
      word* p = allocate(CONSTAG, 2, s, sp); 
      p[1] = (word)s[sp-1];
      p[2] = (word)s[sp];
      s[sp-1] = (int)p;
      sp--;
    } break;
    case CAR: {
      word* p = (word*)s[sp]; 
      if (p == 0) 
        { printf("Cannot take car of null\n"); return -1; }
      s[sp] = (int)(p[1]);
    } break;
    case CDR: {
      word* p = (word*)s[sp]; 
      if (p == 0) 
        { printf("Cannot take cdr of null\n"); return -1; }
      s[sp] = (int)(p[2]);
    } break;
    case SETCAR: {
      word v = (word)s[sp--];
      word* p = (word*)s[sp]; 
      p[1] = v;
    } break;
    case SETCDR: {
      word v = (word)s[sp--];
      word* p = (word*)s[sp]; 
      p[2] = v;
    } break;
    default:                  
      printf("Illegal instruction %d at address %d\n", p[pc-1], pc-1);
      return -1;
    }
  }
}
Exemplo n.º 8
0
static real FindMinimum(This *t, cBounds *b, real *xmin, real fmin)
{
  Vector(real, hessian, NDIM*NDIM);
  Vector(real, gfree, NDIM);
  Vector(real, p, NDIM);
  Vector(real, tmp, NDIM);
  Vector(count, ifree, NDIM);
  Vector(count, ifix, NDIM);
  real ftmp, fini = fmin;
  ccount maxeval = t->neval + 50*t->ndim;
  count nfree, nfix;
  count dim, local;

  Clear(hessian, t->ndim*t->ndim);
  for( dim = 0; dim < t->ndim; ++dim )
    Hessian(dim, dim) = 1;

  /* Step 1: - classify the variables as "fixed" (sufficiently close
               to a border) and "free",
             - if the integrand is flat in the direction of the gradient
               w.r.t. the free dimensions, perform a local search. */

  for( local = 0; local < 2; ++local ) {
    bool resample = false;
    nfree = nfix = 0;
    for( dim = 0; dim < t->ndim; ++dim ) {
      if( xmin[dim] < b[dim].lower + (1 + fabsx(b[dim].lower))*QEPS ) {
        xmin[dim] = b[dim].lower;
        ifix[nfix++] = dim;
        resample = true;
      }
      else if( xmin[dim] > b[dim].upper - (1 + fabsx(b[dim].upper))*QEPS ) {
        xmin[dim] = b[dim].upper;
        ifix[nfix++] = Tag(dim);
        resample = true;
      }
      else ifree[nfree++] = dim;
    }

    if( resample ) fini = fmin = Sample(t, xmin);

    if( nfree == 0 ) goto releasebounds;

    Gradient(t, nfree, ifree, b, xmin, fmin, gfree);
    if( local || Length(nfree, gfree) > GTOL ) break;

    ftmp = LocalSearch(t, nfree, ifree, b, xmin, fmin, tmp);
    if( ftmp > fmin - (1 + fabsx(fmin))*RTEPS )
      goto releasebounds;
    fmin = ftmp;
    XCopy(xmin, tmp);
  }

  while( t->neval <= maxeval ) {

    /* Step 2a: perform a quasi-Newton iteration on the free
                variables only. */

    if( nfree > 0 ) {
      real plen, pleneps;
      real minstep;
      count i, mini = 0, minfix = 0;
      Point low;

      LinearSolve(t, nfree, hessian, gfree, p);
      plen = Length(nfree, p);
      pleneps = plen + RTEPS;

      minstep = INFTY;
      for( i = 0; i < nfree; ++i ) {
        count dim = Untag(ifree[i]);
        if( fabsx(p[i]) > EPS ) {
          real step;
          count fix;
          if( p[i] < 0 ) {
            step = (b[dim].lower - xmin[dim])/p[i];
            fix = dim;
          }
          else {
            step = (b[dim].upper - xmin[dim])/p[i];
            fix = Tag(dim);
          }
          if( step < minstep ) {
            minstep = step;
            mini = i;
            minfix = fix;
          }
        }
      }

      if( minstep*pleneps <= DELTA ) {
fixbound:
        ifix[nfix++] = minfix;

        if( mini < --nfree ) {
          creal diag = Hessian(mini, mini);

          Clear(tmp, mini);
          for( i = mini; i < nfree; ++i )
            tmp[i] = Hessian(i + 1, mini);

          for( i = mini; i < nfree; ++i ) {
            Move(&Hessian(i, 0), &Hessian(i + 1, 0), i);
            Hessian(i, i) = Hessian(i + 1, i + 1);
          }
          RenormalizeCholesky(t, nfree, hessian, tmp, diag);

          Move(&ifree[mini], &ifree[mini + 1], nfree - mini);
          Move(&gfree[mini], &gfree[mini + 1], nfree - mini);
        }
        continue;
      }

      low = LineSearch(t, nfree, ifree, p, xmin, fmin, tmp,
        Min(minstep, 1.), Min(minstep, 100.), Dot(nfree, gfree, p),
        RTEPS/pleneps, DELTA/pleneps, .2);

      if( low.dx > 0 ) {
        real fdiff;

        fmin = low.f;
        XCopy(xmin, tmp);

        Gradient(t, nfree, ifree, b, xmin, fmin, tmp);
        BFGS(t, nfree, hessian, tmp, gfree, p, low.dx);
        XCopy(gfree, tmp);

        if( fabsx(low.dx - minstep) < QEPS*minstep ) goto fixbound;

        fdiff = fini - fmin;
        fini = fmin;
        if( fdiff > (1 + fabsx(fmin))*FTOL ||
            low.dx*plen > (1 + Length(t->ndim, xmin))*FTOL ) continue;
      }
    }

    /* Step 2b: check whether freeing any fixed variable will lead
                to a reduction in f. */

releasebounds:
    if( nfix > 0 ) {
      real mingrad = INFTY;
      count i, mini = 0;
      bool repeat = false;

      Gradient(t, nfix, ifix, b, xmin, fmin, tmp);

      for( i = 0; i < nfix; ++i ) {
        creal grad = Sign(ifix[i])*tmp[i];
        if( grad < -RTEPS ) {
          repeat = true;
          if( grad < mingrad ) {
            mingrad = grad;
            mini = i;
          }
        }
      }

      if( repeat ) {
        gfree[nfree] = tmp[mini];
        ifree[nfree] = Untag(ifix[mini]);
        Clear(&Hessian(nfree, 0), nfree);
        Hessian(nfree, nfree) = 1;
        ++nfree;

        --nfix;
        Move(&ifix[mini], &ifix[mini + 1], nfix - mini);
        continue;
      }
    }

    break;
  }

  return fmin;
}