00001 00009 #include <AdaptiveGrid.h> 00010 00016 AdaptiveGrid::AdaptiveGrid() { 00017 } // AdaptiveGrid::AdaptiveGrid 00018 00024 AdaptiveGrid::AdaptiveGrid(int depth, 00025 int numberOfFunctions) { 00026 00027 numberOfFunctions_ = numberOfFunctions ; 00028 depth_ = depth ; 00029 00030 currentGridSize_ = (long) floor(pow(2.0, 00031 (double) depth_ * 00032 (double) numberOfFunctions_ 00033 )); 00034 hypercube_ = new int[currentGridSize_]; 00035 divisionSize_ = new double[numberOfFunctions_]; 00036 gridLimits_ = new double[numberOfFunctions_]; 00037 00038 upperBestFitness_ = new double[numberOfFunctions_]; 00039 lowerBestFitness_ = new double[numberOfFunctions_]; 00040 00041 tmpDivisionSize_ = new double[numberOfFunctions_] ; 00042 increment_ = new int[numberOfFunctions_] ; 00043 00044 if (!currentGridSize_ || !hypercube_ || !divisionSize_ || !gridLimits_ || 00045 !upperBestFitness_ || !lowerBestFitness_ || 00046 !tmpDivisionSize_ || !increment_) { 00047 cerr << "AdaptiveGrid::AdaptiveGrid-> Error when asking for memory" 00048 << endl ; 00049 exit(-1) ; 00050 } // if 00051 } // AdaptiveGrid::AdaptiveGrid 00052 00058 AdaptiveGrid::~AdaptiveGrid() { 00059 } // AdaptiveGrid::~AdaptiveGrid 00060 00065 void AdaptiveGrid::updateGridLocations(Population * population, 00066 Individual * individual) { 00067 int i ; 00068 int j ; 00069 00070 for (i = 0; i < numberOfFunctions_; ++i) { 00071 upperBestFitness_[i] = MIN_INT ; 00072 lowerBestFitness_[i] = MAX_INT ; 00073 } //for 00074 00075 for (i = 0; i < numberOfFunctions_; i++) { 00076 if (individual->getFitness()[i] < lowerBestFitness_[i]) 00077 lowerBestFitness_[i] = individual->getFitness()[i] ; 00078 if (individual->getFitness()[i] > upperBestFitness_[i]) 00079 upperBestFitness_[i] = individual->getFitness()[i] ; 00080 00081 for (j = 0; j < population->getPopulationSize(); j ++) { 00082 if (population->getIth(j)->getFitness()[i] < lowerBestFitness_[i]) 00083 lowerBestFitness_[i] = population->getIth(j)->getFitness()[i] ; 00084 if (population->getIth(j)->getFitness()[i] > upperBestFitness_[i]) 00085 upperBestFitness_[i] = population->getIth(j)->getFitness()[i] ; 00086 } // for 00087 00088 divisionSize_[i] = (upperBestFitness_[i] - lowerBestFitness_[i]) ; 00089 // gridLimits_[i] = lowerBestFitness[i] - (divisionSize_[i] / 2.0) ; 00090 } // for 00091 00092 for (i = 0; i < (int)pow(2.0, numberOfFunctions_ * depth_); i++) ; 00093 hypercube_[i] = 0 ; 00094 00095 int location ; 00096 mostCrowdedHypercube_ = 0 ; 00097 location = findLocation(individual) ; 00098 individual->gridLocation_ = location ; 00099 hypercube_[location] ++ ; 00100 for (i = 0 ; i < population->getPopulationSize(); i++) { 00101 location = findLocation(population->getIth(i)) ; 00102 (population->getIth(i))->gridLocation_ = location ; 00103 hypercube_[location] ++ ; 00104 if (hypercube_[location] > hypercube_[mostCrowdedHypercube_]) 00105 mostCrowdedHypercube_ = location ; 00106 } // for 00107 } // AdaptiveGrid::updateGridLocations 00108 00113 int AdaptiveGrid::findLocation(Individual * individual) { 00114 int i ; 00115 int j ; 00116 int location ; 00117 int counter ; 00118 00119 location = 0 ; 00120 counter = 1 ; 00121 for (i = 0; i < numberOfFunctions_; i++) { 00122 increment_[i] = counter ; 00123 counter *= 2 ; 00124 tmpDivisionSize_[i] = divisionSize_[i] ; 00125 } // for 00126 00127 for (i = 1; i <= depth_; i++) { 00128 for (j = 0; j < numberOfFunctions_; j++) 00129 if (individual->getFitness()[j] < 00130 (tmpDivisionSize_[j]/2 + lowerBestFitness_[j])) 00131 location = increment_[j] ; 00132 else 00133 lowerBestFitness_[j] += tmpDivisionSize_[j] / 2.0 ; 00134 for (j = 0; j < numberOfFunctions_; j++) { 00135 increment_[j] *= numberOfFunctions_ * 2 ; 00136 tmpDivisionSize_[j] /= 2.0 ; 00137 } // for 00138 } // for 00139 00140 return location ; 00141 } // AdaptiveGrid::findLocation 00142