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