private static void SolveWarehouse(GAMSWorkspace gmsWS, int NumberOfWarehouses, GAMSDatabase result, Object dbMutex) { // instantiate GAMSOptions and define some scalars GAMSOptions gmsOpt = gmsWS.AddOptions(); gmsOpt.AllModelTypes = "Gurobi"; gmsOpt.Defines.Add("Warehouse", NumberOfWarehouses.ToString()); gmsOpt.Defines.Add("Store", "65"); gmsOpt.Defines.Add("fixed", "22"); gmsOpt.Defines.Add("disaggregate", "0"); gmsOpt.OptCR = 0.0; // Solve to optimality // create a GAMSJob from string and write results to the result database GAMSJob gmsJ = gmsWS.AddJobFromString(GetModelText()); gmsJ.Run(gmsOpt); // need to lock database write operations lock (dbMutex) result.GetParameter("objrep").AddRecord(NumberOfWarehouses.ToString()).Value = gmsJ.OutDB.GetVariable("obj").FindRecord().Level; foreach (GAMSVariableRecord supplyRec in gmsJ.OutDB.GetVariable("supply")) { if (supplyRec.Level > 0.5) { lock (dbMutex) result.GetSet("supplyMap").AddRecord(NumberOfWarehouses.ToString(), supplyRec.Keys[0], supplyRec.Keys[1]); } } }
private Node[] ReadNodes(GAMSDatabase gdxFile) { var n = gdxFile.GetSet(_str.NodeSet); var s = gdxFile.GetParameter(_str.StartTimes); var e = gdxFile.GetParameter(_str.EndTimes); Node[] nodes = new Node[n.NumberRecords]; double v = 0; double w = 0; int c = 0; foreach (GAMSSetRecord r in n) { var startTime = s.FindRecord(r.Keys); var endTime = s.FindRecord(r.Keys); v = (startTime != null) ? startTime.Value : 0.0; w = (endTime != null) ? endTime.Value : 0.0; ResourceConstraint[] constraint = new ResourceConstraint[] { new ResourceConstraint(v, w) }; nodes[c] = new Node(c, r.Keys[0], constraint, c, c); c++; } return(nodes); }
private Arc[] GetArcs(GAMSDatabase gdxFile, Node[] nodes) { Random rand = new Random(); //for debug run GAMSParameter par = gdxFile.GetParameter(_str.FzMatrix); Arc[] arcs = new Arc[par.NumberRecords]; int c = 0; var nLookup = nodes.ToDictionary(n => n.Label); foreach (GAMSParameterRecord rec in par) { var n1 = nLookup[rec.Keys[0]]; var n2 = nLookup[rec.Keys[1]]; var v = rec.Value; //debug run with random dual Arc a = new Arc(c, n1, n2, v, v - rand.Next(0, 100), new double[] { v }); arcs[c] = a; c++; } return(arcs); }
static void Main(string[] args) { GAMSWorkspace ws; if (Environment.GetCommandLineArgs().Length > 1) { ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]); } else { ws = new GAMSWorkspace(); } GAMSCheckpoint cp = ws.AddCheckpoint(); // initialize a GAMSCheckpoint by running a GAMSJob GAMSJob t12 = ws.AddJobFromString(GetModelText()); t12.Run(cp); // create a GAMSModelInstance and solve it multiple times with different scalar bmult GAMSModelInstance mi = cp.AddModelInstance(); double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 }; GAMSDatabase db = ws.AddDatabase(); GAMSSet scen = db.AddSet("scen", 1, ""); GAMSParameter bmult = db.AddParameter("bmultlist", 1, ""); GAMSParameter zscen = db.AddParameter("zscen", 1, ""); int i = 0; foreach (double b in bmultlist) { bmult.AddRecord("s" + i).Value = b; scen.AddRecord("s" + i++); } GAMSSet dict = db.AddSet("dict", 3, ""); dict.AddRecord(scen.Name, "scenario", ""); dict.AddRecord("bmult", "param", bmult.Name); dict.AddRecord("z", "level", zscen.Name); GUSSCall(dict, mi, "transport us lp min z"); foreach (GAMSParameterRecord rec in db.GetParameter(zscen.Name)) { Console.WriteLine(rec.Keys[0] + " obj: " + rec.Value); } //******************* GAMSModelInstance mi2 = cp.AddModelInstance(); GAMSDatabase db2 = ws.AddDatabase(); GAMSSet scen2 = db2.AddSet("scen", 1, ""); GAMSParameter zscen2 = db2.AddParameter("zscen", 1, ""); GAMSParameter xup = db2.AddParameter("xup", 3, ""); for (int j = 0; j < 4; j++) { foreach (GAMSSetRecord irec in t12.OutDB.GetSet("i")) { foreach (GAMSSetRecord jrec in t12.OutDB.GetSet("j")) { xup.AddRecord("s" + j, irec.Keys[0], jrec.Keys[0]).Value = j + 1; } } scen2.AddRecord("s" + j); } GAMSSet dict2 = db2.AddSet("dict", 3, ""); dict2.AddRecord(scen2.Name, "scenario", ""); dict2.AddRecord("x", "lower", xup.Name); dict2.AddRecord("z", "level", zscen2.Name); GUSSCall(dict2, mi2, "transport us lp min z", output: Console.Out); foreach (GAMSParameterRecord rec in db2.GetParameter(zscen2.Name)) { Console.WriteLine(rec.Keys[0] + " obj: " + rec.Value); } }
private static void SolveWarehouse(GAMSWorkspace gmsWS, int NumberOfWarehouses, GAMSDatabase result, Object dbMutex) { // instantiate GAMSOptions and define some scalars GAMSOptions gmsOpt = gmsWS.AddOptions(); gmsOpt.AllModelTypes = "Gurobi"; gmsOpt.Defines.Add("Warehouse", NumberOfWarehouses.ToString()); gmsOpt.Defines.Add("Store", "65"); gmsOpt.Defines.Add("fixed", "22"); gmsOpt.Defines.Add("disaggregate", "0"); gmsOpt.OptCR = 0.0; // Solve to optimality // create a GAMSJob from string and write results to the result database GAMSJob gmsJ = gmsWS.AddJobFromString(GetModelText()); gmsJ.Run(gmsOpt); // need to lock database write operations lock (dbMutex) result.GetParameter("objrep").AddRecord(NumberOfWarehouses.ToString()).Value = gmsJ.OutDB.GetVariable("obj").FindRecord().Level; foreach (GAMSVariableRecord supplyRec in gmsJ.OutDB.GetVariable("supply")) if (supplyRec.Level > 0.5) lock(dbMutex) result.GetSet("supplyMap").AddRecord(NumberOfWarehouses.ToString(), supplyRec.Keys[0], supplyRec.Keys[1]); }