有没有办法将 long double 转换为 double 并在 c++ 中相应地缩放范围?
is there a way to cast a long double to a double and scale the range accordingly in c++?
我正在使用 C++ 中的遗传算法。为了加速进化,我以使用 long double 的整个范围的方式对适应度值取幂。这使得分数之间的差异更大,并且使好的分数比普通分数好得多,这样好的分数(呈指数级)更有可能被选择用于复制。
现在,我想将每个分数转换回双精度并相应地缩放它,以便我可以进行平均等操作。问题是,我似乎无法使用这里看到的典型缩放公式 Mapping a numeric range onto another 将 long double 的范围压缩到 double 的范围,我也想不出另一种方法来进行转换。
void DNA::fitnessFunction(const vector<double>& target){
//long double maxScore = pow((long double)8., (long double)5461.);
long double score = 0.;
for(int i = 0; i< numberOfGenes; i++){
int difference= abs (target[i] - genes[i]);
//if difference > 50: threshold
double distance = (double) (255.0 - difference) / 255.0;
score+= distance;
}
score = utilities::map(score, 0, genes.size(), 0.0, 5461.);
long double temp = pow((long double)8., score);
this->fitness = (double)utilities::map(temp, (long double) std::numeric_limits<long double>::min(), (long double)std::numeric_limits<long double>::max(), (long double)std::numeric_limits<double>::min(), (long double) std::numeric_limits<double>::max());
}
我建议计算 真实 适应度值并在选择步骤中进行所需的缩放。目前,您将适应度计算与适应度缩放混合在一起。将这两个步骤分开还有一个好处,即您可以尝试不同的 健身缩放 策略。
你可以看看下面的class:ExponentialRankSelector。该示例在 Java 中,但它应该能让您更好地理解我的意思。
我正在使用 C++ 中的遗传算法。为了加速进化,我以使用 long double 的整个范围的方式对适应度值取幂。这使得分数之间的差异更大,并且使好的分数比普通分数好得多,这样好的分数(呈指数级)更有可能被选择用于复制。
现在,我想将每个分数转换回双精度并相应地缩放它,以便我可以进行平均等操作。问题是,我似乎无法使用这里看到的典型缩放公式 Mapping a numeric range onto another 将 long double 的范围压缩到 double 的范围,我也想不出另一种方法来进行转换。
void DNA::fitnessFunction(const vector<double>& target){
//long double maxScore = pow((long double)8., (long double)5461.);
long double score = 0.;
for(int i = 0; i< numberOfGenes; i++){
int difference= abs (target[i] - genes[i]);
//if difference > 50: threshold
double distance = (double) (255.0 - difference) / 255.0;
score+= distance;
}
score = utilities::map(score, 0, genes.size(), 0.0, 5461.);
long double temp = pow((long double)8., score);
this->fitness = (double)utilities::map(temp, (long double) std::numeric_limits<long double>::min(), (long double)std::numeric_limits<long double>::max(), (long double)std::numeric_limits<double>::min(), (long double) std::numeric_limits<double>::max());
}
我建议计算 真实 适应度值并在选择步骤中进行所需的缩放。目前,您将适应度计算与适应度缩放混合在一起。将这两个步骤分开还有一个好处,即您可以尝试不同的 健身缩放 策略。
你可以看看下面的class:ExponentialRankSelector。该示例在 Java 中,但它应该能让您更好地理解我的意思。