コード例 #1
0
ファイル: kmeans.cpp プロジェクト: ashwath/heuristics2012
// Performs K-means clustering
vector<vector<point> > kmeans(vector<point> initcluster, int k){
	
	vector<point> centeroids, old_centeroids; // vector maintaining k centeroids
  	vector<vector<point> > clusterList; // for storing the list of clusters
  	vector<double> distance; // temp vector, used for finding the min_distance
  	int centeroids_change = 1; //Flag which helps the loop to terminate 
  	double min;
  	int destination,num = 0;

  	// Intial centeroids are random
	for(int i = 0; i < k; ++i){
		centeroids.push_back(initcluster[i]);
	}
	// loop for finding the optimal clusters and centeroids
	//while(centeroids_change){
	while(num <1500){ // will loop 2000 times, will consider it near optimal solution

		clusterList.clear(); // clearing the datastructure, after last loop.
		
		// initializing the clusterList
		vector<point> v;
		for (int i = 0; i < k; ++i)
		{
			clusterList.push_back(v);
		}
	
		for (int i = 0; i < initcluster.size()-1; ++i){	
			distance.clear();// will empty the distance vector for future calculations
			
			min = find_distance(initcluster[i], centeroids[0]); //assumption, will improve while running the code
			destination = 0;
		
			for (int j = 0; j < centeroids.size(); ++j){
				distance.push_back(find_distance(initcluster[i], centeroids[j]));
				if (distance[j] <= min){  
					min = distance[j];
					destination = j;
				}	
			}	
			clusterList.at(destination).push_back(initcluster[i]);						
		}
	
		num ++;
		//cout << "Itr" << num << endl;
		old_centeroids = centeroids;
	
		centeroids = mean_of_cluster(clusterList, k);
		//centeroids_change = check_change(old_centeroids, centeroids);
	}

	cout<< "Number of loops iterated for finding Optimal Solution: "<< num<<"\n";
	// Printing final K centeroids
	cout << "Final Centeroids:"<<"\n";
	for(int i=0; i <centeroids.size();++i){
		cout<<centeroids[i].id<<" "<<centeroids[i].x<<" "<<centeroids[i].y<<" "<<centeroids[i].z<<"\n";
	}
	clusterList.push_back(centeroids);

	return clusterList;
}
コード例 #2
0
// returns the nodes closest to mean for each of the clusters in clusterlist
vector<point> mean_of_cluster(vector<vector<point> > clusterList){
	point sum, mean,candidate;
	float distance;
	float min_distance;
	vector<point> new_centeroids;


	for (int i = 0; i < clusterList.size(); ++i){	
		sum.x = 0;
		sum.y = 0;

		for (int j = 0; j < clusterList.at(i).size(); ++j){
			sum.x = sum.x + clusterList[i][j].x;
			sum.y = sum.y + clusterList[i][j].y;
		}

		mean.x = sum.x / clusterList.at(i).size();
		mean.y = sum.y / clusterList.at(i).size();

		// have to find the closest node to this mean
		min_distance = find_distance(mean, clusterList[i][0]);// assumption
		candidate = clusterList[i][0];
		for (int n = 0; n < clusterList.at(i).size(); ++n)
		{
			distance = find_distance(mean, clusterList[i][n]);
			if(distance <= min_distance){
				min_distance = distance;
				candidate = clusterList[i][n];
			} 
		}

		new_centeroids.push_back(candidate);		
	}
	return new_centeroids;
}
コード例 #3
0
// Merge clusters which are close to each other
vector<vector<point> > unionClusters(vector<vector<point> > clusterList, vector<point> centeroids) {
	
	vector<double> radius;
	double centeroid_dist;

	for (int i = 0; i < centeroids.size()-1; ++i){	
		for (int j = i+1; j < centeroids.size(); ++j){ 	
			centeroid_dist = find_distance(centeroids.at(i), centeroids.at(j));			
			radius = updateradius(clusterList, centeroids); // finding radius of each cluster and storing in radius vector
			
			if(centeroids.size()<=5) continue; // will not merge if total num of clusters have reduced to 5
			
			else if(centeroid_dist < (radius.at(i) + radius.at(j)/1.5)){ // merging of clusters 		
					// merging the two clusters
					for(int n=0; n < clusterList.at(j).size(); ++n){
						clusterList.at(i).push_back(clusterList[j][n]);
					}
					// clean up
					clusterList.erase(clusterList.begin()+j);// erasing the extra cluster
					centeroids.erase(centeroids.begin()+j);// erasing the extra centroid
					// updating the centroids
					centeroids = mean_of_cluster(clusterList);
					break;
			}
			
		}
	}

	return clusterList;
}
コード例 #4
0
ファイル: kmeans.cpp プロジェクト: ashwath/heuristics2012
// returns the nodes closest to mean for each of the clusters in clusterlist
vector<point> mean_of_cluster(vector<vector<point> > clusterList, int k){
	point sum, mean,candidate;
	vector<double> distance;
	double min_distance;
	vector<point> new_centeroids;


	for (int i = 0; i < clusterList.size(); ++i)
	{	
		sum.x = 0;
		sum.y = 0;
		sum.z = 0;

		for (int j = 0; j < clusterList[i].size(); ++j)
		{
			sum.x = sum.x + clusterList[i][j].x;
			sum.y = sum.y + clusterList[i][j].y;
			sum.z = sum.z + clusterList[i][j].z;
		}

		mean.x = sum.x / k;
		mean.y = sum.y / k;
		mean.z = sum.z / k;

		// have to find the closest node to this mean
		distance.clear();
		min_distance = find_distance(mean, clusterList[i][0]);// assumption
		candidate = clusterList[i][0];
		for (int n = 0; n < clusterList[i].size(); ++n)
		{
			distance.push_back(find_distance(mean, clusterList[i][n]));
			if(distance[n] < min_distance){
				min_distance = distance[n];
				candidate.x = clusterList[i][n].x;
				candidate.y = clusterList[i][n].y;
				candidate.z = clusterList[i][n].z;
				candidate.id = clusterList[i][n].id;
			} 
		}

		new_centeroids.push_back(candidate);		
	}
	return new_centeroids;
}
コード例 #5
0
ファイル: hw1.c プロジェクト: mk200789/Fundamental-Algorithms
int expand(int line_count, int vertex[][6], int m, int n, int current_x, int current_y){
	int i, j;
	
	for (i=0; i<=line_count; i++){
		for (j=0; j<6; j+=2){
			if (i==m && j==n){
			}
			else{
				if (check_if_in_triangle(line_count, vertex, vertex[i][j], vertex[i][j+1]) == 0){

					if (check_intersect(line_count, vertex, current_x, current_y, vertex[i][j], vertex[i][j+1])== 0){

						if( vertex_exist(vertex[i][j], vertex[i][j+1], current_x, current_y) == 0){

							valid_vertex[k][0] = current_x;
							valid_vertex[k][1] = current_y;
							valid_vertex[k][2] = vertex[i][j];
							valid_vertex[k][3] = vertex[i][j+1];
							valid_vertex[k][4] = find_distance(current_x, current_y, vertex[i][j], vertex[i][j+1]);
							valid_vertex[k][5] = -1.0;
							valid_vertex[k++][6] = -1.0;

							if (vertex_exist(current_x, current_y, vertex[i][j], vertex[i][j+1]) == 0){
								valid_vertex[k][0] = vertex[i][j];
								valid_vertex[k][1] = vertex[i][j+1];
								valid_vertex[k][2] = current_x;
								valid_vertex[k][3] = current_y;
								valid_vertex[k][4] = find_distance(current_x, current_y, vertex[i][j], vertex[i][j+1]);
								valid_vertex[k][5] = -1.0;
								valid_vertex[k++][6] = -1.0;
							}
						}
						else{
						}
					}
					else{
					}
				}
			}
		}
	}
}
コード例 #6
0
//finding radius of each cluster and storing in radius vector
vector<double> updateradius(vector<vector<point> > clusterList, vector<point> centeroids ){
		double max, radius_dist;
		point p1;
		vector<point> chk;
		vector<double> radius;

		for ( int i = 0; i < clusterList.size(); ++i)
		{	//cout<<"hello2.5"<<endl;
			max = find_distance(centeroids.at(i), clusterList[i][0]); // assumption
			p1 = clusterList[i][0];
			for (int j = 0; j < clusterList.at(i).size(); ++j)
			{ //cout<<"hello2.75"<<endl;
				radius_dist = find_distance(centeroids.at(i),clusterList[i][j]);
				if(radius_dist > max){
					max = radius_dist;
					p1 = clusterList[i][j];
				}
			}
			chk.push_back(p1);
			radius.push_back(max);
		}

		return radius;
	}	
