public CCorrCphs(CPatch pCph1, CPatch pCph2, List <CEdge> pSharedCEdgeLt = null, double fdblSharedSegLength = 0, int fintSharedCEdgeCount = 0) { this.GID = _intStaticGID++; if (pSharedCEdgeLt == null) { this.SharedCEdgeLt = new List <CEdge>(); } else { this.SharedCEdgeLt = new List <CEdge>(pSharedCEdgeLt); } //this.SharedCEdgeLt = new List<CEdge>(); //this.SharedCEdgeLt.AddRange(pSharedCEdgeLt); this.dblSharedSegLength = fdblSharedSegLength; this.intSharedCEdgeCount = fintSharedCEdgeCount; //if (intSharedCEdgeCount == 0 || dblSharedSegLength == 0) //{ // throw new ArgumentException("this should not happen!"); //} if (pCph1.GID <= pCph2.GID) { FrCph = pCph1; ToCph = pCph2; } else { FrCph = pCph2; ToCph = pCph1; } }
private CRegion Compute(CRegion lscrg, CRegion sscrg, int intFinalTypeIndex, string strAreaAggregation, SortedDictionary <CCorrCphs, CCorrCphs> ExistingCorrCphsSD, CStrObjLtDt StrObjLtDt, double[,] padblTD) { CRegion._intNodeCount = 1; CRegion._intEdgeCount = 0; var currentCrg = lscrg; //after an aggregation, we whould have the largest compactness //it's urgent to remove the smallest one while (currentCrg.GetCphCount() > 1) { var smallestcph = currentCrg.GetSmallestCph(); var CphRecordsEb = currentCrg.GetNeighborCphRecords(smallestcph); double dblMinCost = double.MaxValue; CCorrCphs minunitingCorrCphs = null; CPatch minunitedcph = null; CPatch minactivecph = null; CPatch minpassivecph = null; foreach (var cphrecord in CphRecordsEb) { var neighborcph = cphrecord.Cph; var unitingCorrCphs = cphrecord.CorrCphs; var unitedcph = smallestcph.Unite(neighborcph, unitingCorrCphs.dblSharedSegLength); CPatch activecph = neighborcph; CPatch passivecph = smallestcph; if (padblTD[currentCrg.GetCphTypeIndex(smallestcph), intFinalTypeIndex] < padblTD[currentCrg.GetCphTypeIndex(neighborcph), intFinalTypeIndex]) { activecph = smallestcph; passivecph = neighborcph; } double dblCostType = padblTD[currentCrg.GetCphTypeIndex(activecph), currentCrg.GetCphTypeIndex(passivecph)] * passivecph.dblArea / lscrg.dblArea; double dblCostShape = 0; if (CConstants.strShapeConstraint == "MaxAvgC_EdgeNo" || CConstants.strShapeConstraint == "MaxAvgC_Comb") { if (lscrg.GetCphCount() - 2 > 0) { double dblNewSumComp = currentCrg.dblSumComp - cphrecord.CorrCphs.FrCph.dblComp - cphrecord.CorrCphs.ToCph.dblComp + unitedcph.dblComp; double dblNewAvgComp = dblNewSumComp / (currentCrg.GetCphCount() - 1); dblCostShape = (1 - dblNewAvgComp) / (lscrg.GetCphCount() - 2); } } else if (CConstants.strShapeConstraint == "MinIntBound") { if (lscrg.GetCphCount() - 2 > 0) { double dblNewLength = currentCrg.dblInteriorSegLength - cphrecord.CorrCphs.dblSharedSegLength; dblCostShape = dblNewLength * (lscrg.GetCphCount() - 1) / (lscrg.GetCphCount() - 2) / (currentCrg.GetCphCount() - 1) / lscrg.dblInteriorSegLength; } } else { //CConstants.strShapeConstraint == "MaxMinC_EdgeNo" || //CConstants.strShapeConstraint == "MaxMinC_Comb" || throw new ArgumentException("We didn't consider the case!"); } double dblCost = (1 - CAreaAgg_Base.dblLamda) * dblCostType + CAreaAgg_Base.dblLamda * dblCostShape; if (dblCost < dblMinCost) { dblMinCost = dblCost; minunitingCorrCphs = unitingCorrCphs; minunitedcph = unitedcph; minactivecph = activecph; minpassivecph = passivecph; } } var newAdjCorrCphsSD = CRegion.ComputeNewAdjCorrCphsSDAndUpdateExistingCorrCphsSD (currentCrg.AdjCorrCphsSD, minunitingCorrCphs, minunitedcph, ExistingCorrCphsSD); var newcrg = currentCrg.GenerateCrgChildAndComputeExactCost(lscrg, newAdjCorrCphsSD, minactivecph, minpassivecph, minunitedcph, minunitingCorrCphs, padblTD); newcrg.d = newcrg.dblCostExact; CRegion._intNodeCount++; CRegion._intEdgeCount++; currentCrg = newcrg; } RecordResultForCrg(StrObjLtDt, lscrg, currentCrg, intFinalTypeIndex); return(currentCrg); }
/// <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); }
//public bool isSharedCEdgeLtComplete { get; set; } //public CCorrCphs(CPatch pCph1, CPatch pCph2) //{ // this.GID = _intStaticGID++; // SharedCEdgeLt = new List<CEdge>(); // if (pCph1.GID <= pCph2.GID) // { // FrCph = pCph1; // ToCph = pCph2; // } // else // { // FrCph = pCph2; // ToCph = pCph1; // } //} public CCorrCphs(CPatch pCph1, CPatch pCph2, CCorrCphs pCorrCphs) : this(pCph1, pCph2, pCorrCphs.SharedCEdgeLt, pCorrCphs.dblSharedSegLength, pCorrCphs.intSharedCEdgeCount) { }
/// <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); }