示例#1
0
文件: map.c 项目: long5313828/ythtbbs
//产生一个随机地图, rate决定地图的渲染率. 地图大小应在mcMap中设定
int
gen_map(McMap * mcMap, double rate)
{
	int i, j, retv = 0, count = 0;
	int prev = 0, trans = 0, next = 0;
	double sum;
	pLinkList pList;
	pQueuePoint pqp;

	pList = init_queue();
	if (pList == NULL)
		return -1;

	srandom(time(NULL));

	//随机选择一个渲染点
	i = random() % (mcMap->row - 2) + 1;
	j = random() % (mcMap->col - 2) + 1;

	sum = mcMap->row * mcMap->col + 0.1;
	while (count / sum < rate) {	//保证一定的渲染率
		pqp = calloc(1, sizeof (QueuePoint));
		if (pqp == NULL) {
			retv = -1;
			goto END;
		}
		pqp->i = i;
		pqp->j = j;
		insert_node(pList, pqp);	//加入渲染队列
		mcMap->map[i][j].exit |= NODE_QUEUED;

		while (!is_empty(pList)) {
			pqp = read_first(pList);	//取队列首点

			//产生该点的随机出口
			gen_exit(mcMap, pqp->i, pqp->j, count / (rate * sum),
				 &prev, &trans, &next);

			//产生该点的东东, ncp, box, boss...
			gen_item(mcMap, pqp->i, pqp->j);

			//加入该点的出口点
			if (check_exit_node(pList, mcMap, pqp->i, pqp->j, 0) ||
			    check_exit_node(pList, mcMap, pqp->i, pqp->j, 1) ||
			    check_exit_node(pList, mcMap, pqp->i, pqp->j, 2) ||
			    check_exit_node(pList, mcMap, pqp->i, pqp->j, 3)) {
				retv = -1;
				goto END;
			}
			delete_node(pList, 0, 1);	//从队列中删除该点
			if ((++count) / sum > rate)	//渲染率达到就跳出
				goto END;
		}		//end of while(...)
		random_select(mcMap, &i, &j);	//如果一个点不够渲染率, 则再选择一个
	}
      END:
	free_queue(pList, 1);	//释放队列资源
	return retv;
}
示例#2
0
  void learning() {
    for(int rd = 0; rd < rounds; ++rd) {
      int j = random_select();
      int jj = j;
      bool flag = false;
      if(j >= kdim) { flag = true; jj -= kdim; }
      double delta = lambda;
      for(int i = 0; i < nsamples; ++i) {
        if(X[i][jj] != 0) {
	        double tmp = flag ? -X[i][jj] : X[i][jj];
          delta += L_diff(Z[i], Y[i]) * tmp;
        }
      }
      delta *= 1. / nsamples;
      double ita = std::max(-W[j], -delta * 4);
      W[j] += ita;
      for(int i = 0; i < nsamples; ++i) {
        if(X[i][jj] != 0) {
	        double tmp = flag ? -X[i][jj] : X[i][jj];
          Z[i] += ita * tmp;
        }
      }
    } // round loop
  }