Example #1
0
int
GA2DBinaryStringGenome::read(STD_ISTREAM & is)
{
  static char c;
  unsigned int i=0, j=0;
  while(!is.fail() && !is.eof() && j < ny) {
    is >> c;
    if(isdigit(c)){
      gene(i, j, ((c == '0') ? 0 : 1));
      if(++i >= nx){		// ready for next row
	i=0;
	j++;
      }
    }
  }

  _evaluated = gaFalse;

  if(is.eof() && 
     ((j < ny) ||	     // didn't get some lines
      (i < nx && i != 0))){   // stopped early on a row
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(STD_IOS_BADBIT | is.rdstate());
    return 1;
  }

  return 0;
}
Example #2
0
// Read the incoming data as a list of phenotype values.  It would be nice to
// do this either as binary or decimal read, but oh well...  not much need.
int
GABin2DecGenome::read(STD_ISTREAM & is)
{
  float value;
  for(unsigned int i=0; i<phenotypes().nPhenotypes(); i++){
    is >> value;
    if(is.fail() || is.eof()) return 1;
    phenotype(i, value);
  }
  return 0;
}
// We read data from a stream as a series of 1's and 0's.  We want a continuous
// stream (ie no spaces) but if there are spaces then we can handle them.  We
// ignore all whitespace.  We ignore anything that is not a digit.  If it is a
// zero then we set to zero.  Anything else is a 1.
int
GA1DBinaryStringGenome::read(STD_ISTREAM & is)
{
  static char c;
  unsigned int i=0;

  while(!is.fail() && !is.eof() && i<nx) {
    is >> c;
    if(isdigit(c)) gene(i++, ((c == '0') ? 0 : 1));
  }

  _evaluated = gaFalse;

  if(is.eof() && i < nx){
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(STD_IOS_BADBIT | is.rdstate());
    return 1;
  }

  return 0;
}
// The read specialization takes in each character whether it is whitespace or
// not and stuffs it into the genome.  This is unlike the default array read.
template <> int
GA1DArrayAlleleGenome<char>::read(STD_ISTREAM & is)
{
  unsigned int i=0;
  char c;
  do{
    is.get(c);
    if(!is.fail()) gene(i++, c);
  } while(!is.fail() && !is.eof() && i < nx);

  if(is.eof() && i < nx){
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(STD_IOS_BADBIT | is.rdstate());
    return 1;
  }
  return 0;
}
Example #5
0
// Read name-value pairs from the stream.  If the first item is a number, then
// we expect that many name-value pairs.  If not, then we read until the end of
// the stream.  Be sure not to duplicate the last one, and be sure to dump any
// items from a # to end-of-line.
//   Parse for name-value pairs, where the pairs are separated by whitespace.
// *Every* parameter must be specified as a name-value pair (no single name 
// with no value allowed).  If we get a single name with no value that will
// screw up the name-value pairing for any remaining pairs.  We could check for
// this situation, but for now we just let it happen and dump the errors. (i'll
// do a better parsing algorithm at some later point).
//   We return the number of items that we were able to read from the stream.
// If we come across an item that is unrecognized, we dump an error message.
//   The buffer length is limited here, so don't try to read in any really long
// lines.
//   We parse only for items that we know about - if we come across a pair
// whose name we do not know about, we ignore the pair and go on to the next.
// There's no global list to tell us what type things are, and we don't assume.
//   We don't allow setting pointers using this method.
int
GAParameterList::read(STD_ISTREAM& is, GABoolean flag){
  int nfound = 0;
  if(n == 0) return nfound;

  char buf[BUFSIZE];
  char name[NAMESIZE];
  int toggle = 0, count = 0, npairs = -1;

  is.width(sizeof(buf));	// don't allow to overrun buffer

  do{
    is >> buf;
    if(npairs == -1){
      if(IsNumeric(buf)){
	npairs = atoi(buf);
	is >> buf;
      }
      else{
	npairs = MAX_PAIRS;
      }
    }
    if(is.eof()){
      break;
    }
    else if(buf[0] == '#'){		// dump to end-of-line
      is.get(buf, BUFSIZE);		// fails on lines longer than BUFSIZE
    }
    else if(toggle == 0){
      strcpy(name, buf);
      toggle = 1;
    }
    else if(toggle == 1){
      int found = 0;

      count += 1;
      toggle = 0;

      for(unsigned int i=0; i<n && !found; i++){
	if(strcmp(name, p[i]->fullname()) == 0 ||
	   strcmp(name, p[i]->shrtname()) == 0){
	  found = 1;

	  int ival;
	  float fval;
	  double dval;
	  
	  switch(p[i]->type()){
	  case GAParameter::BOOLEAN:
	  case GAParameter::INT:
	    ival = atoi(buf);
	    set(name, (void*)&ival);
	    nfound += 1;
	    break;
	  case GAParameter::CHAR:
	  case GAParameter::STRING:
	    set(name, (void*)buf);
	    nfound += 1;
	    break;
	  case GAParameter::FLOAT:
	    fval = (float)atof(buf);
	    set(name, (void*)&fval);
	    nfound += 1;
	    break;
	  case GAParameter::DOUBLE:
	    dval = (double)atof(buf);
	    set(name, (void*)&dval);
	    nfound += 1;
	    break;
	  case GAParameter::POINTER:
	  default:
	    break;
	  }

// Move this parameter to the front of the list

//	  if(i > 0) {
//	    GAParameter *tmpptr = p[i];
//	    memmove(&(p[1]), &(p[0]), i * sizeof(GAParameter*));
//	    p[0] = tmpptr;
//	  }
	  if(i < n-1) {
	    GAParameter *tmpptr = p[i];
	    memmove(&(p[i]), &(p[i+1]), (n-i-1) * sizeof(GAParameter*));
	    p[n-1] = tmpptr;
	  }
	}
      }

      if(!found && flag == gaTrue){
	strcpy(_gaerrbuf1, "");
	strcat(_gaerrbuf1, "unrecognized variable name '");
	strcat(_gaerrbuf1, name);
	strcat(_gaerrbuf1, "'");
	GAErr(GA_LOC, "GAParameterList", "read", _gaerrbuf1);
      }
    }
  } while(!is.eof() && count < npairs);