static int Main(string[] args) { int status = 127; const string DATADIR = "../.."; OplFactory oplF = new OplFactory(); try { int capFlour = 20; double best; double curr = double.MaxValue; OplRunConfiguration rc0 = oplF.CreateOplRunConfiguration(DATADIR+"/mulprod.mod",DATADIR+"/mulprod.dat"); OplDataElements dataElements = rc0.GetOplModel().MakeDataElements(); Cplex cplex = oplF.CreateCplex(); do { best = curr; OplRunConfiguration rc = oplF.CreateOplRunConfiguration(rc0.GetOplModel().ModelDefinition,dataElements); rc.Cplex.SetOut(null); rc.GetOplModel().Generate(); Console.Out.WriteLine("Solve with capFlour = " + capFlour); if ( rc.Cplex.Solve() ) { curr = rc.Cplex.ObjValue; Console.Out.WriteLine(); Console.Out.WriteLine( "OBJECTIVE: " +curr); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } capFlour++; // Change the value of Capacity["flour"] in dataElements dataElements.GetElement("Capacity").AsNumMap().Set("flour", capFlour); rc.End(); } while ( best != curr && status == 0); } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } oplF.End(); Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); return status; }
// The purpose of this sample is to output a multidimensional array x[i][j] to illustrate how arrays and subarrays are managed. // To access the elements of an array, you must first access the subarrays up to the last dimension, then you can get the values. // Here, as there are two dimensions, you have to get one subarray from which you can directly get the values. // // The array of integers is indexed by two sets of strings.. // // The simplified model is: // // {string} s1 = ...; // {string} s2 = ...; // {int} x[s1][s2] = ...; // static int sample2() { int status = 127; const string DATADIR = "../.."; OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); try { OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplRunConfiguration rc = oplF.CreateOplRunConfiguration(DATADIR + "/iterators.mod"); OplModel opl = rc.GetOplModel(); opl.Generate(); // Get the x, s1 and s2 elements from the OplModel. IIntMap x = opl.GetElement("x").AsIntMap(); ISymbolSet s1 = opl.GetElement("s1").AsSymbolSet(); ISymbolSet s2 = opl.GetElement("s2").AsSymbolSet(); // Iterate on the first indexer. IEnumerator it1 = s1.GetEnumerator(); while (it1.MoveNext()) { // Get the second dimension array from the first dimension. IIntMap sub = x.GetSub((string)it1.Current); // Iterate on the second indexer of x (that is the indexer of the subarray). IEnumerator it2 = s2.GetEnumerator(); while (it2.MoveNext()) { // This is the last dimension of the array, so you can directly use the get method. Console.Out.WriteLine(it1.Current + " " + it2.Current + " " + sub.Get((string)it2.Current)); } } Console.Out.WriteLine("---------------------"); status = 0; } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 1; } catch (IloException ex) { Console.Out.WriteLine("### exception: " + ex.Message); status = 2; } catch (System.Exception ex) { Console.Out.WriteLine("### UNEXPECTED ERROR ..." + ex.Message); status = 3; } oplF.End(); return(status); }
static int Main(string[] args) { int status = 127; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(); OplSettings settings = oplF.CreateOplSettings(errHandler); Cplex masterCplex = oplF.CreateCplex(); masterCplex.SetOut(null); OplErrorHandler errorHandler = oplF.CreateOplErrorHandler(); OplRunConfiguration masterRC = oplF.CreateOplRunConfiguration(DATADIR + "/cutstock_change.mod", DATADIR + "/cutstock_change.dat"); masterRC.ErrorHandler = errorHandler; masterRC.Cplex = masterCplex; OplModel masterOpl = masterRC.OplModel; masterOpl.Generate(); OplDataElements masterDataElements = masterOpl.MakeDataElements(); OplModelSource subSource = oplF.CreateOplModelSource(DATADIR + "/cutstock-sub.mod"); OplModelDefinition subDef = oplF.CreateOplModelDefinition(subSource, settings); Cplex subCplex = oplF.CreateCplex(); subCplex.SetOut(null); int nWdth = masterDataElements.GetElement("Amount").AsIntMap().Size; ArrayList masterVars = new ArrayList(); INumVarMap cuts = masterOpl.GetElement("Cut").AsNumVarMap(); for (int i = 1; i <= nWdth; i++) { masterVars.Add(cuts.Get(i)); } double best; double curr = double.MaxValue; do { best = curr; // Make master model Console.Out.WriteLine("Solve master."); if (masterCplex.Solve()) { curr = masterCplex.ObjValue; Console.Out.WriteLine("OBJECTIVE: " + curr); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } // set sub model data OplDataElements subDataElements = oplF.CreateOplDataElements(); subDataElements.AddElement(masterDataElements.GetElement("RollWidth")); subDataElements.AddElement(masterDataElements.GetElement("Size")); subDataElements.AddElement(masterDataElements.GetElement("Duals")); // get reduced costs and set them in sub problem INumMap duals = subDataElements.GetElement("Duals").AsNumMap(); for (int i = 1; i <= nWdth; i++) { IForAllRange forAll = (IForAllRange)masterOpl.GetElement("ctFill").AsConstraintMap().Get(i); duals.Set(i, masterCplex.GetDual(forAll)); } // make sub model OplModel subOpl = oplF.CreateOplModel(subDef, subCplex); subOpl.AddDataSource(subDataElements); subOpl.Generate(); Console.Out.WriteLine("Solve sub."); if (subCplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + subCplex.ObjValue); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } if (subCplex.ObjValue > -RC_EPS) { break; } ; // Add variable in master model INumVar newVar = masterCplex.NumVar(0, double.MaxValue); IObjective masterObj = masterOpl.Objective; masterCplex.SetLinearCoef(masterObj, newVar, 1); for (int i = 1; i <= nWdth; i++) { double coef = subCplex.GetValue(subOpl.GetElement("Use").AsIntVarMap().Get(i)); IForAllRange forAll = (IForAllRange)masterOpl.GetElement("ctFill").AsConstraintMap().Get(i); masterCplex.SetLinearCoef(forAll, newVar, coef); } masterVars.Add(newVar); subOpl.End(); } while (best != curr && status == 0); INumVar[] masterVarsA = (INumVar[])masterVars.ToArray(typeof(INumVar)); masterCplex.Add(masterCplex.Conversion(masterVarsA, NumVarType.Int)); if (masterCplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + masterCplex.ObjValue); } oplF.End(); } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); return(status); }
// The purpose of this sample is to check the result of filtering by iterating on the generated data element. // // The data element is an array of strings that is indexed by a set of strings. // It is filled as the result of an iteration on a set of tuples that filters out the duplicates. // It is based on the model used in "Sparsity" run configuration of the "transp" example. // // // The simplified model is: // // {string} Products = ...; // tuple Route { string p; string o; string d; } // {Route} Routes = ...; // {string} orig[p in Products] = { o | <p,o,d> in Routes }; // static int sample1() { int status = 127; const string DATADIR = "../.."; OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); try { OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplRunConfiguration rc = oplF.CreateOplRunConfiguration(DATADIR + "/transp2.mod", DATADIR + "/transp2.dat"); OplModel opl = rc.GetOplModel(); opl.Generate(); Console.Out.WriteLine("Verification of the computation of orig: "); // Get the orig, Routes, Product elements from the OplModel. ISymbolSetMap orig = opl.GetElement("Orig").AsSymbolSetMap(); ITupleSet Routes = opl.GetElement("Routes").AsTupleSet(); ISymbolSet Products = opl.GetElement("Products").AsSymbolSet(); Console.Out.Write("Products = "); for (int j = 0; j <= Products.Size - 1; j++) { Console.Out.Write(Products.GetValue(j) + " "); } Console.Out.WriteLine(); // Iterate through the orig to see the result of the data element filtering. IEnumerator it1 = Products.GetEnumerator(); while (it1.MoveNext()) { string p = ((string)it1.Current); // This is the last dimension of the array (as it is a one-dimensional array), so you can use the get method directly. Console.Out.WriteLine("for p = " + p + " we have " + orig.Get(p)); } Console.Out.WriteLine("---------------------"); // Iterate through the TupleSet. IEnumerator it2 = Routes.GetEnumerator(); while (it2.MoveNext()) { ITuple t = ((ITuple)it2.Current); // Get the string "p" from the tuple. string p = t.GetStringValue("p"); // if "p" is in the indexer, we will try to add the "o" string to the array. if (Products.Contains(p)) { Console.Out.WriteLine("for p = " + p + " we will have " + t.GetStringValue("o") + " from " + t); } } Console.Out.WriteLine("---------------------"); status = 0; } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } oplF.End(); return(status); }
static int Main(string[] args) { int status = 127; const string DATADIR = "../.."; const double RC_EPS = 0.000001; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(); OplSettings settings = oplF.CreateOplSettings(errHandler); // Make master model Cplex masterCplex = oplF.CreateCplex(); masterCplex.SetOut(null); OplRunConfiguration masterRC0 = oplF.CreateOplRunConfiguration(DATADIR + "/cutstock.mod", DATADIR + "/cutstock.dat"); masterRC0.Cplex = masterCplex; OplDataElements masterDataElements = masterRC0.OplModel.MakeDataElements(); // prepare sub model source, definition and engine OplModelSource subSource = oplF.CreateOplModelSource(DATADIR + "/cutstock-sub.mod"); OplModelDefinition subDef = oplF.CreateOplModelDefinition(subSource, settings); Cplex subCplex = oplF.CreateCplex(); subCplex.SetOut(null); const int nbItems = 5; IIntRange items = masterCplex.IntRange(1, 5); double best; double curr = double.MaxValue; do { best = curr; masterCplex.ClearModel(); OplRunConfiguration masterRC = oplF.CreateOplRunConfiguration(masterRC0.OplModel.ModelDefinition, masterDataElements); masterRC.Cplex = masterCplex; masterRC.OplModel.Generate(); Console.Out.WriteLine("Solve master."); if (masterCplex.Solve()) { curr = masterCplex.ObjValue; Console.Out.WriteLine("OBJECTIVE: " + curr); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } // prepare sub model data OplDataElements subDataElements = oplF.CreateOplDataElements(); subDataElements.AddElement(masterDataElements.GetElement("RollWidth")); subDataElements.AddElement(masterDataElements.GetElement("Size")); subDataElements.AddElement(masterDataElements.GetElement("Duals")); // get reduced costs and set them in sub problem INumMap duals = subDataElements.GetElement("Duals").AsNumMap(); for (int i = 1; i <= nbItems; i++) { IForAllRange forAll = (IForAllRange)masterRC.OplModel.GetElement("ctFill").AsConstraintMap().Get(i); duals.Set(i, masterCplex.GetDual(forAll)); } //make sub model OplModel subOpl = oplF.CreateOplModel(subDef, subCplex); subOpl.AddDataSource(subDataElements); subOpl.Generate(); Console.Out.WriteLine("Solve sub."); if (subCplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + subCplex.ObjValue); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } if (subCplex.ObjValue > -RC_EPS) { break; } // Add variable in master model IIntMap newFill = masterCplex.IntMap(items); for (int i = 1; i <= nbItems; i++) { int coef = (int)subCplex.GetValue(subOpl.GetElement("Use").AsIntVarMap().Get(i)); newFill.Set(i, coef); } ITupleBuffer buf = masterDataElements.GetElement("Patterns").AsTupleSet().MakeTupleBuffer(-1); buf.SetIntValue("id", masterDataElements.GetElement("Patterns").AsTupleSet().Size); buf.SetIntValue("cost", 1); buf.SetIntMapValue("fill", newFill); buf.Commit(); subOpl.End(); masterRC.End(); } while (best != curr && status == 0); oplF.End(); } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); return(status); }
static int Main(string[] args) { int status = 127; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(); OplSettings settings = oplF.CreateOplSettings(errHandler); Cplex masterCplex = oplF.CreateCplex(); masterCplex.SetOut(null); OplErrorHandler errorHandler = oplF.CreateOplErrorHandler(); OplRunConfiguration masterRC = oplF.CreateOplRunConfiguration(DATADIR + "/cutstock_change.mod", DATADIR + "/cutstock_change.dat"); masterRC.ErrorHandler = errorHandler; masterRC.Cplex = masterCplex; OplModel masterOpl = masterRC.OplModel; masterOpl.Generate(); OplDataElements masterDataElements = masterOpl.MakeDataElements(); OplModelSource subSource = oplF.CreateOplModelSource(DATADIR + "/cutstock-sub.mod"); OplModelDefinition subDef = oplF.CreateOplModelDefinition(subSource, settings); Cplex subCplex = oplF.CreateCplex(); subCplex.SetOut(null); int nWdth = masterDataElements.GetElement("Amount").AsIntMap().Size; ArrayList masterVars = new ArrayList(); INumVarMap cuts = masterOpl.GetElement("Cut").AsNumVarMap(); for (int i = 1; i <= nWdth; i++) { masterVars.Add(cuts.Get(i)); } double best; double curr = double.MaxValue; do { best = curr; // Make master model Console.Out.WriteLine("Solve master."); if (masterCplex.Solve()) { curr = masterCplex.ObjValue; Console.Out.WriteLine("OBJECTIVE: " + curr); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } // set sub model data OplDataElements subDataElements = oplF.CreateOplDataElements(); subDataElements.AddElement(masterDataElements.GetElement("RollWidth")); subDataElements.AddElement(masterDataElements.GetElement("Size")); subDataElements.AddElement(masterDataElements.GetElement("Duals")); // get reduced costs and set them in sub problem INumMap duals = subDataElements.GetElement("Duals").AsNumMap(); for (int i = 1; i <= nWdth; i++) { IForAllRange forAll = (IForAllRange)masterOpl.GetElement("ctFill").AsConstraintMap().Get(i); duals.Set(i, masterCplex.GetDual(forAll)); } // make sub model OplModel subOpl = oplF.CreateOplModel(subDef, subCplex); subOpl.AddDataSource(subDataElements); subOpl.Generate(); Console.Out.WriteLine("Solve sub."); if (subCplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + subCplex.ObjValue); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } if (subCplex.ObjValue > -RC_EPS) { break; }; // Add variable in master model INumVar newVar = masterCplex.NumVar(0, double.MaxValue); IObjective masterObj = masterOpl.Objective; masterCplex.SetLinearCoef(masterObj, newVar, 1); for (int i = 1; i <= nWdth; i++) { double coef = subCplex.GetValue(subOpl.GetElement("Use").AsIntVarMap().Get(i)); IForAllRange forAll = (IForAllRange)masterOpl.GetElement("ctFill").AsConstraintMap().Get(i); masterCplex.SetLinearCoef(forAll, newVar, coef); } masterVars.Add(newVar); subOpl.End(); } while (best != curr && status == 0); INumVar[] masterVarsA = (INumVar[])masterVars.ToArray(typeof(INumVar)); masterCplex.Add(masterCplex.Conversion(masterVarsA, NumVarType.Int)); if (masterCplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + masterCplex.ObjValue); } oplF.End(); } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); return status; }
int Run() { int status = 127; OplFactory oplF = new OplFactory(); if (_cl.CompileName != null) { OplCompiler compiler = oplF.CreateOplCompiler(); StreamWriter ofs = new StreamWriter(_cl.CompileName, false); OplModelSource modelSource = oplF.CreateOplModelSource(_cl.ModelFileName); compiler.Compile(modelSource, ofs); ofs.Close(); Trace("Compile"); return 0; } if (_cl.ModelFileName == null && !_cl.IsProject) return 0; Trace("initial"); OplRunConfiguration rc; OplErrorHandler errHandler = oplF.CreateOplErrorHandler(); if (_cl.IsProject) { OplProject prj = oplF.CreateOplProject(_cl.getProjectPath()); rc = prj.MakeRunConfiguration(_cl.getRunConfigurationName()); } else { if (_cl.DataFileNames.Length == 0) rc = oplF.CreateOplRunConfiguration(_cl.ModelFileName); else rc = oplF.CreateOplRunConfiguration(_cl.ModelFileName, _cl.DataFileNames); } rc.ErrorHandler = errHandler; OplModel opl = rc.OplModel; OplSettings settings = opl.Settings; settings.IsWithLocations = true; settings.IsWithNames = true; settings.IsForceElementUsage = _cl.IsForceElementUsage; status = 9; if (opl.ModelDefinition.hasMain()) { status = opl.Main(); Console.Out.WriteLine("main returns " + status); Trace("main"); } else if (errHandler.Ok) { opl.Generate(); Trace("generate model"); if (opl.HasCplex) { if (_cl.ExportName != null) { opl.Cplex.ExportModel(_cl.ExportName); Trace("export model " + _cl.ExportName); } if (_cl.IsRelaxation) { Console.Out.WriteLine("RELAXATIONS to obtain a feasible problem: "); opl.PrintRelaxation(Console.Out); Console.Out.WriteLine("RELAXATIONS done."); } if (_cl.IsConflict) { Console.Out.WriteLine("CONFLICT in the infeasible problem: "); opl.PrintConflict(Console.Out); Console.Out.WriteLine("CONFLICT done."); } if (!_cl.IsRelaxation && !_cl.IsConflict) { bool result = false; try { result = opl.Cplex.Solve(); } catch (IloException ex) { Console.Out.WriteLine("### ENGINE exception: " + ex.Message); } if (result) { Trace("solve"); Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue.ToString("F")); opl.PostProcess(); Trace("post process"); if (_cl.IsVerbose) opl.PrintSolution(Console.Out); status = 0; } else { Trace("no solution"); status = 1; } } } else {//opl.hasCP() bool result = false; try { result = opl.CP.Solve(); } catch (IloException ex) { Console.Out.WriteLine("### Engine exception: " + ex.Message); } if (result) { Trace("solve"); if (opl.CP.HasObjective()) { Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.CP.ObjValue.ToString("F")); } else { Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: no objective"); } opl.PostProcess(); Trace("post process"); if (_cl.IsVerbose) opl.PrintSolution(Console.Out); status = 0; } else { Trace("no solution"); status = 1; } } } if (_cl.ExternalDataName != null) { StreamWriter ofs = new StreamWriter(_cl.ExternalDataName, false); opl.PrintExternalData(ofs); ofs.Close(); Trace("write external data " + _cl.ExternalDataName); } if (_cl.InternalDataName != null) { StreamWriter ofs = new StreamWriter(_cl.InternalDataName, false); opl.PrintInternalData(ofs); ofs.Close(); Trace("write internal data " + _cl.InternalDataName); } Trace("done"); return status; }
static int Main(string[] args) { int status = 127; const string DATADIR = "../.."; OplFactory oplF = new OplFactory(); try { int capFlour = 20; double best; double curr = double.MaxValue; OplRunConfiguration rc0 = oplF.CreateOplRunConfiguration(DATADIR + "/mulprod.mod", DATADIR + "/mulprod.dat"); OplDataElements dataElements = rc0.GetOplModel().MakeDataElements(); Cplex cplex = oplF.CreateCplex(); do { best = curr; OplRunConfiguration rc = oplF.CreateOplRunConfiguration(rc0.GetOplModel().ModelDefinition, dataElements); rc.Cplex.SetOut(null); rc.GetOplModel().Generate(); Console.Out.WriteLine("Solve with capFlour = " + capFlour); if (rc.Cplex.Solve()) { curr = rc.Cplex.ObjValue; Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + curr); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } capFlour++; // Change the value of Capacity["flour"] in dataElements dataElements.GetElement("Capacity").AsNumMap().Set("flour", capFlour); rc.End(); } while (best != curr && status == 0); } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } oplF.End(); Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); return(status); }
static int Main(string[] args) { int status = 127; const string DATADIR = "../.."; const double RC_EPS = 0.000001; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(); OplSettings settings = oplF.CreateOplSettings(errHandler); // Make master model Cplex masterCplex = oplF.CreateCplex(); masterCplex.SetOut(null); OplRunConfiguration masterRC0 = oplF.CreateOplRunConfiguration(DATADIR + "/cutstock.mod", DATADIR + "/cutstock.dat"); masterRC0.Cplex = masterCplex; OplDataElements masterDataElements = masterRC0.OplModel.MakeDataElements(); // prepare sub model source, definition and engine OplModelSource subSource = oplF.CreateOplModelSource(DATADIR + "/cutstock-sub.mod"); OplModelDefinition subDef = oplF.CreateOplModelDefinition(subSource, settings); Cplex subCplex = oplF.CreateCplex(); subCplex.SetOut(null); const int nbItems = 5; IIntRange items = masterCplex.IntRange(1, 5); double best; double curr = double.MaxValue; do { best = curr; masterCplex.ClearModel(); OplRunConfiguration masterRC = oplF.CreateOplRunConfiguration(masterRC0.OplModel.ModelDefinition, masterDataElements); masterRC.Cplex = masterCplex; masterRC.OplModel.Generate(); Console.Out.WriteLine("Solve master."); if (masterCplex.Solve()) { curr = masterCplex.ObjValue; Console.Out.WriteLine("OBJECTIVE: " + curr); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } // prepare sub model data OplDataElements subDataElements = oplF.CreateOplDataElements(); subDataElements.AddElement(masterDataElements.GetElement("RollWidth")); subDataElements.AddElement(masterDataElements.GetElement("Size")); subDataElements.AddElement(masterDataElements.GetElement("Duals")); // get reduced costs and set them in sub problem INumMap duals = subDataElements.GetElement("Duals").AsNumMap(); for (int i = 1; i <= nbItems; i++) { IForAllRange forAll = (IForAllRange)masterRC.OplModel.GetElement("ctFill").AsConstraintMap().Get(i); duals.Set(i, masterCplex.GetDual(forAll)); } //make sub model OplModel subOpl = oplF.CreateOplModel(subDef, subCplex); subOpl.AddDataSource(subDataElements); subOpl.Generate(); Console.Out.WriteLine("Solve sub."); if (subCplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + subCplex.ObjValue); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } if (subCplex.ObjValue > -RC_EPS) { break; } // Add variable in master model IIntMap newFill = masterCplex.IntMap(items); for (int i = 1; i <= nbItems; i++) { int coef = (int)subCplex.GetValue(subOpl.GetElement("Use").AsIntVarMap().Get(i)); newFill.Set(i, coef); } ITupleBuffer buf = masterDataElements.GetElement("Patterns").AsTupleSet().MakeTupleBuffer(-1); buf.SetIntValue("id", masterDataElements.GetElement("Patterns").AsTupleSet().Size); buf.SetIntValue("cost", 1); buf.SetIntMapValue("fill", newFill); buf.Commit(); subOpl.End(); masterRC.End(); } while (best != curr && status == 0); oplF.End(); } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex.Message); status = 4; } Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); return status; }
int Run() { int status = 127; OplFactory oplF = new OplFactory(); if (_cl.CompileName != null) { OplCompiler compiler = oplF.CreateOplCompiler(); StreamWriter ofs = new StreamWriter(_cl.CompileName, false); OplModelSource modelSource = oplF.CreateOplModelSource(_cl.ModelFileName); compiler.Compile(modelSource, ofs); ofs.Close(); Trace("Compile"); return(0); } if (_cl.ModelFileName == null && !_cl.IsProject) { return(0); } Trace("initial"); OplRunConfiguration rc; OplErrorHandler errHandler = oplF.CreateOplErrorHandler(); if (_cl.IsProject) { OplProject prj = oplF.CreateOplProject(_cl.getProjectPath()); rc = prj.MakeRunConfiguration(_cl.getRunConfigurationName()); } else { if (_cl.DataFileNames.Length == 0) { rc = oplF.CreateOplRunConfiguration(_cl.ModelFileName); } else { rc = oplF.CreateOplRunConfiguration(_cl.ModelFileName, _cl.DataFileNames); } } rc.ErrorHandler = errHandler; OplModel opl = rc.OplModel; OplSettings settings = opl.Settings; settings.IsWithLocations = true; settings.IsWithNames = true; settings.IsForceElementUsage = _cl.IsForceElementUsage; status = 9; if (opl.ModelDefinition.hasMain()) { status = opl.Main(); Console.Out.WriteLine("main returns " + status); Trace("main"); } else if (errHandler.Ok) { opl.Generate(); Trace("generate model"); if (opl.HasCplex) { if (_cl.ExportName != null) { opl.Cplex.ExportModel(_cl.ExportName); Trace("export model " + _cl.ExportName); } if (_cl.IsRelaxation) { Console.Out.WriteLine("RELAXATIONS to obtain a feasible problem: "); opl.PrintRelaxation(Console.Out); Console.Out.WriteLine("RELAXATIONS done."); } if (_cl.IsConflict) { Console.Out.WriteLine("CONFLICT in the infeasible problem: "); opl.PrintConflict(Console.Out); Console.Out.WriteLine("CONFLICT done."); } if (!_cl.IsRelaxation && !_cl.IsConflict) { bool result = false; try { result = opl.Cplex.Solve(); } catch (IloException ex) { Console.Out.WriteLine("### ENGINE exception: " + ex.Message); } if (result) { Trace("solve"); Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue.ToString("F")); opl.PostProcess(); Trace("post process"); if (_cl.IsVerbose) { opl.PrintSolution(Console.Out); } status = 0; } else { Trace("no solution"); status = 1; } } } else {//opl.hasCP() bool result = false; try { result = opl.CP.Solve(); } catch (IloException ex) { Console.Out.WriteLine("### Engine exception: " + ex.Message); } if (result) { Trace("solve"); if (opl.CP.HasObjective()) { Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.CP.ObjValue.ToString("F")); } else { Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: no objective"); } opl.PostProcess(); Trace("post process"); if (_cl.IsVerbose) { opl.PrintSolution(Console.Out); } status = 0; } else { Trace("no solution"); status = 1; } } } if (_cl.ExternalDataName != null) { StreamWriter ofs = new StreamWriter(_cl.ExternalDataName, false); opl.PrintExternalData(ofs); ofs.Close(); Trace("write external data " + _cl.ExternalDataName); } if (_cl.InternalDataName != null) { StreamWriter ofs = new StreamWriter(_cl.InternalDataName, false); opl.PrintInternalData(ofs); ofs.Close(); Trace("write internal data " + _cl.InternalDataName); } Trace("done"); return(status); }
static void Main(string[] args) { // Assume we run in bin/Debug or bin/Release const string DATADIR = "../.."; int status = 127; CommandLineParser parser = new CommandLineParser(); // set some default values for the sample parser.modelFileName = DATADIR + "/data/oil.mod"; parser.configurationFileName = DATADIR + "/data/db_mssql.xml"; parser.Parse(args); // set default datafilename if none specified if (parser.dataFileNames.Count == 0) { parser.dataFileNames.Add(DATADIR + "/data/oil.dat"); } // parse the data source configuration CustomDataSourceConfiguration configuration = new CustomDataSourceConfiguration(parser.configurationFileName); // if this is the default sample, write some default data if (parser.CreateSampleDatabase) { CreateDefaultSampleDatabase(configuration); } try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplRunConfiguration rc = null; if (parser.dataFileNames.Count == 0) { rc = oplF.CreateOplRunConfiguration(parser.modelFileName); } else { rc = oplF.CreateOplRunConfiguration(parser.modelFileName, (String[])parser.dataFileNames.ToArray(typeof(String))); } OplModel opl = rc.GetOplModel(); OplModelDefinition def = opl.ModelDefinition; OplDataSource dataSource = new SqlCustomDataSource(oplF, def, configuration); opl.AddDataSource(dataSource); opl.Generate(); if (parser.externalDataName != null) { using (TextWriter wt = File.CreateText(parser.externalDataName)) opl.PrintExternalData(wt); } bool success = opl.HasCplex ? opl.Cplex.Solve() : opl.CP.Solve(); if (success) { opl.PostProcess(); // Write results to result table Console.WriteLine("Writing results"); SqlWriter writer = new SqlWriter(configuration, opl); writer.WriteResults(); } oplF.End(); status = 0; } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 2; } catch (ILOG.Concert.Exception ex) { Console.WriteLine(ex.Message); status = 3; } catch (System.Exception ex) { Console.WriteLine(ex); Console.WriteLine(ex.Message); status = 4; } Environment.ExitCode = status; Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); }