Exemplo n.º 1
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));
            }
        }
        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);
        }