private IEnumerable <CRegion> AggregateAndUpdateQ_Common(CRegion crg, CRegion lscrg, CRegion sscrg, SortedSet <CRegion> Q, SortedDictionary <CCorrCphs, CCorrCphs> pAdjCorrCphsSD, CCorrCphs unitingCorrCphs, List <SortedDictionary <CRegion, CRegion> > ExistingCrgSDLt, List <SortedDictionary <CPatch, CPatch> > ExistingCphSDLt, SortedDictionary <CCorrCphs, CCorrCphs> ExistingCorrCphsSD, double[,] padblTD, int intEstSteps) { var newcph = crg.ComputeNewCph(unitingCorrCphs, ExistingCphSDLt); var newAdjCorrCphsSD = CRegion.ComputeNewAdjCorrCphsSDAndUpdateExistingCorrCphsSD(pAdjCorrCphsSD, unitingCorrCphs, newcph, ExistingCorrCphsSD); var frcph = unitingCorrCphs.FrCph; var tocph = unitingCorrCphs.ToCph; int intfrTypeIndex = crg.GetCphTypeIndex(frcph); int inttoTypeIndex = crg.GetCphTypeIndex(tocph); //if the two cphs have the same type, then we only need to aggregate the smaller one into the larger one //(this will certainly have smaller cost in terms of area) //otherwise, we need to aggregate from two directions if (intfrTypeIndex == inttoTypeIndex) { if (frcph.dblArea >= tocph.dblArea) { yield return(GenerateCrgAndUpdateQ(crg, lscrg, sscrg, Q, ExistingCrgSDLt, newAdjCorrCphsSD, frcph, tocph, newcph, unitingCorrCphs, padblTD, intEstSteps)); } else { yield return(GenerateCrgAndUpdateQ(crg, lscrg, sscrg, Q, ExistingCrgSDLt, newAdjCorrCphsSD, tocph, frcph, newcph, unitingCorrCphs, padblTD, intEstSteps)); } } else { //The aggregate can happen from two directions yield return(GenerateCrgAndUpdateQ(crg, lscrg, sscrg, Q, ExistingCrgSDLt, newAdjCorrCphsSD, frcph, tocph, newcph, unitingCorrCphs, padblTD, intEstSteps)); yield return(GenerateCrgAndUpdateQ(crg, lscrg, sscrg, Q, ExistingCrgSDLt, newAdjCorrCphsSD, tocph, frcph, newcph, unitingCorrCphs, padblTD, intEstSteps)); } }
/// <summary> /// compute cost during generating a new Crg /// </summary> /// <param name="newAdjCorrCphsSD"></param> /// <param name="activecph"></param> /// <param name="passivecph"></param> /// <param name="unitedcph"></param> /// <param name="intFinalTypeIndex"></param> /// <param name="padblTD"></param> /// <returns></returns> public CRegion GenerateCrgAndUpdateQ(CRegion crg, CRegion lscrg, CRegion sscrg, SortedSet <CRegion> Q, List <SortedDictionary <CRegion, CRegion> > ExistingCrgSDLt, SortedDictionary <CCorrCphs, CCorrCphs> newAdjCorrCphsSD, CPatch activecph, CPatch passivecph, CPatch unitedcph, CCorrCphs unitingCorrCphs, double[,] padblTD, int intEstSteps) { int intactiveTypeIndex = crg.GetCphTypeIndex(activecph); int intpassiveTypeIndex = crg.GetCphTypeIndex(passivecph); var newcrg = crg.GenerateCrgChildAndComputeCost(lscrg, newAdjCorrCphsSD, activecph, passivecph, unitedcph, unitingCorrCphs, padblTD); //var intGIDLt = new List<int>(); //var intTypeLt = new List<int>(); //if (newcrg.intSumCphGID == 32961 && newcrg.intSumTypeIndex == 91) //{ // foreach (var cph in newcrg.GetCphCol()) // { // intGIDLt.Add(cph.GID); // } // foreach (var inttype in newcrg.GetCphTypeIndexCol()) // { // intTypeLt.Add(inttype); // } // foreach (var item in Q) // { // if (item.intSumCphGID == 65794 && item.intSumTypeIndex == 172) // { // int ss = 5; // } // } //} //if (CRegion._intStaticGID == 2008) //{ // int kes = 8; //} CRegion outcrg; if (ExistingCrgSDLt[newcrg.GetCphCount()].TryGetValue(newcrg, out outcrg)) { int intResult = newcrg.dblCostExact.CompareTo(outcrg.dblCostExact); //int intResult = CCmpMethods.CmpDbl_CoordVerySmall(newcrg.dblCostExact, outcrg.dblCostExact); if (intResult == -1) { //from the idea of A* algorithm, we know that outcrg must be in Q //var Q = new SortedSet<CRegion>(CRegion.pCmpCrg_Cost_CphGIDTypeIndex); //there is no decrease key function for SortedSet, so we have to remove it and later add it again if (Q.Remove(outcrg) == true) { //we don't use newcrg dicrectly because some regions may use ourcrg as their child outcrg.cenumColor = newcrg.cenumColor; outcrg.dblCostExactType = newcrg.dblCostExactType; outcrg.dblCostExactComp = newcrg.dblCostExactComp; outcrg.dblCostExactArea = newcrg.dblCostExactArea; outcrg.dblCostExact = newcrg.dblCostExact; outcrg.d = newcrg.dblCostExact + outcrg.dblCostEst; outcrg.AggCphs = newcrg.AggCphs; outcrg.parent = newcrg.parent; newcrg = outcrg; //var cphcount = newcrg.GetCphCount(); //var dblCostExactType = newcrg.dblCostExactType; //var dblCostEstType = newcrg.dblCostEstType; //var dblCostExactComp = newcrg.dblCostExactComp * newcrg.dblArea; //var dblCostEstComp = newcrg.dblCostEstComp * newcrg.dblArea; //if ((newcrg.GetCphCount() == 3 || newcrg.GetCphCount() == 4) // && newcrg.dblCostEstType == 0 && newcrg.dblCostEstComp == 0) //{ // int sst = 9; //} //var dblCostExactArea = newcrg.dblCostExactArea; //var dblCostExact = newcrg.dblCostExact; //var d = newcrg.dblCostExact + newcrg.dblCostEst; //Console.WriteLine(" GID: " + newcrg.GID + " CphCount: " + cphcount + // " EstType: " + dblCostEstType + " EstComp: " + dblCostEstComp + // " EstSum: " + newcrg.dblCostEst); Q.Add(newcrg); } else { if (intEstSteps == 0) { throw new ArgumentException("outcrg should be removed! you might be overestimating!"); } else { // if intEstSteps != 0, we are overestimating, // outcrg may have been removed from queue Q as the node with least cost } } } else { //we don't need to do operation Q.Add(newcrg); } } else { ComputeEstCost(lscrg, sscrg, newcrg, padblTD, intEstSteps); //var cphcount = newcrg.GetCphCount(); //var dblCostExactType = newcrg.dblCostExactType; //var dblCostEstType = newcrg.dblCostEstType; //var dblCostExactComp = newcrg.dblCostExactComp * newcrg.dblArea; //var dblCostEstComp = newcrg.dblCostEstComp * newcrg.dblArea; //if ((newcrg.GetCphCount() == 3 || newcrg.GetCphCount() == 4) // && newcrg.dblCostEstType == 0 && newcrg.dblCostEstComp == 0) //{ // int sst = 9; //} //var dblCostExactArea = newcrg.dblCostExactArea; //var dblCostExact = newcrg.dblCostExact; //var d = newcrg.dblCostExact + newcrg.dblCostEst; //Console.WriteLine(" GID: " + newcrg.GID + " CphCount: " + cphcount + // " EstType: " + dblCostEstType + " EstComp: " + dblCostEstComp + // " EstSum: " + newcrg.dblCostEst); Q.Add(newcrg); ExistingCrgSDLt[newcrg.GetCphCount()].Add(newcrg, newcrg); CRegion._intNodeCount++; } CRegion._intEdgeCount++; //the returned newcrg is just for counting, and thus not important return(newcrg); }
/// <summary> /// compute cost during generating a new Crg /// </summary> /// <param name="newAdjCorrCphsSD"></param> /// <param name="activecph"></param> /// <param name="passivecph"></param> /// <param name="unitedcph"></param> /// <param name="intFinalTypeIndex"></param> /// <param name="padblTD"></param> /// <returns></returns> public CRegion GenerateCrgAndUpdateQ(CRegion crg, CRegion lscrg, CRegion sscrg, SortedSet <CRegion> Q, List <SortedDictionary <CRegion, CRegion> > ExistingCrgSDLt, SortedDictionary <CCorrCphs, CCorrCphs> newAdjCorrCphsSD, CPatch activecph, CPatch passivecph, CPatch unitedcph, CCorrCphs unitingCorrCphs, double[,] padblTD, int intEstSteps) { int intactiveTypeIndex = crg.GetCphTypeIndex(activecph); int intpassiveTypeIndex = crg.GetCphTypeIndex(passivecph); var newcrg = crg.GenerateCrgChildAndComputeExactCost(lscrg, newAdjCorrCphsSD, activecph, passivecph, unitedcph, unitingCorrCphs, padblTD); if (ExistingCrgSDLt[newcrg.GetCphCount()].TryGetValue(newcrg, out CRegion outcrg)) { int intResult = newcrg.dblCostExact.CompareTo(outcrg.dblCostExact); //int intResult = CCmpMethods.CmpDbl_CoordVerySmall(newcrg.dblCostExact, outcrg.dblCostExact); if (intResult == -1) { //from the idea of A* algorithm, we know that outcrg must be in Q //var Q = new SortedSet<CRegion>(CRegion.pCmpCrg_Cost_CphGIDTypeIndex); //there is no decrease key function for SortedSet, so we have to remove it and later add it again if (Q.Remove(outcrg) == true) { //if (outcrg.GID == 1698) //{ // Console.WriteLine("Before Replacing Region 1698"); // foreach (var cpg in outcrg.CphCpgSD_Area_CphGID.Values) // { // Console.WriteLine(cpg.ID); // } //} //we don't use newcrg dicrectly because some regions may use outcrg as their child //outcrg.cenumColor = newcrg.cenumColor; //outcrg.dblCostExactType = newcrg.dblCostExactType; //outcrg.dblCostExactComp = newcrg.dblCostExactComp; //outcrg.dblCostExactArea = newcrg.dblCostExactArea; //outcrg.dblCostExact = newcrg.dblCostExact; //outcrg.d = newcrg.dblCostExact + outcrg.dblCostEst; //outcrg.CphCpgSD_Area_CphGID = newcrg.CphCpgSD_Area_CphGID; //outcrg.AggCphs = newcrg.AggCphs; //outcrg.parent = newcrg.parent; //newcrg = outcrg; newcrg.dblCostEst = outcrg.dblCostEst; newcrg.dblCostEstType = outcrg.dblCostEstType; newcrg.dblCostEstComp = outcrg.dblCostEstComp; newcrg.dblCostEstArea = outcrg.dblCostEstArea; newcrg.d = newcrg.dblCostExact + outcrg.dblCostEst; //var cphcount = newcrg.GetCphCount(); //var dblCostExactType = newcrg.dblCostExactType; //var dblCostEstType = newcrg.dblCostEstType; //var dblCostExactComp = newcrg.dblCostExactComp * newcrg.dblArea; //var dblCostEstComp = newcrg.dblCostEstComp * newcrg.dblArea; //if ((newcrg.GetCphCount() == 3 || newcrg.GetCphCount() == 4) // && newcrg.dblCostEstType == 0 && newcrg.dblCostEstComp == 0) //{ // int sst = 9; //} //var dblCostExactArea = newcrg.dblCostExactArea; //var dblCostExact = newcrg.dblCostExact; //var d = newcrg.dblCostExact + newcrg.dblCostEst; //Console.WriteLine(" GID: " + newcrg.GID + " CphCount: " + cphcount + // " EstType: " + dblCostEstType + " EstComp: " + dblCostEstComp + // " EstSum: " + newcrg.dblCostEst); //if (outcrg.GID == 1682) //{ // Console.WriteLine("Replacing Region 1682"); // foreach (var cpg in outcrg.CphCpgSD_Area_CphGID.Values) // { // Console.WriteLine(cpg.ID); // } //} Q.Add(newcrg); //We also need to update ExistingCrgSDLt, otherwise we will have an error when Q.Remove(outcrg): //we will take outcrg from ExistingCrgSDLt and then remove outcrg from Q. //outcrg and newcrg are the same accoriding to the comparer of ExistingCrgSDLt, //but they are different accoriding to the comparer of Q. ExistingCrgSDLt[newcrg.GetCphCount()].Remove(outcrg); ExistingCrgSDLt[newcrg.GetCphCount()].Add(newcrg, newcrg); } else { if (intEstSteps == 0) { throw new ArgumentException("We should be able to remove outcrg! We might be overestimating!"); } else { // if intEstSteps != 0, we are overestimating, // outcrg may have been removed from queue Q as the node with least cost } } } else { //we don't need to do operation Q.Add(newcrg); } } else { ComputeEstCost(lscrg, sscrg, newcrg, padblTD, intEstSteps); Q.Add(newcrg); ExistingCrgSDLt[newcrg.GetCphCount()].Add(newcrg, newcrg); CRegion._intNodeCount++; } CRegion._intEdgeCount++; //the returned newcrg is just for counting, and thus not important return(newcrg); }