コード例 #1
0
ファイル: Graph.cpp プロジェクト: ColumbiaDVMM/INDetector
// reachability test, from ni -> nj, according to weight
bool Graph::Reachable(int ni, int nj)
{
	MyStack * stack;
	int tmp,result[1000],result_e[1000],neib_no=0,k;
	int nt,i;

	for(i=0;i<node_num;i++)
		NODE_PTR(i)->color = 0;

	stack = new MyStack;
	stack->stack_push(ni,0);
	NODE_PTR(i)->color = 1;

	while(stack->stack_pop(&nt,&tmp)!=-1)
	{
		neib_no = GetNeighborNodeAndEdge(nt, result,result_e);
		for(k=0;k<neib_no;k++)
		{
			if(result[k]==nj && EDGE_PTR(result_e[k])->weight > 0.5)
			{
				delete stack;
				return true;
			}
				
			if(NODE_PTR(result[k])->color==0 && EDGE_PTR(result_e[k])->weight > 0.5)  // hasn't been dried
			{
				stack->stack_push(result[k],0);
				NODE_PTR(result[k])->color=1;
			}
		}
	};
	delete stack;

	return false;
}
コード例 #2
0
ファイル: write_outfile.cpp プロジェクト: sandeep8650/Huffzip
/* function to write compressed file
 * @return: total number of written bytes to compressed file i.e. size of compressed file
 */
