Esempio n. 1
0
void classifier_hyperrect::mutation()
{
	int i;
	int attribute, value;

	modif = 1;

	if(tGlobals->numClasses>1 && !rnd<0.10) {
		attribute = tGlobals->numAttributesMC;
		value=0;
	} else {
		attribute=rnd(0,tGlobals->numAttributesMC-1);
		value=rnd(0,tReal->attributeSize[attribute]-1);
	}

	if (attribute != tGlobals->numAttributesMC) {
		float oldValue=getGene(attribute,value);
		if (ai.getTypeOfAttribute(attribute) == REAL) {
			float newValue;
			float minOffset, maxOffset;
			minOffset = maxOffset =
			    0.5 * ai.getSizeDomain(attribute);
			newValue = mutationOffset(oldValue, minOffset,
						  maxOffset);
			if (newValue < ai.getMinDomain(attribute))
				newValue = ai.getMinDomain(attribute);
			if (newValue > ai.getMaxDomain(attribute))
				newValue = ai.getMaxDomain(attribute);
			setGene(attribute, value, newValue);
		} else {
			if(oldValue==1) setGene(attribute,value,0);
			else setGene(attribute,value,1);
		}
	} else {
		int newValue;
		int oldValue = (int) getGene(attribute, value);
		do {
			newValue = rnd(0, ai.getNumClasses()-1);
		} while (newValue == oldValue || tGlobals->defaultClassPolicy!=DISABLED && newValue==tGlobals->defaultClass);
		setGene(attribute, value, newValue);
	}
}
Esempio n. 2
0
void updateValueFunction(struct ValueFunction *valueFunction,struct GeneScore *geneScore){
	testGene(valueFunction,geneScore);
	// printf("New gene total score: %d\n",geneScore->score);
	if(geneScore->score > getGeneScore(valueFunction,0)->score)
	{
		struct Battle *battleA;
		struct Battle *battleB;
		int i;
		for(i=1;i<VF_SIZE;i++)
		{
			battleA = duel(getGene(valueFunction,i),geneScore->gene);
			battleB = duel(getGene(valueFunction,i),getGene(valueFunction,0));
			getGeneScore(valueFunction,i)->score += (battleA->score - battleB->score);
			destroyBattle(battleA);
			destroyBattle(battleB);
		}
		memcpy(valueFunction->geneScores[0],geneScore,sizeof(struct GeneScore));
		insertionSort(valueFunction->geneScores,VF_SIZE);
	}
}
Esempio n. 3
0
// test genescore against the value function
void testGene(struct ValueFunction *valueFunction,struct GeneScore *geneScore){
	int i;
	struct Battle *battle;
	// don't test against lowest-valued gene
	for(i=1;i<VF_SIZE;i++)
	{
		battle = duel(geneScore->gene,getGene(valueFunction,i));
		geneScore->score += battle->score;
		destroyBattle(battle);
	}
}
Esempio n. 4
0
int main(){
	int iterations;
	int print;
	int silent;
	printf("Enter total number of iterations:\n");
	scanf("%d",&iterations);
	printf("Enter number of iterations to average score over: \n");
	scanf("%d",&print);
	printf("Enter 1 to run program in background and save results to results.txt: \n");
	scanf("%d",&silent);
	if(silent == 1)
	{
		daemon(1,0);
	}

	// creates a value function set with VF_SIZE random good genes
	struct ValueFunction *valueFunction = createValueFunction();
	if(silent != 1)
		printf("Genes built\n");
	int i;
	int j;
	struct Battle *battle;
	// makes each gene fight each other gene and calculates their total score
	for(i=0;i<VF_SIZE;i++)
	{
		for(j=i;j<VF_SIZE;j++)
		{
			battle = duel(getGene(valueFunction,i),getGene(valueFunction,j));
			getGeneScore(valueFunction,i)->score += battle->score;
			getGeneScore(valueFunction,j)->score += (20 - battle->score);
			destroyBattle(battle);
		}
	}
	geneSort(valueFunction->geneScores,VF_SIZE);
	// print the score of every gene
	// for(i=0;i<VF_SIZE;i++)
	// {
	// 	printf("Gene %d total score: %d\n",i,getGeneScore(valueFunction,i)->score);
	// }

	char *threes = "33333333333333333333333333333333333333333333333333";
	// battle = duel(getGene(valueFunction,VF_SIZE-1),threes);
	// printf("Best gene score against threes: %d\n",battle->score);
	// destroyBattle(battle);
	// test adding another gene
	struct GeneScore *geneScore = createBetterGeneScore();
	float average = 0;
	for(i=0;i<iterations;i++)
	{
		updateValueFunction(valueFunction,geneScore);
		average += ((float)geneScore->score)/(float)print;
		if(i % print == 0 && i != 0){
			if(silent == 1)
			{
				FILE *fptr;
				fptr = fopen("results.txt","w");
				for(i=0;i<VF_SIZE;i++)
				{
					fprintf(fptr,"%s #%d average score: %f\n",getGene(valueFunction,i),i,(float)getGeneScore(valueFunction,i)->score/VF_SIZE);
				}	
				battle = duel(getGene(valueFunction,VF_SIZE-1),threes);
				fprintf(fptr,"Best gene score against threes: %d\n",battle->score);
				destroyBattle(battle);
				fclose(fptr);
				average = (float)0;

			}
			else
			{	
				printf("Average score: %f\n",average);
				average = (float)0;
			}
		}
		// geneScore = createBetterGeneScore();
		geneScore = createNthGeneScore(3);
	}
	destroyGeneScore(geneScore);

	battle = duel(getGene(valueFunction,VF_SIZE-1),threes);
	// print the score of every gene
	if(silent == 1)
	{
		FILE *fptr;
		fptr = fopen("results.txt","w");
		for(i=0;i<VF_SIZE;i++)
		{
			fprintf(fptr,"%s #%d average score: %f\n",getGene(valueFunction,i),i,(float)getGeneScore(valueFunction,i)->score/VF_SIZE);
		}
		fprintf(fptr,"Best gene score against threes: %d\n",battle->score);
		fclose(fptr);
	}
	else
	{
		for(i=0;i<VF_SIZE;i++)
		{
			printf("Gene %d average score: %f\n",i,(float)getGeneScore(valueFunction,i)->score/VF_SIZE);
			printf("Gene: %s\n",getGene(valueFunction,i));
		}	
	}
	destroyBattle(battle);
	destroyValueFunction(valueFunction);


	return 0;
}