Пример #1
0
void test_goto_loop_unroll_factor(const T* first, int count, const char *label) {
  int i;
  
  start_timer();
  
  for(i = 0; i < iterations; ++i) {
    T result = 0;
	int n = 0;
	
	if ((count - n) >= F) {
loop2_start:
		loop_inner_body<F,T>::do_work(result,first, n);
		n += F;
		
		if (n < (count - (F-1)))
			goto loop2_start;
	}

	if (n < count) {
loop_start:
		result += complete_hash_func( first[n] );
		++n;
		
		if (n != count)
			goto loop_start;
	}
	
    check_sum<T>(result);
  }
  
  record_result( timer(), label );
}
Пример #2
0
//你要完成的功能总入口
void search_route(char *graph[5000], int edge_num, char *condition)
{
    unsigned short result[] = {2, 6, 3};//示例中的一个解

    for (int i = 0; i < 3; i++)
        record_result(result[i]);
}
Пример #3
0
//你要完成的功能总入口
void search_route(char *graph[5000], int edge_num, char *condition)
{
	u_short vertexCondition[600];
	conditionCharToUshort(vertexCondition, condition);
    for (int i = 0; i < 600; ++i)
    {
    	std::cout << std::setw(2) << vertexCondition[i];
    	if((i+1) % 10 == 0)
    		std::cout << std::endl;
    }
    std::cout << "startVertex = " << startVertex << std::endl;
    std::cout << "endVertex   = " << endVertex << std::endl;

    std::cout << "\ntest grap to crosslist ................................" << std::endl;
    pCrossListHead  crosslist = creatCrossList(edge_num);
    u_short vertexNum = graphCharToCrosList(crosslist, graph, edge_num);
    std::cout << "vertexNum = " << vertexNum << std::endl;
   

    for (int i = 0; i < vertexNum; ++i)
    {
        std::cout << "mytovertex" << i << " is:" << mapMyNoToVertex[i] << std::endl;
    }
#if 0
    for (int i = 0; i < vertexNum; ++i) //less than 1
    {
        std::cout << "vertextomy" << i << " is " << mapVertexToMyNo[i]  << std::endl;
    }
#endif
	printCrossList(crosslist, creatNodeNum);
    unsigned short result[] = {2, 6, 3};//示例中的一个解
    for (int i = 0; i < 3; i++)
        record_result(result[i]);
}
Пример #4
0
//你要完成的功能总入口
void search_route(char *topo[5000], int edge_num, char *demand)
{
    unsigned short result[] = {2, 6, 3};//示例中的一个解

    for (int i = 0; i < 3; i++)
        record_result(result[i]);
}
Пример #5
0
//你要完成的功能总入口
void search_route(char *topo[5000], int edge_num, char *demand)
{
	graph=CreatGraph(topo,edge_num);
	AnalysisCondition(demand);
	pthread_t thread_id;

	pthread_mutex_init(&lock,NULL);
	pthread_cond_init(&condition,NULL);
	pthread_mutex_lock(&lock);
	struct timespec outtime;
	struct timeval now;
	gettimeofday(&now, NULL);
	 outtime.tv_sec = now.tv_sec + 9;
	 //outtime.tv_nsec = now.tv_usec * 1000;
	pthread_create(&thread_id,NULL,run,NULL);
	pthread_cond_timedwait(&condition,&lock,&outtime);
	pthread_mutex_unlock(&lock);
	pthread_cancel(thread_id);
	vector<int> *path=solution.GetPath();
	if(path->size()){
		for(int id:*path)
			record_result(id);
	}
    delete graph;
}
Пример #6
0
//你要完成的功能总入口
void search_route(char *topo[5000], int edge_num, char *demand)
{
	// DATA EXTRACTION:
	// Extract data from charater string array topo[][] to 2 dimensional array 'topoArray'.
	// string format:
	// 		linkID, sourceID, destinationID, cost (end of line is not \n!)
	int topoArray[edge_num][4];
	int includingSet[MAX_INCLUDING_SET];
	int cntPass;					// record valid number of elements in includingSet.
	int sourceID = 0, destinationID = 0;
	memset(topoArray, 0, sizeof(topoArray));	// initialize to 0.
	memset(includingSet, 0, sizeof(includingSet));

	getTopoArray(edge_num, topo, topoArray);
	getDemand(demand, includingSet, sourceID, destinationID, cntPass);

	//================================================================================
	// CREAT ADJACENT LIST.
	EdgeNode* nodeArray[MAX_VERTEX_NUM];		// array containing the first element of each list. Index of the array is nodeID.
	memset(nodeArray,0,sizeof(nodeArray));
	change2List(nodeArray,topoArray,edge_num);
#ifdef ROUTEDEBUG
	testChange2List(nodeArray);
#endif // DEBUG

	bool nodeArrayState[MAX_VERTEX_NUM];
	memset(nodeArray,0,sizeof(nodeArrayState));

	//priority_queue<int,vector<int>,greater<int> > pq;
	unsigned short result[] = {2, 6, 3};//示例中的一个解
	for (int i = 0; i < 3; i++)
		record_result(result[i]);
}
void test_accumulate(Iterator first, Iterator last, T zero, const char *label) {
	int i;

	start_timer();

	for(i = 0; i < iterations; ++i)
		check_sum( double( accumulate(first, last, zero) ) );

	record_result( timer(), label );
}
Пример #8
0
void search_route(char *topo[5000], int edge_num, char *demand)
{
	int n1, n2, num, cost;
	char vS[MAXS*2] = {'\0'};
	
   	for(int i=0; i<MAXV; i++){
		for(int j=0; j<MAXV; j++)
			netMap[i][j].num = INVALID;  //edge between i and j
  	}

	printf("init netMap ok\n");
	for(int i=0; i<edge_num; i++){
		sscanf(topo[i], "%d,%d,%d,%d", &num, &n1, &n2, &cost);
		if(INVALID != netMap[n1][n2].num && cost >= netMap[n1][n2].cost) continue;
		netMap[n1][n2].cost = cost;
		netMap[n1][n2].num = num;
	}
	printf("fill netMap ok\n");

	sscanf(demand, "%d,%d,%s", &start, &end, vS);

	char *tmp = vS;
	int index = 0;
	int val = 0;
	while(*tmp != '\0'){
		while(*tmp != '|' && *tmp != '\0'){
			val = val*10 + (*tmp - '0');
			tmp++;
		}
		vSearch[index++] = val;
		numofvS++;
		if( '\0' ==  *tmp) break;
		val = 0;
		tmp++;
	}

	for(index = 0; index < numofvS; index++){
		printf("%d ", vSearch[index]);
	}
	printf("\n");

	
	dfs(start, 0);
	if(0 == resultnum){
	    printf("NA\n"); 
	}
	else{
		for (int i = 0; i < resultnum; i++)
      		record_result(result[i]);
	}
	return;
}
void test_quicksort(Iterator firstSource, Iterator lastSource, Iterator firstDest,
					Iterator lastDest, T zero, const char *label) {
	int i;

	start_timer();

	for(i = 0; i < iterations; ++i) {
		::copy(firstSource, lastSource, firstDest);
		quicksort< Iterator, T>( firstDest, lastDest );
		verify_sorted( firstDest, lastDest );
	}
	
	record_result( timer(), label );
}
void test_hoisted_variable1(T* first, int count, T v1, const char *label) {
  int i;
  
  start_timer();
  
  for(i = 0; i < iterations; ++i) {
    T result = 0;
    for (int n = 0; n < count; ++n) {
		result += first[n];
	}
	result += count * v1;
    check_shifted_variable_sum<T, Shifter>(result, v1);
  }
  
  record_result( timer(), label );
}
Пример #11
0
void output_ans(int *a)
{
	std::cerr << "ans = " << score << std::endl;
	for (int i = 1; i <= a[0]; ++i)
	{
		record_result(a[i] - 1);
		if(i > 1)
			std::cerr << "|";
		std::cerr << a[i] - 1;
	}
	std::cerr << std::endl;

	for (int i = 1, u = S; i <= a[0]; ++i)
	{
		std::cerr << u << "->";
		u = edge[a[i]].v;
	}
	std::cerr << T << std::endl;
}
Пример #12
0
void search_route(TopoNode *t, int n, DemandSet *d) {
  topo = t;
  node_scope = n;
  demand = d;
  path = new Edge[node_scope]();
  path_size = 0;
  path_bitmap = new Bitmap(node_scope);
  good = new Path *[node_scope]();
  dangling = new TopoNode[node_scope]();
  visited = new Bitmap(node_scope);

  topo[demand->end].out_degree = 0;

  Path *end = new Path();
  end->size = 0;
  end->pass_count = 0;
  end->path = new Edge[1]();
  end->bitmap = new Bitmap(node_scope);
  end->bitmap->set(demand->end);
  end->next = NULL;
  good[demand->end] = end;

  LOG("DEMAND => ");
  BITMAP_SHOW(demand->bitmap);

  LOG("ROUTE SEARCH START ...\n");
  search_node(0, demand->start);
  LOG("ROUTE SEARCH END ...\n");

  for (int i = 0; i < path_size; i++) {
    record_result(path[i].arrow->number);
  }

  /**
  delete[] path;
  delete path_bitmap;
  delete good_nodes;
  delete good;
  delete dangling;
  **/
}
Пример #13
0
void test_for_loop_unroll_factor(const T* first, int count, const char *label) {
  int i;
  
  start_timer();
  
  for(i = 0; i < iterations; ++i) {
    T result = 0;
	int n = 0;
	
    for (; n < (count - (F-1)); n += F) {
		loop_inner_body<F,T>::do_work(result,first, n);
	}
	
    for (; n < count; ++n) {
		result += complete_hash_func( first[n] );
	}
	
    check_sum<T>(result);
  }
  
  record_result( timer(), label );
}
Пример #14
0
//你要完成的功能总入口
void search_route(char *topo[5000], int edge_num, char *demand)
{
	
	start_time = get_now_time();
	read_topo(topo);
	read_demand(demand);
	
	if((node_size<=100) || node_size > 500)
	{
		int tmp_up = find_cnt_up + 2;
		bool flag = false;
		
		if(node_size<=500)
		{
			flag = true;
		}
		if(node_size <= 100) // 顶点小于100搜一次即可
		{
			find_cnt_up = 4;
			tmp_up = find_cnt_up+1;
		}
		else if(must_node_cnt < 15)
		{
			tmp_up++;
			find_cnt_up++;
		}
	
		//if(node_size >= 200)
		//	tmp_up += 5;
	
		cout << "flag:" << flag << endl; 
	
		while(find_cnt_up < tmp_up) //&& good_cost < last_cost)
		{
			//last_cost = good_cost;
			bool tag[MAX_NODE_NUM] = {0};
			int cnt = must_node_cnt;

		 	tag[sid] = true;
			int now_time = get_now_time();
			int res,tb;
			if(node_size>=100 && node_size<150) 
				res = dfs(sid,tag,cnt,tb);		
			else res = dfs_large(sid,tag,cnt,tb,flag);

			if(res == END_OUT)
				break;
			cout << "find res : " << res << endl;	
			find_cnt_up++;
		}
	}
	else 
	{
		lp_solver(sid,eid,edge_cnt,edge_set,node_size,must_node,ans_sum,ans,node_next);
		update_ans(ans_sum,ans);
	}

	//cout << ans_sum << endl;
	cout << "route_len: " << good_sum << endl;
	cout << "good_cost: " << good_cost << endl;

	for (int i = 0; i < good_sum; i++)
	{
	      if (0 == i)
			//cout << edge_set[ans[i]].edge_id;
			cout << good_ans[i];
	      else cout<< "|" << good_ans[i];
	
  	      record_result(edge_set[good_ans[i]].edge_id);
	}
	cout << endl;		
	end_time = get_now_time();
	cout << "used time : " << (end_time - start_time) << endl;

}
Пример #15
0
int main(int argc, char **argv) {
    int i;
    FLOAT *result;
    time_t start, end;

    minid = 0;
    process_args(argc, argv);

    if (jump_p == 0) jump_p = 0.15;
    follow_p = 1.0 - jump_p;

//    maxid = 0;
    N = 1 + (maxid - minid);

    assert((biases = (FLOAT *)calloc((maxid+1) , sizeof(FLOAT))) != NULL);
    assert((old_pr = (FLOAT *)malloc((maxid+1) * sizeof(FLOAT))) != NULL);
    assert((new_pr = (FLOAT *)malloc((maxid+1) * sizeof(FLOAT))) != NULL);

    struct node_info anode;
    anode.out_random = 0;
    graph_nodes.insert(graph_nodes.end(), maxid+1, anode);

    FILE* linkfp = fopen(linkfile, "r");
    //read all links
    int srcid, dstid;
    FLOAT edgeweight;
    while( (fscanf(linkfp, "%d\t%d\t%f\n", &srcid, &dstid, &edgeweight)) != EOF){
      if (edgeweight <= 0) continue;
      graph_nodes[dstid].inlinks.insert(graph_nodes[dstid].inlinks.end(), srcid);
      graph_nodes[dstid].inweights.insert(graph_nodes[dstid].inweights.end(), edgeweight);
      graph_nodes[srcid].out_random += edgeweight;
    }

    fclose(linkfp);

    //read the node biases for random jump
    if (biasfile != NULL) {
      FILE* biasfp = fopen(biasfile, "r");
      int srcid;
      FLOAT bias;
      double total_bias = 0;
      while( (fscanf(biasfp, "%d\t%f\n", &srcid, &bias)) != EOF) {
        biases[srcid] = bias;
        total_bias += bias;
      }
      fclose (biasfp);
      // now normalize the biases
      for (int i=minid; i <= maxid; i++) {
           biases[i] /= total_bias;
      }
      printf ("total:.......... %f\n", total_bias);
    }
    //there is no bias file, so jump uniformly
    else {
      double initial_prob = 1.0 / (N + .0);
      for (int i=0; i < minid; i++) {
        biases[i] = 0;
      }
      for (int i=minid; i <= maxid; i++) {
        biases[i] = initial_prob;
      }
    }

    fprintf(proglog, "Computing outp");
    start = time(NULL);
    compute_outp(minid, maxid);
    end = time(NULL);
    fprintf(proglog, " time = %ds (outp)\n", (int)(end-start));
    
    result = doit();
    record_result(result);
    if (result) free(result);
    
    free(biases);
}
Пример #16
0
int main(){
		int gender;
		int wealthpercentile;
		int i,j,l;
		int deductgrid;
		int wealth;
		int wx;
		int grid;
		int temp_test;
		int Sstart;


		double alpha;
		int offset;
	    char filename[11]={"table.txt"};


		task_group tasks;

		time_t time_began,time_end;


		/*
		output variable 
		*/

		//memset(&CalcStruct[0],0,sizeof(CALCSTRUCT));

		gender=1;				//0 is female 1 is male
		deductgrid=DEDUCTGRID;
		//wealthpercentile=3;		//0-9 different wealth distribution 


		/*
		calculating declaration:
		this time run for gird*4 total size 
		from 4-8
		so the program make some adjustment
		
		*/


		cout<<"calculating declaration: this time run for gird*4 total size from 4-8 \n so the program make some adjustment"<<endl;



				
		cur_time();
		offset =0;
		Sstart=START;

		Parallel_LTCI  *LTCI[10];

		
		{
			

		for(gender=1;gender<2;gender++){
		// initilize the class of LTCI 
		for(wealthpercentile=Sstart;wealthpercentile<10-offset;wealthpercentile ++)
		{
		switch (wealthpercentile){
		case 0: wealth=40000;	alpha=0.98;	wx=0;		grid=20;	break;
		case 1: wealth=58450;	alpha=0.98;	wx=0;		grid=20;	break;
		case 2: wealth=93415;	alpha=0.91;	wx=20000;	grid=40;	break;
		case 3: wealth=126875;	alpha=0.82;	wx=30000;	grid=75;	break;
		case 4: wealth=169905;	alpha=0.70;	wx=10000;	grid=100;	break;
		case 5: wealth=222570;	alpha=0.60;	wx=50000;	grid=130;	break;
		case 6: wealth=292780;	alpha=0.52;	wx=20000;	grid=175;	break;
		case 7: wealth=385460;	alpha=0.41;	wx=40000;	grid=225;	break;
		case 8: wealth=525955;	alpha=0.35;	wx=40000;	grid=300;	break;
		case 9: wealth=789475;	alpha=0.26;	wx=75000;	grid=450;	break;


		}
		//wealth =wealth*224.937/172.192;


		LTCI[int(wealthpercentile)-Sstart]=new Parallel_LTCI(wealthpercentile,gender,deductgrid,wealth,wx,grid,alpha);
		

		}
		

			calcSetup();
			inputData(gender);


			for(i=0;i<10-Sstart-offset;i++){
				Setup(LTCI[i]);
				inData(gender,LTCI[i]);

			}



		cout<<"calculating for 0:female 1: male ------"<<gender<<endl;
		cout<<"deductile period is : \t"<<deductgrid<<endl;
		cout<<"**********************************************"<<endl;
		cout<<"**********************************************"<<endl;




		/*task for 1 to 4*/
		
		tasks.run([&gender,&LTCI,&offset,&Sstart](){
			   // int wealthpercentile; 
			for(int wealthpercentile=Sstart;wealthpercentile<5-offset;wealthpercentile++)
				LTCI[wealthpercentile-Sstart]->comput();



		});
		////	/*task for  4*/

		//tasks.run([&gender,&LTCI,&Sstart](){
		//	int wealthpercentile=4-Sstart; 				
		//	LTCI[wealthpercentile]->comput();

		//});



	//	/*task for  5*/
	
			tasks.run([&gender,&LTCI,&Sstart](){
				int wealthpercentile=5-Sstart; 				
				LTCI[wealthpercentile]->comput();

				});

		 /*task for  6*/
			tasks.run([&gender,&LTCI,&Sstart](){
				int wealthpercentile=6-Sstart; 				
				LTCI[wealthpercentile]->comput();

			});


	//		/*task for 7*/
		tasks.run([&gender,&LTCI,&Sstart](){
			int wealthpercentile=7-Sstart;

			LTCI[wealthpercentile]->comput();
		});

			/*task for 8*/
			tasks.run([&gender,&LTCI,&Sstart](){
				int wealthpercentile=8-Sstart;

				LTCI[wealthpercentile]->comput();
			});


			/*task for  9*/
			tasks.run_and_wait([&gender,&LTCI,&Sstart](){
				int wealthpercentile=9-Sstart;			
				LTCI[wealthpercentile]->comput();			

			});


		}
		// output some variables

		if(para.MWcount==0){if (gender==0) para.MW=1.058; else para.MW=0.5;}
		else if(para.MWcount==1){if (gender==0) para.MW=0.6; else para.MW=0.3;}
		else if(para.MWcount==2){if (gender==0) para.MW=1.358127; else para.MW=0.6418727;}
		else if(para.MWcount==3){if (gender==0) para.MW=1.358127; else para.MW=0.6418727;}
		else if(para.MWcount==4){if (gender==0) para.MW=1; else para.MW=1;}

		ofstream out(filename, ios::app);
		if (out.is_open())   
		{
// 			out<<"*********************************************************************"<<endl;
			out<<"deductile grid is :"<<deductgrid<<endl;
			for(i=0;i<10-offset-Sstart;i++) record_result(i,LTCI[i]);
// 			out<<"table  "<<endl;
// 			for(i=0;i<10-offset-Sstart;i++){
// 				out<<i+Sstart<<"0th : \t 1 \t 2 \t 3 \t 4"<<endl;
// 				out<<record.MUstar[i]/record.EPDVMedical[i]<<'\t';
// 				out<<record.Mstar[i]/record.EPDVMedical[i]<<"\t";
// 				out<<(record.MUstar[i] - record.Mstar[i])/record.Istarown[i]<<"\t";
// 				out<<(1-(record.Istarown[i]-(record.MUstar[i]-record.Mstar[i]))/(record.Istarown[i]/para.MW))<<"\t";
// 				out<<record.wequiv[i]<<"\t";
// 				out<<endl;
// 			}
			out<<"*********************************************************************"<<endl;
			out<<"----------------------------------------------------------------------"<<endl;
			out<<"---------------------------------raw data---------------------------- "<<endl;
			out<<"----------------------------------------------------------------------"<<endl;
			out<<"index for the data:  S_Madicaid \t S_Insurance \t S_Madicaid_NI \t S_Medcost \t S_wequiv \t S_medicare \t S_med_joint_NI \t S_med_joint \t S_OOP \t S_OOP_NI"<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_Madicaid[i]<<"\t";
			out<<endl;			
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_Insurance[i]<<"\t";
			out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_Madicaid_NI[i]<<"\t";
				out<<endl;	
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_Medcost[i]<<"\t";
				out<<endl;	
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_wequiv[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_medicare[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_med_joint_NI[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_med_joint[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_OOP[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.S_OOP_NI[i]<<"\t";
				out<<endl;
			out<<"EPDVMedical  wequive Mstar Istar  MUstar Istarnon "<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.EPDVMedical[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.wequiv[i]<<"\t";
				out<<endl;	
			for(i=0;i<10-offset-Sstart;i++) out<<record.Mstar[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.Istarown[i]<<"\t";
				out<<endl;	
			for(i=0;i<10-offset-Sstart;i++) out<<record.MUstar[i]<<"\t";
				out<<endl;
			for(i=0;i<10-offset-Sstart;i++) out<<record.Istarnone[i]<<"\t";
				out<<endl;	



			out.close();  

		}


		for(i=0;i<10-offset-START;i++) delete LTCI[i];


	}



		system("pause");
		return 0;


	}
Пример #17
0
void search_route(char *topo[5000], int edge_num, char *demand)
{
	int i,j,k,m,temp,vertex_num=0,demand_num=0,best_gene_num; //暫存变量i,j,k,m,temp,顶点数vertex_num,总需求列表长度demand_num,种群大小best_gene_num
	//拓扑矩阵topo_array,权值矩阵cost,路径矩阵path,边ID矩阵edge_array,总需求列表demand_col,父代及其子代构成的种群best_gene
	int topo_array[5000][4]={0},cost[MaxVertex][MaxVertex],path[MaxVertex][MaxVertex]={0},edge_array[MaxVertex][MaxVertex]={0},demand_col[MaxDemand+2]={0},best_gene[MaxDemand*2][600];
	bool flag;  //标志位
	for(i=0;i<MaxVertex;i++)			//cost矩阵初始化
{
		std::fill_n(cost[i], MaxVertex, INF);
		cost[i][i]=0;
}
	for(i=0;i<edge_num;i++)			//读取topo.csv表至topo_array矩阵
	{
		j=0;k=0;
		flag=true;
		while((*(topo+i))[j]!='\0')
		{
			if(flag)
			{
				temp=int((*(topo+i))[j] - '0');
				j++;
				flag=false;
			}
			else
			{
				temp=temp*10+int((*(topo+i))[j] - '0');
				j++;
			}
			if((*(topo+i))[j]==',')
			{
				topo_array[i][k]=temp;
				flag=true;
				j++;k++;
			}
			if((*(topo+i))[j]==10||(*(topo+i))[j]=='\0')
			{
				topo_array[i][k]=temp;
				edge_array[i][k]=i;             //路径与边ID的对应表
				break;
			}
		}
		if(topo_array[i][1]>vertex_num)vertex_num=topo_array[i][1];     //求顶点数
		if(topo_array[i][2]>vertex_num)vertex_num=topo_array[i][2];		//求顶点数
		cost[topo_array[i][1]][topo_array[i][2]]=topo_array[i][3];				//初次权值矩阵初始化
		path[topo_array[i][1]][topo_array[i][2]]=topo_array[i][2];				//初次路径矩阵初始化
	}
	vertex_num+=1;			//求顶点数
	for(k=0;k<vertex_num;k++)      	//Floyd算法
	{
		for(i=0;i<vertex_num;i++)
		{
			for(j=0;j<vertex_num;j++)
			{
				if(cost[i][j]>(cost[i][k]+cost[k][j]))
				{
					cost[i][j]=cost[i][k]+cost[k][j];
					path[i][j]=k;
				}
			}
		}
	}
j=0;k=0;
flag=true;
while(demand[j]!='\0')   //读取demand.csv表至demand_col
{
	if(flag)
	{
			temp=int(demand[j] - '0');
			j++;
			flag=false;
	}
	else
	{
			temp=temp*10+int(demand[j] - '0');
			j++;
	}
	if(demand[j]==','||demand[j]=='|')
	{
			demand_col[k]=temp;
			flag=true;
			j++;k++;demand_num++;
	}
	if(demand[j]==10||demand[j]=='\0')
	{
			demand_col[k]=temp;demand_num++;
			break;
	}
}
if(demand_num>4)      //需求总长度大于4,即有3个或以上的经由点的初始种群产生
	{
	best_gene_num=(demand_num-2)*2;
	for(i=1;i<demand_num-1;i++)		//产生父代1
	{
		best_gene[0][i]=demand_col[i+1];
	}
	best_gene[0][0]=demand_col[0];best_gene[0][demand_num-1]=demand_col[1];
	for(i=1;i<demand_num-1;i++)		//产生父代2
		{
			best_gene[1][i]=demand_col[demand_num-i];
		}
	best_gene[1][0]=demand_col[0];best_gene[1][demand_num-1]=demand_col[1];
	m=1;
	for(i=2;i<demand_num-1;i++)
	{
		for(j=0;j<demand_num;j++)
		{
			best_gene[i][j]=best_gene[0][j];
		}
		temp=best_gene[i][m];best_gene[i][m]=best_gene[i][m+1];best_gene[i][m+1]=temp;
		m++;
	}
	m=1;
	for(;i<best_gene_num-1;i++)
	{
		for(j=0;j<demand_num;j++)
		{
			best_gene[i][j]=best_gene[1][j];
		}
		temp=best_gene[i][m];best_gene[i][m]=best_gene[i][m+1];best_gene[i][m+1]=temp;
		m++;
	}
	}
else
{
	best_gene_num=demand_num-2;
	for(i=1;i<demand_num-1;i++)		//产生父代1
		{
			best_gene[0][i]=demand_col[i+1];
		}
		best_gene[0][0]=demand_col[0];best_gene[0][demand_num-1]=demand_col[1];
 if(best_gene_num==2)      //若只有一个经由点则只有一个父代,不需要子代
 {
	 for(i=1;i<demand_num-1;i++)		//产生父代2
	 			{
	 				best_gene[1][i]=demand_col[demand_num-i];
	 			}
	 best_gene[1][0]=demand_col[0];best_gene[1][demand_num-1]=demand_col[1];
 }
}
	print_array(vertex_num,vertex_num,cost);
	print_array(vertex_num,vertex_num,path);
	/*
    for(i=0;i<vertex_num;i++)
    	{
    		for(j=0;j<vertex_num;j++)
    				{
    					printf ("%d ",cost[i][j]);
    				}
    		printf("\n");
    	}
    for(i=0;i<vertex_num;i++)
    	{
    		for(j=0;j<vertex_num;j++)
    				{
    					printf ("%d ",path[i][j]);
    				}
    		printf("\n");
    	}
    	*/
	unsigned short result[] = {2, 6, 3};//示例中的一个解
    for (int i = 0; i < 3; i++)
        record_result(result[i]);
}
Пример #18
0
bool UnitTestDriver::run_tests(std::vector<UnitTest *>& tests_to_run, const ArgumentMap& kwargs)
{
    std::time_t start_time = std::time(0);

    bool verbose = kwargs.count("verbose");
    bool concise = kwargs.count("concise");
    
    std::vector< TestResult > test_results;

    if (verbose && concise)
    {
        std::cout << "--verbose and --concise cannot be used together" << std::endl;
        exit(EXIT_FAILURE);
    }

    if (!concise)
        std::cout << "Running " << tests_to_run.size() << " unit tests." << std::endl;

    for(size_t i = 0; i < tests_to_run.size(); i++){
        UnitTest& test = *tests_to_run[i];

        if (verbose)
            std::cout << "Running " << test.name << "..." << std::flush;

        try
        {
            // time the test
            std::clock_t start = std::clock();

            // run the test
            test.run();

            // test passed
            record_result(TestResult(Pass, std::clock() - start, test), test_results);
        } 
        catch (unittest::UnitTestFailure& f)
        {
            record_result(TestResult(Failure, std::numeric_limits<std::clock_t>::max(), test, f.message), test_results);
        }
        catch (unittest::UnitTestKnownFailure& f)
        {
            record_result(TestResult(KnownFailure, std::numeric_limits<std::clock_t>::max(), test, f.message), test_results);
        }
        catch (std::bad_alloc& e)
        {
            record_result(TestResult(Error, std::numeric_limits<std::clock_t>::max(), test, e.what()), test_results);
        }
        catch (unittest::UnitTestError& e)
        {
            record_result(TestResult(Error, std::numeric_limits<std::clock_t>::max(), test, e.message), test_results);
        }

        // immediate report
        if (!concise)
        {
            if (verbose)
            {
                switch(test_results.back().status)
                {
                    case Pass:
                        std::cout << "\r[PASS] ";
                        std::cout << std::setw(10) << 1000.f * float(test_results.back().elapsed) / CLOCKS_PER_SEC << " ms";
                        break;
                    case Failure:
                        std::cout << "\r[FAILURE]           "; break;
                    case KnownFailure:
                        std::cout << "\r[KNOWN FAILURE]     "; break;
                    case Error:
                        std::cout << "\r[ERROR]             "; break;
                    default:
                        break;
                }

                std::cout << " " << test.name << std::endl;
            }
            else
            {
                switch(test_results.back().status)
                {
                    case Pass:
                        std::cout << "."; break;
                    case Failure:
                        std::cout << "F"; break;
                    case KnownFailure:
                        std::cout << "K"; break;
                    case Error:
                        std::cout << "E"; break;
                    default:
                        break;
                }
            }
        }

        if (!post_test_sanity_check(test, concise))
        {
            return false;
        }

        std::cout.flush();
    }

    double elapsed_minutes = double(std::time(0) - start_time) / 60;

    // summary report
    if (!concise)
        report_results(test_results, elapsed_minutes);


    // if any failures or errors return false
    for(size_t i = 0; i < test_results.size(); i++)
        if (test_results[i].status != Pass && test_results[i].status != KnownFailure)
            return false;

    // all tests pass or are known failures
    return true;
}
Пример #19
0
//你要完成的功能总入口
void search_route(char *topo[5000], int edge_num, char *demand)
{
    StartTime();

#ifndef NDEBUG
    PrintTime();
#endif // NDEBUG

    for(int i=0;; i++)//input topo[
    {
        int num,countNum;
        num=0;
        countNum=0;
        for(int j=0;; j++)
        {
            if(topo[i][j]>='0' && topo[i][j]<='9')
            {
                num=num*10+topo[i][j]-'0';
            }
            else
            {
                switch(countNum)
                {
                case 0:
                    link=new LINK;
                    link->LinkID=num;
                    num=0;
                    countNum++;
                    break;
                case 1:
                    link->SourceID=num;
                    num=0;
                    countNum++;
                    break;
                case 2:
                    link->DestinationID=num;
                    num=0;
                    countNum++;
                    break;
                case 3:
                    link->Cost=num;
                    num=0;
                    countNum=0;
                    links.push_back(*link);
                    nodes[link->SourceID].nodeLinks.push_back(link);
                    nodes[link->SourceID].ID=link->SourceID;
                    nodes[link->DestinationID].ID=link->DestinationID;
                    break;
                }
                if(countNum==0)
                {
                    break;
                }
            }
        }
        if((int)links.size()==edge_num) break;
    }

    for(int i=0,num=0,countNum=0; ; i++) //input demand
    {
        if(demand[i]>='0' && demand[i]<='9')
        {
            num=num*10+demand[i]-'0';
        }
        else
        {
            switch(countNum)
            {
            case 0:
                SourceID=num;
                num=0;
                countNum++;
                break;
            case 1:
                DestinationID=num;
                num=0;
                countNum++;
                break;
            case 2:
                V.push_back(num);
                num=0;
                break;
            }
        }
        if(demand[i]=='\n' || demand[i]=='\0') break;
    }
    sort(V.begin(),V.end());

    if(StartDeepSearch()==false)
    {
        cout<<"NA"<<endl;
    }
    else
    {
        int linkSize;
        linkSize=path.linkIDs.size();
        for(int i=0; i<linkSize; i++)
        {
            record_result((unsigned short)path.linkIDs[i]);
        }
#ifndef NDEBUG1
        cout<<endl<<path.cost<<endl<<endl;
#endif // NDEBUG1
    }

    /*unsigned short result[] = {2, 6, 3};//示例中的一个解

    for (int i = 0; i < 3; i++)
        record_result(result[i]);*/
}
Пример #20
0
void search_route(char *topo[5000], int edge_num, char *demand)
{
    //unsigned short result[] = {2, 6, 4};
    
    AtoB distM[maxnum][maxnum];
    
    int begin;
    int end;
    int id;
    int dist;

    
    /*
    *init the matrix
    *
    *
	*/

    //TODO use dist[] not matrix to change 600

	for (int i = 0; i < 100; ++i)
	{
		for (int j = 0; j < 100; ++j)
		{
			if (i == j)
			{
				distM[i][j].dist = 0;
			}else{
				distM[i][j].dist = inf;
			}
		}
	}



    // for (int i = 0; i < edge_num; i++){
    	
    //     	int begin = ((int)*(topo[i]+2) - 48);
    //     	int end = ((int)*(topo[i]+4) - 48);

    //     	distM[begin][end].id = ((int)*(topo[i]) - 48);
        	
    //     	distM[begin][end].dist = ((int)*(topo[i]+6)-48);
        	
    // }
    
    /* format the matrix */
    for (int i = 0; i < edge_num; ++i)
    {
    	char* token = strtok( topo[i], ",");
    	int k = 0;
    	while( token != NULL ) {

	        //printf( "result is %s\n", result );
	        if (k == 0)
	        {
	        	id = atoi(token);
	        }else if (k == 1)
	        {
	        	begin = atoi(token);
	        }else if (k == 2)
	        {
	        	end = atoi(token);
	        }else if (k ==3)
	        {
	        	dist = atoi(token);
	        }
    		k++;
	        token = strtok( NULL, "," );

   		}     
    	

    	distM[begin][end].id = id;
        	
    	distM[begin][end].dist = dist;

        printf( "from %d to %d ,token is :%d ", begin, end, distM[begin][end].dist );

	    //token = strtok( NULL, ",");
	    printf("\n");
    }
 
	printf("\n\n\n\n");

/*
*	get the necessary point which is in demand.csv
*
*/
	
	/* for demand */

    int SourceID;
    int DestinationID;
    int IncludingSet[50];
	
	//int len = strlen(demand);
	//printf("length is :%d\t",len);
	char* token = strtok( demand, ",|");
	int i = 0;
	int IncludingSetLength = 0;

    while( token != NULL )
    {
        /* While there are tokens in "string" */
        //printf( "token is :%s ", token );

    	if (i == 0)
    	{
    		SourceID = atoi(token);
    	}else if (i ==1)
    	{
    		DestinationID = atoi(token);
    	}else if (i >= 2)
    	{
    		IncludingSet[IncludingSetLength] = atoi(token);
    		IncludingSetLength++;
    	}

        /* Get next token: */
    	
        token = strtok( NULL, ",|");
        i++;
    }
    printf( "start :%d \n", SourceID );
    printf( "end :%d \n", DestinationID );
    printf( "number :%d \n", IncludingSetLength);
    // for (int i = 0; i < IncludingSetLength; ++i)
    // {
        
    // 	printf( "access :%d ", IncludingSet[i] );

    // }
    

    int arrNum = 1;
    int midDist = inf;

    int totalDist = inf;

    int firstDist = inf;
    int lastDist = inf;

    int tempDist = 0;

    int firstdistance[maxnum];     // 表示当前点到源点的最短路径长度
    int firstprevpoint[maxnum];     // 记录当前点的前一个结点

    for(int i=0; i<=edge_num/2; ++i)
        firstdistance[i] = maxint;

    int lastdistance[maxnum];     // 表示当前点到源点的最短路径长度
    int lastprevpoint[maxnum];     // 记录当前点的前一个结点

    for(int i=0; i<=edge_num/2; ++i)
        lastdistance[i] = maxint;

    int copyIncludingSet[50];

    do 
    {  
        /* Dijkstra(int SourceID, int DestinationID, int distance[], int prevpoint[], AtoB distM[][]);*/

        midDist = midPath(IncludingSetLength, IncludingSet, distM);
        firstDist = Dijkstra(IncludingSet[0] , SourceID, firstdistance, firstprevpoint, distM);
        lastDist = Dijkstra(DestinationID, IncludingSet[IncludingSetLength], lastdistance, lastprevpoint, distM);

        tempDist = firstDist + midDist + lastDist;
        cout << firstDist << " " << midDist << " " << lastDist << "\n" ;
        if (tempDist < totalDist)
        {
            totalDist = tempDist;
            //copy the shortest IncludingSet to the new array
            //the last time we copied is the shortest IncludingSet
            
            //memcpy(copyIncludingSet,IncludingSet, IncludingSetLength*sizeof(int)); 

            for (int i = 0; i < IncludingSetLength; ++i)
            {
                copyIncludingSet[i] = IncludingSet[i];
            }
            cout << "\n\n\n\n\n\n\n\n\n\n copy \n\n\n\n\n\n\n" ;
            
        }

        arrNum++;
            
    }while (next_permutation(IncludingSet, (IncludingSet+IncludingSetLength)));

    cout << "array number is :" << arrNum-1 << endl;
    
    printf("\n\n\n\n\n");
    for (int i = 0; i < IncludingSetLength; ++i)
    {
        printf("access %d", copyIncludingSet[i]);
    }
    printf("\n\n\n\n\n");

    midDist = midPath(IncludingSetLength, IncludingSet, distM);

    firstDist = Dijkstra(IncludingSet[0] , SourceID, firstdistance, firstprevpoint, distM);
    searchPath(firstprevpoint, SourceID, IncludingSet[0]);

    lastDist = Dijkstra(DestinationID, IncludingSet[IncludingSetLength], lastdistance, lastprevpoint, distM);
    searchPath(lastprevpoint, IncludingSet[IncludingSetLength], DestinationID);

    printf("\n\n\n\n\n");




 //    int distance[maxnum];     // 表示当前点到源点的最短路径长度
 //    int prevpoint[maxnum];     // 记录当前点的前一个结点

	// for(int i=0; i<=edge_num/2; ++i)
 //        distance[i] = maxint;

 //    int case0;

 //    // you can set startpoint and endpoint as you want
 //    case0 = Dijkstra(DestinationID, SourceID, distance, prevpoint, distM);
 
 //    // 最短路径长度
 //    cout << "startPoint到endPoint的最短路径长度: " << case0 << endl;

	// cout << "tartPoint到endPoint的的路径: " << endl;
	// searchPath(prevpoint, SourceID, DestinationID);

/* wirte to file */
    for (int i = 0; i < 3; i++)
        record_result(*topo[i]);
}
Пример #21
0
static void output_result(struct stack stack) {
    struct path** path;
    for (path = stack.bottom; path < stack.top; path++) {
        record_result((*path)->index);
    }
}