예제 #1
0
파일: main.c 프로젝트: sergafts/softdevalg
int main(int argc, char *argv[]){
	char inputFile[256], outputFile[256], confFile[256];
	int inputFlag = 0, outputFlag = 0, confFlag = 0;
	FILE* input=NULL;
	FILE* output=NULL;
	FILE* config=NULL;
	srand(time(NULL));

	int confNums[5];
	int r = 365;
	int choice;
	while (1)
	{
		static struct option long_options[] =
		{
			/* Use flags like so:
			{"verbose",	no_argument,	&verbose_flag, 'V'}*/
			/* Argument styles: no_argument, required_argument, optional_argument */
			{"version", no_argument,	0,	'v'},
			{"help",	no_argument,	0,	'h'},
			{"inputFile", required_argument, 0, 'i'},
			{"conf", required_argument, 0, 'c'},
			{"output", required_argument, 0, 'o'},
			{"rSize", required_argument, 0, 'r'},
			{"default", no_argument, 0, 'd'},
			{0,0,0,0}
		};
	
		int option_index = 0;
	
		/* Argument parameters:
			no_argument: " "
			required_argument: ":"
			optional_argument: "::" */
	
		choice = getopt_long( argc, argv, "vhi:c:o:r:d",
					long_options, &option_index);
	
		if (choice == -1)
			break;
	
		switch( choice )
		{
			case 'v':
				printf("beta version\n");
				
				break;
	
			case 'h':
				printf("ussage <executable> –d <inputFile file> –c <configuration file> -o <output file> -I <init fun> -A <assign fun> -U <update fun> --complete\n");
				
				break;
			case 'd':
				strcpy(inputFile, "./inputFiles/bio_small_input.dat");
				strcpy(outputFile, "./inputFiles/conform.dat");
				strcpy(confFile,"./inputFiles/cluster_5.conf" );
				inputFlag = 1;
				outputFlag = 1;
				confFlag = 1;

				break;
			case 'i':
				strcpy(inputFile, optarg);
				inputFlag = 1;
				break;
			case 'c':
				strcpy(confFile, optarg);
				confFlag = 1;

				break;
			case 'o':
				strcpy(outputFile, optarg);
				outputFlag = 1;

				break;
			case 'r':
				r = atoi(optarg);;

				break;
	
			case '?':
				/* getopt_long will have already printed an error */
				break;
	
			default:
				/* Not sure how to get here... */
				return EXIT_FAILURE;
		}
	}
	
	/* Deal with non-option arguments here */
	if ( optind < argc )
	{
		while ( optind < argc )
		{
			
		}
	}
	if ( !inputFlag )
	{
		printf("Give me an input file\n");
		scanf("%s", inputFile);
	}
	if ( !confFlag )
	{
		printf("Give me an conf file\n");
		scanf("%s", confFile);
	}
	if ( !outputFlag )
	{
		printf("Give me an output file\n");
		scanf("%s", outputFile);
	}
	printf("inputFile %s, output %s, confingure %s,r = %d \n",inputFile, outputFile, confFile, r );


	if((input=fopen(inputFile,"r")) == NULL){
		perror(inputFile);
		exit(-1);
	}
	if((output=fopen(outputFile,"w")) == NULL){
		perror(outputFile);
		exit(-1);
	}
	
	if((config=fopen(confFile,"r")) == NULL){
		perror(confFile);
		exit(-1);
	}

	//ReadDataMolecule(NULL, input);
	SpecifyDatasetMolConf("molecule");

	ParseConfig(config,confNums);
	parseData(input, r);
	void (*init)() = KMedoidPlusPlusInit;
	double (*assigment)() = PamAssign;
	int (*update)() = ClaransUpdate;
	int kCluster = 19;
	//int j = 0;
	int j = 0;



	init(kCluster);

	FirstAssignment();

	assigment(confNums[1],confNums[2]);

	j = 0;
	while(update(assigment,confNums)){
		fprintf(output,"Update loop #%d\n",j);
		printf("Update loop #%d\n",j);
		j++;
	}
	PrintClusters(output);
	double silhouette=Silhouette(output);
	fprintf(output, "%d\n%f\n",kCluster,silhouette );
	
	PrintConform(output);
	data.destroyInput();
	DestroyData();

	
	printf("Silhouette: %f\n",silhouette);
	fclose(input);
	fclose(output);
	fclose(config);


	return 0;
}
예제 #2
0
/** Ester, Kriegel, Sander, Xu; Proceedings of 2nd International Conference
  * on Knowledge Discovery and Data Mining (KDD-96); pp 226-231.
  */
