Пример #1
0
        private void RunILP(CParameterInitialize ParameterInitialize, double dblTimeLimit, bool chkSmallestChecked)
        {
            ParameterInitialize.cboShapeConstraint.SelectedIndex = 1;
            ParameterInitialize.chkSmallest.Checked = chkSmallestChecked;

            //var objResultSD = new SortedDictionary<string, List<object>>();
            var StrObjLtDt = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);

            _pCAreaAgg_ILP = new CAreaAgg_ILP(ParameterInitialize);
            _pCAreaAgg_ILP.dblTimeLimit = dblTimeLimit;
            _pCAreaAgg_ILP.AreaAggregation();
            StrObjLtDt.Merge(_pCAreaAgg_ILP.StrObjLtDt);
            CAreaAgg_Base.SaveData(StrObjLtDt, ParameterInitialize, "ILP", Convert.ToInt32(_pCAreaAgg_ILP.dblTimeLimit).ToString());
        }
Пример #2
0
        private void RunGreedy(CParameterInitialize ParameterInitialize,
                               int cboShapeConstraintSelectedIndex, bool chkSmallestChecked)
        {
            ParameterInitialize.cboShapeConstraint.SelectedIndex = cboShapeConstraintSelectedIndex;
            ParameterInitialize.chkSmallest.Checked = chkSmallestChecked;

            //var objResultSD = new SortedDictionary<string, List<object>>();
            var StrObjLtDt = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);

            _pCAreaAgg_Greedy = new CAreaAgg_Greedy(ParameterInitialize);
            _pCAreaAgg_Greedy.AreaAggregation();
            StrObjLtDt.Merge(_pCAreaAgg_Greedy.StrObjLtDt);
            CAreaAgg_Base.SaveData(StrObjLtDt, ParameterInitialize, "Greedy");
        }
Пример #3
0
        private void RunAStar(CParameterInitialize ParameterInitialize,
                              int intQuitCount, int cboShapeConstraintSelectedIndex, bool chkSmallestChecked)
        {
            ParameterInitialize.cboShapeConstraint.SelectedIndex = cboShapeConstraintSelectedIndex;
            ParameterInitialize.chkSmallest.Checked = chkSmallestChecked;

            //var objResultSD = new SortedDictionary<string, List<object>>();
            var StrObjLtDt = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);

            _pCAreaAgg_AStar = new CAreaAgg_AStar(ParameterInitialize);
            _pCAreaAgg_AStar.AreaAggregation(intQuitCount);
            StrObjLtDt.Merge(_pCAreaAgg_AStar.StrObjLtDt);
            CAreaAgg_Base.SaveData(StrObjLtDt, ParameterInitialize, "AStar", intQuitCount.ToString());
        }
        public void btnAStar_Click(object sender, EventArgs e)
        {
            var ParameterInitialize = _DataRecords.ParameterInitialize;
            //var objDataLtLt = new List<List<object>>();
            //var objResultSD = new SortedDictionary<string, List<object>>();
            var StrObjLtDt = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);

            //Read Datasets
            _pCAreaAgg_AStar = new CAreaAgg_AStar(ParameterInitialize);
            _pCAreaAgg_AStar.AreaAggregation(Convert.ToInt32(txtNodes.Text));

            this.txtEvaluation.Text = _pCAreaAgg_AStar.dblCost.ToString();
            StrObjLtDt.Merge(_pCAreaAgg_AStar.StrObjLtDt);
            _pCAreaAgg_Base = _pCAreaAgg_AStar as CAreaAgg_Base;
            CAreaAgg_Base.SaveData(StrObjLtDt, _pCAreaAgg_Base, ParameterInitialize, "AStar", txtNodes.Text);
            MessageBox.Show("Done!");
        }
        public void btnGreedy_Click(object sender, EventArgs e)
        {
            var ParameterInitialize = _DataRecords.ParameterInitialize;

            var objResultSD = new SortedDictionary <string, List <object> >();
            var StrObjLtDt  = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);
            //Read Datasets
            var pCAreaAgg_Greedy = new CAreaAgg_Greedy(ParameterInitialize);

            pCAreaAgg_Greedy.AreaAggregation();

            this.txtEvaluation.Text = pCAreaAgg_Greedy.dblCost.ToString();
            StrObjLtDt.Merge(pCAreaAgg_Greedy.StrObjLtDt);
            _pCAreaAgg_Base = pCAreaAgg_Greedy as CAreaAgg_Base;
            CAreaAgg_Base.SaveData(StrObjLtDt, _pCAreaAgg_Base, ParameterInitialize, "Greedy");
            MessageBox.Show("Done!");
        }
