|
|
|
@ -57,30 +57,42 @@ namespace komvo
|
|
|
|
|
int[] toSwap = new int[Points.Count - 1]; |
|
|
|
|
for (int i = 0; i <= toSwap.Length - 1; i++) toSwap[i] = i + 2; |
|
|
|
|
|
|
|
|
|
int index = 0; |
|
|
|
|
bool first = true; |
|
|
|
|
double opLength = 0, prevResult = 0; |
|
|
|
|
string opRoute = string.Empty; |
|
|
|
|
|
|
|
|
|
Permutations<int> perm = new Permutations<int>(toSwap); |
|
|
|
|
foreach(IList<int> p in perm) |
|
|
|
|
{ |
|
|
|
|
string route = string.Empty; |
|
|
|
|
for (int i = 0; i < toSwap.Length; i++) route += p[i].ToString() + ";"; |
|
|
|
|
opLength = CountLength(Matrix, route); |
|
|
|
|
int[] route = new int[Points.Count - 1]; |
|
|
|
|
for (int i = 0; i < toSwap.Length; i++) route[i] = p[i]; |
|
|
|
|
prevResult = CountLength(Matrix, route); |
|
|
|
|
//WriteLine(opLength); |
|
|
|
|
|
|
|
|
|
if (index != 0) |
|
|
|
|
if (first) |
|
|
|
|
{ |
|
|
|
|
if (opLength > prevResult) |
|
|
|
|
{ |
|
|
|
|
opLength = prevResult; |
|
|
|
|
opRoute = route; |
|
|
|
|
} |
|
|
|
|
opLength = prevResult; |
|
|
|
|
first = false; |
|
|
|
|
} |
|
|
|
|
else prevResult = opLength; |
|
|
|
|
|
|
|
|
|
index++; |
|
|
|
|
if(opLength > prevResult) |
|
|
|
|
{ |
|
|
|
|
opLength = prevResult; |
|
|
|
|
opRoute = string.Empty; |
|
|
|
|
foreach (int i in route) opRoute += i + ";"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//int[] toSwapNew = new int[Points.Count - 1]; |
|
|
|
|
//for (int i = 0; i <= toSwap.Length - 1; i++) toSwapNew[i] = i + 2; |
|
|
|
|
//foreach (int i in toSwapNew) Write(i + " "); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WriteLine("\n\n1;{1}1 - оптимальный маршрут длинной {0:00.00}\nИз {2} маршрутов при {3} точках\nПосчитано за {4} мс\n\n", |
|
|
|
|
opLength, opRoute, Fact(Points.Count-1), Points.Count, stopwatch.ElapsedMilliseconds); |
|
|
|
|
stopwatch.Stop(); |
|
|
|
@ -95,22 +107,21 @@ namespace komvo
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static double CountLength(double[,] mtr, string route) |
|
|
|
|
public static double CountLength(double[,] mtr, int[] route) |
|
|
|
|
{ |
|
|
|
|
double result = 0; |
|
|
|
|
string[] index = route.Split(';'); |
|
|
|
|
|
|
|
|
|
for (int j = 0; j < index.Length - 1; j++) |
|
|
|
|
for (int j = 0; j < route.Length - 1; j++) |
|
|
|
|
{ |
|
|
|
|
if (j == 0) result += mtr[0, int.Parse(index[j]) - 1]; |
|
|
|
|
else if (j == index.Length - 1) |
|
|
|
|
if (j == 0) result += mtr[0, route[j] - 1]; |
|
|
|
|
else if (j == route.Length - 1) |
|
|
|
|
{ |
|
|
|
|
result += mtr[int.Parse(index[j - 1]) - 2, int.Parse(index[j]) - 2]; |
|
|
|
|
result += mtr[0, int.Parse(index[j]) - 2]; |
|
|
|
|
result += mtr[route[j - 1] - 2, route[j] - 2]; |
|
|
|
|
result += mtr[0, route[j] - 2]; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
result += mtr[int.Parse(index[j - 1]) - 2, int.Parse(index[j]) - 2]; |
|
|
|
|
result += mtr[route[j - 1] - 2, route[j] - 2]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|