int Cluster_DBSCAN::Cluster() {
  std::vector<int> NeighborPts;
  std::vector<int> Npts2; // Will hold neighbors of a neighbor
  std::vector<int> FramesToCluster;
  ClusterDist::Cframes cluster_frames;
  // First determine which frames are being clustered.
  // FIXME: Just use sieved array?
  for (int frame = 0; frame < (int)FrameDistances_.Nframes(); ++frame)
    if (!FrameDistances_.IgnoringRow( frame ))
      FramesToCluster.push_back( frame );
  // Calculate Kdist function
  if (!kdist_.Empty()) {
    if (kdist_.Size() == 1)
      ComputeKdist( kdist_.Front(), FramesToCluster );
    else
      ComputeKdistMap( kdist_, FramesToCluster );
    return 0;
  }
  // Set up array to keep track of points that have been visited.
  // Make it the size of FrameDistances so we can index into it. May
  // waste memory during sieving but makes code easier.
  std::vector<bool> Visited( FrameDistances_.Nframes(), false );
  // Set up array to keep track of whether points are noise or in a cluster.
  Status_.assign( FrameDistances_.Nframes(), UNASSIGNED);
  mprintf("\tStarting DBSCAN Clustering:\n");
  ProgressBar cluster_progress(FramesToCluster.size());
  int iteration = 0;
  for (std::vector<int>::iterator point = FramesToCluster.begin();
                                  point != FramesToCluster.end(); ++point)
  {
    if (!Visited[*point]) {
      // Mark this point as visited
      Visited[*point] = true;
      // Determine how many other points are near this point
      RegionQuery( NeighborPts, FramesToCluster, *point );
      if (debug_ > 0) {
        mprintf("\tPoint %i\n", *point + 1);
        mprintf("\t\t%u neighbors:", NeighborPts.size());
      }
      // If # of neighbors less than cutoff, noise; otherwise cluster
      if ((int)NeighborPts.size() < minPoints_) {
        if (debug_ > 0) mprintf(" NOISE\n");
        Status_[*point] = NOISE;
      } else {
        // Expand cluster
        cluster_frames.clear();
        cluster_frames.push_back( *point );
        // NOTE: Use index instead of iterator since NeighborPts may be
        //       modified inside this loop.
        unsigned int endidx = NeighborPts.size();
        for (unsigned int idx = 0; idx < endidx; ++idx) {
          int neighbor_pt = NeighborPts[idx];
          if (!Visited[neighbor_pt]) {
            if (debug_ > 0) mprintf(" %i", neighbor_pt + 1);
            // Mark this neighbor as visited
            Visited[neighbor_pt] = true;
            // Determine how many other points are near this neighbor
            RegionQuery( Npts2, FramesToCluster, neighbor_pt );
            if ((int)Npts2.size() >= minPoints_) {
              // Add other points to current neighbor list
              NeighborPts.insert( NeighborPts.end(), Npts2.begin(), Npts2.end() );
              endidx = NeighborPts.size();
            }
          }
          // If neighbor is not yet part of a cluster, add it to this one.
          if (Status_[neighbor_pt] != INCLUSTER) {
            cluster_frames.push_back( neighbor_pt );
            Status_[neighbor_pt] = INCLUSTER;
          }
        }
        // Remove duplicate frames
        // TODO: Take care of this in Renumber?
        std::sort(cluster_frames.begin(), cluster_frames.end());
        ClusterDist::Cframes::iterator it = std::unique(cluster_frames.begin(), 
                                                        cluster_frames.end());
        cluster_frames.resize( std::distance(cluster_frames.begin(),it) );
        // Add cluster to the list
        AddCluster( cluster_frames );
        if (debug_ > 0) {
          mprintf("\n");
          PrintClusters();
        }
      }
    }
    cluster_progress.Update(iteration++);
  } // END loop over FramesToCluster
  // Calculate the distances between each cluster based on centroids
  CalcClusterDistances();

  return 0;
}