向量数组在这里发生了什么?

时间:2019-06-02 12:51:58

标签: c++ memory segmentation-fault ant-colony

我正在通过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个ant为止。 我为轮盘赌方法尝试了不同的种子,但它总是在同一位置崩溃。

我也改变了殖民地的蚂蚁数量。如果每个城市只有一只蚂蚁,则该程序可以正常执行,但数量更多时,该程序总是在第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++;
    }

}

1 个答案:

答案 0 :(得分:1)

那部分非常可疑:

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

因为 i map 的大小,但是您将其用作可能的字符串/向量的索引,所以您可能会使用不确定的行为

可能想要

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;

因此不是 Map 中的有效索引,但是有明显的理由要求查看代码,因此原因可能是 vector 的写出破坏你的记忆元素。

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

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

其中ant_i值为52,而 costs 有52个条目和 Colony 260,因此错误涉及 costs

请注意ant_i由循环设置

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

,在这种情况下,num_ants的值260比定义为

成本的大小大得多

 vector<double> costs(cities.size(), 0);

但是 cost 只是分配和设置但从未读取,因此其目标只是破坏内存。

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