/// <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);
        }