예제 #1
0
        private static List <CCorrCphs> ComputeNewAdjCorrCphsSD_WithoutReplaced(SortedDictionary <CCorrCphs, CCorrCphs> pAdjCorrCphsSD,
                                                                                CCorrCphs unitingCorrCphs, CPatch newcph, out List <CCorrCphs> AddKeyLt)
        {
            var newAdjCorrCphsLt = new List <CCorrCphs>(pAdjCorrCphsSD.Count);

            AddKeyLt = new List <CCorrCphs>();
            foreach (var pCorrCphs in pAdjCorrCphsSD.Keys)
            {
                if (pCorrCphs.CompareTo(unitingCorrCphs) == 0)
                {
                    continue;
                }
                else if (pCorrCphs.FrCph.CompareTo(unitingCorrCphs.FrCph) == 0 ||
                         pCorrCphs.FrCph.CompareTo(unitingCorrCphs.ToCph) == 0)
                {
                    AddKeyLt.Add(new CCorrCphs(pCorrCphs.ToCph, newcph, pCorrCphs));
                }
                else if (pCorrCphs.ToCph.CompareTo(unitingCorrCphs.FrCph) == 0 ||
                         pCorrCphs.ToCph.CompareTo(unitingCorrCphs.ToCph) == 0)
                {
                    AddKeyLt.Add(new CCorrCphs(pCorrCphs.FrCph, newcph, pCorrCphs));
                }
                else
                {
                    newAdjCorrCphsLt.Add(pCorrCphs);
                }
            }

            return(newAdjCorrCphsLt);
        }
예제 #2
0
 public static CPatch TryGetNeighbor(CPatch cph, CCorrCphs unitingCorrCphs)
 {
     if (cph.CompareTo(unitingCorrCphs.FrCph) == 0)
     {
         return(unitingCorrCphs.ToCph);
     }
     else if (cph.CompareTo(unitingCorrCphs.ToCph) == 0)
     {
         return(unitingCorrCphs.FrCph);
     }
     else
     {
         return(null);
     }
 }
예제 #3
0
        public static SortedDictionary <CCorrCphs, CCorrCphs> ComputeNewAdjCorrCphsSDAndUpdateExistingCorrCphsSD(
            SortedDictionary <CCorrCphs, CCorrCphs> pAdjCorrCphsSD,
            CCorrCphs unitingCorrCphs, CPatch newcph, SortedDictionary <CCorrCphs, CCorrCphs> ExistingCorrCphsSD)
        {
            //pNewAdjCorrCphsLt_WithoutReplaced is list of AdjCorrCphs without the patches related to uniting
            List <CCorrCphs> AddKeyLt;  //CCorrCphs that are new keys for new AdjCorrCphsSD
            var pNewAdjCorrCphsLt_WithoutReplaced =
                ComputeNewAdjCorrCphsSD_WithoutReplaced(pAdjCorrCphsSD, unitingCorrCphs, newcph, out AddKeyLt);
            var incrementalAdjCorrCphsSD = ComputeIncrementalAdjCorrCphsSD(AddKeyLt, ExistingCorrCphsSD);

            var NewAdjCorrCphsSD = GenerateNewAdjCorrCphsSDAndUpdateExistingCorrCphsSD
                                       (pNewAdjCorrCphsLt_WithoutReplaced, ExistingCorrCphsSD, incrementalAdjCorrCphsSD);

            return(NewAdjCorrCphsSD);
        }
예제 #4
0
        public CPatch ComputeNewCph(CCorrCphs unitingCorrCphs, List <SortedDictionary <CPatch, CPatch> > ExistingCphSDLt)
        {
            var newcph = unitingCorrCphs.FrCph.Unite(unitingCorrCphs.ToCph, unitingCorrCphs.dblSharedSegLength);

            //test whether this newcph has already been constructed before
            CPatch outcph;

            if (ExistingCphSDLt[newcph.CpgSS.Count].TryGetValue(newcph, out outcph))
            {
                newcph = outcph;
            }
            else
            {
                ExistingCphSDLt[newcph.CpgSS.Count].Add(newcph, newcph);
            }
            return(newcph);
        }
        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);
        }
