public Dictionary <string, object> searchNeighborhood(Operation[] operations, Job[] jobs, Machine[] machines, Operation[] criticalPath) { this.possibleOperations.Clear(); Dictionary <string, object> retDict = new Dictionary <string, object> (); List <Machine[]> retMachines = new List <Machine[]> (); List <int> retEvaluations = new List <int> (); if (tabuListOperations.ToArray().Length == MAX_LENGTH) { tabuListOperations.RemoveAt(0); } int retMachineIndex = 0; this.machines = machines; this.criticalPath = criticalPath; Schedule schedule_start = new Schedule(operations, jobs, machines, 0, problemset); Schedule schedule_end = new Schedule(operations, jobs, machines, 1, problemset); Dictionary <string, object> dict = schedule_start.computeStartTime(); int[] startTime = (int[])dict["startTime"]; int[] endTime = schedule_end.computeEndTime(); for (int i = 0; i < criticalPath.Length - 1; i++) { Machine[] tempMachine = new Machine[machines.Length]; //复制tempMachine for (int k = 0; k < machines.Length; k++) { Operation[] operationList = new Operation[machines[k].getOperations().Length]; for (int j = 0; j < machines[k].getOperations().Length; j++) { operationList[j] = machines[k].getOperations() [j]; } Machine machines1 = new Machine(k); machines1.setOperations(operationList); tempMachine[k] = machines1; } //找到邻居之前先判断是否在关键路径和同一机器上;是否在禁忌表里 if (criticalPath[i].getMachineId() == criticalPath[i + 1].getMachineId() && !isInTabuList(criticalPath[i + 1].getOperationNumber(), criticalPath[i].getOperationNumber(), this.tabuListOperations.ToArray())) { TabuListOperation tabuListOperation = new TabuListOperation(); tabuListOperation.startOperationNumber = criticalPath[i + 1].getOperationNumber(); tabuListOperation.endOperationNumber = criticalPath[i].getOperationNumber(); //找到了邻居之后立即入队 possibleOperations.Add(tabuListOperation); //备选的tabuListOperation对 int indexInMachine_1 = searchOperation(machines[criticalPath[i].getMachineId()].getOperations(), criticalPath[i]); int indexInMachine_2 = searchOperation(machines[criticalPath[i + 1].getMachineId()].getOperations(), criticalPath[i + 1]); Operation[] a = machines[criticalPath[i].getMachineId()].getOperations(); Operation[] tempOperations = new Operation[a.Length]; for (int j = 0; j < a.Length; j++) { tempOperations[j] = a[j]; } Operation o1 = tempOperations[indexInMachine_1]; Operation o2 = tempOperations[indexInMachine_2]; tempOperations[indexInMachine_1] = o2; tempOperations[indexInMachine_2] = o1; tempMachine[criticalPath[i].getMachineId()].setOperations(tempOperations); //设置operations retMachines.Add(tempMachine); //返回的邻居+1 //创建一个返回数组,记录所有邻居的evaluation int r_b_new_1 = machines[criticalPath[i].getMachineId()].findMachinePredecessor(criticalPath[i]) [0].getProcessTime(); if (machines[criticalPath[i].getMachineId()].findMachinePredecessor(criticalPath[i]) [0].getOperationNumber() != -1) { r_b_new_1 = startTime[machines[criticalPath[i].getMachineId()].findMachinePredecessor(criticalPath[i]) [0].getOperationNumber()] + machines[criticalPath[i].getMachineId()].findMachinePredecessor(criticalPath[i]) [0].getProcessTime(); } int r_b_new_2 = jobs[criticalPath[i + 1].getJobId()].findJobPredecessor(criticalPath[i + 1]) [0].getProcessTime(); if (jobs[criticalPath[i + 1].getJobId()].findJobPredecessor(criticalPath[i + 1]) [0].getOperationNumber() != -1) { r_b_new_2 = startTime[jobs[criticalPath[i + 1].getJobId()].findJobPredecessor(criticalPath[i + 1]) [0].getOperationNumber()] + jobs[criticalPath[i + 1].getJobId()].findJobPredecessor(criticalPath[i + 1]) [0].getProcessTime(); } int r_b_new = Math.Max(r_b_new_1, r_b_new_2); //r_b_new int r_a_new_2 = jobs[criticalPath[i].getJobId()].findJobPredecessor(criticalPath[i]) [0].getProcessTime(); if (jobs[criticalPath[i].getJobId()].findJobPredecessor(criticalPath[i]) [0].getOperationNumber() != -1) { r_a_new_2 = startTime[jobs[criticalPath[i].getJobId()].findJobPredecessor(criticalPath[i]) [0].getOperationNumber()] + jobs[criticalPath[i].getJobId()].findJobPredecessor(criticalPath[i]) [0].getProcessTime(); } int r_a_new = Math.Max(r_b_new + criticalPath[i + 1].getProcessTime(), r_a_new_2); int t_a_new_1 = 0; if (machines[criticalPath[i + 1].getMachineId()].findMachineSuccessor(criticalPath[i + 1]) [0].getOperationNumber() != -2) { t_a_new_1 = endTime[machines[criticalPath[i + 1].getMachineId()].findMachineSuccessor(criticalPath[i + 1]) [0].getOperationNumber()]; } int t_a_new_2 = 0; Console.WriteLine(criticalPath[i]); if (jobs[criticalPath[i].getJobId()].findJobSuccessor(criticalPath[i]) [0].getOperationNumber() != -2) { t_a_new_2 = endTime[jobs[criticalPath[i].getJobId()].findJobSuccessor(criticalPath[i]) [0].getOperationNumber()]; } int t_a_new = Math.Max(t_a_new_1, t_a_new_2) + criticalPath[i].getProcessTime(); int t_b_new_2 = 0; if (jobs[criticalPath[i + 1].getJobId()].findJobSuccessor(criticalPath[i + 1]) [0].getOperationNumber() != -2) { t_b_new_2 = endTime[jobs[criticalPath[i + 1].getJobId()].findJobSuccessor(criticalPath[i + 1]) [0].getOperationNumber()]; } int t_b_new = Math.Max(t_a_new, t_b_new_2) + criticalPath[i + 1].getProcessTime(); retEvaluations.Add(Math.Max(r_a_new + t_a_new, r_b_new + t_b_new)); retMachineIndex++; } // if(criticalPath[i].getMachineId () == criticalPath[i + 1].getMachineId () && retMachines.ToArray().Length == 0) { // TabuListOperation tabuListOperation = new TabuListOperation (); // tabuListOperation.startOperationNumber = criticalPath[i + 1].getOperationNumber (); // tabuListOperation.endOperationNumber = criticalPath[i].getOperationNumber (); //找到了邻居之后立即入队 // possibleOperations.Add (tabuListOperation); //备选的tabuListOperation对 // int indexInMachine_1 = searchOperation (machines[criticalPath[i].getMachineId ()].getOperations (), criticalPath[i]); // int indexInMachine_2 = searchOperation (machines[criticalPath[i + 1].getMachineId ()].getOperations (), criticalPath[i + 1]); // Operation[] a = machines[criticalPath[i].getMachineId ()].getOperations (); // Operation[] tempOperations = new Operation[a.Length]; // for (int j = 0; j < a.Length; j++) { // tempOperations[j] = a[j]; // } // Operation o1 = tempOperations[indexInMachine_1]; // Operation o2 = tempOperations[indexInMachine_2]; // tempOperations[indexInMachine_1] = o2; // tempOperations[indexInMachine_2] = o1; // tempMachine[criticalPath[i].getMachineId ()].setOperations (tempOperations); //设置operations // retMachines.Add (tempMachine); // retMachineIndex++; // } } retDict.Add("candidateMachines", retMachines.ToArray()); retDict.Add("candidateEvaluations", retEvaluations.ToArray()); return(retDict); }
public override Dictionary <string, object> searchNeighborhood(Machine[] machines, Operation[] criticalPath) { this.possibleOperations.Clear(); Dictionary <string, object> retDict = new Dictionary <string, object> (); List <Machine[]> retMachines = new List <Machine[]> (); if (tabuListOperations.ToArray().Length == MAX_LENGTH) { tabuListOperations.RemoveAt(0); } int retMachineIndex = 0; this.machines = machines; // this.criticalPath = criticalPath; for (int i = 0; i < criticalPath.Length - 1; i++) { Machine[] tempMachine = new Machine[machines.Length]; //复制tempMachine for (int k = 0; k < machines.Length; k++) { Operation[] operations = new Operation[machines[k].getOperations().Length]; for (int j = 0; j < machines[k].getOperations().Length; j++) { operations[j] = machines[k].getOperations() [j]; } Machine machines1 = new Machine(k); machines1.setOperations(operations); tempMachine[k] = machines1; } //找到邻居之前先判断是否在关键路径和同一机器上;是否在禁忌表里 if (criticalPath[i].getMachineId() == criticalPath[i + 1].getMachineId() && !isInTabuList(criticalPath[i + 1].getOperationNumber(), criticalPath[i].getOperationNumber(), this.tabuListOperations.ToArray())) { // Console.WriteLine(criticalPath[i] + "和" + criticalPath[i + 1]); TabuListOperation tabuListOperation = new TabuListOperation(); tabuListOperation.startOperationNumber = criticalPath[i + 1].getOperationNumber(); tabuListOperation.endOperationNumber = criticalPath[i].getOperationNumber(); //找到了邻居之后立即入队 possibleOperations.Add(tabuListOperation); //备选的tabuListOperation对 int indexInMachine_1 = searchOperation(machines[criticalPath[i].getMachineId()].getOperations(), criticalPath[i]); int indexInMachine_2 = searchOperation(machines[criticalPath[i + 1].getMachineId()].getOperations(), criticalPath[i + 1]); Operation[] a = machines[criticalPath[i].getMachineId()].getOperations(); Operation[] tempOperations = new Operation[a.Length]; for (int j = 0; j < a.Length; j++) { tempOperations[j] = a[j]; } Operation o1 = tempOperations[indexInMachine_1]; Operation o2 = tempOperations[indexInMachine_2]; tempOperations[indexInMachine_1] = o2; tempOperations[indexInMachine_2] = o1; tempMachine[criticalPath[i].getMachineId()].setOperations(tempOperations); //设置operations retMachines.Add(tempMachine); retMachineIndex++; } // if(criticalPath[i].getMachineId () == criticalPath[i + 1].getMachineId () && retMachines.ToArray().Length == 0) { // TabuListOperation tabuListOperation = new TabuListOperation (); // tabuListOperation.startOperationNumber = criticalPath[i + 1].getOperationNumber (); // tabuListOperation.endOperationNumber = criticalPath[i].getOperationNumber (); //找到了邻居之后立即入队 // possibleOperations.Add (tabuListOperation); //备选的tabuListOperation对 // int indexInMachine_1 = searchOperation (machines[criticalPath[i].getMachineId ()].getOperations (), criticalPath[i]); // int indexInMachine_2 = searchOperation (machines[criticalPath[i + 1].getMachineId ()].getOperations (), criticalPath[i + 1]); // Operation[] a = machines[criticalPath[i].getMachineId ()].getOperations (); // Operation[] tempOperations = new Operation[a.Length]; // for (int j = 0; j < a.Length; j++) { // tempOperations[j] = a[j]; // } // Operation o1 = tempOperations[indexInMachine_1]; // Operation o2 = tempOperations[indexInMachine_2]; // tempOperations[indexInMachine_1] = o2; // tempOperations[indexInMachine_2] = o1; // tempMachine[criticalPath[i].getMachineId ()].setOperations (tempOperations); //设置operations // retMachines.Add (tempMachine); // retMachineIndex++; // } } retDict.Add("candidateMachines", retMachines.ToArray()); return(retDict); }
public Dictionary <string, object> searchNeighborhood(Machine[] machines, Operation[] criticalPath) { this.possibleOperations.Clear(); Dictionary <string, object> retDict = new Dictionary <string, object> (); List <Machine[]> retMachines = new List <Machine[]> (); Dictionary <int, List <Operation> > blockList = new Dictionary <int, List <Operation> > (); if (tabuListOperations.ToArray().Length == MAX_LENGTH) { tabuListOperations.RemoveAt(0); } this.machines = machines; this.criticalPath = criticalPath; for (int i = 0; i < criticalPath.Length - 1; i++) { //复制tempMachine //找到邻居之前先判断是否在关键路径和同一机器上;是否在禁忌表里 if (criticalPath[i].getMachineId() == criticalPath[i + 1].getMachineId() && !isInTabuList(criticalPath[i + 1].getOperationNumber(), criticalPath[i].getOperationNumber(), this.tabuListOperations.ToArray())) { List <int> arrayList = new List <int>(blockList.Keys); if (!isContain(arrayList.ToArray(), criticalPath[i].getMachineId())) { List <Operation> oper = new List <Operation> (); oper.Add(criticalPath[i]); oper.Add(criticalPath[i + 1]); blockList[criticalPath[i].getMachineId()] = oper; } else { blockList[criticalPath[i].getMachineId()].Add(criticalPath[i]); blockList[criticalPath[i].getMachineId()].Add(criticalPath[i + 1]); } } } List <int> test = new List <int> (blockList.Keys); for (int i = 0; i < blockList.Count; i++) { // 每次循环都创建一个tempMachine Machine[] tempMachine = new Machine[machines.Length]; for (int k = 0; k < machines.Length; k++) { Operation[] operations = new Operation[machines[k].getOperations().Length]; for (int j = 0; j < machines[k].getOperations().Length; j++) { operations[j] = machines[k].getOperations() [j]; } Machine machines1 = new Machine(k); machines1.setOperations(operations); tempMachine[k] = machines1; } Machine[] tempMachine_2 = new Machine[machines.Length]; for (int k = 0; k < machines.Length; k++) { Operation[] operations = new Operation[machines[k].getOperations().Length]; for (int j = 0; j < machines[k].getOperations().Length; j++) { operations[j] = machines[k].getOperations() [j]; } Machine machines1 = new Machine(k); machines1.setOperations(operations); tempMachine_2[k] = machines1; } if (i == 0) { if (blockList[test[i]].ToArray().Length >= 2) { //add to tabuListOperation TabuListOperation tabuListOperation = new TabuListOperation(); tabuListOperation.startOperationNumber = blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 1].getOperationNumber(); tabuListOperation.endOperationNumber = blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getOperationNumber(); possibleOperations.Add(tabuListOperation); //备选的tabuListOperation对 int indexInMachine_1 = searchOperation(machines[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getMachineId()].getOperations(), blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2]); int indexInMachine_2 = searchOperation(machines[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 1].getMachineId()].getOperations(), blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 1]); Operation[] a = machines[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getMachineId()].getOperations(); Operation[] tempOperations = new Operation[a.Length]; for (int j = 0; j < a.Length; j++) { tempOperations[j] = a[j]; } Operation o1 = tempOperations[indexInMachine_1]; Operation o2 = tempOperations[indexInMachine_2]; tempOperations[indexInMachine_1] = o2; tempOperations[indexInMachine_2] = o1; tempMachine[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getMachineId()].setOperations(tempOperations); //设置operations retMachines.Add(tempMachine); } } else if (i < blockList.Count - 1) { if (blockList[test[i]].ToArray().Length >= 2) { //add to temp TabuListOperation tabuListOperation = new TabuListOperation(); tabuListOperation.startOperationNumber = blockList[test[i]].ToArray() [0].getOperationNumber(); tabuListOperation.endOperationNumber = blockList[test[i]].ToArray() [1].getOperationNumber(); possibleOperations.Add(tabuListOperation); //备选的tabuListOperation对 int indexInMachine_1 = searchOperation(machines[blockList[test[i]].ToArray() [0].getMachineId()].getOperations(), blockList[test[i]].ToArray() [0]); int indexInMachine_2 = searchOperation(machines[blockList[test[i]].ToArray() [1].getMachineId()].getOperations(), blockList[test[i]].ToArray() [1]); Operation[] a = machines[blockList[test[i]].ToArray() [0].getMachineId()].getOperations(); Operation[] tempOperations = new Operation[a.Length]; for (int j = 0; j < a.Length; j++) { tempOperations[j] = a[j]; } Operation o1 = tempOperations[indexInMachine_1]; Operation o2 = tempOperations[indexInMachine_2]; tempOperations[indexInMachine_1] = o2; tempOperations[indexInMachine_2] = o1; tempMachine[blockList[test[i]].ToArray() [0].getMachineId()].setOperations(tempOperations); //设置operations retMachines.Add(tempMachine); //后两个 TabuListOperation tabuListOperation_2 = new TabuListOperation(); tabuListOperation_2.startOperationNumber = blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 1].getOperationNumber(); tabuListOperation_2.endOperationNumber = blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getOperationNumber(); possibleOperations.Add(tabuListOperation_2); //备选的tabuListOperation对 int indexInMachine_1_2 = searchOperation(machines[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getMachineId()].getOperations(), blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2]); int indexInMachine_2_2 = searchOperation(machines[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 1].getMachineId()].getOperations(), blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 1]); Operation[] b = machines[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getMachineId()].getOperations(); Operation[] tempOperations_2 = new Operation[a.Length]; for (int j = 0; j < a.Length; j++) { tempOperations_2[j] = a[j]; } Operation o1_2 = tempOperations_2[indexInMachine_1_2]; Operation o2_2 = tempOperations_2[indexInMachine_2_2]; tempOperations_2[indexInMachine_1_2] = o2_2; tempOperations_2[indexInMachine_2_2] = o1_2; tempMachine_2[blockList[test[i]].ToArray() [blockList[test[i]].ToArray().Length - 2].getMachineId()].setOperations(tempOperations_2); //设置operations retMachines.Add(tempMachine_2); } } else { if (blockList[test[i]].ToArray().Length >= 2) { //add to temp TabuListOperation tabuListOperation = new TabuListOperation(); tabuListOperation.startOperationNumber = blockList[test[i]].ToArray() [0].getOperationNumber(); tabuListOperation.endOperationNumber = blockList[test[i]].ToArray() [1].getOperationNumber(); possibleOperations.Add(tabuListOperation); //备选的tabuListOperation对 int indexInMachine_1 = searchOperation(machines[blockList[test[i]].ToArray() [0].getMachineId()].getOperations(), blockList[test[i]].ToArray() [0]); int indexInMachine_2 = searchOperation(machines[blockList[test[i]].ToArray() [1].getMachineId()].getOperations(), blockList[test[i]].ToArray() [1]); Operation[] a = machines[blockList[test[i]].ToArray() [0].getMachineId()].getOperations(); Operation[] tempOperations = new Operation[a.Length]; for (int j = 0; j < a.Length; j++) { tempOperations[j] = a[j]; } Operation o1 = tempOperations[indexInMachine_1]; Operation o2 = tempOperations[indexInMachine_2]; tempOperations[indexInMachine_1] = o2; tempOperations[indexInMachine_2] = o1; tempMachine[blockList[test[i]].ToArray() [0].getMachineId()].setOperations(tempOperations); //设置operations retMachines.Add(tempMachine); } } } retDict.Add("candidateMachines", retMachines.ToArray()); return(retDict); }