private static bool Opt2(PathWithValues path, double[,] distances, int[] weights) { var tmpNodes = path.CloneNodes(); int totalNodesCount = path.Nodes.Count; double distDif = 0; for (int i = 0; i < totalNodesCount - 2; i++) { for (int j = i + 2; j < totalNodesCount; j++) { if (j == totalNodesCount - 1) { distDif = distances[path.GetNodeAt(i), path.GetNodeAt(j)] + distances[path.GetNodeAt(i + 1), path.GetNodeAt(0)] - (distances[path.GetNodeAt(i), path.GetNodeAt(i + 1)] + distances[path.GetNodeAt(j), path.GetNodeAt(0)]); } else { distDif = distances[path.GetNodeAt(i), path.GetNodeAt(j)] + distances[path.GetNodeAt(i + 1), path.GetNodeAt(j + 1)] - distances[path.GetNodeAt(i), path.GetNodeAt(i + 1)] - distances[path.GetNodeAt(j), path.GetNodeAt(j + 1)]; } if (distDif < 0) { path.Distance += distDif; for (int k = 0; k < j - i; k++) { path.Nodes[i + k + 1] = tmpNodes[j - k]; } PathGenerator.Verify(path, distances, weights); return(true); } } } return(false); }