这里的矢量数组发生了什么?



我正在通过 C++ 年的 ACO 实现来解决旅行推销员问题。但是,我发现到目前为止我构建的程序给出了分段错误。 (注意:出于调试目的,我将算法限制为只执行殖民地的一次迭代)。

首先,我从一个文件中总共提取了52个城市,我分发了蚂蚁,以便每个城市都有相同数量的蚂蚁。

为了存储每对城市之间的距离,我使用了一个名为 Map(方阵)的双精度向量向量。但是,在执行过程中,这些向量似乎被删除了。在这种情况下,它在计算编号 55 的蚂蚁的路径时发生。我添加了一段代码,只是为了突出显示它崩溃的确切位置:

//DEBUGGING SECTION
cout << "Size Roulette: " << Roulette.size() << endl;
cout << "Size Remain: " << RemainingCities.size() << endl;
cout << "Size Map: " << Map.size() << " x " << Map[0].size() << endl;
int k = 0;
cout << "Test: Map access: " << endl;
for(int i = 0; i < Map.size(); ++i) //  HERE IT CRASHES AT ANT NUMBER 55
cout << Map[0][i] << " ";
cout << endl;
cout << "Test: Operation: " << Map[Colony[ant_i][city_i-1]][RemainingCities[k]] << endl;
Roulette[k] = pow((MAX_DIST - Map[Colony[ant_i][city_i-1]][RemainingCities[k]]), heur_coef) + pow((pheromones[Colony[ant_i][city_i-1]][RemainingCities[k]]), pher_coef);
//END OF DEBUGGING SECTION

在那里,函数 Map[0].size() 通常返回 52(就像 Map.size(),因为它应该是一个方阵),但在崩溃迭代中它返回看起来像内存地址的内容,当我尝试访问任何元素时,就会发生分割错误。

我已经检查了内存访问是否始终正确,并且我可以毫无问题地访问任何其他变量,除了 Map 直到第 55 只蚂蚁。 我已经为轮盘赌方法尝试了不同的种子,但它总是在同一个地方崩溃。

我还改变了蚁群的数量。如果每个城市只有一只蚂蚁,程序可以毫无问题地执行,但对于任何更高的数量,程序总是在第 55 只蚂蚁崩溃。

您可以从 github 下载完整的 cpp 文件和读取 .tsp 文件:

https://github.com/yitosmash/ACO

无论如何,我将把完整的功能留在这里:

