我需要用分支定界算法解决TSP问题。这是代码。问题是,代码仅在小型实例(即三个城市)中起作用,但每次都不起作用。
我一直在寻找一些错误,但我找不到任何错误。 成本矩阵是我的代码中的macierz,城市数量存储在变量ileMiast中。 我认为其余的很清楚。 不要打扰sort函数或FinalMatrix数组。它只是呈现最终订单。
public double branchibound(int cities)
{
double totalCost = 0;
FinalMatrix = new ArrayList(ileMiast);
/*
* The ReducedCostMatrix calculates the reduced
* cost matrix
* */
totalCost = this.ReducedCostMatrix(cities);
/*
* Now we would work on the reduced cost
* Matrix to check which nodes to be inluded
* yet or not.
* */
int count = 0;
for (int i = 0; i < cities; i++)
for (int j = 0; j < cities; j++)
{
double[][] tempReduceRow = new double[cities][];
for (int l = 0; l < cities; l++)
tempReduceRow[l] = new double[cities];
for (int l = 0; l < cities; l++)
for (int m = 0; m < cities; m++)
tempReduceRow[l][m] = ReducedMatrix[l][m];
double[][] tempReduceCol = new double[cities][];
for (int l = 0; l < cities; l++)
tempReduceCol[l] = new double[cities];
for (int l = 0; l < cities; l++)
for (int m = 0; m < cities; m++)
tempReduceCol[l][m] = ReducedMatrix[l][m];
/*
* We only include those edges with value 0.
* Now, we will have only those with minimum left
* child and maximum right child.
* */
if (ReducedMatrix[i][j] == 0)
{
OmitLeft(i, j, ref tempReduceRow);
double LBound = totalCost + ReducedCostMatrix(cities, ref tempReduceRow);
OmitRight(i, j, ref tempReduceCol);
double RBound = totalCost + ReducedCostMatrix(cities, ref tempReduceCol);
FinalMatrix.Add(new double[] { LBound, RBound, count });
}
}
//sort();
return totalCost;
}
private void sort()
{
double[] values = new double[3];
double[] a = new double[3];
values = (double[])this.FinalMatrix[0];
int i = 0;
int prevIndex = 0;
for (i = 0; i < this.FinalMatrix.Count; i++)
for (int j = 0; j < this.FinalMatrix.Count; j++)
{
a = (double[])this.FinalMatrix[j];
if (a[1] > values[1] || a[0] < values[0])
{
FinalMatrix[prevIndex] = FinalMatrix[j];
FinalMatrix[j] = values;
values = a;
prevIndex = j;
}
}
}
public String getString()
{
String str = new String('A', 20);
str = "";
foreach (double[] a in this.FinalMatrix)
str += (char)(a[2]);
return str;
}
private void OmitLeft(int row, int col, ref double[][] temp)
{
for (int j = 0; j < temp.Length; j++)
temp[row][j] = -1;
for (int j = 0; j < temp.Length; j++)
temp[j][col] = -1;
}
private void OmitRight(int row, int col, ref double[][] temp)
{
temp[row][col] = -1;
}
private double ReducedCostMatrix(int cities)
{
double minBound = 0;
ReducedMatrix = new double[cities][];
for (int i = 0; i < cities; i++)
ReducedMatrix[i] = new double[cities];
/*
* Here we make a new Reduced Matrix
* by copying the original matrix
* into the new one.
* */
for (int i = 0; i < cities; i++)
for (int j = 0; j < cities; j++)
ReducedMatrix[i][j] = macierz[i][j];
minBound = ReducedRows(cities);
minBound += ReducedCols(cities);
return minBound;
}
private double ReducedCostMatrix(int cities, ref double[][] temp)
{
double minBound = 0;
minBound = ReducedRows(cities, ref temp);
minBound += ReducedCols(cities, ref temp);
return minBound;
}
private double ReducedRows(int cities)
{
double min = 65535;
bool flag = false;
double minBound = 0;
for (int i = 0; i < cities; i++)
{
min = 65535;
for (int j = 0; j < cities; j++)
if (this.ReducedMatrix[i][j] < min && this.ReducedMatrix[i][j] != -1)
{
min = this.ReducedMatrix[i][j];
flag = true;
}
if (flag)
{
this.SubtractRow(i, min);
minBound += min;
}
}
return minBound;
}
private double ReducedRows(int cities, ref double[][] temp)
{
double min = 65535;
double minBound = 0;
for (int i = 0; i < cities; i++)
{
min = 65535;
bool flag = true;
for (int j = 0; j < cities; j++)
if (temp[i][j] < min && temp[i][j] != -1)
{
min = temp[i][j];
flag = false;
}
if (!flag)
{
this.SubtractRow(i, min, ref temp);
minBound += min;
}
}
return minBound;
}
private double ReducedCols(int cities)
{
double min = 65535;
double minBound = 0;
for (int j = 0; j < cities; j++)
{
for (int i = 0; i < cities; i++)
if (this.ReducedMatrix[i][j] < min && this.ReducedMatrix[i][j] != -1)
min = ReducedMatrix[i][j];
this.SubtractCol(j, min);
minBound += min;
}
return minBound;
}
private double ReducedCols(int cities, ref double[][] temp)
{
double min = 65535;
double minBound = 0;
for (int j = 0; j < cities; j++)
{
for (int i = 0; i < cities; i++)
if (temp[i][j] < min && temp[i][j] != -1)
min = temp[i][j];
SubtractCol(j, min, ref temp);
minBound += min;
}
return minBound;
}
private void SubtractRow(int row, double sub)
{
for (int j = 0; j < this.ReducedMatrix[0].Length; j++)
if (this.ReducedMatrix[row][j] == -1)
continue;
else
this.ReducedMatrix[row][j] -= sub;
}
private void SubtractRow(int row, double sub, ref double[][] temp)
{
for (int j = 0; j < temp[0].Length; j++)
if (temp[row][j] == -1)
continue;
else
temp[row][j] -= sub;
}
private void SubtractCol(int col, double sub)
{
for (int j = 0; j < this.ReducedMatrix[0].Length; j++)
if (this.ReducedMatrix[j][col] == -1)
continue;
else
this.ReducedMatrix[j][col] -= sub;
}
private void SubtractCol(int col, double sub, ref double[][] temp)
{
for (int j = 0; j < temp.Length; j++)
if (temp[j][col] == -1)
continue;
else
temp[j][col] -= sub;
}
提前致谢