rendered paste body#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include <math.h>
#define kMaxFlowers 11
class ai_World
{
public:
int temperature[kMaxFlowers];
int water[kMaxFlowers];
int sunlight[kMaxFlowers];
int nutrient[kMaxFlowers];
int beneficialInsect[kMaxFlowers];
int harmfulInsect[kMaxFlowers];
int currentTemperature;
int currentWater;
int currentSunlight;
int currentNutrient;
int currentBeneficialInsect;
int currentHarmfulInsect;
ai_World();
void ai_World::Encode(void);
int ai_World::Fitness(int flower);
void ai_World::Evolve(void);
int ai_World::tb_Rnd(int start, int end);
//~ai_World();
};
void ai_World::Encode(void)
{
int i;
srand((unsigned) time(NULL));
for (i=1;i<kMaxFlowers;i++)
{
temperature[i]=rand() % 75 ;
water[i]=rand() % 75;
sunlight[i]=rand() % 75;
nutrient[i]=rand() % 75;
beneficialInsect[i]=rand() % 75;
harmfulInsect[i]=rand() % 75;
}
currentTemperature=rand() % 75;
currentWater=rand() % 75;
currentSunlight=rand() % 75;
currentNutrient=rand() % 75;
currentBeneficialInsect=rand() % 75;
currentHarmfulInsect=rand() % 75;
}
int ai_World::Fitness(int flower)
{
int theFitness=0;
theFitness = abs(temperature[flower] - currentTemperature);
theFitness = theFitness+abs(water[flower] - currentWater);
theFitness = theFitness+abs(sunlight[flower] -
currentSunlight);
theFitness = theFitness+abs(nutrient[flower] -
currentNutrient);
theFitness = theFitness+abs(beneficialInsect[flower] -
currentBeneficialInsect);
theFitness = theFitness+abs(harmfulInsect[flower] -
currentHarmfulInsect);
return (theFitness);
}
int ai_World::tb_Rnd(int start, int end)
{
if (start > end)
return 0;
else
{
//srand(time(0));
return (rand() % (end + 1) + start);
}
}
void ai_World::Evolve(void)
{
int fitTemperature[kMaxFlowers];
int fitWater[kMaxFlowers];
int fitSunlight[kMaxFlowers];
int fitNutrient[kMaxFlowers];
int fitBeneficialInsect[kMaxFlowers];
int fitHarmfulInsect[kMaxFlowers];
int fitness[kMaxFlowers];
int i;
int leastFit=0;
int leastFitIndex;
srand((unsigned) time(NULL));
for (i=1;i<kMaxFlowers;i++)
if (Fitness(i)>leastFit)
{
leastFit=Fitness(i);
leastFitIndex=i;
}
temperature[leastFitIndex]=temperature[rand() %10];
water[leastFitIndex]=water[rand() %10];
sunlight[leastFitIndex]=sunlight[rand() %10];
nutrient[leastFitIndex]=nutrient[rand() %10];
beneficialInsect[leastFitIndex]=beneficialInsect[rand() %10];
harmfulInsect[leastFitIndex]=harmfulInsect[rand() %10];
for (i=1;i<kMaxFlowers;i++)
{
fitTemperature[i]=temperature[rand() %10];
fitWater[i]=water[rand() %10];
fitSunlight[i]=sunlight[rand() %10];
fitNutrient[i]=nutrient[rand() %10];
fitBeneficialInsect[i]=beneficialInsect[rand() %10];
fitHarmfulInsect[i]=harmfulInsect[rand() %10];
}
for (i=1;i<kMaxFlowers;i++)
{
temperature[i]=fitTemperature[i];
water[i]=fitWater[i];
sunlight[i]=fitSunlight[i];
nutrient[i]=fitNutrient[i];
beneficialInsect[i]=fitBeneficialInsect[i];
harmfulInsect[i]=fitHarmfulInsect[i];
}
for (i=1;i<kMaxFlowers;i++)
{
///PILAS AKI------------------------------
if (tb_Rnd(1,100)==1)
temperature[i]=rand() %75;
if (tb_Rnd(1,100)==1)
water[i]=rand() %75;
if (tb_Rnd(1,100)==1)
sunlight[i]=rand() %75;
if (tb_Rnd(1,100)==1)
nutrient[i]=rand() %75;
if (tb_Rnd(1,100)==1)
beneficialInsect[i]=rand() %75;
if (tb_Rnd(1,100)==1)
harmfulInsect[i]=rand() %75;
}
}
int main()
{
ai_World a;
return 1;
}