예제 #6
0
        public CRegion GenerateCrgChildAndComputeCost(CRegion lscrg, SortedDictionary <CCorrCphs, CCorrCphs> newAdjCorrCphsSD,
                                                      CPatch activecph, CPatch passivecph, CPatch unitedcph, CCorrCphs unitingCorrCphs, double[,] padblTD)
        {
            var intactiveTypeIndex  = this.GetCphTypeIndex(activecph);
            var intpassiveTypeIndex = this.GetCphTypeIndex(passivecph);

            var newCphTypeIndexSD = new SortedDictionary <CPatch, int>(this.CphTypeIndexSD_Area_CphGID, CPatch.pCmpCPatch_Area_CphGID);

            newCphTypeIndexSD.Remove(activecph);
            newCphTypeIndexSD.Remove(passivecph);
            newCphTypeIndexSD.Add(unitedcph, intactiveTypeIndex);

            //****if I update the codes below, then I should consider updating the codes in function GenerateCrgAndUpdateQ
            //for transfering information to outcrg
            //e.g., outcrg.newCph = newcrg.newCph;
            CRegion newcrg = new CRegion(this.ID);

            newcrg.dblArea       = this.dblArea;
            newcrg.cenumColor    = CEnumColor.gray;
            newcrg.parent        = this;
            newcrg.AggCphs       = new CValTri <CPatch, CPatch, CPatch>(activecph, passivecph, unitedcph);
            newcrg.AdjCorrCphsSD = newAdjCorrCphsSD;
            newcrg.CphTypeIndexSD_Area_CphGID = newCphTypeIndexSD;
            newcrg.intSumCphGID    = this.intSumCphGID - activecph.GID - passivecph.GID + unitedcph.GID;
            newcrg.intSumTypeIndex = this.intSumTypeIndex - intpassiveTypeIndex;
            //newcrg.intEdgeCount = this.intEdgeCount - intDecreaseEdgeCount;
            newcrg.intInteriorEdgeCount = this.intInteriorEdgeCount - unitingCorrCphs.intSharedCEdgeCount;
            newcrg.intExteriorEdgeCount = this.intExteriorEdgeCount;
            newcrg.dblInteriorSegLength = this.dblInteriorSegLength - unitingCorrCphs.dblSharedSegLength;
            newcrg.dblExteriorSegLength = this.dblExteriorSegLength;

            if (CConstants.blnComputeMinComp == true)
            {
                ComputeMinCompIncremental(newcrg, this, activecph, passivecph, unitedcph);
            }
            else if (CConstants.blnComputeAvgComp == true)
            {
                newcrg.dblSumComp = this.dblSumComp - activecph.dblComp
                                    - passivecph.dblComp + unitedcph.dblComp;
                newcrg.dblAvgComp = newcrg.dblSumComp / newcrg.GetCphCount();
            }

            double dblTypeCost = passivecph.dblArea * padblTD[intactiveTypeIndex, intpassiveTypeIndex];

            ComputeExactCost(lscrg, newcrg, dblTypeCost);

            return(newcrg);
        }
