Beispiel #1
0
void CCovSqexpARD::aKgrad_param(MatrixXd* out,const muint_t i) const 
{
	//lengthscales
	MatrixXd L = params.block(1,0,params.rows()-1,1);
	//rescale with length
	MatrixXd x1l = X * L.asDiagonal().inverse();
	//squared exponential distance
	MatrixXd RV;
	sq_dist(&RV,x1l,x1l);
	RV*= -0.5;
	(*out) = RV.unaryExpr(std::ptr_fun(exp));

	if (i==0) {
		(*out)*=2.*params(0);
	}
	else if (i<getNumberParams())
	{
		//lengthscale derivative:
		//1. get col (i-1) from X:
		MatrixXd sq;
		sq_dist(&sq,X.col(i-1),X.col(i-1));
		//3. elementwise product
        (*out)*=std::pow(params(0),2)*pow(params(i),-3);
		(*out).array()*=sq.array();
	}
	else
	{
		throw CLimixException("Parameter outside range");
	}
}
Beispiel #2
0
void CCovSqexpARD::aKhess_param(MatrixXd* out, const muint_t i, const muint_t j) const 
{

    muint_t i0, j0;
    
    if (i>j)    {i0=i; j0=j;}
    else        {muint_t temp=i; i0=j; j0=temp;}
    
    if ((i0==0) && (j0==0)) {
    	MatrixXd L = params.block(1,0,params.rows()-1,1);
    	MatrixXd x1l = X * L.asDiagonal().inverse();
    	MatrixXd RV;
    	sq_dist(&RV,x1l,x1l);
    	RV*= -0.5;
    	(*out) = 2*RV.unaryExpr(std::ptr_fun(exp));
    }
    else if ((j0==0) && (i0<getNumberParams()))
    {
        this->aKgrad_param(out,0);
        MatrixXd sq;
		sq_dist(&sq,X.col(i0-1),X.col(i0-1));
        (*out)*=pow(params(i0),-3);
		(*out).array()*=sq.array();
    }
    else if ((i0<getNumberParams()) && (j0!=i0))
    {
        this->aK(out);
        MatrixXd sq;
		sq_dist(&sq,X.col(i0-1),X.col(i0-1));
        (*out).array()*=sq.array();
        sq_dist(&sq,X.col(j0-1),X.col(j0-1));
        (*out).array()*=sq.array();
        (*out)*=pow(params(i0),-3)*pow(params(j0),-3);
    }
    else if ((i0<getNumberParams()) && (j0==i0))
    {
        //this->aK(out);
        MatrixXd sq;
		sq_dist(&sq,X.col(i0-1),X.col(i0-1));
        //(*out)=pow(params(i0),-6)*sq.unaryExpr(std::bind1st(std::ptr_fun(pow), 2))-3.0*pow(params(i0),-4)*sq;
        (*out)=sq;
        (*out).array()*=sq.array();
        (*out)*=pow(params(i0),-6);
        (*out)=(*out)-3*pow(params(i0),-4)*sq;
        (*out).array()*=K().array();
    }
    else
    {
        throw CLimixException("Parameter outside range");
    }
}
Beispiel #3
0
void CCovSqexpARD::aKcross(MatrixXd* out, const CovarInput& Xstar ) const 
{
	//lengthscales
	MatrixXd L = params.block(1,0,params.rows()-1,1);
	//rescale with length
	MatrixXd x1l = Xstar * L.asDiagonal().inverse();
	MatrixXd x2l = this->X * L.asDiagonal().inverse();
	//squared exponential distance
	MatrixXd RV;
	sq_dist(&RV,x1l,x2l);
	RV*= -0.5;
	(*out) = std::pow(params(0),2)*RV.unaryExpr(std::ptr_fun(exp));
} // end :: K
Beispiel #4
0
// argv = ["randmst", 0, numpoints, numtrials, dimension]
int main(int argc, char **argv)
{	
	// ensure correct usage
	if (argc != 5)
	{
		printf("Usage: ./randmst 0 numpoints numtrials dimension\n");
		return -1;
	}

	//TODO: check to see these are integers
	int dimension = atoi(argv[4]);

	// the number of verticies
	int n = atoi(argv[2]);

	int numtrials = atoi(argv[3]);

	// seed random number generator
	srand(time(NULL));

	// store the sum of the weights, to be divided by numtrials later
	float totalweight = 0;

	float cutoff= 8.0*((float)dimension)/((float) n+1);

	// the array of vertices (each entry is an array)
	float** verts = malloc(n*sizeof(float*));

	// initialize the vertices array
	for (int i = 0; i < n; i++)
	{
		verts[i] = malloc(dimension*sizeof(float));
	}

	// populate graph Graph
	vertex* Graph= malloc(n*sizeof(vertex));

	// perform numtrials number of trials and add weight to totalweight
	for (int t = 0; t < numtrials; t++ )
	{

		// initialize the vertices array
		for (int i = 0; i < n; i++)
		{
			for(int j = 0; j < dimension; j++)
			{
				verts[i][j] = (float)rand()/(float)RAND_MAX;
			}
		}


		// TODO: define prim
		queue q = init(n);
		queue* Q = &q;


		for (int i = 0; i < n; i++)
		{
			vertex v = {INFTY, NULL};
			Graph[i]=v;

			for(int j = 0; j < i; j++)
			{
				float squareDistance = sq_dist(verts[i], verts[j], dimension);
				if(squareDistance<cutoff)
				{
					AdjListNode* jNodePtr= newAdjListNode(j, squareDistance);
					AdjListNode* iNodePtr= newAdjListNode(i, squareDistance);
					if(!Graph[i].adjacentVertices)
					{
						Graph[i].adjacentVertices=jNodePtr;

					}
					else
					{
						jNodePtr -> next = Graph[i].adjacentVertices;
						Graph[i].adjacentVertices= jNodePtr;
					}
					if(!Graph[j].adjacentVertices)
					{
						Graph[j].adjacentVertices=iNodePtr;

					}
					else
					{
						iNodePtr -> next = Graph[j].adjacentVertices;
						Graph[j].adjacentVertices=iNodePtr;
					}
				}
			}
		}


		// populate the weights array with the euclidean distances
		// also build adjacency lists

		float treeweight = Prim(Q, Graph);
		totalweight += treeweight;

		// free shit
		for (int i = 0; i < n; i++)
		{
			AdjListNode* trash = Graph[i].adjacentVertices;
			while(!trash)
			{
				AdjListNode* t = trash->next;
				free(trash);
				trash = t;
			}
		}
		

	}
	// free willy
	for (int i = 0; i < n; i++)
	{
		free(verts[i]);
	}
	free(verts);
	free(Graph);
	// printf("%f\n", totalweight);
	// printf("The average weight of a %i-dimensional minimum spanning tree with with %i verticies is: \n", dimension, n);
	printf("%f %i %i %i \n", totalweight / numtrials, n, numtrials, dimension);

	
}
Beispiel #5
0
int main()
{
 int exit = 0;

 init();

 BITMAP *cursor_bmp = load_bitmap("cursor.bmp", NULL);

 VECTOR temp_shape[100];
 LINE_LOOP loop[1000];
 int n = 0, sn = 0, timeout = 100, i, j;

 while(!exit)
  {
   if(keypressed())
    {
     if(key[KEY_ESC]) { exit = 1; }
     if(key[KEY_C] && n > 2 && sn < 1000)
      {
       loop[sn].point = (VECTOR *)malloc(n * sizeof(VECTOR));
       for(i = 0; i < n; i++)
        loop[sn].point[i] = temp_shape[i];
       loop[sn].n = n;
       loop[sn].closed = 1;
       sn++;
       n = 0;
      }

     if(key[KEY_V] && n > 1 && sn < 1000)
      {
       loop[sn].point = (VECTOR *)malloc(n * sizeof(VECTOR));
       for(i = 0; i < n; i++)
        loop[sn].point[i] = temp_shape[i];
       loop[sn].n = n;
       loop[sn].closed = 0;
       sn++;
       n = 0;
      }
    }

   VECTOR cursor;
   cursor.x = (mouse_x / 5) * 5;
   cursor.y = (mouse_y / 5) * 5;
   timeout--;

   if(mouse_b == 1 && timeout < 1 && n < 100)
    {
     int error_flag = 0;
     timeout = 100;

     if(n > 0)
      if(sq_dist(cursor, temp_shape[n - 1]) < 25)
       error_flag = 1;

     if(n > 1)
      if(fabs(VECTOR_SLOPE(VECTOR_DIFF(temp_shape[n - 1], temp_shape[n - 2])) -
              VECTOR_SLOPE(VECTOR_DIFF(temp_shape[n - 1], cursor))) < 0.2 ||
         (temp_shape[n - 1].x == temp_shape[n - 2].x && temp_shape[n - 2].x == cursor.x))
       error_flag = 1;

     if(!error_flag)
      {
       temp_shape[n] = cursor;
       n++;
      }
    }

   clear_to_color(buffer, makecol(128, 128, 128));

   if(sn > 0)
    for(j = 0; j < sn; j++)
     vector_loop(buffer, loop[j].point, loop[j].n, loop[j].closed, 0);
    vector_loop(buffer, temp_shape, n, -2, 0);

   if(n > 1)
    {
     vector_loop(buffer, temp_shape, n, 0, 0);
     vector_line(buffer, temp_shape[n - 1], temp_shape[0], makecol(128, 0, 0));
    }

   if(n > 0)
    {
     vector_line(buffer, temp_shape[n - 1], cursor, makecol(0, 128, 128));
     vector_line(buffer, cursor, temp_shape[0], makecol(0, 128, 128));
    }

   draw_sprite(buffer, cursor_bmp, cursor.x - 4, cursor.y - 5);
   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  }

 clear_keybuf();
 clear_to_color(buffer, makecol(128, 128, 128));

 float wall_width = 20.0;

 LINE_LOOP *final_shape;
 final_shape = (LINE_LOOP *)malloc(sn * sizeof(LINE_LOOP));

 if(sn > 0)
  for(j = 0; j < sn; j++)
   {
    if(loop[j].n > 2 && !loop[j].closed)
     {
      VECTOR norm;
      int pn = loop[j].n;
      final_shape[j] = new_shape(pn * 2, 0);

      for(i = 0; i < pn; i++)
       final_shape[j].point[i] = loop[j].point[i];

      norm = NORMALIZED_NORMAL(loop[j].point[0], loop[j].point[1]);
      final_shape[j].point[pn] = VECTOR_SUM(loop[j].point[0], USCALE_VECTOR(norm, wall_width));
      norm = NORMALIZED_NORMAL(loop[j].point[pn - 2], loop[j].point[pn - 1]);
      final_shape[j].point[pn * 2 - 1] = VECTOR_SUM(loop[j].point[pn - 1], USCALE_VECTOR(norm, wall_width));

      for(i = 1; i < pn - 1; i++)
       final_shape[j].point[pn + i] = make_wall(loop[j].point[i - 1], loop[j].point[i], loop[j].point[i + 1], wall_width);
     }

    if(loop[j].n > 2 && loop[j].closed)
     {
      int pn = loop[j].n;
      final_shape[j] = new_shape(pn, 1);
      for(i = 0; i < pn; i++)
       final_shape[j].point[i] = loop[j].point[i];
     }

    if(loop[j].n == 2)
     {
      final_shape[j] = new_shape(4, 0);
      VECTOR norm = USCALE_VECTOR(NORMALIZED_NORMAL(loop[j].point[0], loop[j].point[1]), wall_width);
      final_shape[j].point[0] = loop[j].point[0];
      final_shape[j].point[1] = loop[j].point[1];
      final_shape[j].point[2] = VECTOR_SUM(loop[j].point[0], norm);
      final_shape[j].point[3] = VECTOR_SUM(loop[j].point[1], norm);
     }
   }

 draw_map_sketch(buffer, final_shape, sn);
 save_map("map.txt", final_shape, sn);

 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
 readkey();

 free_map(final_shape, sn);
 for(i = 0; i < sn; i++)
  free(loop[i].point);
 destroy_bitmap(cursor_bmp);
 destroy_bitmap(buffer);
 return 0;
}