NEAT icon indicating copy to clipboard operation
NEAT copied to clipboard

UTF-8 Start genome files

Open Lip651 opened this issue 6 years ago • 0 comments

I think one of the huge drawbacks of the package is the lack of interpretability depending on your system. I had huge problems with compiling the examples, just because the input files creating the start genome were not interpretable by my system. I am running on Ubuntu 18.04.04. I had to rewrite the input files in a UTF-8 Format and modify genome such that it would be able to read them.

Here is the code working if you have UTF-8 files:

Genome::Genome(int id, std::ifstream &iFile) {

	char delimiters[] = " \n";
	//int pause;
	genome_id=id;
	wstring line;

  std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(iFile.rdbuf());
  std::wistream wf(&conv);

	getline(wf, line);
	char curword[128];

	//Loop until file is finished, parsing each line
	while (getline(wf, line)) {

		int curwordnum = 0;
		wstring ws_line(line);
		string ws_line_str(ws_line.begin(), ws_line.end());
		const char* char_line =  ws_line_str.c_str();
		int wordcount = NEAT::getUnitCount(char_line, delimiters);

		cout << char_line << endl;

    std::stringstream ss(char_line);
		//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
    ss >> curword;
		//Check for end of Genome
		if (strcmp(curword,"genomeend")==0) {
			//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
            ss >> curword;
			int idcheck = atoi(curword);
			//iFile>>idcheck;
			if (idcheck!=genome_id) printf("ERROR: id mismatch in genome");
		}

		//Ignore genomestart if it hasn't been gobbled yet
		else if (strcmp(curword,"genomestart")==0) {
			++curwordnum;
			cout<<"genomestart"<<endl;
		}

		//Ignore comments surrounded by - they get printed to screen
		else if (strcmp(curword,"/*")==0) {
			//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
            ss >> curword;
			while (strcmp(curword,"*/")!=0) {
				//cout<<curword<<" ";
				//strcpy(curword, NEAT::getUnit(curline, curwordnum++, delimiters));
        ss >> curword;
			}
			//cout<<endl;
		}

		//Read in a trait
		else if (strcmp(curword,"trait")==0) {
			Trait *newtrait;

			char argline[1024];
			//strcpy(argline, NEAT::getUnits(curline, curwordnum, wordcount, delimiters));

			curwordnum = wordcount + 1;
			ss.getline(argline, 1024);
			//Allocate the new trait
			newtrait=new Trait(argline);

			//Add trait to vector of traits
			traits.push_back(newtrait);
		}

		//Read in a node
		else if (strcmp(curword,"node")==0) {
			NNode *newnode;

			char argline[1024];
			//strcpy(argline, NEAT::getUnits(curline, curwordnum, wordcount, delimiters));
			curwordnum = wordcount + 1;
			ss.getline(argline, 1024);
			//Allocate the new node
			newnode=new NNode(argline,traits);

			//Add the node to the list of nodes
			nodes.push_back(newnode);
		}

		//Read in a Gene
		else if (strcmp(curword,"gene")==0) {
			Gene *newgene;

			char argline[1024];
			//strcpy(argline, NEAT::getUnits(curline, curwordnum, wordcount, delimiters));
			curwordnum = wordcount + 1;

      ss.getline(argline, 1024);
      //std::cout << "New gene: " << ss.str() << std::endl;
			//Allocate the new Gene
      newgene=new Gene(argline,traits,nodes);

			//Add the gene to the genome
			genes.push_back(newgene);
		 std::cout<<"Added gene " << newgene << std::endl;
		}
	}
}

Lip651 avatar Feb 09 '20 14:02 Lip651