예제 #7
0
        //public void AddCph(SortedDictionary<CPatch, int> pCphTypeIndexSD)
        //{
        //    foreach (var pCphTypeIndex in pCphTypeIndexSD)
        //    {
        //        this.CphTypeIndexSD.Add(pCphTypeIndex.Key, pCphTypeIndex.Value);
        //    }
        //}



        /// <summary>
        /// Get the adjacnet relationships between CPatches
        /// </summary>
        /// <param name="cphlt"></param>
        public SortedDictionary <CCorrCphs, CCorrCphs> SetInitialAdjacency()
        {
            // we need this variable here, because it has different comparator with pAdjCorrCphsSD
            var ExistingCorrCphsSD0 = new SortedDictionary <CCorrCphs, CCorrCphs>(CCorrCphs.pCmpCCorrCphs_CphsGID);
            //why SortedDictionary? Because we want to get the value of an element.
            //The element may have the same key with another element.
            SortedDictionary <CEdge, CPatch> cedgeSD = new SortedDictionary <CEdge, CPatch>(new CCmpCEdgeCoordinates());
            var pAdjCorrCphsSD = new SortedDictionary <CCorrCphs, CCorrCphs>();

            if (this.GetCphCount() > 1)
            {
                foreach (var cph in this.GetCphCol())
                {
                    var cpgSK = new Stack <CPolygon>();
                    cpgSK.Push(cph.GetSoloCpg());

                    do
                    {
                        var cpg = cpgSK.Pop();
                        foreach (CEdge cedge in cpg.CEdgeLt)  //Note that there is only one element in cph.CpgSS
                        {
                            //cedge.PrintMySelf();

                            CPatch EdgeShareCph;
                            bool   isShareEdge = cedgeSD.TryGetValue(cedge, out EdgeShareCph);

                            //if cedge already exists in cedgeSD, then we now have found the Patch which shares this cedge
                            if (isShareEdge == true)
                            {
                                CCorrCphs ExsitedCorrCphs;   //SharedEdgesLk belongs to cph.AdjacentSD
                                var       CorrCphs = new CCorrCphs(cph, EdgeShareCph);

                                //whether we have already known that cph and EdgeShareCph are adjacent patches
                                bool isKnownAdjacent = ExistingCorrCphsSD0.TryGetValue(CorrCphs, out ExsitedCorrCphs);
                                if (isKnownAdjacent == true)
                                {
                                    ExsitedCorrCphs.SharedCEdgeLt.Add(cedge);
                                    ExsitedCorrCphs.dblSharedSegLength += cedge.dblLength;
                                    ExsitedCorrCphs.intSharedCEdgeCount++;
                                }
                                else
                                {
                                    //List<CEdge> NewCphSharedEdgesLt = new List<CEdge>();
                                    //CorrCphs.SharedCEdgeLt = new List<CEdge>();
                                    CorrCphs.SharedCEdgeLt.Add(cedge);
                                    CorrCphs.dblSharedSegLength += cedge.dblLength;
                                    CorrCphs.intSharedCEdgeCount++;

                                    pAdjCorrCphsSD.Add(CorrCphs, CorrCphs);
                                    ExistingCorrCphsSD0.Add(CorrCphs, CorrCphs);
                                }

                                this.intInteriorEdgeCount++;
                                this.dblInteriorSegLength += cedge.dblLength;
                                //every edge belongs to two polygons, if we have found the two polygons,
                                //we can remove the shared edge from the SortedDictionary
                                cedgeSD.Remove(cedge);
                            }
                            else  //if cedge doesn't exist in cedgeSD, then we add this cedge
                            {
                                cedgeSD.Add(cedge, cph);
                                //this.intEdgeCount++;
                            }
                        }

                        if (cpg.HoleCpgLt != null)
                        {
                            foreach (var holecpg in cpg.HoleCpgLt)
                            {
                                cpgSK.Push(holecpg);
                            }
                        }
                    } while (cpgSK.Count > 0);
                }
            }

            this.intExteriorEdgeCount = cedgeSD.Count;
            foreach (var cedgekvp in cedgeSD)
            {
                this.dblExteriorSegLength += cedgekvp.Key.dblLength;
            }
            //******************************************************************************************************//
            //to use less memory, we don't save the shared edgelist. in the method of MaximizeMinArea,
            //we don't need the shared edgelist.
            foreach (var item in pAdjCorrCphsSD)
            {
                item.Value.SharedCEdgeLt.Clear();
            }
            //******************************************************************************************************//
            foreach (var cphkvp in CphTypeIndexSD_Area_CphGID)
            {
                cphkvp.Key.AdjacentCphSS = new SortedSet <CPatch>();
            }
            foreach (var pAdjacency_CorrCphs in pAdjCorrCphsSD.Keys)
            {
                pAdjacency_CorrCphs.FrCph.AdjacentCphSS.Add(pAdjacency_CorrCphs.ToCph);
                pAdjacency_CorrCphs.ToCph.AdjacentCphSS.Add(pAdjacency_CorrCphs.FrCph);
            }

            if (CConstants.blnComputeMinComp == true)
            {
                this.ComputeMinComp();
            }
            else if (CConstants.blnComputeAvgComp == true)
            {
                foreach (var cph in this.GetCphCol())
                {
                    this.dblSumComp += cph.dblComp;
                }

                this.dblAvgComp = this.dblSumComp / this.GetCphCount();
            }

            this.AdjCorrCphsSD = pAdjCorrCphsSD;

            //foreach (var pAdjCorrCphs in pAdjCorrCphsSD)
            //{
            //    Console.WriteLine("frcph:  " + (pAdjCorrCphs.Key.FrCph.ID+1).ToString()
            //        + "     tocph:  " + (pAdjCorrCphs.Key.ToCph.ID + 1).ToString() +
            //        "     Interiorlength:  " + pAdjCorrCphs.Key.dblSharedSegLength);
            //}
            return(ExistingCorrCphsSD0);
        }
예제 #8
0
        /// <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);
        }
예제 #9
0
        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.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);
        }
예제 #11
0
 public CCphRecord(CPatch cph, CCorrCphs corrcphs)
 {
     this.Cph      = cph;
     this.CorrCphs = corrcphs;
 }