Пример #6
0
        public void btnRunILP_Click(object sender, EventArgs e)
        {
            CParameterInitialize ParameterInitialize = _DataRecords.ParameterInitialize;

            ParameterInitialize.cboShapeConstraint.SelectedIndex = 1;

            var StrObjLtDt = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);

            //读取数据
            _pCAreaAgg_ILP = new CAreaAgg_ILP(ParameterInitialize);
            _pCAreaAgg_ILP.AreaAggregation();

            //this.txtEvaluation.Text = _pCAreaAgg_ILP.dblCost.ToString();
            StrObjLtDt.Merge(_pCAreaAgg_ILP.StrObjLtDt);
            CAreaAgg_AStar.SaveData(StrObjLtDt, ParameterInitialize, "ILP", Convert.ToInt32(_pCAreaAgg_ILP.dblTimeLimit).ToString());


            _pCAreaAgg_Base = _pCAreaAgg_ILP as CAreaAgg_Base;
            MessageBox.Show("Done!");
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="LSCrg"></param>
        /// <param name="SSCrg"></param>
        /// <param name="StrObjLtDt"></param>
        /// <param name="adblTD"></param>
        /// <param name="EstStepsCostVPDt">Results from A*</param>
        /// <param name="strAreaAggregation"></param>
        /// <returns></returns>
        public CRegion Greedy(CRegion LSCrg, CRegion SSCrg, CStrObjLtDt StrObjLtDt, double[,] adblTD,
                              Dictionary <int, CValPair <int, double> > EstStepsCostVPDt, string strAreaAggregation)
        {
            long lngStartMemory     = GC.GetTotalMemory(true);
            var  pStopwatchOverHead = Stopwatch.StartNew();

            var ExistingCorrCphsSD0 = LSCrg.SetInitialAdjacency();  //also count the number of edges

            AddLineToStrObjLtDt(StrObjLtDt, LSCrg);


            Console.WriteLine();
            Console.WriteLine("Crg:  ID  " + LSCrg.ID + ";    n  " + LSCrg.GetCphCount() + ";    m  " +
                              LSCrg.AdjCorrCphsSD.Count + "   " + CConstants.strShapeConstraint + "   " + strAreaAggregation);


            long lngTimeOverHead = pStopwatchOverHead.ElapsedMilliseconds;

            pStopwatchOverHead.Stop();

            var  pStopwatchLast = new Stopwatch();
            long lngTime        = 0;

            CRegion resultcrg = new CRegion(-2);

            try
            {
                pStopwatchLast.Restart();
                var ExistingCorrCphsSD = new SortedDictionary <CCorrCphs, CCorrCphs>
                                             (ExistingCorrCphsSD0, ExistingCorrCphsSD0.Comparer);
                LSCrg.cenumColor = CEnumColor.white;

                resultcrg = Compute(LSCrg, SSCrg, SSCrg.GetSoloCphTypeIndex(),
                                    strAreaAggregation, ExistingCorrCphsSD, StrObjLtDt, adblTD);
            }
            catch (System.OutOfMemoryException ex)
            {
                Console.WriteLine(ex.Message);
            }
            lngTime = pStopwatchLast.ElapsedMilliseconds + lngTimeOverHead;


            Console.WriteLine("d: " + resultcrg.d
                              + "            Type: " + resultcrg.dblCostExactType
                              + "            Compactness: " + resultcrg.dblCostExactComp);

            EstStepsCostVPDt.TryGetValue(LSCrg.ID, out CValPair <int, double> outEstStepsCostVP);
            if (outEstStepsCostVP.val1 == 0 &&
                CCmpMethods.CmpDbl_CoordVerySmall(outEstStepsCostVP.val2, resultcrg.d) == 0)
            {
                StrObjLtDt.SetLastObj("EstSteps/Gap%", 0); //optimal solutions
            }
            else
            {
                StrObjLtDt.SetLastObj("EstSteps/Gap%", 100); //not sure, at least feasible solutions
            }
            //we don't need to +1 because +1 is already included in _intStaticGID
            //int intExploredRegionAll = CRegion._intStaticGID - CRegion._intStartStaticGIDLast;
            StrObjLtDt.SetLastObj("#Edges", CRegion._intEdgeCount);
            StrObjLtDt.SetLastObj("Time_F(ms)", lngTime);
            StrObjLtDt.SetLastObj("Time_L(ms)", lngTime);
            StrObjLtDt.SetLastObj("Time(ms)", lngTime);
            StrObjLtDt.SetLastObj("Memory(MB)", CHelpFunc.GetConsumedMemoryInMB(false, lngStartMemory));

            Console.WriteLine("We have visited " +
                              CRegion._intNodeCount + " Nodes and " + CRegion._intEdgeCount + " Edges.");

            return(resultcrg);
        }
        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);
        }
