125 lines
4.1 KiB
C++
125 lines
4.1 KiB
C++
#include "Settings.h"
|
|
#include "../population/Population.h"
|
|
#include "../individual/Phenotype.h"
|
|
#include "../individual/genome/strategies/GenotypeToPhenotypeStrategy.h"
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include "KolchShindyalov.h"
|
|
|
|
void KolchShindyalov::test01(){
|
|
std::ofstream statFile("statFile.txt");
|
|
std::string stat;
|
|
|
|
// Èíèöèàëèçàöèÿ
|
|
int maxGenerations = Settings::Iterations;
|
|
long initPopSize = Settings::InitPopSize;
|
|
float ratioBetweenPops = Settings::InitRatioBetweenPops;
|
|
long int maxIndivids = Settings::KMaxParameter;
|
|
|
|
Position3D<double> position(0, 0, 0, 1000, 1000, 1000);
|
|
Region region(position, maxIndivids);
|
|
|
|
srand(0);
|
|
int RANGE = 1000;
|
|
int PRECISION = 10;
|
|
float gValue;
|
|
|
|
std::vector<Individual*> individs;
|
|
|
|
// Ñòîõàñòè÷åñêîå ñîçäàíèå ïîïóëÿöèè
|
|
for(int i = 0; i < initPopSize; i++){
|
|
// Ãåíîì
|
|
std::vector<Chromosome> fGenome;
|
|
std::vector<Chromosome> mGenome;
|
|
|
|
std::vector<Chromosome> chroms;
|
|
for(int j = 0; j < 2; j++){
|
|
//float gValue
|
|
// Îäíà õðîìîñîìà ñ øåñòüþ ãåíàìè
|
|
RANGE = 10;
|
|
gValue = -(float)(rand()%(RANGE*PRECISION+1))/PRECISION;
|
|
Gene geneF1(Gene::Continious, "C1", gValue);
|
|
gValue = -(float)(rand()%(RANGE*PRECISION+1))/PRECISION;
|
|
Gene geneF2(Gene::Continious, "C2", gValue);
|
|
|
|
PRECISION=1;
|
|
RANGE = 2;
|
|
gValue = (rand()%(RANGE*PRECISION + 1)) / (float) RANGE / PRECISION;
|
|
Gene geneF3(Gene::Continious, "C3", gValue);
|
|
gValue = (rand()%(RANGE*PRECISION + 1)) / (float) RANGE / PRECISION;
|
|
Gene geneF4(Gene::Continious, "C4", gValue);
|
|
gValue = (rand()%(RANGE*PRECISION + 1)) / (float) RANGE / PRECISION;
|
|
Gene geneF5(Gene::Continious, "C5", gValue);
|
|
gValue = (rand()%(RANGE*PRECISION + 1)) / (float) RANGE / PRECISION;
|
|
Gene geneF6(Gene::Continious, "E", gValue);
|
|
gValue = rand()%2;
|
|
Gene geneF7(Gene::Discrete,"Feedback", gValue);
|
|
|
|
Chromosome chromF("Chrom 1"); // ïàïà / ìàìà
|
|
chromF.insertGeneToEnd(geneF1);
|
|
chromF.insertGeneToEnd(geneF2);
|
|
chromF.insertGeneToEnd(geneF3);
|
|
chromF.insertGeneToEnd(geneF4);
|
|
chromF.insertGeneToEnd(geneF5);
|
|
chromF.insertGeneToEnd(geneF6);
|
|
chromF.insertGeneToEnd(geneF7);
|
|
|
|
chroms.push_back(chromF);
|
|
} // (END) for(int i = 0; i < 2; i++)
|
|
|
|
fGenome.push_back(chroms.at(0));
|
|
mGenome.push_back(chroms.at(1));
|
|
Genotype* genotype = new Genotype(fGenome, mGenome);
|
|
// (END) Ãåíîì
|
|
|
|
// Ôåíîòèï
|
|
Trait trait1(Trait::Continious, "X", 10.0f);
|
|
Trait trait2(Trait::Continious, "Xopt", 10.0f);
|
|
Trait trait3(Trait::Continious, "sigmaX", 3.0f);
|
|
|
|
Phenotype* phenotype = new Phenotype(trait1);
|
|
phenotype->addTrait(trait2);
|
|
phenotype->addTrait(trait3);
|
|
// (END) Ôåíîòèï
|
|
|
|
Individual* newInd = new Individual(genotype, phenotype, 0, Individual::hermaphrodite, 0);
|
|
newInd->setGenToPhenStrategy(GenotypeToPhenotypeStrategies::getInstance("kolchan_shindyal_gen_to_phen"));
|
|
newInd->setPhenToFitnessStrategy(PhenotypeToFitnessStrategy::getInstance("kolch_shindyal_phen_to_fit"));
|
|
individs.push_back(newInd);
|
|
} // (END) for(int i = 0; i < size; i++)
|
|
|
|
AsexualPopulation population(individs);
|
|
population.setBreedingStrategy(PopulationBreedingStrategy::getInstance("kolchan_shindyal_breeding"));
|
|
population.setRegion(®ion);
|
|
// (END) Ñîçäàíèå ïîïóëÿöèè
|
|
|
|
// Èòåðàöèîííûé öèêë
|
|
for(int i = 0; i < maxGenerations; i++){
|
|
// Ðàçìíîæèòü
|
|
population.breedAll();
|
|
//--- Ñòàòèñòèêà ---
|
|
stat = population.getSatistics();
|
|
statFile<<stat<<std::endl;
|
|
|
|
if(Settings::WRITE_FULL_STATISTICS_TO_FILE){
|
|
std::stringstream ss;
|
|
std::stringstream ss2;
|
|
std::stringstream ss3;
|
|
ss<<"generation."<<(i)<<".xls";
|
|
ss2<<"generation."<<(i)<<".txt";
|
|
ss3<<"gmaxAB."<<(i)<<".txt";
|
|
std::ofstream genFile(ss.str().c_str());
|
|
std::ofstream genFile2(ss2.str().c_str());
|
|
std::ofstream genFile3(ss3.str().c_str());
|
|
population.putGeneticStatisticsToStream(genFile);
|
|
population.putGeneticSimpleStatisticsToStream(genFile2);
|
|
population.putGeneticMaxModuleStatisticsToStream(genFile3);
|
|
genFile.close();
|
|
genFile2.close();
|
|
genFile3.close();
|
|
}
|
|
//------------------
|
|
}
|
|
statFile.close();
|
|
} |