void ACO(const vector<City>& cities, const vector<vector<double>>& Map, int max_it, int num_ants, double decay, double heur_coef, double pher_coef, double pher_coef_elit)
{
srand(30);
//Initialise colony of ants (each ant is a vector of city indices)
vector<vector<int>> Colony(num_ants, vector<int>(cities.size(), 0));
//Initialise pheromone matrix
vector<vector<double>> pheromones(cities.size(), vector<double>(cities.size(), 0));
//Initialise costs vector(for etilist expansion)
vector<double> costs(cities.size(), 0);
//Auxiliar vector of indices
vector<int> cityIndices(cities.size());
for (int i = 0; i < cities.size(); ++i)
cityIndices[i] = i;
//Longest distance from Map, used for heuristic values.
vector<double> longests(cities.size(), 0);
for(int i = 0; i < cities.size(); ++i)
longests[i] = *(max_element(Map[i].begin(), Map[i].end()));
const double MAX_DIST = *(max_element(longests.begin(), longests.end()));
longests.clear();

int i=0;
while(i<max_it)
{
for(int ant_i = 0; ant_i < num_ants; ++ant_i)
{
cout << "Ant: " << ant_i << endl;
//City for ant_i to start at; each ant is assigned a determined starting city
int starting_city = (int) ((float)ant_i/num_ants*cities.size());
//cout << starting_city << endl;
Colony[ant_i][0] = starting_city;
//Get a vector with the cities left to visit
vector<int> RemainingCities = cityIndices;
//Remove starting city from remaining cities
RemainingCities.erase(RemainingCities.begin() + starting_city);
//Create path for ant_i
for(int city_i = 1; city_i < Colony[ant_i].size(); ++city_i)
{
cout << "Calculating city number: " << city_i << endl;
//Create roulette for next city selection
vector<double> Roulette(RemainingCities.size(), 0);
double total = 0;
//DEBUGGING SECTION
cout << "Size Roulette: " << Roulette.size() << endl;
cout << "Size Remain: " << RemainingCities.size() << endl;
cout << "Size Map: " << Map.size() << " x " << Map[0].size() << endl;
int k = 0;
cout << "Test: Map access: " << endl;
for(int i = 0; i < Map.size(); ++i) //  HERE IT CRASHES AT ANT NUMBER 55
cout << Map[0][i] << " ";
cout << endl;
cout << "Test: Operation: " << Map[Colony[ant_i][city_i-1]][RemainingCities[k]] << endl;
Roulette[k] = pow((MAX_DIST - Map[Colony[ant_i][city_i-1]][RemainingCities[k]]), heur_coef) + pow((pheromones[Colony[ant_i][city_i-1]][RemainingCities[k]]), pher_coef);
//END OF DEBUGGING SECTION
for(int j = 0; j < RemainingCities.size(); ++j)
{
//Heuristic value is MAX_DIST - current edge.
Roulette[j] = pow((MAX_DIST - Map[Colony[ant_i][city_i-1]][RemainingCities[j]]), heur_coef) + pow((pheromones[Colony[ant_i][city_i-1]][RemainingCities[j]]), pher_coef);
total += Roulette[j];
}
cout << endl;
//Transform roulette into stacked probabilities
Roulette[0] = Roulette[0]/total;
for(int j = 1; j < Roulette.size(); ++j)
Roulette[j] = Roulette[j-1] + Roulette[j] / total;
//Select a city from Roulette
int chosen = 0;
double r = (double) rand()/RAND_MAX;
while(Roulette[chosen] < r)
chosen++;
//Add chosen city to
Colony[ant_i][city_i] = RemainingCities[chosen];
RemainingCities.erase(RemainingCities.begin() + chosen);
}
cout << endl;
//Save cost of ant_i, for elitist expansion
costs[ant_i] = pathCost(Colony[ant_i], Map);
}
i++;
}
}

这部分非常可疑:

for(int i = 0; i < Map.size(); ++i) //  HERE IT CRASHES AT ANT NUMBER 55
cout << Map[0][i] << " ";

因为我地图的大小,但您将其用作可能的字符串/向量中的索引,因此您可能会以未定义的行为脱离字符串/向量

可能你想要

for(int i = 0; i < Map.size(); ++i)
cout << Map[i] << " ";

for(int i = 0; i < Map[0].size(); ++i)
cout << Map[0][i] << " ";

正如我在评论中所说,RemainingCities[0]价值观 - 163172699首先

cout << "Test: Operation: " << Map.at(Colony.at(ant_i).at(city_i-1)).at(RemainingCities.at(k)) << endl;

so 不是 Map中的有效索引,但有明显的理由查看代码,因此原因是可能写出破坏内存元素的向量

检测我用.at(...)替换了所有[...]的位置,我遇到的第一个错误是在行的ACO

costs.at(ant_i) = pathCost(Colony.at(ant_i), Map);

其中ant_i值为 52,成本值为 52,殖民地值为 260,因此错误涉及成本

请注意,ant_i由循环设置

for(int ant_i = 0; ant_i < num_ants; ++ant_i)

在这种情况下,num_ants价值 260 远远超过成本规模

,成本规模定义为
vector<double> costs(cities.size(), 0);

成本只是分配和设置,但从不读取,所以它的目标只是破坏内存。

如果我删除有关它的两行,则不再有错误并且程序正常结束,则.at(...)中没有异常,并且valgrind也没有检测到错误。

相关内容

  • 没有找到相关文章