C# “类型”数组上的置换;地点;,来自GoogleMapsAPI NuGet软件包
这是我在StackOverflow上的第一篇帖子,如果我做错了什么,请告诉我,英语不是我的母语,如果有任何语法错误,请原谅我 我的问题是如何排列“位置”类型数组中的项目,我需要获得用户给定的所有可能的路径点排列,然后根据时间或距离计算最佳路线。(我不想使用常规路线计算) 我搜索过算法,但当我在函数的参数中放入类型为“Location[]”的数组时,我得到一个错误,即对象需要是IEnumerable,我不知道如何转换为IEnumerable,如果可能的话,我从未使用过IEnumerable 如果有帮助,这是我计算路线的代码:C# “类型”数组上的置换;地点;,来自GoogleMapsAPI NuGet软件包,c#,arrays,algorithm,google-maps,permutation,C#,Arrays,Algorithm,Google Maps,Permutation,这是我在StackOverflow上的第一篇帖子,如果我做错了什么,请告诉我,英语不是我的母语,如果有任何语法错误,请原谅我 我的问题是如何排列“位置”类型数组中的项目,我需要获得用户给定的所有可能的路径点排列,然后根据时间或距离计算最佳路线。(我不想使用常规路线计算) 我搜索过算法,但当我在函数的参数中放入类型为“Location[]”的数组时,我得到一个错误,即对象需要是IEnumerable,我不知道如何转换为IEnumerable,如果可能的话,我从未使用过IEnumerable 如果有
//Gets the waypoints from a listBox provided by the user, "mode" selects between best time and best distance
//backgroundworker so the UI dont freezes, and return the optimal waypoint order
public Location[] CalcularRota(Location[] waypoints, int mode, BackgroundWorker work, DoWorkEventArgs e)
{
//Declarations
string origem = "";
string destino = "";
Rota[] prop = new Rota[100]; //this index is the number of times the algorithm will be executed, more equals accuracy but much more time to complete
Rota bestDist = new Rota();
Rota bestTime = new Rota();
DirectionService serv = new DirectionService();
DirectionRequest reqs = new DirectionRequest();
DirectionResponse resp;
Random rnd = new Random();
Location[] rndWays;
int dist = 0;
int ti = 0;
bestDist.Distance = 1000000000; //put higher values for the first comparation to be true (end of code)
bestTime.Time = 1000000000;
if (waypoints != null)
{
reqs.Sensor = false;
reqs.Mode = TravelMode.driving;
for (int i = 0; i < prop.Length; i++) //initializes prop
prop[i] = new Rota();
for (int i = 0; i < prop.Length; i++)
{
rndWays = waypoints.OrderBy(x => rnd.Next()).ToArray(); //randomizes the order, I want to get all permutations and then test
//but I dont know how so I've been using randomized
dist = ti = 0;
origem = prop[0].ToString(); //save this particular waypoint's origin and destination
destino = prop[1].ToString();
reqs.Origin = origem;
reqs.Destination = destino;
if (waypoints.Length > 0)
reqs.Waypoints = rndWays;
resp = serv.GetResponse(reqs); //request the route with X order of waypoints to google
if (resp.Status == ServiceResponseStatus.Ok) //wait the response otherwise the program crashes
{
for (int j = 0; j < resp.Routes[0].Legs.Length; j++) //gets the distance and time of this particular order
{
ti += int.Parse(resp.Routes[0].Legs[j].Duration.Value);
dist += int.Parse(resp.Routes[0].Legs[j].Distance.Value);
}
}
prop[i].Origem = origem; //saves this waypoints order details for further comparison
prop[i].Destino = destino;
prop[i].Distance = dist;
prop[i].Time = ti;
prop[i].Order = rndWays;
work.ReportProgress(i); //report the progress
}
for (int i = 0; i < prop.Length; i++) //gets the best distance and time
{
if (bestDist.Distance > prop[i].Distance)
{
bestDist.Distance = prop[i].Distance;
bestDist.Time = prop[i].Time;
bestDist.Order = prop[i].Order;
bestDist.Origem = prop[i].Origem;
bestDist.Destino = prop[i].Destino;
}
if (bestTime.Time > prop[i].Time)
{
bestTime.Distance = prop[i].Distance;
bestTime.Time = prop[i].Time;
bestTime.Order = prop[i].Order;
bestTime.Origem = prop[i].Origem;
bestTime.Destino = prop[i].Destino;
}
}
if (bestDist.Order == bestTime.Order) //if the same waypoint order has the same time and distance
return bestDist.Order; // returns whatever bestDist.Order or bestTime.Order
else if (bestDist.Order != bestTime.Order) //if different returns corresponding to the mode selected
{
if (mode == 1) return bestDist.Order;
if (mode == 2) return bestTime.Order;
}
}
return null;
}
执行此操作并将我的阵列(rndWays)作为重载,我会得到以下错误:
类型“Google.Maps.Location”不能用作泛型类型或方法“Form1.nextPermutat
public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>
并用自己的比较函数替换每个CompareTo()。你能提供具体的代码来显示错误吗?增加了排列算法。
NextPermutation(array);
public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>
public static bool NextPermutation(Location[] elements)