TSP分支和边界-有时结果不正确C#
我需要用分枝定界算法来解决TSP问题。这是密码。问题是,该代码只在小实例(即三个城市)中有效,但并非每次都有效 我一直在寻找一些错误,但没有找到。 成本矩阵是我代码中的macierz,城市数存储在变量ileMiast中。 我想其余的都很清楚。 不要费心于排序函数或FinalMatrix数组。这只是最后的定单TSP分支和边界-有时结果不正确C#,c#,traveling-salesman,branch-and-bound,C#,Traveling Salesman,Branch And Bound,我需要用分枝定界算法来解决TSP问题。这是密码。问题是,该代码只在小实例(即三个城市)中有效,但并非每次都有效 我一直在寻找一些错误,但没有找到。 成本矩阵是我代码中的macierz,城市数存储在变量ileMiast中。 我想其余的都很清楚。 不要费心于排序函数或FinalMatrix数组。这只是最后的定单 public double branchibound(int cities) { double totalCost = 0; Fina
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;
}
公共双分支银行(国际城市)
{
双重总成本=0;
FinalMatrix=新阵列列表(ileMiast);
/*
*约化成本矩阵计算约化成本
*成本矩阵
* */
总成本=此。缩减成本矩阵(城市);
/*
*现在我们将致力于降低成本
*检查要包含哪些节点的矩阵
*但不管怎样。
* */
整数计数=0;
对于(int i=0;i值[1]| | a[0]<值[0])
{
最终矩阵[prevIndex]=最终矩阵[j];
最终矩阵[j]=值;
数值=a;
prevIndex=j;
}
}
}
公共字符串getString()
{
String str=新字符串('A',20);
str=“”;
foreach(此.FinalMatrix中的双[]a)
str+=(char)(a[2]);
返回str;
}
左(整数行、整数列、双参考[][]温度)
{
对于(int j=0;j