static int Main(string[] args) { int status = 127; const string DATADIR = "../.."; try { OplFactory oplF = new OplFactory(); OplErrorHandler handler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSource(DATADIR + "/lifegameip.mod"); OplSettings settings = oplF.CreateOplSettings(handler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); Cplex cplex = oplF.CreateCplex(); cplex.ReadVMConfig(DATADIR + "/process.vmc"); OplModel opl = oplF.CreateOplModel(def, cplex); opl.Generate(); cplex.Solve(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue); opl.PostProcess(); opl.PrintSolution(Console.Out); if (cplex.HasVMConfig()) Console.Out.WriteLine("cplex has a VM config"); else throw new IloException("cplex does not have a VM config"); cplex.DelVMConfig(); if (cplex.HasVMConfig()) throw new IloException("cplex still has a VM config"); else Console.Out.WriteLine("cplex VM config was removed"); 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 void Main(string[] args) { int status = 127; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSourceFromString(GetModelText(), "carseq"); OplSettings settings = oplF.CreateOplSettings(errHandler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); CP cp = oplF.CreateCP(); OplModel opl = oplF.CreateOplModel(def, cp); OplDataSource dataSource = new MyData(oplF); opl.AddDataSource(dataSource); opl.Generate(); if (cp.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + opl.CP.ObjValue); opl.PostProcess(); opl.PrintSolution(Console.Out); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } 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; } Environment.ExitCode = status; Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); }
static int Main(string[] args) { int status = 127; const string DATADIR = "../.."; try { OplFactory oplF = new OplFactory(); OplErrorHandler handler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSource(DATADIR + "/mulprod.mod"); OplSettings settings = oplF.CreateOplSettings(handler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); Cplex cplex = oplF.CreateCplex(); OplModel opl = oplF.CreateOplModel(def, cplex); OplDataSource dataSource = oplF.CreateOplDataSource(DATADIR + "/mulprod.dat"); opl.AddDataSource(dataSource); opl.Generate(); if (cplex.Solve()) { Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue); opl.PostProcess(); opl.PrintSolution(Console.Out); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } 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 sample3() { int status = 0; OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); try { OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplSettings settings = oplF.CreateOplSettings(errHandler); OplModelSource src = oplF.CreateOplModelSourceFromString(getModelTextSample3(), "tuple array iterator"); OplModelDefinition def = oplF.CreateOplModelDefinition(src, settings); Cplex cplex = oplF.CreateCplex(); OplModel opl = oplF.CreateOplModel(def, cplex); opl.Generate(); // get the string set used to index the array of tuples ITupleMap arrayT = opl.GetElement("arrayT").AsTupleMap(); ISymbolSet ids = opl.GetElement("ids").AsSymbolSet(); // iterate on the index set to retrieve the tuples stored in the array IEnumerator it = ids.GetEnumerator(); while (it.MoveNext()) { Console.Out.Write("arrayT[" + it.Current + "] = "); IMapIndexArray id = oplF.MapIndexArray(0); id.Add(it.Current.ToString()); ITuple t = arrayT.MakeTuple(); arrayT.GetAt(id, t); Console.Out.WriteLine(t); } } catch (ILOG.OPL.OplException ex) { Console.WriteLine(ex.Message); status = 1; } catch (IloException e) { status = 2; Console.Out.WriteLine("### exception: " + e.Message); } catch (System.Exception ex) { status = 3; Console.Out.WriteLine("### UNEXPECTED ERROR ..." + ex.Message); } oplF.End(); return(status); }
static void Main(string[] args) { int status = 127; const string DATADIR = "../.."; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSource(DATADIR + "/customDataSource.mod"); OplSettings settings = oplF.CreateOplSettings(errHandler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); Cplex cplex = oplF.CreateCplex(); OplModel opl = oplF.CreateOplModel(def, cplex); OplDataSource dataSource = new MyCustomDataSource(oplF); opl.AddDataSource(dataSource); opl.Generate(); 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.Message); status = 4; } Environment.ExitCode = status; Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); }
static int Main(string[] args) { int status = 0; try { OplFactory oplF = new OplFactory(); OplErrorHandler handler = oplF.CreateOplErrorHandler(Console.Out); //OplModelSource modelSource = oplF.CreateOplModelSource(DATADIR + "/Modelo3.mod"); OplModelSource modelSource; OplDataSource dataSource; if (args[0].Contains("1")) { modelSource = oplF.CreateOplModelSource("C://Users//Richard Sobreiro//Desktop//PFCCodigos//Backend//VRPTW_Server//Ceplex//MultipleVehicleRoutingProblem.mod"); dataSource = oplF.CreateOplDataSource("C://Users//Richard Sobreiro//Desktop//PFCCodigos//Backend//VRPTW_Server//Ceplex//MultipleVehicleRoutingProblem.dat"); } else { modelSource = oplF.CreateOplModelSource("C://Users//Richard Sobreiro//Desktop//PFCCodigos//Backend//VRPTW_Server//Ceplex//VRP.mod"); dataSource = oplF.CreateOplDataSource("C://Users//Richard Sobreiro//Desktop//PFCCodigos//Backend//VRPTW_Server//Ceplex//VRP.dat"); } OplSettings settings = oplF.CreateOplSettings(handler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); Cplex cplex = oplF.CreateCplex(); OplModel opl = oplF.CreateOplModel(def, cplex); opl.AddDataSource(dataSource); opl.Generate(); if (cplex.Solve()) { Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue); opl.PostProcess(); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } oplF.End(); } catch (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); }
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; const string DATADIR = "../.."; try { OplFactory oplF = new OplFactory(); OplErrorHandler handler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSource(DATADIR + "/lifegameip.mod"); OplSettings settings = oplF.CreateOplSettings(handler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); Cplex cplex = oplF.CreateCplex(); cplex.ReadVMConfig(DATADIR + "/process.vmc"); OplModel opl = oplF.CreateOplModel(def, cplex); opl.Generate(); cplex.Solve(); Console.Out.WriteLine(); Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue); opl.PostProcess(); opl.PrintSolution(Console.Out); if (cplex.HasVMConfig()) { Console.Out.WriteLine("cplex has a VM config"); } else { throw new IloException("cplex does not have a VM config"); } cplex.DelVMConfig(); if (cplex.HasVMConfig()) { throw new IloException("cplex still has a VM config"); } else { Console.Out.WriteLine("cplex VM config was removed"); } 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; }
static void Main(string[] args) { int status = 127; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSourceFromString(GetModelText(), "carseq"); OplSettings settings = oplF.CreateOplSettings(errHandler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); CP cp = oplF.CreateCP(); OplModel opl = oplF.CreateOplModel(def, cp); OplDataSource dataSource = new MyData(oplF); opl.AddDataSource(dataSource); opl.Generate(); if (cp.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + opl.CP.ObjValue); opl.PostProcess(); opl.PrintSolution(Console.Out); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } 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; } Environment.ExitCode=status; Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); }
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 void Main(string[] args) { int nbWarehouses = -1; int nbStores = -1; int fixedP = -1; int disaggregate = -1; for (int i = 0; i < args.Length; i++) { if ("-h".Equals(args[i])) { usage(); } else if ("fixed".Equals(args[i])) { if (i == args.Length) { usage(); } fixedP = System.Int32.Parse(args[++i]); } else if ("nbWarehouses".Equals(args[i])) { if (i == args.Length) { usage(); } nbWarehouses = System.Int32.Parse(args[++i]); } else if ("nbStores".Equals(args[i])) { if (i == args.Length) { usage(); } nbStores = System.Int32.Parse(args[++i]); } else if ("disaggregate".Equals(args[i])) { if (i == args.Length) { usage(); } disaggregate = System.Int32.Parse(args[++i]); } else { break; } } if (fixedP == -1 || nbWarehouses == -1 || nbStores == -1 || disaggregate == -1) { usage(); } int status = 127; try { OplFactory.DebugMode = true; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSourceFromString(GetModelText(), "warehouse"); OplSettings settings = oplF.CreateOplSettings(errHandler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); Cplex cplex = oplF.CreateCplex(); OplModel opl = oplF.CreateOplModel(def, cplex); OplDataSource dataSource = new MyParams(oplF, nbWarehouses, nbStores, fixedP, disaggregate); opl.AddDataSource(dataSource); opl.Generate(); if (cplex.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue); opl.PostProcess(); opl.PrintSolution(Console.Out); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; } 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; } Environment.ExitCode = status; Console.WriteLine("--Press <Enter> to exit--"); Console.ReadLine(); }
public void RescheduleLotMachineAllocation() { // Clear scheduled lots foreach (Machine machine in LithographyArea.Machines) { ScheduledLotsPerMachine[machine.Name].Clear(); } // Set weight of each Lot for (int j = 0; j < Queue.Length; j++) { Lot peekLot = Queue.PeekAt(j); Queue.PeekAt(j).WeightDueDate = GetDueDateWeight(peekLot); Queue.PeekAt(j).WeightWIPBalance = GetWIPBalanceWeight(peekLot); } int status = 127; string solutionAsString = ""; try { OplFactory.DebugMode = false; OplFactory oplF = new OplFactory(); OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out); OplModelSource modelSource = oplF.CreateOplModelSource($"{Directory.GetCurrentDirectory()}/Input/LithoFinal.mod"); OplSettings settings = oplF.CreateOplSettings(errHandler); OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings); // Change stream to be able to read solution as a string (probably not the correct way of doing) StringWriter strWriter = new StringWriter(); oplF.SetOut(strWriter); CP cp = oplF.CreateCP(); OplModel opl = oplF.CreateOplModel(def, cp); OplDataSource dataSource = new MyCustomDataSource(oplF, Queue, LithographyArea, this); opl.AddDataSource(dataSource); opl.Generate(); if (cp.Solve()) { Console.Out.WriteLine("OBJECTIVE: " + opl.CP.ObjValue); opl.PostProcess(); // Get solution as string solutionAsString = strWriter.ToString(); status = 0; } else { Console.Out.WriteLine("No solution!"); status = 1; this.LithographyArea.HandleDispatcherError(); ScheduleEndEvent(GetTime); return; } oplF.End(); } catch (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.WriteLine(status); //Console.ReadLine(); //Console.WriteLine(solutionAsString); //Console.ReadLine(); ChangeLotMachineAllocation(solutionAsString); Console.WriteLine($"Scheduled for {LithographyArea.StartDate.AddSeconds(GetTime)}"); }