void spatialRobocodeCreatureDelta::doMutation() {
    if (!participates) {
        humanRobot = randomint(MAXROBOTNUMBER);
    } else {
        critter->smartSureMutate(10000);
	}
    // what to do if its invalid etc hmm oh well nothing for just now;
}
void spatialRobocodeMvPParasite::loadParasite(ifstream &fin,int layer) {
	int maxParasite;
	switch (layer) {
		case 0: maxParasite = NUMBEROFPARASITES -8;
			break;
		case 1: maxParasite = NUMBEROFPARASITES -5;
			break;
		case 2: 
			maxParasite = NUMBEROFPARASITES - 3;
			break;
		default:
			maxParasite = NUMBEROFPARASITES;
			break;
	}
	parasite=randomint(maxParasite);
}	
Exemplo n.º 3
0
int smallfiletest(int maxsize, int number)
{
  assert(maxsize > 0);
  assert(number > 0);

  int i;
  uint64_t totalsize = 0;
  uint64_t totaltime = 0;
  unsigned int filesize = 0;
  char filename[128] = {0};
  char *tmpprefix = allocrandomstring(4);

  for(i = 0; i < number; i++)
    {
      snprintf(filename, 128, "./tmp/%s_%08d", tmpprefix, i);
      filesize = ((unsigned int)randomint()) % maxsize + 1;
      totalsize += filesize;
      totaltime += randfile(filename, filesize, 4096);
    }
  free(tmpprefix);
  printf("total write size is %ju, total write time is %ju, speed is: %fKByte/s\n", totalsize, totaltime, (double)totalsize*1000000000/totaltime/1024);
  return 0;
}
void spatialRobocodeMvPParasite::doMutation(int layer) {
	int maxParasite;
	switch (layer) {
		case 0: maxParasite = NUMBEROFPARASITES -8;
			break;
		case 1: maxParasite = NUMBEROFPARASITES -5;
			break;
		case 2: 
			maxParasite = NUMBEROFPARASITES - 3;
			break;
		default:
			maxParasite = NUMBEROFPARASITES;
			break;
	}
	parasite=randomint(maxParasite);
	// changed it from below, any parasite can mutate into any other one (valid for layer)
	/*if (randomint(2)) {
		parasite++;
		if (parasite >= maxParasite) parasite =0;
	} else {
		parasite--;
		if (parasite < 0) parasite = maxParasite-1;
	}*/
}
spatialRobocodeCreatureDelta::spatialRobocodeCreatureDelta(bool make) {
    participates= true;
	thisCreatureDeltaNumber = CreatureDeltaIdentifierCount++;
	compsWon = 0;
	geneticAge =0;
	if (make) {
		int maxTree;
		bool done = false;
		geneticAge = 0;
		do {
			maxTree = randomint(8)+2;
			critter.reset(new cr_data);
			critter->makeptc2(maxTree);
			done = critter->isValid();
		} while (!done);
		isInvalid = false;
		//cout << "New valid spatialRobocodeCreatureDelta(true) CreatureDelta created, with max size " << maxTree << "and id " << thisCreatureDeltaNumber << endl;
		char fname[200];
		char jname[200];
		char gname[200];
		sprintf(gname,"Gen%d",currentGen);

		//cout << "Thinking of saving " << getName() << endl;
		if (!(getCreature()->isValid())) {
			cout << "Weird, newly created CreatureDelta is not valid\n";
			exit(1);
		}
		sprintf(fname,"%s",getName());
		sprintf(jname,"%s.java",fname);
		ofstream robotout;
		robotout.open(jname);
		robocodeDelta::Instance()->print_grammar_individual(robotout,getCreature()->getExpression(),getCreature()->getExpressionLength(),fname,gname);
		robotout.close();
	}	
	
}
Exemplo n.º 6
0
void cr_data::variableCrossover(const crPtr &op1,const crPtr &op2,crPtr &c1,crPtr &c2) {
	static int NumberOfChoices = 2; // so we choose between two operators
    int cross1,cross2;
    if (!op1) {
        cout << "variableCrossover returned a null parent1\n";
        exit(1);
    }
    if (!op2) {
        cout << "variableCrossover returned a null parent2\n";
        exit(1);
    }
    if (!op1->isValid()) {
        cout << "variable crossover invalid parent1\n";
        exit(1);
    }
    if (!op2->isValid()) {
        cout << "variable crossover invalid parent2\n";
        exit(1);
    }
    cross1 = (op1->getCrossMethod()) % NumberOfChoices;
    cross2 = (op2->getCrossMethod()) % NumberOfChoices;
    
    crPtr t1,t2;
    c1.reset();
    c2.reset();
    bool done = false;
    int FailCount = 0;
	int methodUsed;
	//if (*op1 > *op2) methodUsed = cross1; else methodUsed = cross2;
	methodUsed = (randomint(2)?cross1:cross2);
	if (methodUsed -1 > NumberOfChoices) {
		cout << "Houston we have a problem, cross1 was " << cross1 << " and cross2: " << cross2 << endl;
		exit(1);
	}
     do {
        FailCount++;
		t1.reset();
		t2.reset();
       switch(methodUsed) { // choose randomly between the parents methods
            case 0: // single point Bit
//				methodUsed = 0;
                cr_data::singlePointBitOperator(op1,op2,t1,t2);
                break;
            case 1: // two Point Replacement
//				methodUsed = 1;
  
				cr_data::gpReplacementOperator(op1,op2,t1,t2);
                break;
        }
        if (!c1) {
            if (t1 && t1->isValid()) {
                c1 = t1;
				c1->setCrossMethod(methodUsed);
                if (t2 && t2->isValid()) {
                    c2 = t2;
					c2->setCrossMethod(methodUsed);
                    done = true;
                }
            } else if (t2 && t2->isValid()) {
                c1 = t2;
				c1->setCrossMethod(methodUsed);
            }
        } else if (!c2) {
            if (t1 && t1->isValid()) {
                c2 = t1;
				c2->setCrossMethod(methodUsed);
                done = true;
            } else if (t2 && t2->isValid()) {
                c2 = t2;
				c2->setCrossMethod(methodUsed);
                done = true;
            }
        }
        if (FailCount > 25) break; // Give up eventually don't want it to hang here.
    } while (!done);
    if (c1 != NULL) {
            if (randomint(10000) < variableCrossoverMutateChance) c1->setCrossMethod(randomint(NumberOfChoices));
    }
    if (c2 != NULL ) {
            if (randomint(10000) < variableCrossoverMutateChance) c1->setCrossMethod(randomint(NumberOfChoices));
    }
    
}
spatialRobocodeMvPParasite::spatialRobocodeMvPParasite(bool make) {
	thisParasiteNumber = parasiteIdentifierCount++;
	geneticAge =0;
	parasite = randomint(NUMBEROFPARASITES-8);
}
void spatialRobocodeParasiteDelta::doMutation(int i) {
    if (!participates) humanrobot = randomint(MAXROBOTNUMBER);
	else ParasiteDelta->smartSureMutate(10000);
}
void cr_data::twoPointReplacementSAGAOperator(const crPtr &op1,const crPtr &op2,crPtr &child1,crPtr &child2) {
    int thisInvalidCount;
	thisInvalidCount = 0;
	entity *pge0,*pge1;
	long *codons0,*codons1;
	pge0 = op1->theEntity;
	pge1 = op2->theEntity;
	if (pge0->getUsefulDna() > pge0->getDnaLength()) cout << "Well here is a problem\n";
	int len1 = pge0->getUsefulDna()/CODON;
	int len2 = pge1->getUsefulDna()/CODON; 
	//Note here limited to Useful dna as this is the only part which has codons for the match up
	
	if (len1 > 32000) {
		cout << "Length of dna divided by " << CODON << " is greater than 32000 ( " << len1 << ") this is going to cause an overflow on randomint, so chopping down\n";
		len1 = 32000;
	}
	if (len2 > 32000) {
		cout << "Length of dna divided by " << CODON << " is greater than 32000 ( " << len2 << ") this is going to cause an overflow on randomint, so chopping down\n";
		len2 = 32000;
	}
	const int *pdna1 = pge0->getDna();
	const int *pdna2 = pge1->getDna();
	codons0 = pge0->getCodons();
	codons1 = pge1->getCodons();
	bool valid = false;
	// okay the Matching only makes sense at the CODON boundaries.
	do {
		int c_slice1 = (randomint(len1-1)+1); // so generate a number from 1..len1-1 inclusive
		int c_slice2 = (randomint(len2-2)+1); // so generate a number from 1..len2-1 
		if (c_slice1 < 1) {
			cout << "ERROR slice1 is " << c_slice1 << ", must have non-sensible dna lengths (overflow) \n";
			c_slice1 = 5;
		}
		if (c_slice2 < 1) {
			cout << "ERROR slice2 is " << c_slice2 << ", must have non-sensible dna lengths (overflow) \n";
			c_slice2 = 5;
		}
		
#ifdef PROTECTIONENABLED
		if (USEPROTECTION) {
			// - first of all clear protection bits.
			for (int i=0;i< len1;i++) {
				codons0[i] = codons0[i] & EXPRPROTMASK;
			}
			// now go through setting the protection bits
			for (int i=1;i<len1;i++) { // start at 1, don't allow the whole set to be protected.
				if (codons0[i] & PROTECTEDBITMASK) { // bit eight of codon set
					int eOfexp = (codons0[i] >> EXPRLENGTH) & EXPRMASK;
					while (i< (eOfexp-1)) { // eOfexp-1 as we increment i before using it.
						i++; // this isnt a mistake we do not want to set it on the first one.
						codons0[i] = codons0[i] | INPROTECTIONMASK; //set last bit
					}
				}
			}
			// Okay the protection bits are now set so slide up and down until we find a c_slice we can use
			bool done = false;
			int low = c_slice1;
			int high = c_slice1;
			bool low_limit_reached = false;
			bool high_limit_reached = false;
			int found = -1;
			while (!done) {
				if (!(codons0[low] & INPROTECTIONMASK)) { // top bit not set
					found = low;
					done = true;
				} else if (!(codons0[high] & INPROTECTIONMASK)) {
					found = high;
					done = true;
				} else {
					if (low <=0) {
						if (high_limit_reached) done = true;
						else low_limit_reached = true;
						low = 0;
					} else low--;
					if (high >= len1-1) {
						if (low_limit_reached) done = true;
						else high_limit_reached = true;
					} else high++;
				}
			}
			c_slice1 = found;
			if (found <= 0) {
				cout << "The whole parent appears to have been protected!";
				// The question is what do we do here?
				// My first thought is to return the  parents untouched.
				// But this might "reward" survival of the genome by some
				// evolutionary stopping behaviour - but then if the others don't ... hmm
				// well thats what I will try initially. @@@@ Some @'s to make me 
				// think about it again later.
				child1 = op1;
				child2 = op2;
				//if (newMethodMutation) copyMut(op1,op2,toreturn);
				return ;
			}
		} // okay so we have now found a c_slice1 that is not within a protected part of the genome.
#endif	
		//  long i= 0;
		// we need to match up slice 1 and slice 2;
		int expr_to_match = ((codons0[c_slice1] >> CODON) & LHSMASK); // take the top bits
		if (expr_to_match ==0) {
			cout << "Problem\n";
			
		}
		
		// Search from the chosen point, higher and lower to find a match
		// with the new protection strategy we now need to prepare the second set of codons
		// to set the appropriate "protection bits"
#ifdef PROTECTIONENABLED
		if (USEPROTECTION) {
			// - first of all clear protection bits.
			for (int i=0;i< len2;i++)
				codons1[i] = codons1[i] & EXPRPROTMASK;
			// now go through setting the protection bits
			for (int i=1;i<len2;i++) {
				if (codons1[i] & PROTECTEDBITMASK) { // bit eight of codon set
					int eOfexp = (codons1[i] >> EXPRLENGTH) & EXPRMASK;
					while (i< (eOfexp-1)) { // eOfexp-1 as we increment i before using it.
						i++; // this isnt a mistake we do not want to set it on the first one.
						codons1[i] = codons1[i] | INPROTECTIONMASK; //set last bit
					}
				}
			}
		}