/// <summary> /// 将给定swap和邻域最优解队列比对,队列不满优选解总数TabuSearch.numCandidate则插入,否则比较得分;该队列为从小到大排列 /// </summary> /// <param name="bestSwaps"></param> /// <param name="swap"></param> /// <returns></returns> public static void compareBestSwaps(LinkedList <NeighborSwap> bestSwaps, NeighborSwap swap) { bool hasInsert = false; LinkedListNode <NeighborSwap> recursiveNode = bestSwaps.First; while (recursiveNode != null) { //if (swap.score < recursiveNode.Value.score) { // //已经存在该值,不存储 // return; //} //else if (swap.score < recursiveNode.Value.score) { bestSwaps.AddBefore(recursiveNode, swap); hasInsert = true; break; } recursiveNode = recursiveNode.Next; } if (hasInsert) { //该节点已经插入,需要判断是否总长度超了 if (bestSwaps.Count > TabuSearch.numCandidate) { bestSwaps.RemoveLast(); } } else if (bestSwaps.Count < TabuSearch.numCandidate)//比较完了,自己还没有插进去,但是总长度小,直接插在结尾 { bestSwaps.AddLast(swap); } }
public static LinkedList <NeighborSwap> getBestNeighborBySwapNoAfter(LinkedList <RcpspJob> projJobs, bool withLambda) { LinkedList <NeighborSwap> bestSwaps = new LinkedList <NeighborSwap>(); LinkedListNode <RcpspJob> recursiveNode = projJobs.First; while (recursiveNode != null) { if (recursiveNode.Next == null) { break; } LinkedListNode <RcpspJob> recursiveAfterNode = projJobs.First; //int i = 1;//2为89,3为88 bool afterCur = false; while (recursiveAfterNode != null) { if (recursiveAfterNode == recursiveNode) { afterCur = true; } else if (recursiveAfterNode.Value != recursiveNode.Value) { if (afterCur) { swapNeighbor(projJobs, recursiveNode, recursiveAfterNode); } else { swapNeighbor(projJobs, recursiveAfterNode, recursiveNode); } //检测满足紧后关系 if (checkFeasiable(projJobs)) { double score = calScheduleCore(projJobs, withLambda); NeighborSwap swap = new NeighborSwap(recursiveNode.Value, recursiveAfterNode.Value, score); compareBestSwaps(bestSwaps, swap); } if (afterCur) { swapNeighbor(projJobs, recursiveAfterNode, recursiveNode); } else { swapNeighbor(projJobs, recursiveNode, recursiveAfterNode); } //if (i-- < 0) break; } recursiveAfterNode = recursiveAfterNode.Next; } recursiveNode = recursiveNode.Next; } return(bestSwaps); }
public static LinkedList <NeighborSwap> getBestNeighborByRandomSwap(LinkedList <RcpspJob> projJobs, bool withLambda) { //随机位置 Random rand = new Random(Convert.ToInt32((new DateTime()).ToBinary())); int size = TabuSearch.numCandidate; while (size > 0) { rand.Next(1, projJobs.Count); } LinkedList <NeighborSwap> bestSwaps = new LinkedList <NeighborSwap>(); LinkedListNode <RcpspJob> recursiveNode = projJobs.First; while (recursiveNode != null) { if (recursiveNode.Next == null) { break; } LinkedListNode <RcpspJob> recursiveAfterNode = recursiveNode.Next; int i = 3; while (recursiveAfterNode != null) { swapNeighbor(projJobs, recursiveNode, recursiveAfterNode); //检测满足紧后关系 if (checkFeasiable(projJobs)) { double score = calScheduleCore(projJobs, withLambda); NeighborSwap swap = new NeighborSwap(recursiveNode.Value, recursiveAfterNode.Value, score); compareBestSwaps(bestSwaps, swap); } swapNeighbor(projJobs, recursiveAfterNode, recursiveNode); recursiveAfterNode = recursiveAfterNode.Next; if (i-- < 0) { break; } } recursiveNode = recursiveNode.Next; } #if DEBUG foreach (NeighborSwap swp in bestSwaps) { System.Diagnostics.Debug.WriteLine("index :" + TabuSearch.numberIter + "swap (" + swp.from.id + "," + swp.to.id + ") and core is " + swp.score); } #endif return(bestSwaps); }
/// <summary> /// 通过置换得到numCadidate个最优领域解 /// </summary> /// <param name="projJobs"></param> /// <returns></returns> public static LinkedList <NeighborSwap> getBestNeighborBySwap(LinkedList <RcpspJob> projJobs, bool withLambda) { LinkedList <NeighborSwap> bestSwaps = new LinkedList <NeighborSwap>(); LinkedListNode <RcpspJob> recursiveNode = projJobs.First; while (recursiveNode != null) { if (recursiveNode.Next == null) { break; } LinkedListNode <RcpspJob> recursiveAfterNode = recursiveNode.Next; int i = 3;//2为89,3为88 while (recursiveAfterNode != null) { swapNeighbor(projJobs, recursiveNode, recursiveAfterNode); //检测满足紧后关系 if (checkFeasiable(projJobs)) { double score = calScheduleCore(projJobs, withLambda); NeighborSwap swap = new NeighborSwap(recursiveNode.Value, recursiveAfterNode.Value, score); compareBestSwaps(bestSwaps, swap); } swapNeighbor(projJobs, recursiveAfterNode, recursiveNode); recursiveAfterNode = recursiveAfterNode.Next; if (i-- < 0) { break; } } recursiveNode = recursiveNode.Next; } #if DEBUG foreach (NeighborSwap swp in bestSwaps) { System.Diagnostics.Debug.WriteLine("index :" + TabuSearch.numberIter + "swap (" + swp.from.id + "," + swp.to.id + ") and core is " + swp.score); } #endif return(bestSwaps); }
//比较两个置换是否相等,部分先后,其中(a,b)==(a,b);(a,b)==(b,a) public override bool Equals(object obj) { //判断与之比较的类型是否为null。这样不会造成递归的情况 if (obj == null) { return(false); } if (GetType() != obj.GetType()) { return(false); } NeighborSwap equObj = (NeighborSwap)obj; if ((this.from == equObj.from && this.to == equObj.to) || (this.from == equObj.to && this.to == equObj.from)) { return(true); } else { return(false); } }