问题描述
我正在尝试用“多个销售人员”解决TSP问题的一种变体,我有一系列的n
航点和m
无人机,我想产生一种平衡的结果无人机和返回可接受的最短旅行时间之间的航路点数量。目前,我真的不太担心寻找最佳解决方案,我只想在这一点上起作用。传统的TSP会运行多次。我的示例针对一系列航路点:
[0,1,2,3,4,5,6,7,8,9,10,11]
其中0 == 11
是起点和终点。假设我有4架无人机,我想生成类似以下内容的东西:
Drone A = [0,11]
Drone B = [0,11]
Drone C = [0,11]
Drone D = [0,11]
但是,我正在努力在交叉功能中生成一致的输出。我当前的功能如下:
DNA DNA::crossover( DNA &parentB)
{
// sol holds the individual solution for
// each drone
std::vector<std::vector<std::size_t>> sol;
// contains the values in flattened sol
// used to check for duplicates
std::vector<std::size_t> flat_sol;
// returns the number of solutions
// required
int number_of_paths = this→getSolution().size();
// limits the number of waypoints required for each drone
// subtracting 2 to remove “0” and “11”
std::size_t max_wp_per_drone = ((number_of_cities-2)/number_of_drones) + 1;
for(std::size_t i = 0; i < number_of_paths; i++)
{
int start = rand() % (this->getSolution().at(i).size() -2) + 1;
int end = start + 1 + rand() % ((this->getSolution().at(i).size()-2) - start +1);
std::vector<std::size_t>::const_iterator first = this->getSolution().at(i).begin()+start;
std::vector<std::size_t>::const_iterator second = this- >getSolution().at(i).begin()+end;
// First Problem occurs here… Sometimes,newOrder can return nothing based on
//the positions of start and end. Tried to mitigate by putting a while loop
to regenerate the vector
std::vector<std::size_t> newOrder(first,second);
// RETURNS a vector from the vector of vectors sol
flat_sol = flatten(sol);
// compare new Order with solution and remove any duplicates..
for(std::size_t k = 0; k < newOrder.size(); k++ )
{
int duplicate = newOrder.at(k);
if(std::find(flat_sol.begin(),flat_sol.end(),duplicate) != flat_sol.end())
{
// second problem is found here,sometimes,// new order might only return a vector with a single value
// or values that have already been assigned to another drone.
// In this case,those values are removed and newOrder is Now 0
newOrder.erase(newOrder.begin()+k);
}
}
// attempt to create the vectors here.
for(std::size_t j = 1; j <=parentB.getSolution().at(i).size()-2; j++)
{
int city = parentB.getSolution().at(i).at(j);
if(newOrder.empty())
{
if(std::find(flat_sol.begin(),city) == flat_sol.end())
{
newOrder.push_back(city);
}
}
else if((std::find(newOrder.begin(),newOrder.end(),city) == newOrder.end())
&&(std::find(flat_sol.begin(),city) == flat_sol.end())
&& newOrder.size() < max_wp_per_drone )
{
newOrder.push_back(city);
}
}
sol.push_back(newOrder);
}
// waypoints and number_of drones are kNown,//0 and 11 are appended to each vector in sol in the constructor.
return DNA(sol,waypoints,number_of_drones);
}
[0,11]
[0,11]
// This output is missing one waypoint.
[0,11]
// This output is correct.
不幸的是,这意味着在我后代的新孩子中。我得到正确的输出似乎是随机的。例如,对于一代人来说,我的人口规模有40个正确的孩子和60个缺少航路点的孩子,而在某些情况下,我有更多的正确的孩子。任何提示或帮助,表示赞赏。
解决方法
采用略有不同的方法解决了此问题。我没有在执行交叉操作之前拆分一系列航点,而只是传递了一系列航点
[0,1,2,3,4,5,6,7,8,9,10,11]
进行交叉,并在计算每个集合的适用性时,我根据m
无人机划分航路点,并找到每一代的最佳解决方案。新的分频器功能如下:
DNA DNA::crossover( DNA &parentB)
{
int start = rand () % (this->getOrder().size()-1);
int end = getRandomInt<std::size_t>(start +1,this->getOrder().size()-1);
std::vector<std::size_t>::const_iterator first = this->getOrder().begin() + start;
std::vector<std::size_t>::const_iterator second = this->getOrder().begin() + end;
std::vector<std::size_t> newOrder(first,second);
for(std::size_t i = 0; i < parentB.getOrder().size(); i++)
{
int city = parentB.getOrder().at(i);
if(std::find(newOrder.begin(),newOrder.end(),city) == newOrder.end())
{
newOrder.push_back(city);
}
}
return DNA(newOrder,waypoints,number_of_drones);
}