예제 #12
0
        public CRegion GenerateCrgChildAndComputeExactCost(CRegion lscrg, SortedDictionary <CCorrCphs, CCorrCphs> newAdjCorrCphsSD,
                                                           CPatch activecph, CPatch passivecph, CPatch unitedcph, CCorrCphs unitingCorrCphs, double[,] padblTD)
        {
            var intactiveTypeIndex  = this.GetCphTypeIndex(activecph);
            var intpassiveTypeIndex = this.GetCphTypeIndex(passivecph);
            var corecpg             = this.GetCoreCpg(activecph);

            var newCphCpgSD = new SortedDictionary <CPatch, CPolygon>(this.CphCpgSD_Area_CphGID, CPatch.pCmpCPatch_Area_CphGID);

            if (newCphCpgSD.Remove(activecph) == false || newCphCpgSD.Remove(passivecph) == false)
            {
                throw new ArgumentOutOfRangeException("This should be impossible!");
            }
            newCphCpgSD.Add(unitedcph, corecpg);

            //var passivecpg = this.GetCoreCpg(passivecph);

            //if (lscrg.GetCphCount() - this.GetCphCount() == 4)
            //{
            //    if ((corecpg.ID == 5058 && passivecpg.ID == 5060) || (corecpg.ID == 5060 && passivecpg.ID == 5058))
            //    {
            //        Console.WriteLine(passivecpg.ID + "     " + passivecpg.intTypeIndex + "     " + passivecph.dblArea);
            //        Console.WriteLine(corecpg.ID + "     " + corecpg.intTypeIndex + "     " + activecph.dblArea);
            //        Console.WriteLine();
            //    }
            //}

            //if (lscrg.GetCphCount() - this.GetCphCount() == 5)
            //{
            //    if ((corecpg.ID == 5060 && passivecpg.ID == 5061))
            //    {
            //        Console.WriteLine(passivecpg.ID + "     " + passivecpg.intTypeIndex + "     " + passivecph.dblArea);
            //        Console.WriteLine(corecpg.ID + "     " + corecpg.intTypeIndex + "     " + activecph.dblArea);
            //        Console.WriteLine();
            //    }
            //}


            //var intLSCphCount = lscrg.GetCphCount();
            //var intThisCphCount = this.GetCphCount();
            //if (CRegion._intStaticGID == 1698)
            //{
            //    foreach (var cpg in this.CphCpgSD_Area_CphGID.Values)
            //    {
            //        Console.WriteLine(cpg.ID);
            //    }
            //    int ss = 5;
            //}

            //****if I update the codes below, then I should consider updating the codes in function GenerateCrgAndUpdateQ
            //for transfering information to outcrg
            //e.g., outcrg.newCph = newcrg.newCph;
            var newcrg = new CRegion(this.ID)
            {
                dblArea              = this.dblArea,
                cenumColor           = CEnumColor.gray,
                parent               = this,
                AggCphs              = new CValTri <CPatch, CPatch, CPatch>(activecph, passivecph, unitedcph),
                AdjCorrCphsSD        = newAdjCorrCphsSD,
                CphCpgSD_Area_CphGID = newCphCpgSD,
                intSumCphGID         = this.intSumCphGID - activecph.GID - passivecph.GID + unitedcph.GID,
                intSumTypeIndex      = this.intSumTypeIndex - intpassiveTypeIndex,
                intInteriorEdgeCount = this.intInteriorEdgeCount - unitingCorrCphs.intSharedCEdgeCount,
                intExteriorEdgeCount = this.intExteriorEdgeCount,
                dblInteriorSegLength = this.dblInteriorSegLength - unitingCorrCphs.dblSharedSegLength,
                dblExteriorSegLength = this.dblExteriorSegLength
                                       //intEdgeCount = this.intEdgeCount - intDecreaseEdgeCount
            };

            //if (newcrg.GID == 1698)
            //{
            //    Console.WriteLine("face id of region 1698:");
            //    foreach (var cpg in newcrg.CphCpgSD_Area_CphGID.Values)
            //    {
            //        Console.WriteLine(cpg.ID);
            //    }
            //    int ss = 5;
            //}

            if (CConstants.blnComputeMinComp == true)
            {
                ComputeMinCompIncremental(newcrg, this, activecph, passivecph, unitedcph);
            }
            else if (CConstants.blnComputeAvgComp == true)
            {
                newcrg.dblSumComp = this.dblSumComp - activecph.dblComp
                                    - passivecph.dblComp + unitedcph.dblComp;
                newcrg.dblAvgComp = newcrg.dblSumComp / newcrg.GetCphCount();
            }

            double dblTypeCost = passivecph.dblArea * padblTD[intactiveTypeIndex, intpassiveTypeIndex];

            ComputeExactCost(lscrg, newcrg, dblTypeCost);

            return(newcrg);
        }