public CTSP_BranchAndBound(ref CTSP_Distances distances, int upperBound, int numVertex) { _distances = distances; _upperBound = upperBound + ((upperBound * _PERCUP) / 100); _numVertex = numVertex; _bestCost = int.MaxValue; _optimalRoute = new List<int>(); _tree = new CBranch(-1,-1, 0, _upperBound, new List<int>()); _TSPupperBound = new CTSP_UpperBound(ref _distances); }
public string makeFromFile(ref Stream file) { try { _distances = new CTSP_Distances(); XmlDocument Reader = new XmlDocument(); Reader.Load(file); XmlNode root = Reader.DocumentElement; _numVertex = root.SelectNodes("//graph//vertex").Count; _distances.newMatrix(_numVertex); int numRow = 0; int numEdge = 0; XmlNodeList Vertexlist1 = root.SelectNodes("//graph//vertex//edge"); foreach (XmlNode Vertex in Vertexlist1) { if (isChangeRow(numEdge, _numVertex)) numRow++; if (isVertexItself(numEdge, _numVertex, numRow)) { _distances.Matrix[numRow, Convert.ToInt32(VertexPositionRelativeRow(numEdge, _numVertex))] = 0; numEdge++; } int cost = getCost(Vertex.Attributes[0].Value); _distances.Matrix[numRow, Convert.ToInt32(Vertex.FirstChild.Value)] = cost; numEdge++; } return string.Empty; } catch (Exception e) { return e.Message; } }
private List<List<int>> Kruskal(CTSP_Distances distances, ref List<int> partialroute) { CTSP_Distances adyacencia = distances; List<List<int>> arbol = new List<List<int>>(); List<int> pertenece = new List<int>(); // indica a que árbol pertenece el nodo for (int i = 0; i < _numVertex; i++) { pertenece.Add(i); arbol.Add(new List<int>()); for (int j = 0; j < _numVertex; j++) { if (partialroute.Count > i && partialroute[i] == j) adyacencia.Matrix[i, j] = 0; arbol[i].Add(0); } } int nodoA = 0; int nodoB = 0; int arcos = 1; while (arcos < _numVertex) { // Encontrar el arco mínimo que no forma ciclo y guardar los nodos y la distancia. int min = int.MaxValue; for (int i = 0; i < _numVertex; i++) for (int j = 0; j < _numVertex; j++) if (min > adyacencia.Matrix[i,j] && adyacencia.Matrix[i, j] != 0 && pertenece[i] != pertenece[j]) { min = adyacencia.Matrix[i, j]; nodoA = i; nodoB = j; } // Si los nodos no pertenecen al mismo árbol agrego el arco al árbol mínimo. if (pertenece[nodoA] != pertenece[nodoB]) { arbol[nodoA][nodoB] = min; arbol[nodoB][nodoA] = min; // Todos los nodos del árbol del nodoB ahora pertenecen al árbol del nodoA. int temp = pertenece[nodoB]; pertenece[nodoB] = pertenece[nodoA]; for (int k = 0; k < _numVertex; k++) if (pertenece[k] == temp) pertenece[k] = pertenece[nodoA]; arcos++; } } return arbol; }
public CTSP_UpperBound(ref CTSP_Distances distances) { _distances = distances; _route = new List<int>(); _upperBound = int.MaxValue; }