f_type write_outfile(char *filename,f_type *freq,struct huffcode *codeTable,struct node *root, f_type total_bytes){
	f_type written_bytes=0,read_bytes=0;
	NODE_PTR(ptr);
	struct buffer buff={0,-1};
	unsigned b;
	string outfilename=string(filename) + extension;
	FILE *infile=fopen(filename,"rb");
	if(!infile){
		fprintf(stderr,"Source file opening error: %s\n",strerror(errno));
		return 0;
	}
	FILE *outfile=fopen(outfilename.c_str(),"wbx");
	for(int i=1;i<=5 && !outfile;i++){
		outfilename.insert(outfilename.begin()+outfilename.find_last_of('.'),'1');
		outfile=fopen(outfilename.c_str(),"wbx");
	}
	if(!outfile){
		fclose(infile);
		fprintf(stderr,"Destination file opening error: %s\n",strerror(errno));
		return 0;
	}
	written_bytes=fwrite(freq,sizeof(f_type),TABLE_SIZE,outfile);
	if(written_bytes==0){
		fclose(outfile);
		fclose(infile);
		return 0;
	}
	while((b=fgetc(infile))!=EOF){
		read_bytes++;
		for(int i=0;i<codeTable[b].length;i++){
			buff.byte[++buff.top]=codeTable[b].code[i];
			if(buff.top==7){//buffer is full, so write it to file
				fputc(buff.byte.to_ulong(),outfile);
				written_bytes++;
				buff.byte=0;
				buff.top=-1;
			}
		}
		PROGRESS(read_bytes,total_bytes);
	}
	printf("\n");
	if(buff.top!=-1){
		written_bytes+=2;
		fputc(buff.byte.to_ulong(),outfile);
		fputc(buff.top + 1,outfile);//this is length of code in last byte
	}
	else{
		written_bytes++;
		fputc(8,outfile);//length of code in last byte is 8
	}
	fclose(infile);
	fclose(outfile);
	return written_bytes;
}
コード例 #3
0
// Depth-First-Search to get the maximum likelihood assignment of matching
// used by Gibbs sampling
void MatchMRFGraph::DFSSearch(int * state_seq)
{
	SortMachine sm;
	int *node_fill, *state_fill,i,j,n,nid,sid;

	memset(state_seq,0,sizeof(int)*node_num);
	sm.Init(node_num*m_state_num);
	for(i=0;i<node_num;i++)
		for(j=0;j<m_state_num;j++)
	{
		sm.m_Arr[i*m_state_num+j].ind = i;
		sm.m_Arr[i*m_state_num+j].ind2 = j;
		sm.m_Arr[i*m_state_num+j].d = ((IGraphNode *)NODE_PTR(i))->phi[j];
	}
	sm.Sort();

// occupation array
	node_fill = new int[node_num];
	memset(node_fill,0,node_num*sizeof(int));
	state_fill = new int[m_state_num];
	memset(state_fill,0,m_state_num*sizeof(int));

	state_seq[sm.m_Arr[0].ind] = sm.m_Arr[0].ind2;
	node_fill[sm.m_Arr[0].ind] = 1; state_fill[sm.m_Arr[0].ind2] = 1;

	n = 1;
	while(n<node_num*m_state_num)
	{
		nid = sm.m_Arr[n].ind;
		sid = sm.m_Arr[n++].ind2;
		if(node_fill[nid]>0 || state_fill[sid]>0)
			continue;

		state_seq[nid] = sid;
		node_fill[nid] = 1;
		state_fill[sid] = 1;
	}

	delete state_fill;
	delete node_fill;
}
コード例 #4
0
/**Function********************************************************************

  Synopsis [Executes a trace on the fsm given at construction time
  using BDDs]

  Description [The trace is executed using BDDs, that is a proof that
  the fsm is compatible with the trace is built (if such proof
  exists). Incomplete traces are filled-in with compatible values for
  state and input variables.

  Given trace can be either complete or incomplete.

  The number of performed steps (transitions) is returned in *n_steps,
  if a non-NULL pointer is given. If the initial state is not
  compatible -1 is written.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Trace_ptr
bdd_partial_trace_executor_execute
(const PartialTraceExecutor_ptr partial_executor, const Trace_ptr trace,
 const NodeList_ptr language, int* n_steps)
{
  /* local references to self */
  const BDDPartialTraceExecutor_ptr self = \
    BDD_PARTIAL_TRACE_EXECUTOR(partial_executor);

  const BaseTraceExecutor_ptr executor = \
    BASE_TRACE_EXECUTOR(partial_executor);

  Trace_ptr res = TRACE(NULL); /* failure */

  int count = -1;
  boolean success = true;

  DdManager* dd;
  BddStates trace_states;
  TraceIter step = TRACE_END_ITER;

  BddStates fwd_image = (BddStates) NULL;
  node_ptr path = Nil; /* forward constrained images will be used
                          later to compute the complete trace */

  const char* trace_description = "BDD Execution";

  /* 0- Check prerequisites */
  BDD_PARTIAL_TRACE_EXECUTOR_CHECK_INSTANCE(self);
  TRACE_CHECK_INSTANCE(trace);
  BDD_FSM_CHECK_INSTANCE(self->fsm);
  BDD_ENC_CHECK_INSTANCE(self->enc);

  dd = BddEnc_get_dd_manager(self->enc);

  step = trace_first_iter(trace);
  nusmv_assert(TRACE_END_ITER != step);

  trace_states = TraceUtils_fetch_as_bdd(trace, step,
                                         TRACE_ITER_SF_SYMBOLS, self->enc);

  /* 1- Check Start State */
  {
    bdd_ptr init_bdd = BddFsm_get_init(self->fsm);
    bdd_ptr invar_bdd = BddFsm_get_state_constraints(self->fsm);
    BddStates source_states; /* last known states */

    source_states = bdd_and(dd, init_bdd, invar_bdd);
    bdd_and_accumulate(dd, &source_states, trace_states);

    bdd_free(dd, invar_bdd);
    bdd_free(dd, init_bdd);
    bdd_free(dd, trace_states);

    if (!bdd_is_false(dd, source_states)) {

      boolean terminate = false;
      path = cons(NODE_PTR(bdd_dup(source_states)), Nil);

      ++ count;

      /* 2- Check Consecutive States are related by transition relation */
      do {
        BddStates last_state; /* (unshifted) next state */
        BddStates next_state; /* next state constraints */

        BddInputs next_input; /* next input constraints */
        BddStatesInputsNexts next_combo; /* state-input-next constraints */

        BddStatesInputsNexts constraints;

        step = TraceIter_get_next(step);
        if (TRACE_END_ITER != step) {

          next_input = \
            TraceUtils_fetch_as_bdd(trace, step, TRACE_ITER_I_SYMBOLS,
                                    self->enc);
          next_combo = \
            TraceUtils_fetch_as_bdd(trace, step, TRACE_ITER_COMBINATORIAL,
                                    self->enc);
          last_state = \
            TraceUtils_fetch_as_bdd(trace, step, TRACE_ITER_SF_SYMBOLS,
                                    self->enc);

          next_state = BddEnc_state_var_to_next_state_var(self->enc, last_state);

          if (0 < BaseTraceExecutor_get_verbosity(executor)) {
            fprintf(BaseTraceExecutor_get_output_stream(executor),
                    "-- executing step %d ... ", 1 + count);
            fflush(BaseTraceExecutor_get_output_stream(executor));
          }

          /* building constrained fwd image */
          constraints = bdd_and(dd, next_input, next_combo);
          bdd_and_accumulate(dd, &constraints, next_state);

          fwd_image =
            BddFsm_get_sins_constrained_forward_image(self->fsm, source_states,
                                                      constraints);

           /* test whether the constrained fwd image is not empty */
          if (!bdd_is_false(dd, fwd_image)) {
            if (0 < BaseTraceExecutor_get_verbosity(executor)) {
              fprintf(BaseTraceExecutor_get_output_stream(executor), "done\n");
            }
            path = cons(NODE_PTR(fwd_image), path);
            ++ count;
          }
          else {
            if (0 < BaseTraceExecutor_get_verbosity(executor)) {
              fprintf(BaseTraceExecutor_get_output_stream(executor),
                      "failed!\n");
            }
            terminate = true;
            success = false;
          }

          /* no longer used bdd refs */
          bdd_free(dd, next_input);
          bdd_free(dd, next_combo);
          bdd_free(dd, last_state);
          bdd_free(dd, next_state);

          bdd_free(dd, source_states);
          source_states = bdd_dup(fwd_image);
        }

        else {
          if (0 == count) {
            fprintf(BaseTraceExecutor_get_error_stream(executor),
                    "Warning: trace has no transitions.\n");
          }
          terminate = true;
        }
      } while (!terminate); /* loop on state/input pairs */
    } /* if has initial state */

    else {
      fprintf(BaseTraceExecutor_get_error_stream(executor),
              "Error: starting state is not initial state.\n");
      success = false;
    }

    /* 3- If last state could be reached a complete trace exists */
    if (success) {
      if (0 < count) {
        res = \
          bdd_partial_trace_executor_generate(self, fwd_image, path,
                                              count, language,
                                              trace_description);
      }

      else { /* generates a complete state of trace of length 0 */
        res = \
          bdd_partial_trace_executor_generate(self, source_states,
                                              Nil, 0, language,
                                              trace_description);
      }

      nusmv_assert(TRACE(NULL) != res);

      /* cleanup */
      walk_dd(dd, bdd_free, path);
      free_list(path);
    }

    bdd_free(dd, source_states);
  }

  /* as a final stage, verify loopbacks consistency using internal
     service. The incomplete trace is compatible (that is, a valid
     completion exists) iff exactly len(Trace) steps have been
     performed *and* loopback data for the incomplete trace applies to
     the complete one as well. */
  if (TRACE(NULL) != res) {
    if (Trace_get_length(trace) == count &&
        partial_trace_executor_check_loopbacks(partial_executor, trace, res)) {

      fprintf(BaseTraceExecutor_get_output_stream(executor),
              "-- Trace was successfully completed.\n");
    }

    else {
      Trace_destroy(res);
      res = TRACE(NULL);
    }
  }

  if (TRACE(NULL) == res) {
    fprintf(BaseTraceExecutor_get_output_stream(executor),
            "-- Trace could not be completed.\n");
  }

  if (NIL(int) != n_steps) { *n_steps = count; }
  return res;
}
コード例 #5
0
ファイル: Graph.cpp プロジェクト: ColumbiaDVMM/INDetector
int Graph::Ypos(int nodei)
{
	Node * n1;
	n1 = NODE_PTR(nodei);
	return n1->vy;
}
コード例 #6
0
ファイル: AIManager.cpp プロジェクト: mattrudder/AckZombies
/**
* CAIManager::linkNode
* @date Modified April 26, 2006
*/
void CAIManager::linkNode(CAINode* poNode)
{
	CObjectManager* poObjectManager = CObjectManager::getInstancePtr();
	CCollisionGeometry* poGeo = CCollisionGeometry::getInstancePtr();

	CObjectManager::ObjectList oNodeList;
	poObjectManager->getObjects(OBJ_AI_NODE, &oNodeList);
	CObjectManager::ObjectList::iterator oNodeIter;

	float fLength = 0.0f;
	D3DXVECTOR3 vDirection;
	std::vector<D3DXVECTOR3> vPoints;
	std::vector<float> vLengths;
	bool bLink = true;

	for (oNodeIter = oNodeList.begin(); oNodeIter != oNodeList.end(); ++oNodeIter)
	{
		bLink = true;

		// don't check against same node
		if (poNode == NODE_PTR(oNodeIter))
			continue;

		D3DXVec3Subtract(&vDirection, &NODE_PTR(oNodeIter)->getPosition(), &poNode->getPosition());
		D3DXVec3Normalize(&fLength, &vDirection, &vDirection);

		// if the node is too far away, don't even try to link to it
		// make it relative to the radius es
		if (fLength > (MAX_LINK_DISTANCE + NODE_PTR(oNodeIter)->m_fRadius + poNode->m_fRadius))
			continue;

		// collision check
		if (poGeo->checkRayCollision(vPoints, vLengths, poGeo->getRoot(), poNode->getPosition(), vDirection))
		{
			// have to check to see if the collision happened after the node we want to link to
			for (unsigned int i = 0; i < vLengths.size(); ++i)
			{
				if (vLengths[i] < fLength)
				{
					// no link
					bLink = false;
					break;
				}
			}

			if (bLink)
			{
				// will most likely to be a goal node connection
				// and for the sake of iteration we want it first
				poNode->m_loLinks.push_front(NODE_PTR(oNodeIter));

				// link it both ways
				NODE_PTR(oNodeIter)->m_loLinks.push_front(poNode);
			}
		}
		// not a single collision so they can link
		else
		{
			// will most likely to be a goal node connection
			// and for the sake of iteration we want it first
			poNode->m_loLinks.push_front(NODE_PTR(oNodeIter));

			// link it both ways
			NODE_PTR(oNodeIter)->m_loLinks.push_front(poNode);
		}

		vPoints.clear();
		vLengths.clear();
	}
}
コード例 #7
0
ファイル: AIManager.cpp プロジェクト: mattrudder/AckZombies
/**
* CAIManager::createNodeLinks
* @date Modified April 26, 2006
*/
void CAIManager::createNodeLinks(void)
{
	CObjectManager* poObjectManager = CObjectManager::getInstancePtr();
	CCollisionGeometry* poGeo = CCollisionGeometry::getInstancePtr();

	CObjectManager::ObjectList oNodeList;
	poObjectManager->getObjects(OBJ_AI_NODE, &oNodeList);
	CObjectManager::ObjectList::iterator PrimaryIter, SecondIter;

	float fLength = 0.0f;
	D3DXVECTOR3 vDirection;
	std::vector<D3DXVECTOR3> vPoints;
	std::vector<float> vLengths;
	bool bLink = true;

	for (PrimaryIter = oNodeList.begin(); PrimaryIter != oNodeList.end(); ++PrimaryIter)
	{
		for (SecondIter = oNodeList.begin(); SecondIter != oNodeList.end(); ++SecondIter)
		{
			bLink = true;

			// don't check against same node
			if (NODE_PTR(PrimaryIter) == NODE_PTR(SecondIter))
				continue;

			D3DXVec3Subtract(&vDirection, &NODE_PTR(SecondIter)->getPosition(), &NODE_PTR(PrimaryIter)->getPosition());
			D3DXVec3Normalize(&fLength, &vDirection, &vDirection);

			// if the node is too far away, don't even try to link to it
			if (fLength > (MAX_LINK_DISTANCE + NODE_PTR(PrimaryIter)->m_fRadius + NODE_PTR(SecondIter)->m_fRadius))
				continue;

			// collision check
			if (poGeo->checkRayCollision(vPoints, vLengths, poGeo->getRoot(), NODE_PTR(PrimaryIter)->getPosition(), vDirection))
			{
				// have to check to see if the collision happened after the node we want to link to
				for (unsigned int i = 0; i < vLengths.size(); ++i)
				{
					if (vLengths[i] < fLength)
					{
						// no link
						bLink = false;
						break;
					}
				}

				if (bLink)
				{
					NODE_PTR(PrimaryIter)->m_loLinks.push_back(NODE_PTR(SecondIter));
				}
			}
			// not a single collision so they can link
			else
			{
				NODE_PTR(PrimaryIter)->m_loLinks.push_back(NODE_PTR(SecondIter));
			}

			vPoints.clear();
			vLengths.clear();			
		}
	}
}