Ejemplo n.º 1
0
void M2MFstAligner::expectation( ){
  /*
    Here we compute the arc posteriors.  This routine is almost 
    fun to implement in the FST paradigm.
  */
  for( unsigned int i=0; i<fsas.size(); i++ ){
    //Compute Forward and Backward probabilities
    ShortestDistance( fsas.at(i), &alpha );
    ShortestDistance( fsas.at(i), &beta, true );

    //Compute the normalized Gamma probabilities and 
    // update our running tally
    for( StateIterator<VectorFst<LogArc> > siter(fsas.at(i)); !siter.Done(); siter.Next() ){
      LogArc::StateId q = siter.Value();
      for( ArcIterator<VectorFst<LogArc> > aiter(fsas.at(i),q); !aiter.Done(); aiter.Next() ){
	const LogArc&      arc = aiter.Value();
	const LogWeight& gamma = Divide(Times(Times(alpha[q], arc.weight), beta[arc.nextstate]), beta[0]); 
	//Check for any BadValue results, otherwise add to the tally.
        //We call this 'prev_alignment_model' which may seem misleading, but
        // this conventions leads to 'alignment_model' being the final version.
	if( gamma.Value()==gamma.Value() ){
	  prev_alignment_model[arc.ilabel] = Plus(prev_alignment_model[arc.ilabel], gamma);
	  total = Plus(total, gamma);
	}
      }
    }
    alpha.clear();
    beta.clear();
  }
}
Ejemplo n.º 2
0
BOOL CcdrawDoc::AddBlockLabel(CBlockLabel &blabel, double d)
{
	int i;
	double x,y;
	BOOL AddFlag=TRUE;

	x=blabel.x; y=blabel.y;

	// test to see if ``too close'' to existing node...
	for (i=0;i<blocklist.GetSize();i++)
		if(blocklist[i].GetDistance(x,y)<d) AddFlag=FALSE;

	// can't put a block label on top of an existing node...
	for (i=0;i<nodelist.GetSize();i++)
		if(nodelist[i].GetDistance(x,y)<d) return FALSE;

	// can't put a block label on a line, either...
	for (i=0;i<linelist.GetSize();i++)
		if(ShortestDistance(x,y,i)<d) return FALSE ;

	// if all is OK, add point in to the node list...
	if(AddFlag==TRUE) blocklist.Add(blabel);
	
	return TRUE;
}
Ejemplo n.º 3
0
vector<int> monster::UniformCostSearch()
{
    int NUM_VERTICIES = 64;
    // A Dijkstra method with weights all 1 or NULL

    // start is MonsterRoom and looking for path to PlayerRoom... return is next room to enter based on path
    vector<int> NodeDistances; // dist[]
    bool SPTreeset[64];        // Q? Shortest Paths T/F
    vector<int> previous;      // previous gets things put in it which should be the next hop /// we can have at most 64 hops, so

    // GOTTA FIGURE OUT HOW TO STORE PREVIOUS SO WE CAN KEEP A PATH


    for (int i = 0; i < NUM_VERTICIES; i++)
    {
        NodeDistances.push_back(INFINITY);
        SPTreeset[i] = false;
    }
    NodeDistances[PlayerRoom] = 0; // my starting point to myself is myself
    previous.clear();
    int vertex = PlayerRoom; // can't be player room... Except watch out before player moves... but since move only gets called after player moves...
    // entering into loop
    while (vertex != MonsterRoom)
    {
        // just search for shortest route (fewest jumps) to Player Location
        vertex = ShortestDistance(NodeDistances, SPTreeset); // returns vertex not already set in SPTreeset ( after return it is set though )

        if (vertex == 0)
            break;

        previous.push_back(vertex); // keep track of the path from the first vertex match
        FindNextDistances(NodeDistances, vertex);
    }
    return previous;
}
Ejemplo n.º 4
0
BOOL CcdrawDoc::AddNode(CNode &node, double d)
{
	int i,k;
	CSegment segm;
	CArcSegment asegm;
	CComplex c,a0,a1,a2;
	double x,y;
	double R;

	x=node.x; y=node.y;

	// test to see if ``too close'' to existing node...
	for (i=0;i<nodelist.GetSize();i++)
		if(nodelist[i].GetDistance(x,y)<d) return FALSE;

	// can't put a node on top of a block label; do same sort of test.
	for (i=0;i<blocklist.GetSize();i++)
		if(blocklist[i].GetDistance(x,y)<d) return FALSE;

	// if all is OK, add point in to the node list...
	nodelist.Add(node);

	// test to see if node is on an existing line; if so, 
	// break into two lines;
	k=(int) linelist.GetSize();
	for(i=0;i<k;i++)
	{
		if (fabs(ShortestDistance(x,y,i))<d)
		{
			segm=linelist[i];
			linelist[i].n1=(int) nodelist.GetSize()-1;
			segm.n0=(int) nodelist.GetSize()-1;
			linelist.Add(segm);
		}
	}

	// test to see if node is on an existing arc; if so, 
	// break into two arcs;
	k=(int) arclist.GetSize();
	for(i=0;i<k;i++)
	{
		if (ShortestDistanceFromArc(CComplex(x,y),arclist[i])<d)
		{
			a0.Set(nodelist[arclist[i].n0].x,nodelist[arclist[i].n0].y);
			a1.Set(nodelist[arclist[i].n1].x,nodelist[arclist[i].n1].y);
			a2.Set(x,y);
			GetCircle(arclist[i],c,R);
			asegm=arclist[i];
			arclist[i].n1=(int) nodelist.GetSize()-1;
			arclist[i].ArcLength=arg((a2-c)/(a0-c))*180./PI;
			asegm.n0=(int) nodelist.GetSize()-1;
			asegm.ArcLength=arg((a1-c)/(a2-c))*180./PI;
			arclist.Add(asegm);
		}
	}
	return TRUE;
}
Ejemplo n.º 5
0
int CbelaviewDoc::ClosestSegment(double x, double y)
{
	double d0,d1;
	int i,j;

	if(linelist.GetSize()==0) return -1;

	j=0;
	d0=ShortestDistance(x,y,0);
	for(i=0;i<linelist.GetSize();i++){
		d1=ShortestDistance(x,y,i);
		if(d1<d0){
			d0=d1;
			j=i;
		}
	}

	return j;
}
Ejemplo n.º 6
0
BOOL CcdrawDoc::AddSegment(CComplex p0, CComplex p1, CSegment &segm, double tol)
{
	int i,j,k,n0,n1;
	double xi,yi,t;
	CComplex p[2];
	CArray< CComplex, CComplex&> newnodes;

	newnodes.RemoveAll();

	n0=ClosestNode(p0.re,p0.im);
	n1=ClosestNode(p1.re,p1.im);

	// don't add if line is degenerate
	if (n0==n1) return FALSE;
	
	// don't add if the line is already in the list;
	for(i=0;i<linelist.GetSize();i++){
		if ((linelist[i].n0==n0) && (linelist[i].n1==n1)) return FALSE;
		if ((linelist[i].n0==n1) && (linelist[i].n1==n0)) return FALSE;
	}

	segm.IsSelected=FALSE; segm.n0=n0; segm.n1=n1; 
	
	// check to see if there are intersections with segments
	for(i=0;i<linelist.GetSize();i++)
		if(GetIntersection(n0,n1,i,&xi,&yi)==TRUE)  newnodes.Add(CComplex(xi,yi));

	// check to see if there are intersections with arcs
	for(i=0;i<arclist.GetSize();i++){
		j=GetLineArcIntersection(segm,arclist[i],p);
		if (j>0) for(k=0;k<j;k++) newnodes.Add(p[k]);
	}

	// add nodes at intersections
	if(tol==0)
	{
		if (nodelist.GetSize()<2) t=1.e-08;
		else{
			CComplex p0,p1;
			p0=nodelist[0].CC();
			p1=p0;
			for(i=1;i<nodelist.GetSize();i++)
			{
				if(nodelist[i].x<p0.re) p0.re=nodelist[i].x;
				if(nodelist[i].x>p1.re) p1.re=nodelist[i].x;
				if(nodelist[i].y<p0.im) p0.im=nodelist[i].y;
				if(nodelist[i].y>p1.im) p1.im=nodelist[i].y;
			}
			t=abs(p1-p0)*CLOSE_ENOUGH;
		}
	}
	else t=tol;

	for(i=0;i<newnodes.GetSize();i++) 
		AddNode(newnodes[i].re,newnodes[i].im,t);
	
	// Add proposed line segment
	linelist.Add(segm);

	// check to see if proposed line passes through other points;
    // if so, delete line and create lines that link intermediate points;
    // does this by recursive use of AddSegment;
	double d,dmin;
    UnselectAll();
    if (tol==0) dmin=abs(nodelist[n1].CC()-nodelist[n0].CC())*1.e-05;
	else dmin=tol;

    k=(int) linelist.GetSize()-1;
    for(i=0;i<nodelist.GetSize();i++)
    {
        if( (i!=n0) && (i!=n1) )
        {
            d=ShortestDistance(nodelist[i].x,nodelist[i].y,k);
			// catch a special case...
			if (abs(nodelist[i].CC()-nodelist[n0].CC())<dmin) d=2.*dmin;
			if (abs(nodelist[i].CC()-nodelist[n1].CC())<dmin) d=2.*dmin;
	        if (d<dmin){
                linelist[k].ToggleSelect();
                DeleteSelectedSegments();
                AddSegment(n0,i,&segm,dmin);
                AddSegment(i,n1,&segm,dmin);
                i=(int) nodelist.GetSize();
            }

        }
    }

	return TRUE;
}