我正在尝试为调度问题创建禁忌搜索算法。变量“Initialsequence”(声明为static)在从其他方法调用时将其值重置为null。 “Initialsequence”中的值填充在方法“CreateInitialSolution()”中,当我尝试在另一个方法“Create Neighborhood Solution()”中使用它时,该值为null。你能告诉我为什么吗?
namespace TEST_CODE
{
class Program
{
#region Parameters
public static int No_mc;
public static int No_prod;
public static int Max_jobs;
public static int[] No_jobs;
public static int Tot_jobs;
public static double[,] Run_time;
public static double[, ,] Setup_time;
public static double[] Setupst_time;
public static double[,] Due_time;
public static double[] Mc_avail_time;
public static double[,] Job_release_time;
public static int[] No_storage;
public static double[] wt_product;
public static double wt_producer;
public static double wt_customer;
public static double[][] tempDue_time;
public static batch[,] Initialsequence;
public static int BigM = 10000;
#endregion Variables
public class batch
{
public int Family { get; set; }
public int Job { get; set; }
public batch(int family, int job)
{
Family = family;
Job = job;
}
}
public static void CreateInitialSolution()
{
// CreateInitialSolution
int[] sequence = new int[Tot_jobs + 1];
batch[,] Initialsequence = new batch[No_mc + 1, Tot_jobs + 1];
for (int i = 0; i < Tot_jobs; i++)
{
var min = tempDue_time.SelectMany((subArr, k) => subArr.Select((value, j) => new { k, j, value }))
.OrderBy(x => x.value)
.First();
int firstIndex = min.k;
int secondIndex = min.j;
Initialsequence[1, i + 1] = new batch(firstIndex + 1, secondIndex + 1);
tempDue_time[firstIndex][secondIndex] = BigM;
}
for (int i = 2; i <= No_mc; i++)
{
for (int j = 1; j <= Tot_jobs; j++)
{
Initialsequence[i, j] = Initialsequence[1, j];
}
}
}
public static void CreateNeighborhoodSolution()
{
// Create Neighborhood solution
List<batch[,]> NSJ = new List<batch[,]>();
for (int i = 1; i < Tot_jobs; i++)
{
for (int j = i + 1; j <= Tot_jobs; j++)
{
batch[,] Tempseq = new batch[No_mc + 1, Tot_jobs + 1];
Array.Copy(Initialsequence, Tempseq, Initialsequence.Length);
batch tempstr = new batch(Tempseq[1, i].Family, Tempseq[1, i].Job);
Tempseq[1, i] = Tempseq[1, j];
Tempseq[1, j] = tempstr;
for (int k = 2; k <= No_mc; k++)
{
for (int l = 1; l <= Tot_jobs; l++)
{
Tempseq[k, l] = Tempseq[1, l];
}
}
NSJ.Add(Tempseq);
}
}
}
答案 0 :(得分:0)
在CreateInitialSolution
内,您正在重新定义变量Initialsequence
。这隐藏了类级变量。所以改变这个:
batch[,] Initialsequence = new batch[No_mc + 1, Tot_jobs + 1];
对此:
Initialsequence = new batch[No_mc + 1, Tot_jobs + 1];
答案 1 :(得分:0)
这是因为你实际上没有初始化你的Program
类变量Initialsequence
,而是你正在创建一个同名的本地方法变量,一旦你的方法执行完成就会超出范围,因此实际变量是仍null
。如下面的代码块所示
public static void CreateInitialSolution()
{
// CreateInitialSolution
batch[,] Initialsequence = new batch[No_mc + 1, Tot_jobs + 1];
SideNote:停止定义具有相同名称的变量,当您寻求帮助时会导致混淆和其他变量