コード例 #7
0
ファイル: AI.c プロジェクト: maksverver/Amazes
const char *pick_move(MazeMap *mm, int distsq)
{
    Point dst;

    find_distance(mm, dist, mm->loc.r, mm->loc.c);

    if (mm_count_squares(mm) < WIDTH*HEIGHT)
        dst = explore(mm);
    else /* mm_count_squares(mm) == WIDTH*HEIGHT */
        dst = squash(mm, distsq);

    if (dst.r == mm->loc.r && dst.c == mm->loc.c)
        return "T";

    return construct_turn(mm, dist, mm->loc.r, mm->loc.c, mm->dir, dst.r, dst.c,
                          NULL, NULL);
}
コード例 #8
0
ファイル: source.c プロジェクト: gpvihari/spyrobo
void main()
{
	lcd_init();
	init_serial();
	Echo = Trig = 0;
	TMOD |= 0x01;//Timer 0 in 16-bit mode																				 

	P1=0xf0;    // set port as input port
	P2=0x00;    // set port as output port
	DelayMs(100);
	while(1)
	{
		if(P1==0xf8)
		{
			P2=0x0a;		 //00001010	front
		}
		else if(P1==0xf1)
		{
			P2=0x02;		 //00000010	
		}    
		else if(P1==0xf2)
		{
			P2=0x08;		//00001000
		}    
		else if(P1==0xf4)
		{
			P2=0x05;		//00000101	back
		}
		else 	if(msg1=='*')
		{
			find_distance();
			msg1=0;
		}
		else
		{
			P2=0x00;
		}
	}
}
コード例 #9
0
ファイル: flatfly_onchip.C プロジェクト: pranamibhatt/booksim
//ugal now uses modified comparison, modefied getcredit
void ugal_flatfly_onchip( const Router *r, const Flit *f, int in_channel,
			  OutputSet *outputs, bool inject )
{
  // ( Traffic Class , Routing Order ) -> Virtual Channel Range
  int vcBegin = 0, vcEnd = gNumVCs-1;
  if ( f->type == Flit::READ_REQUEST ) {
    vcBegin = gReadReqBeginVC;
    vcEnd = gReadReqEndVC;
  } else if ( f->type == Flit::WRITE_REQUEST ) {
    vcBegin = gWriteReqBeginVC;
    vcEnd = gWriteReqEndVC;
  } else if ( f->type ==  Flit::READ_REPLY ) {
    vcBegin = gReadReplyBeginVC;
    vcEnd = gReadReplyEndVC;
  } else if ( f->type ==  Flit::WRITE_REPLY ) {
    vcBegin = gWriteReplyBeginVC;
    vcEnd = gWriteReplyEndVC;
  }
  assert(((f->vc >= vcBegin) && (f->vc <= vcEnd)) || (inject && (f->vc < 0)));

  int out_port;

  if(inject) {

    out_port = 0;

  } else {

    int dest  = flatfly_transformation(f->dest);

    int rID =  r->GetID();
    int _concentration = gC;
    int found;
    int debug = 0;
    int tmp_out_port, _ran_intm;
    int _min_hop, _nonmin_hop, _min_queucnt, _nonmin_queucnt;
    int threshold = 2;

    if ( in_channel < gC ){
      if(gTrace){
	cout<<"New Flit "<<f->src<<endl;
      }
      f->ph   = 0;
    }

    if(gTrace){
      int load = 0;
      cout<<"Router "<<rID<<endl;
      cout<<"Input Channel "<<in_channel<<endl;
      //need to modify router to report the buffere depth
      load +=r->GetBuffer(in_channel);
      cout<<"Rload "<<load<<endl;
    }

    if (debug){
      cout << " FLIT ID: " << f->id << " Router: " << rID << " routing from src : " << f->src <<  " to dest : " << dest << " f->ph: " <<f->ph << " intm: " << f->intm <<  endl;
    }
    // f->ph == 0  ==> make initial global adaptive decision
    // f->ph == 1  ==> route nonminimaly to random intermediate node
    // f->ph == 2  ==> route minimally to destination

    found = 0;

    if (f->ph == 1){
      dest = f->intm;
    }


    if (dest >= rID*_concentration && dest < (rID+1)*_concentration) {

      if (f->ph == 1) {
	f->ph = 2;
	dest = flatfly_transformation(f->dest);
	if (debug)   cout << "      done routing to intermediate ";
      }
      else  {
	found = 1;
	out_port = dest % gC;
	if (debug)   cout << "      final routing to destination ";
      }
    }

    if (!found) {

      if (f->ph == 0) {
	_min_hop = find_distance(flatfly_transformation(f->src),dest);
	_ran_intm = find_ran_intm(flatfly_transformation(f->src), dest);
	tmp_out_port =  flatfly_outport(dest, rID);
	if (f->watch){
	  *gWatchOut << GetSimTime() << " | " << r->FullName() << " | "
		     << " MIN tmp_out_port: " << tmp_out_port;
	}

	_min_queucnt =   r->GetCredit(tmp_out_port, -1, -1);

	_nonmin_hop = find_distance(flatfly_transformation(f->src),_ran_intm) +    find_distance(_ran_intm, dest);
	tmp_out_port =  flatfly_outport(_ran_intm, rID);

	if (f->watch){
	  *gWatchOut << GetSimTime() << " | " << r->FullName() << " | "
		     << " NONMIN tmp_out_port: " << tmp_out_port << endl;
	}
	if (_ran_intm >= rID*_concentration && _ran_intm < (rID+1)*_concentration) {
	  _nonmin_queucnt = numeric_limits<int>::max();
	} else  {
	  _nonmin_queucnt =   r->GetCredit(tmp_out_port, -1, -1);
	}

	if (debug){
	  cout << " _min_hop " << _min_hop << " _min_queucnt: " <<_min_queucnt << " _nonmin_hop: " << _nonmin_hop << " _nonmin_queucnt :" << _nonmin_queucnt <<  endl;
	}

	if (_min_hop * _min_queucnt   <= _nonmin_hop * _nonmin_queucnt +threshold) {

	  if (debug) cout << " Route MINIMALLY " << endl;
	  f->ph = 2;
	} else {
	  // route non-minimally
	  f->minimal = 0;
	  if (debug)  { cout << " Route NONMINIMALLY int node: " <<_ran_intm << endl; }
	  f->ph = 1;
	  f->intm = _ran_intm;
	  dest = f->intm;
	  if (dest >= rID*_concentration && dest < (rID+1)*_concentration) {
	    f->ph = 2;
	    dest = flatfly_transformation(f->dest);
	  }
	}
      }

      // find minimal correct dimension to route through
      out_port =  flatfly_outport(dest, rID);

      // if we haven't reached our destination, restrict VCs appropriately to avoid routing deadlock
      if(out_port >= gC) {
	int const available_vcs = (vcEnd - vcBegin + 1) / 2;
	assert(available_vcs > 0);
	if(f->ph == 1) {
	  vcEnd -= available_vcs;
	} else {
	  assert(f->ph == 2);
	  vcBegin += available_vcs;
	}
      }

      found = 1;
    }

    if (!found) {
      cout << " ERROR: output not found in routing. " << endl;
      cout << *f; exit (-1);
    }

    if (out_port >= gN*(gK-1) + gC)  {
      cout << " ERROR: output port too big! " << endl;
      cout << " OUTPUT select: " << out_port << endl;
      cout << " router radix: " <<  gN*(gK-1) + gK << endl;
      exit (-1);
    }

    if (debug) cout << "        through output port : " << out_port << endl;
    if(gTrace){cout<<"Outport "<<out_port<<endl;cout<<"Stop Mark"<<endl;}
  }

  outputs->Clear( );

  outputs->AddRange( out_port , vcBegin, vcEnd );
}
コード例 #10
0
ファイル: act.psionic.c プロジェクト: TempusMUD/Tempuscode
bool
can_psidrain(struct creature *ch, struct creature *vict, int *out_dist, bool msg)
{
    const char *to_char = NULL;
    const char *to_vict = NULL;
    int dist;

    if (CHECK_SKILL(ch, SKILL_PSIDRAIN) < 30 || !IS_PSIONIC(ch)) {
        to_char = "You have no idea how.";
        goto failure;
    }

    if (ROOM_FLAGGED(ch->in_room, ROOM_NOPSIONICS) && GET_LEVEL(ch) < LVL_GOD) {
        to_char = "Psychic powers are useless here!";
        goto failure;
    }

    if (GET_LEVEL(vict) >= LVL_AMBASSADOR && GET_LEVEL(ch) < GET_LEVEL(vict)) {
        to_char = "You cannot locate them.";
        to_vict = "$n has just tried to psidrain you.";
        goto failure;
    }

    if (vict == ch) {
        to_char = "Ha ha... Funny!";
        goto failure;
    }

    if (is_fighting(ch) && vict->in_room != ch->in_room) {
        to_char = "You cannot focus outside the room during battle!";
        goto failure;
    }

    if (ch->in_room != vict->in_room &&
        ch->in_room->zone != vict->in_room->zone) {
        to_char = "$N is not in your zone.";
        goto failure;
    }

    if (ROOM_FLAGGED(vict->in_room, ROOM_NOPSIONICS)
        && GET_LEVEL(ch) < LVL_GOD) {
        to_char = "Psychic powers are useless where $E is!";
        goto failure;
    }

    if (!ok_to_attack(ch, vict, msg))
        return false;

    if (GET_MOVE(ch) < 20) {
        to_char = "You are too physically exhausted.";
        goto failure;
    }

    dist = find_distance(ch->in_room, vict->in_room);
    if (out_dist)
        *out_dist = dist;
    if (dist > ((GET_LEVEL(ch) / 6))) {
        to_char = "$N is out of your psychic range.";
        goto failure;
    }

    if (NULL_PSI(vict)) {
        to_char = "It is pointless to attempt this on $M.";
        goto failure;
    }

    return true;

failure:
    if (msg) {
        if (to_char)
            act(to_char, false, ch, NULL, vict, TO_CHAR);
        if (to_vict)
            act(to_vict, false, ch, NULL, vict, TO_VICT);
    }
    return false;
}
コード例 #11
0
ファイル: hw1.c プロジェクト: mk200789/Fundamental-Algorithms
void start_graph(int line_count, int vertex[][6], int start_x, int start_y, int target_x, int target_y){
	/*
		Retrieve all valid vertex that will graph all possible routes in the graph.
	*/

	int i, j;


	//initialize valid_vertex[][] to zero
	for (i=0; i<600; i++){
		valid_vertex[i][0] = 0.0;
		valid_vertex[i][1] = 0.0;
		valid_vertex[i][2] = 0.0;
		valid_vertex[i][3] = 0.0;
		valid_vertex[i][4] = 0.0;
		valid_vertex[i][5] = 0.0;
		valid_vertex[i][6] = 0.0;
	}

	//store the start point in valid_vertex 
	valid_vertex[k][0] = start_x;	// from x value
	valid_vertex[k][1] = start_y;   // from y value
	valid_vertex[k][2] = start_x;	// to x value
	valid_vertex[k][3] = start_y;	// to y value
	valid_vertex[k][4] = 0.0;		// distance from point a to b
	valid_vertex[k][5] = 0.0;		// total distance
	valid_vertex[k++][6] = 0.0;		// -2.0 target , 1.0 has visited not included, 0.0 using, -1.0 not yet visited



	//handles the lines connecting from the starting point and branch out
	for (i=0; i<=line_count; i++){

		for (j=0; j<6; j+=2){

			if (check_if_in_triangle(line_count, vertex, vertex[i][j], vertex[i][j+1]) == 0){

				if (check_intersect(line_count, vertex, start_x, start_y, vertex[i][j], vertex[i][j+1]) == 0){
					
					valid_vertex[k][0] = start_x;
					valid_vertex[k][1] = start_y;
					valid_vertex[k][2] = vertex[i][j];
					valid_vertex[k][3] = vertex[i][j+1];
					valid_vertex[k][4] = find_distance(vertex[i][j], vertex[i][j+1], start_x, start_y);
					valid_vertex[k][5] = -1.0;
					valid_vertex[k++][6] = -1.0;
					
					valid_vertices(line_count, vertex, i, vertex[i][j], vertex[i][j+1]);

				}
			}
		}
	}

	//handles the lines connecting to the target point.
	for(i=0; i<=line_count; i++){
		for(j=0; j<6; j+=2){
			if(check_if_in_triangle(line_count, vertex, vertex[i][j], vertex[i][j+1])==0){

				if(check_intersect(line_count, vertex, vertex[i][j], vertex[i][j+1], target_x, target_y) == 0){
					
					if (vertex_exist(vertex[i][j], vertex[i][j+1], target_x, target_y) == 0){
						
						valid_vertex[k][0] = vertex[i][j];
						valid_vertex[k][1] = vertex[i][j+1];
						valid_vertex[k][2] = target_x;
						valid_vertex[k][3] = target_y;
						valid_vertex[k][4] = find_distance(vertex[i][j], vertex[i][j+1], target_x, target_y);
						valid_vertex[k][5] = -1.0;
						valid_vertex[k++][6] = -2.0;
					}
				}
			}
		}
	}
}
コード例 #12
0
// Performs K-means clustering and return the sorted vector of clusters 
vector<point>  kmeans(vector<point> initcluster, int k){		

	vector<point> centeroids, old_centeroids; // vector maintaining k centeroids
  	vector<vector<point> > clusterList; // for storing the list of clusters
  	float distance1; // temp vector, used for finding the min_distance
  	int centeroids_change = 1; //Flag which helps the loop to terminate 
  	float min;
  	int destination,num = 0;

  	// Intial centeroids are random
	for(int i = 0; i < k; ++i){
		centeroids.push_back(initcluster[i]);
	}
	// loop for finding the optimal clusters and centeroids
	while(centeroids_change){
		clusterList.clear(); // clearing the datastructure, after last loop.
		
		// initializing the clusterList
		vector<point> v;
		for (int i = 0; i < k; ++i)
		{
			clusterList.push_back(v);
		}
	
		for (int i = 0; i < initcluster.size()-1; ++i){	
			
			min = find_distance(initcluster[i], centeroids[0]); //assumption, will improve while running the code
			destination = 0;
		
			for (int j = 0; j < centeroids.size(); ++j){
				distance1 = find_distance(initcluster[i], centeroids[j]);
				if (distance1 <= min){  
					min = distance1;
					destination = j;
				}	
			}	
			clusterList.at(destination).push_back(initcluster[i]);						
		}
	
		num ++;
		old_centeroids = centeroids;
	
		centeroids = mean_of_cluster(clusterList);
		centeroids_change = check_change(old_centeroids, centeroids);
	}

	cout<< "Number of loops iterated for finding Optimal Solution: "<< num<<"\n";

/*
	// Printing final K centeroids
	cout << "Initial Centeroids:"<<"\n";
	for(int i=0; i <centeroids.size();++i){
		cout<<centeroids[i].x<<", "<<centeroids[i].y<<"; "<<"\n";
	}
*/
	// merging clusters which overlap
	vector<vector<point> > updatedClusterList = unionClusters(clusterList, centeroids);

	// sorting the cluster List
	sort (updatedClusterList.begin(), updatedClusterList.end(), sortfunction);

	cout<<endl<<"Sorted Clusters";
	print(updatedClusterList);

	updatedClusterList.erase(updatedClusterList.begin()+5, updatedClusterList.end());

    cout<<endl<<endl<<"Final 5 Clusters"; 
    print(updatedClusterList);
	
	centeroids = mean_of_cluster(updatedClusterList);

	return  centeroids;
}