Пример #9
0
        public CRegion AStar(CRegion LSCrg, CRegion SSCrg, CStrObjLtDt StrObjLtDt, string strAreaAggregation,
                             double[,] padblTD, int intQuitCount = 200000, Process proc = null)
        {
            long lngStartMemory = GC.GetTotalMemory(true);


            var pStopwatchOverHead = new Stopwatch();

            pStopwatchOverHead.Start();


            var ExistingCorrCphsSD0 = LSCrg.SetInitialAdjacency();  //also count the number of edges

            AddLineToStrObjLtDt(StrObjLtDt, LSCrg);


            Console.WriteLine();
            Console.WriteLine("Crg:  ID  " + LSCrg.ID + ";    n  " + LSCrg.GetCphCount() + ";    m  " +
                              LSCrg.AdjCorrCphsSD.Count + "   " + intQuitCount + "   " +
                              CConstants.strShapeConstraint + "   " + strAreaAggregation);


            long lngTimeOverHead = pStopwatchOverHead.ElapsedMilliseconds;

            pStopwatchOverHead.Stop();

            Stopwatch pStopwatchLast  = new Stopwatch();
            bool      blnRecordTime_F = false;
            long      lngTime_F       = 0; //running time of the first trying
            long      lngTime_L       = 0; //running time of last trying
            long      lngTimeAll      = lngTimeOverHead;
            var       resultcrg       = new CRegion(-2);
            int       intEstSteps     = 0;
            int       intRound        = _intRound;

            do
            {
                intEstSteps = Convert.ToInt32(Math.Pow(2, intRound)) - 1;
                try
                {
                    //CRegion._intStartStaticGIDLast = CRegion._intStaticGID;
                    pStopwatchLast.Restart();
                    var ExistingCorrCphsSD = new SortedDictionary <CCorrCphs, CCorrCphs>
                                                 (ExistingCorrCphsSD0, ExistingCorrCphsSD0.Comparer);
                    LSCrg.cenumColor = CEnumColor.white;

                    resultcrg = ComputeAccordEstSteps(LSCrg, SSCrg, strAreaAggregation, ExistingCorrCphsSD,
                                                      intEstSteps, StrObjLtDt, padblTD, intQuitCount);
                }
                catch (System.OutOfMemoryException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                if (blnRecordTime_F == false)
                {
                    lngTime_F       = pStopwatchLast.ElapsedMilliseconds + lngTimeOverHead;
                    blnRecordTime_F = true;
                }
                lngTime_L   = pStopwatchLast.ElapsedMilliseconds + lngTimeOverHead;
                lngTimeAll += pStopwatchLast.ElapsedMilliseconds;

                if (resultcrg.ID != -2)
                {
                    break;
                }
                if (intEstSteps > 50)
                {
                    intEstSteps = 64;
                    throw new ArgumentException("We cannot solve the problem! Impossible!");
                }

                intRound++;
            } while (true);
            StrObjLtDt.SetLastObj("EstSteps", intEstSteps);
            Console.WriteLine("d: " + resultcrg.d
                              + "            Type: " + resultcrg.dblCostExactType
                              + "            Compactness: " + resultcrg.dblCostExactComp);

            //we don't need to +1 because +1 is already included in _intStaticGID
            //int intExploredRegionAll = CRegion._intStaticGID - CRegion._intStartStaticGIDLast;
            //double dblConsumedMemoryInMB = CHelpFunc.GetConsumedMemoryInMB(false);

            StrObjLtDt.SetLastObj("#Edges", CRegion._intEdgeCount);
            StrObjLtDt.SetLastObj("Time_F(ms)", lngTime_F);
            StrObjLtDt.SetLastObj("Time_L(ms)", lngTime_L);
            StrObjLtDt.SetLastObj("Time(ms)", lngTimeAll);
            StrObjLtDt.SetLastObj("Memory(MB)", CHelpFunc.GetConsumedMemoryInMB(false, lngStartMemory));

            Console.WriteLine("EstSteps:" + intEstSteps + "      We have visited " +
                              CRegion._intNodeCount + " Nodes and " + CRegion._intEdgeCount + " Edges.");

            return(resultcrg);
        }
Пример #10
0
        private CRegion ComputeAccordEstSteps(CRegion LSCrg, CRegion SSCrg, string strAreaAggregation,
                                              SortedDictionary <CCorrCphs, CCorrCphs> ExistingCorrCphsSD, int intEstSteps, CStrObjLtDt StrObjLtDt,
                                              double[,] padblTD, int intQuitCount = 200000)
        {
            int intRegionID = LSCrg.ID;  //all the regions generated in this function will have the same intRegionID

            ComputeEstCost(LSCrg, SSCrg, LSCrg, padblTD, intEstSteps);

            //LSCrg.InitialEstimatedCost(SSCrg, padblTD, intEstSteps);
            //LSCrg.SetCoreCph(intSSTypeIndex);

            //a region represents a node in graph, ExistingCrgSD stores all the nodes
            //we use this dictionary to make sure that if the two patches have the same cpgs, then they have the same GID
            var ExistingCphSDLt = new List <SortedDictionary <CPatch, CPatch> >(LSCrg.GetCphCount() + 1);

            for (int i = 0; i < ExistingCphSDLt.Capacity; i++)
            {
                var Element = new SortedDictionary <CPatch, CPatch>(CPatch.pCmpCPatch_CpgGID);
                ExistingCphSDLt.Add(Element);
            }

            var ExistingCrgSDLt = new List <SortedDictionary <CRegion, CRegion> >(LSCrg.GetCphCount() + 1);

            for (int i = 0; i < ExistingCrgSDLt.Capacity; i++)
            {
                //we don't compare exact cost first because of there may be rounding problems
                var Element = new SortedDictionary <CRegion, CRegion>(CRegion.pCmpCrg_CphGIDTypeIndex);
                ExistingCrgSDLt.Add(Element);
            }
            ExistingCrgSDLt[LSCrg.GetCphCount()].Add(LSCrg, LSCrg);

            var FinalOneCphCrg = new CRegion(intRegionID);
            var Q        = new SortedSet <CRegion>(CRegion.pCmpCrg_Cost_CphGIDTypeIndex);
            int intCount = 0;

            CRegion._intNodeCount = 1;
            CRegion._intEdgeCount = 0;
            Q.Add(LSCrg);
            while (true)
            {
                intCount++;
                var u = Q.Min;
                if (Q.Remove(u) == false)
                {
                    throw new ArgumentException
                              ("cannot move an element in this queue! A solution might be to make dblVerySmall smaller!");
                }
                u.cenumColor = CEnumColor.black;

                //List<CRegion> crgcol = new List<CRegion>();
                //crgcol.Add(u);

                //OutputMap(crgcol, this._TypePVDt, u.d, intCount, pParameterInitialize);

                //MessageBox.Show("click for next!");

                //if (CConstants.strShapeConstraint == "MaximizeMinComp_EdgeNumber" ||
                //    CConstants.strShapeConstraint == "MinimizeInteriorBoundaries")
                //{
                //    Console.WriteLine("Crg:  ID  " + u.ID + ";      GID:" + u.GID + ";      CphNum:" +  u.GetCphCount() +
                //        ";      d:" + u.d / u.dblArea + ";      ExactCost:" + u.dblCostExact / u.dblArea +
                //        ";      Compactness:" + u.dblCostExactComp + ";      Type:" + u.dblCostExactType / u.dblArea);
                //}
                //else if (CConstants.strShapeConstraint == "NonShape")
                //{
                //    Console.WriteLine("Crg:  ID  " + u.ID + ";      GID:" + u.GID + ";      CphNum:" + u.GetCphCount() +
                //        ";      d:" + u.d + ";      ExactCost:" + u.dblCostExactType);
                //}

                //at the beginning, resultcrg.d is double.MaxValue.
                //Later, when we first encounter that there is only one CPatch in LSCrg,
                //resultcrg.d will be changed to the real cost
                //u.d contains estimation, and resultcrg.d doesn't contains.
                //if u.d > resultcrg.d, then resultcrg.d must already be the smallest cost
                if (u.GetCphCount() == 1)
                {
                    if (u.GetSoloCphTypeIndex() == SSCrg.GetSoloCphTypeIndex())
                    {
                        //Console.WriteLine("The number of nodes we can forget:   " + intCount);
                        //Console.WriteLine("The nodes in the stack:   " + Q.Count);

                        //int intCrgCount = 0;
                        //foreach (var item in ExistingCrgSDLt)
                        //{
                        //    intCrgCount += item.Count;
                        //}

                        FinalOneCphCrg = u;
                        break;
                    }
                    else
                    {
                        throw new ArgumentException("this is impossible!");
                        //continue;
                    }
                }


                foreach (var newcrg in AggregateAndUpdateQ(u, LSCrg, SSCrg, Q, strAreaAggregation,
                                                           ExistingCrgSDLt, ExistingCphSDLt, ExistingCorrCphsSD, _adblTD, intEstSteps))
                {
                    //int intExploredRegionLast = CRegion._intStaticGID - CRegion._intStartStaticGIDLast;
                    //we don't need to +1 because +1 is already included in _intStaticGID

                    if (CRegion._intNodeCount > intQuitCount)
                    {
                        //if we have visited 2000000 regions but haven't found an optimum aggregation sequence,
                        //then we return null and overestimate in the heuristic function
                        return(new CRegion(-2));
                    }
                }
            }

            RecordResultForCrg(StrObjLtDt, LSCrg, FinalOneCphCrg, SSCrg.GetSoloCphTypeIndex());
            return(FinalOneCphCrg);
        }
Пример #11
0
        private void btnRunILPSpecified_Click(object sender, EventArgs e)
        {
            List <int> intSpecifiedIDLt = new List <int>();

            #region intSpecifiedIDLt
            intSpecifiedIDLt.Add(215);
            intSpecifiedIDLt.Add(94);
            intSpecifiedIDLt.Add(590);
            intSpecifiedIDLt.Add(301);
            intSpecifiedIDLt.Add(436);
            intSpecifiedIDLt.Add(386);
            intSpecifiedIDLt.Add(112);
            intSpecifiedIDLt.Add(523);
            intSpecifiedIDLt.Add(452);
            intSpecifiedIDLt.Add(531);
            intSpecifiedIDLt.Add(553);
            intSpecifiedIDLt.Add(397);
            intSpecifiedIDLt.Add(586);
            intSpecifiedIDLt.Add(190);
            intSpecifiedIDLt.Add(179);
            intSpecifiedIDLt.Add(298);
            intSpecifiedIDLt.Add(551);
            intSpecifiedIDLt.Add(492);
            intSpecifiedIDLt.Add(194);
            intSpecifiedIDLt.Add(177);
            intSpecifiedIDLt.Add(477);
            intSpecifiedIDLt.Add(53);
            intSpecifiedIDLt.Add(358);
            intSpecifiedIDLt.Add(252);
            intSpecifiedIDLt.Add(506);
            intSpecifiedIDLt.Add(424);
            intSpecifiedIDLt.Add(543);
            intSpecifiedIDLt.Add(165);
            intSpecifiedIDLt.Add(410);
            intSpecifiedIDLt.Add(97);
            intSpecifiedIDLt.Add(257);
            intSpecifiedIDLt.Add(154);
            intSpecifiedIDLt.Add(248);
            intSpecifiedIDLt.Add(544);
            intSpecifiedIDLt.Add(403);
            intSpecifiedIDLt.Add(537);
            intSpecifiedIDLt.Add(503);
            intSpecifiedIDLt.Add(429);
            intSpecifiedIDLt.Add(343);
            intSpecifiedIDLt.Add(67);
            intSpecifiedIDLt.Add(462);
            intSpecifiedIDLt.Add(272);
            intSpecifiedIDLt.Add(1);
            intSpecifiedIDLt.Add(525);
            intSpecifiedIDLt.Add(463);
            intSpecifiedIDLt.Add(391);
            intSpecifiedIDLt.Add(516);
            intSpecifiedIDLt.Add(186);
            intSpecifiedIDLt.Add(420);
            intSpecifiedIDLt.Add(418);
            intSpecifiedIDLt.Add(371);
            intSpecifiedIDLt.Add(77);
            intSpecifiedIDLt.Add(216);
            intSpecifiedIDLt.Add(14);
            intSpecifiedIDLt.Add(647);
            intSpecifiedIDLt.Add(545);
            intSpecifiedIDLt.Add(444);
            intSpecifiedIDLt.Add(441);
            intSpecifiedIDLt.Add(542);
            intSpecifiedIDLt.Add(381);
            intSpecifiedIDLt.Add(2);
            intSpecifiedIDLt.Add(479);
            intSpecifiedIDLt.Add(133);
            intSpecifiedIDLt.Add(327);
            intSpecifiedIDLt.Add(400);
            intSpecifiedIDLt.Add(569);
            intSpecifiedIDLt.Add(155);
            intSpecifiedIDLt.Add(153);
            intSpecifiedIDLt.Add(521);
            intSpecifiedIDLt.Add(500);
            intSpecifiedIDLt.Add(123);
            intSpecifiedIDLt.Add(349);
            intSpecifiedIDLt.Add(245);
            intSpecifiedIDLt.Add(29);
            intSpecifiedIDLt.Add(520);
            intSpecifiedIDLt.Add(560);
            intSpecifiedIDLt.Add(467);
            intSpecifiedIDLt.Add(33);
            intSpecifiedIDLt.Add(422);
            intSpecifiedIDLt.Add(324);
            intSpecifiedIDLt.Add(566);
            intSpecifiedIDLt.Add(684);
            intSpecifiedIDLt.Add(289);
            intSpecifiedIDLt.Add(559);
            intSpecifiedIDLt.Add(222);
            intSpecifiedIDLt.Add(679);
            intSpecifiedIDLt.Add(451);
            intSpecifiedIDLt.Add(426);
            intSpecifiedIDLt.Add(101);
            intSpecifiedIDLt.Add(47);
            intSpecifiedIDLt.Add(450);
            intSpecifiedIDLt.Add(533);
            intSpecifiedIDLt.Add(207);
            intSpecifiedIDLt.Add(572);
            intSpecifiedIDLt.Add(417);
            intSpecifiedIDLt.Add(217);
            intSpecifiedIDLt.Add(548);
            intSpecifiedIDLt.Add(368);
            intSpecifiedIDLt.Add(119);
            intSpecifiedIDLt.Add(639);
            intSpecifiedIDLt.Add(490);
            intSpecifiedIDLt.Add(437);
            intSpecifiedIDLt.Add(193);
            intSpecifiedIDLt.Add(487);
            intSpecifiedIDLt.Add(447);
            intSpecifiedIDLt.Add(329);
            intSpecifiedIDLt.Add(306);
            intSpecifiedIDLt.Add(243);
            intSpecifiedIDLt.Add(653);
            intSpecifiedIDLt.Add(394);
            intSpecifiedIDLt.Add(379);
            intSpecifiedIDLt.Add(315);
            intSpecifiedIDLt.Add(288);
            intSpecifiedIDLt.Add(224);
            intSpecifiedIDLt.Add(720);
            intSpecifiedIDLt.Add(561);
            intSpecifiedIDLt.Add(491);
            intSpecifiedIDLt.Add(0);
            intSpecifiedIDLt.Add(389);
            intSpecifiedIDLt.Add(72);
            intSpecifiedIDLt.Add(562);
            intSpecifiedIDLt.Add(475);
            intSpecifiedIDLt.Add(582);
            intSpecifiedIDLt.Add(261);
            intSpecifiedIDLt.Add(137);
            intSpecifiedIDLt.Add(42);
            intSpecifiedIDLt.Add(26);
            intSpecifiedIDLt.Add(515);
            intSpecifiedIDLt.Add(364);
            intSpecifiedIDLt.Add(234);
            intSpecifiedIDLt.Add(24);
            intSpecifiedIDLt.Add(469);
            intSpecifiedIDLt.Add(372);
            intSpecifiedIDLt.Add(348);
            intSpecifiedIDLt.Add(267);
            intSpecifiedIDLt.Add(577);
            intSpecifiedIDLt.Add(517);
            intSpecifiedIDLt.Add(405);
            intSpecifiedIDLt.Add(350);
            intSpecifiedIDLt.Add(221);
            intSpecifiedIDLt.Add(58);
            intSpecifiedIDLt.Add(712);
            intSpecifiedIDLt.Add(578);
            intSpecifiedIDLt.Add(555);
            intSpecifiedIDLt.Add(61);
            intSpecifiedIDLt.Add(278);
            intSpecifiedIDLt.Add(180);
            intSpecifiedIDLt.Add(128);
            intSpecifiedIDLt.Add(240);
            intSpecifiedIDLt.Add(466);
            intSpecifiedIDLt.Add(412);
            intSpecifiedIDLt.Add(375);
            intSpecifiedIDLt.Add(113);
            intSpecifiedIDLt.Add(75);
            intSpecifiedIDLt.Add(342);
            intSpecifiedIDLt.Add(135);
            intSpecifiedIDLt.Add(286);
            intSpecifiedIDLt.Add(68);
            intSpecifiedIDLt.Add(23);
            intSpecifiedIDLt.Add(693);
            intSpecifiedIDLt.Add(651);
            intSpecifiedIDLt.Add(539);
            intSpecifiedIDLt.Add(407);
            intSpecifiedIDLt.Add(274);
            intSpecifiedIDLt.Add(725);
            intSpecifiedIDLt.Add(607);
            intSpecifiedIDLt.Add(377);
            intSpecifiedIDLt.Add(370);
            intSpecifiedIDLt.Add(208);
            intSpecifiedIDLt.Add(65);
            intSpecifiedIDLt.Add(556);
            intSpecifiedIDLt.Add(355);
            intSpecifiedIDLt.Add(71);
            intSpecifiedIDLt.Add(52);
            intSpecifiedIDLt.Add(10);
            intSpecifiedIDLt.Add(43);
            intSpecifiedIDLt.Add(78);
            intSpecifiedIDLt.Add(702);
            intSpecifiedIDLt.Add(127);
            intSpecifiedIDLt.Add(579);
            intSpecifiedIDLt.Add(352);
            intSpecifiedIDLt.Add(235);
            intSpecifiedIDLt.Add(141);
            intSpecifiedIDLt.Add(13);
            intSpecifiedIDLt.Add(713);
            intSpecifiedIDLt.Add(518);
            intSpecifiedIDLt.Add(307);
            intSpecifiedIDLt.Add(209);
            intSpecifiedIDLt.Add(114);
            intSpecifiedIDLt.Add(50);
            intSpecifiedIDLt.Add(730);
            intSpecifiedIDLt.Add(714);
            intSpecifiedIDLt.Add(616);
            intSpecifiedIDLt.Add(528);
            intSpecifiedIDLt.Add(468);
            intSpecifiedIDLt.Add(465);
            intSpecifiedIDLt.Add(285);
            intSpecifiedIDLt.Add(271);
            intSpecifiedIDLt.Add(122);
            intSpecifiedIDLt.Add(473);
            intSpecifiedIDLt.Add(259);
            intSpecifiedIDLt.Add(669);
            intSpecifiedIDLt.Add(454);
            intSpecifiedIDLt.Add(369);
            intSpecifiedIDLt.Add(640);
            intSpecifiedIDLt.Add(488);
            intSpecifiedIDLt.Add(263);
            intSpecifiedIDLt.Add(82);
            intSpecifiedIDLt.Add(434);
            intSpecifiedIDLt.Add(385);
            intSpecifiedIDLt.Add(602);
            intSpecifiedIDLt.Add(93);
            intSpecifiedIDLt.Add(612);
            intSpecifiedIDLt.Add(411);
            intSpecifiedIDLt.Add(104);
            intSpecifiedIDLt.Add(249);
            intSpecifiedIDLt.Add(439);
            intSpecifiedIDLt.Add(482);
            intSpecifiedIDLt.Add(625);
            intSpecifiedIDLt.Add(354);
            intSpecifiedIDLt.Add(446);
            intSpecifiedIDLt.Add(461);
            intSpecifiedIDLt.Add(287);
            intSpecifiedIDLt.Add(88);
            intSpecifiedIDLt.Add(685);
            intSpecifiedIDLt.Add(320);
            intSpecifiedIDLt.Add(74);
            intSpecifiedIDLt.Add(605);
            intSpecifiedIDLt.Add(296);
            intSpecifiedIDLt.Add(242);
            intSpecifiedIDLt.Add(17);
            intSpecifiedIDLt.Add(710);
            intSpecifiedIDLt.Add(192);
            intSpecifiedIDLt.Add(353);
            intSpecifiedIDLt.Add(637);
            intSpecifiedIDLt.Add(606);
            intSpecifiedIDLt.Add(510);
            intSpecifiedIDLt.Add(608);
            intSpecifiedIDLt.Add(170);
            intSpecifiedIDLt.Add(673);
            intSpecifiedIDLt.Add(175);
            intSpecifiedIDLt.Add(408);
            intSpecifiedIDLt.Add(597);
            intSpecifiedIDLt.Add(157);
            intSpecifiedIDLt.Add(299);
            intSpecifiedIDLt.Add(641);
            intSpecifiedIDLt.Add(536);
            intSpecifiedIDLt.Add(229);
            intSpecifiedIDLt.Add(225);
            intSpecifiedIDLt.Add(659);
            intSpecifiedIDLt.Add(629);
            intSpecifiedIDLt.Add(648);
            intSpecifiedIDLt.Add(79);
            intSpecifiedIDLt.Add(388);
            intSpecifiedIDLt.Add(387);
            intSpecifiedIDLt.Add(610);
            intSpecifiedIDLt.Add(638);
            intSpecifiedIDLt.Add(48);
            intSpecifiedIDLt.Add(90);
            intSpecifiedIDLt.Add(557);
            intSpecifiedIDLt.Add(655);
            intSpecifiedIDLt.Add(156);
            intSpecifiedIDLt.Add(431);
            intSpecifiedIDLt.Add(728);
            intSpecifiedIDLt.Add(255);
            intSpecifiedIDLt.Add(524);
            intSpecifiedIDLt.Add(39);
            intSpecifiedIDLt.Add(664);
            intSpecifiedIDLt.Add(617);
            intSpecifiedIDLt.Add(581);
            intSpecifiedIDLt.Add(213);
            intSpecifiedIDLt.Add(696);
            intSpecifiedIDLt.Add(309);
            intSpecifiedIDLt.Add(715);
            intSpecifiedIDLt.Add(260);
            intSpecifiedIDLt.Add(615);
            intSpecifiedIDLt.Add(269);
            intSpecifiedIDLt.Add(35);
            intSpecifiedIDLt.Add(313);
            #endregion



            CParameterInitialize ParameterInitialize = _DataRecords.ParameterInitialize;
            var StrObjLtDt = new CStrObjLtDt(CAreaAgg_Base.strKeyLt);
            _pCAreaAgg_ILP = new CAreaAgg_ILP(ParameterInitialize);
            _pCAreaAgg_ILP.AreaAggregation(intSpecifiedIDLt);
            StrObjLtDt.Merge(_pCAreaAgg_ILP.StrObjLtDt);

            //for (int i = 0; i < intSpecifiedIDLt.Count; i++)
            //{

            //    _pCAreaAgg_ILP.AreaAggregation();
            //    StrObjLtDt.Merge(_pCAreaAgg_ILP.StrObjLtDt);

            //}

            _pCAreaAgg_Base = _pCAreaAgg_ILP as CAreaAgg_Base;
            CAreaAgg_AStar.SaveData(StrObjLtDt, ParameterInitialize, "ILP", Convert.ToInt32(_pCAreaAgg_ILP.dblTimeLimit).ToString());
            MessageBox.Show("Done!");
        }
        public CRegion ILP(CRegion LSCrg, CRegion SSCrg, CStrObjLtDt StrObjLtDt,
                           double[,] adblTD, string strAreaAggregation,
                           ref SortedSet <CRegion> CrgOOMSetSS, ref SortedSet <CRegion> CrgOOMSolveSS, ref SortedSet <CRegion> CrgOOTSetSS, ref SortedSet <CRegion> CrgOOTSolveSS,
                           ref SortedSet <CRegion> CrgCplexError3019SS, ref SortedSet <CRegion> CrgOtherErrorsSS, ref string strOtherErrors)
        {
            long lngStartMemory = GC.GetTotalMemory(true);

            var pStopwatch = Stopwatch.StartNew();

            LSCrg.SetInitialAdjacency();  //also count the number of edges
            AddLineToStrObjLtDt(StrObjLtDt, LSCrg);

            //must be below LSCrg.SetInitialAdjacency();
            System.Console.WriteLine();
            System.Console.WriteLine();
            System.Console.WriteLine("Crg:  ID  " + LSCrg.ID + ";    n  " + LSCrg.GetCphCount() + ";    m  " +
                                     LSCrg.AdjCorrCphsSD.Count + "  " + _ParameterInitialize.strAreaAggregation + "================================="
                                     + LSCrg.ID + "  " + _ParameterInitialize.strAreaAggregation + " " + this.dblTimeLimit + " s " + "==============================");



            //double dblMemoryInMB2 = CHelpFunc.GetConsumedMemoryInMB(true);
            var cplex = new Cplex();
            //double dblMemoryInMB3 = CHelpFunc.GetConsumedMemoryInMB(true);

            var  crg        = new CRegion(-1);
            bool blnSolved  = false;
            bool blnSetting = false;

            try
            {
                //Step 3
                //Cplex cplex = new Cplex();
                IIntVar[][][]     var2;
                IIntVar[][][][]   var3;
                IIntVar[][][][][] var4;
                IRange[][]        rng;

                PopulateByRow(cplex, out var2, out var3, out var4, out rng, LSCrg, SSCrg, adblTD, strAreaAggregation);
                //double dblMemoryInMB4 = CHelpFunc.GetConsumedMemoryInMB(true);
                // Step 11
                //cplex.ExportModel("lpex1.lp");

                // Step 9
                double dblRemainTimeLim = this.dblTimeLimit - Convert.ToDouble(pStopwatch.ElapsedMilliseconds) / 1000;
                if (dblRemainTimeLim > 0)
                {
                    blnSetting = true;

                    cplex.SetParam(Cplex.DoubleParam.TiLim, dblRemainTimeLim);

                    //avoid that optimal solutions from CPELX are not optimal
                    //see https://www-01.ibm.com/support/docview.wss?uid=swg1RS02094
                    cplex.SetParam(Cplex.IntParam.AuxRootThreads, -1);
                    cplex.SetParam(Cplex.IntParam.Reduce, 0);  //really work for me
                    cplex.SetParam(Cplex.DoubleParam.CutLo, 0);

                    if (cplex.Solve())
                    {
                        //***********Gap for ILP************

                        #region Display x, y, z, and s
                        //for (int i = 0; i < var3[0].GetLength(0); i++)
                        //{

                        //    Console.WriteLine("Variable x; Time: " + (i + 1).ToString());

                        //    foreach (var x1 in var3[0][i])
                        //    {
                        //        //CPatch



                        //        double[] x = cplex.GetValues(x1);


                        //        foreach (var x0 in x)
                        //        {
                        //            int intWrite = 0;  //avoid some values like 0.999999997 or 2E-09
                        //            if (x0>0.5)
                        //            {
                        //                intWrite = 1;
                        //            }
                        //            Console.Write(intWrite + "     ");
                        //        }
                        //        Console.WriteLine();

                        //    }
                        //    Console.WriteLine();
                        //}

                        #region Display y and z
                        //if (var4[0] != null)
                        //{
                        //    Console.WriteLine("");
                        //    //Console.WriteLine("Variable y:");
                        //    for (int i = 0; i < var4[0].GetLength(0); i++)
                        //    {
                        //        Console.WriteLine("Variable y; Time: " + (i + 1).ToString());
                        //        foreach (var y2 in var4[0][i])
                        //        {
                        //            foreach (var y1 in y2)
                        //            {

                        //                double[] y = cplex.GetValues(y1);


                        //                foreach (var y0 in y)
                        //                {
                        //                    Console.Write(y0 + "     ");
                        //                }
                        //                Console.WriteLine();
                        //            }

                        //            Console.WriteLine();
                        //        }
                        //        //Console.WriteLine();
                        //    }
                        //}

                        //if (var4[1] != null)
                        //{
                        //    Console.WriteLine("");
                        //    //Console.WriteLine("Variable z:");
                        //    for (int i = 0; i < var4[1].GetLength(0); i++)
                        //    {
                        //        Console.WriteLine("Variable z; Time: " + (i + 1).ToString());
                        //        foreach (var z2 in var4[1][i])
                        //        {
                        //            foreach (var z1 in z2)
                        //            {

                        //                double[] z = cplex.GetValues(z1);


                        //                foreach (var z0 in z)
                        //                {
                        //                    Console.Write(z0 + "     ");
                        //                }
                        //                Console.WriteLine();

                        //            }
                        //            Console.WriteLine();
                        //        }
                        //        //Console.WriteLine();
                        //    }
                        //}
                        #endregion


                        //if (_ParameterInitialize.strAreaAggregation == _strSmallest)
                        //{
                        //    Console.WriteLine("");
                        //    Console.WriteLine("Variable s:");
                        //    if (var2[0] != null)
                        //    {
                        //        for (int i = 0; i < var2[0].GetLength(0); i++)
                        //        {


                        //            double[] s = cplex.GetValues(var2[0][i]);


                        //            foreach (var s0 in s)
                        //            {
                        //                Console.Write(s0 + "     ");
                        //            }
                        //            Console.WriteLine();

                        //        }
                        //    }
                        //}
                        #endregion

                        #region Display other results
                        //double[] dj = cplex.GetReducedCosts(var3[0][0][0]);
                        //double[] dj2 = cplex.GetReducedCosts((var3);
                        //double[] pi = cplex.GetDuals(rng[0]);
                        //double[] slack = cplex.GetSlacks(rng[0]);
                        //Console.WriteLine("");
                        //cplex.Output().WriteLine("Solution status = "
                        //+ cplex.GetStatus());
                        //cplex.Output().WriteLine("Solution value = "
                        //+ cplex.ObjValue);
                        //objDataLt[13] = cplex.ObjValue;
                        //int nvars = x.Length;
                        //for (int j = 0; j < nvars; ++j)
                        //{
                        //    cplex.Output().WriteLine("Variable :"
                        //    + j
                        //    + " Value = "
                        //    + x[j]
                        //    + " Reduced cost = "
                        //    + dj[j]);
                        //}
                        //int ncons = slack.Length;
                        //for (int i = 0; i < ncons; ++i)
                        //{
                        //    cplex.Output().WriteLine("Constraint:"
                        //    + i
                        //    + " Slack = "
                        //    + slack[i]
                        //    //+ " Pi = "
                        //    //+ pi[i]
                        //    );
                        //}
                        #endregion
                    }

                    Console.WriteLine("");
                    var strStatus = cplex.GetStatus().ToString();
                    Console.WriteLine("Solution status = " + strStatus);

                    if (strStatus == "Optimal")
                    {
                        blnSolved = true;
                        StrObjLtDt.SetLastObj("EstSteps/Gap%", 0.ToString("F4"));  //keep 4 decimal digits
                        StrObjLtDt.SetLastObj("Cost", cplex.ObjValue);
                        Console.WriteLine("Solution value = " + cplex.ObjValue);
                    }
                    else if (strStatus == "Feasible")
                    {
                        //|best integer-best bound(node)|  / 1e-10 + |best integer|
                        //|cplex.ObjValue-cplex.BestObjValue|  /  1e-10 + |cplex.ObjValue|
                        blnSolved = true;
                        StrObjLtDt.SetLastObj("EstSteps/Gap%", (cplex.MIPRelativeGap * 100).ToString("F4"));  //keep 4 decimal digits
                        StrObjLtDt.SetLastObj("Cost", cplex.ObjValue);
                        Console.WriteLine("Solution value = " + cplex.ObjValue);
                    }
                    else //if (strStatus == "Unknown") //we do not find any solution in a time limit
                    {
                        CrgOOTSolveSS.Add(LSCrg);
                        Console.WriteLine("didn't find any solution in the time limit.");
                    }
                }
                else
                {
                    CrgOOTSetSS.Add(LSCrg);
                }
            }
            catch (ILOG.Concert.Exception e)
            {
                if (e.Message == "CPLEX Error  1001: Out of memory.\n")
                {
                    if (blnSetting == false) //this can happen when we are setting up variables and constraints
                    {
                        Console.Write("During Setting: " + e.Message);
                        CrgOOMSetSS.Add(LSCrg);
                    }
                    else
                    {
                        Console.Write("During Solving: " + e.Message);
                        CrgOOMSolveSS.Add(LSCrg);
                    }
                }
                else if (e.Message == "CPLEX Error  3019: Failure to solve MIP subproblem.\n") //this can really happen
                {
                    Console.Write("During Solving: " + e.Message);
                    CrgCplexError3019SS.Add(LSCrg);
                }
                else  //other eroors, e.g., "CPLEX Error  1004: Null pointer for required data.\n"
                {
                    var strError = "ID: " + LSCrg.ID + "  " + "blnSetting == " + blnSetting.ToString() + ";    " + e.Message;
                    Console.Write(strError);
                    strOtherErrors += strError;
                    CrgOtherErrorsSS.Add(LSCrg);
                    //throw;
                }
            }
            catch (System.OutOfMemoryException e2) //this can really happen, though ILOG.Concert.Exception should occur instead
            {
                if (blnSetting == false)
                {
                    Console.WriteLine("During Setting: System exception " + e2.Message);
                    CrgOOMSetSS.Add(LSCrg);
                    //throw;
                }
                else
                {
                    CrgOOMSolveSS.Add(LSCrg);
                    Console.WriteLine("During Solving: System exception " + e2.Message);
                    //throw;
                }
            }
            finally
            {
                double dblMemoryInMB = CHelpFunc.GetConsumedMemoryInMB(false, lngStartMemory);
                if (blnSolved == false)
                {
                    crg.ID = -2;
                    StrObjLtDt.SetLastObj("EstSteps/Gap%", _intNoSolutionEstSteps.ToString("F4"));
                    //StrObjLtDt.SetLastObj("Cost", -1); //the cost value is -1 by default
                    Console.WriteLine("Crg:  ID  " + LSCrg.ID + ";    n  " + LSCrg.GetCphCount() + ";    m  " +
                                      LSCrg.AdjCorrCphsSD.Count + "  could not be solved by ILP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                }
                StrObjLtDt.SetLastObj("Time_L(ms)", pStopwatch.ElapsedMilliseconds);
                StrObjLtDt.SetLastObj("Time(ms)", pStopwatch.ElapsedMilliseconds);
                StrObjLtDt.SetLastObj("Memory(MB)", dblMemoryInMB);

                cplex.End();
            }



            return(crg);
        }