コード例 #1
0
ファイル: Benders2Stage.cs プロジェクト: nherazo1/Dutties
        static void Main(string[] args)
        {
            GAMSWorkspace ws;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
            }
            else
            {
                ws = new GAMSWorkspace();
            }
            GAMSJob data = ws.AddJobFromString(GetDataText());

            GAMSOptions optData = ws.AddOptions();

            optData.Defines.Add("useBig", "1");
            optData.Defines.Add("nrScen", "100");

            data.Run(optData);

            optData.Dispose();
            GAMSParameter scenarioData = data.OutDB.GetParameter("ScenarioData");

            GAMSOptions opt = ws.AddOptions();

            opt.Defines.Add("datain", data.OutDB.Name);
            int maxiter = 40;

            opt.Defines.Add("maxiter", maxiter.ToString());
            opt.AllModelTypes = "cplexd";

            GAMSCheckpoint cpMaster = ws.AddCheckpoint();
            GAMSCheckpoint cpSub    = ws.AddCheckpoint();

            ws.AddJobFromString(GetMasterText()).Run(opt, cpMaster, data.OutDB);

            GAMSModelInstance masteri  = cpMaster.AddModelInstance();
            GAMSParameter     cutconst = masteri.SyncDB.AddParameter("cutconst", 1, "Benders optimality cut constant");
            GAMSParameter     cutcoeff = masteri.SyncDB.AddParameter("cutcoeff", 2, "Benders optimality coefficients");
            GAMSVariable      theta    = masteri.SyncDB.AddVariable("theta", 0, VarType.Free, "Future profit function variable");
            GAMSParameter     thetaFix = masteri.SyncDB.AddParameter("thetaFix", 0, "");

            masteri.Instantiate("masterproblem max zmaster using lp", opt, new GAMSModifier(cutconst), new GAMSModifier(cutcoeff), new GAMSModifier(theta, UpdateAction.Fixed, thetaFix));

            ws.AddJobFromString(GetSubText()).Run(opt, cpSub, data.OutDB);

            GAMSModelInstance subi     = cpSub.AddModelInstance();
            GAMSParameter     received = subi.SyncDB.AddParameter("received", 1, "units received from master");
            GAMSParameter     demand   = subi.SyncDB.AddParameter("demand", 1, "stochastic demand");

            subi.Instantiate("subproblem max zsub using lp", opt, new GAMSModifier(received), new GAMSModifier(demand));

            opt.Dispose();

            double lowerbound = double.NegativeInfinity, upperbound = double.PositiveInfinity, objmaster = double.PositiveInfinity;
            int    iter = 1;

            do
            {
                Console.WriteLine("Iteration: " + iter);
                // Solve master
                if (1 == iter) // fix theta for first iteration
                {
                    thetaFix.AddRecord().Value = 0;
                }
                else
                {
                    thetaFix.Clear();
                }

                masteri.Solve(GAMSModelInstance.SymbolUpdateType.BaseCase);
                Console.WriteLine(" Master " + masteri.ModelStatus + " : obj=" + masteri.SyncDB.GetVariable("zmaster").FirstRecord().Level);
                if (1 < iter)
                {
                    upperbound = masteri.SyncDB.GetVariable("zmaster").FirstRecord().Level;
                }
                objmaster = masteri.SyncDB.GetVariable("zmaster").FirstRecord().Level - theta.FirstRecord().Level;

                // Set received from master
                received.Clear();
                foreach (GAMSVariableRecord r in masteri.SyncDB.GetVariable("received"))
                {
                    received.AddRecord(r.Keys).Value = r.Level;
                    cutcoeff.AddRecord(iter.ToString(), r.Keys[0]);
                }

                cutconst.AddRecord(iter.ToString());
                double objsub = 0.0;
                foreach (GAMSSetRecord s in data.OutDB.GetSet("s"))
                {
                    demand.Clear();
                    foreach (GAMSSetRecord j in data.OutDB.GetSet("j"))
                    {
                        demand.AddRecord(j.Keys).Value = scenarioData.FindRecord(s.Keys[0], j.Keys[0]).Value;
                    }

                    subi.Solve(GAMSModelInstance.SymbolUpdateType.BaseCase);
                    Console.WriteLine(" Sub " + subi.ModelStatus + " : obj=" + subi.SyncDB.GetVariable("zsub").FirstRecord().Level);


                    double probability = scenarioData.FindRecord(s.Keys[0], "prob").Value;
                    objsub += probability * subi.SyncDB.GetVariable("zsub").FirstRecord().Level;
                    foreach (GAMSSetRecord j in data.OutDB.GetSet("j"))
                    {
                        cutconst.FindRecord(iter.ToString()).Value            += probability * subi.SyncDB.GetEquation("market").FindRecord(j.Keys).Marginal *demand.FindRecord(j.Keys).Value;
                        cutcoeff.FindRecord(iter.ToString(), j.Keys[0]).Value += probability * subi.SyncDB.GetEquation("selling").FindRecord(j.Keys).Marginal;
                    }
                }
                lowerbound = Math.Max(lowerbound, objmaster + objsub);
                iter++;
                if (iter == maxiter + 1)
                {
                    throw new Exception("Benders out of iterations");
                }

                Console.WriteLine(" lowerbound: " + lowerbound + " upperbound: " + upperbound + " objmaster: " + objmaster);
            } while ((upperbound - lowerbound) >= 0.001 * (1 + Math.Abs(upperbound)));

            masteri.Dispose();
            subi.Dispose();
        }
コード例 #2
0
ファイル: Benders2StageMT.cs プロジェクト: nherazo1/Dutties
        static void Main(string[] args)
        {
            GAMSWorkspace ws;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
            }
            else
            {
                ws = new GAMSWorkspace();
            }
            GAMSJob data = ws.AddJobFromString(GetDataText());

            GAMSOptions optData = ws.AddOptions();

            optData.Defines.Add("useBig", "1");
            optData.Defines.Add("nrScen", "100");

            data.Run(optData);

            optData.Dispose();
            GAMSParameter scenarioData = data.OutDB.GetParameter("ScenarioData");

            GAMSOptions opt = ws.AddOptions();

            opt.Defines.Add("datain", data.OutDB.Name);
            int maxiter = 40;

            opt.Defines.Add("maxiter", maxiter.ToString());
            opt.AllModelTypes = "cplexd";

            GAMSCheckpoint cpMaster = ws.AddCheckpoint();
            GAMSCheckpoint cpSub    = ws.AddCheckpoint();

            ws.AddJobFromString(GetMasterText()).Run(opt, cpMaster, data.OutDB);

            GAMSModelInstance masteri  = cpMaster.AddModelInstance();
            GAMSParameter     cutconst = masteri.SyncDB.AddParameter("cutconst", 1, "Benders optimality cut constant");
            GAMSParameter     cutcoeff = masteri.SyncDB.AddParameter("cutcoeff", 2, "Benders optimality coefficients");
            GAMSVariable      theta    = masteri.SyncDB.AddVariable("theta", 0, VarType.Free, "Future profit function variable");
            GAMSParameter     thetaFix = masteri.SyncDB.AddParameter("thetaFix", 0, "");

            masteri.Instantiate("masterproblem max zmaster using lp", opt, new GAMSModifier(cutconst), new GAMSModifier(cutcoeff), new GAMSModifier(theta, UpdateAction.Fixed, thetaFix));

            ws.AddJobFromString(GetSubText()).Run(opt, cpSub, data.OutDB);

            int numThreads = 2;

            GAMSModelInstance[] subi = new GAMSModelInstance[numThreads];
            Queue <Tuple <string, double, Dictionary <string, double> > > demQueue = new Queue <Tuple <string, double, Dictionary <string, double> > >();

            for (int i = 0; i < numThreads; i++)
            {
                subi[i] = cpSub.AddModelInstance();
                GAMSParameter received = subi[i].SyncDB.AddParameter("received", 1, "units received from first stage solution");
                GAMSParameter demand   = subi[i].SyncDB.AddParameter("demand", 1, "stochastic demand");

                subi[i].Instantiate("subproblem max zsub using lp", opt, new GAMSModifier(received), new GAMSModifier(demand));
            }
            opt.Dispose();

            double lowerbound = double.NegativeInfinity, upperbound = double.PositiveInfinity, objmaster = double.PositiveInfinity;
            int    iter = 1;

            do
            {
                Console.WriteLine("Iteration: " + iter);
                // Solve master
                if (1 == iter) // fix theta for first iteration
                {
                    thetaFix.AddRecord().Value = 0;
                }
                else
                {
                    thetaFix.Clear();
                }

                masteri.Solve(GAMSModelInstance.SymbolUpdateType.BaseCase);
                Console.WriteLine(" Master " + masteri.ModelStatus + " : obj=" + masteri.SyncDB.GetVariable("zmaster").FirstRecord().Level);
                if (1 < iter)
                {
                    upperbound = masteri.SyncDB.GetVariable("zmaster").FirstRecord().Level;
                }
                objmaster = masteri.SyncDB.GetVariable("zmaster").FirstRecord().Level - theta.FirstRecord().Level;

                foreach (GAMSSetRecord s in data.OutDB.GetSet("s"))
                {
                    Dictionary <string, double> demDict = new Dictionary <string, double>();
                    foreach (GAMSSetRecord j in data.OutDB.GetSet("j"))
                    {
                        demDict[j.Keys[0]] = scenarioData.FindRecord(s.Keys[0], j.Keys[0]).Value;
                    }
                    demQueue.Enqueue(new Tuple <string, double, Dictionary <string, double> >(s.Keys[0], scenarioData.FindRecord(s.Keys[0], "prob").Value, demDict));
                }

                for (int i = 0; i < numThreads; i++)
                {
                    subi[i].SyncDB.GetParameter("received").Clear();
                }
                foreach (GAMSVariableRecord r in masteri.SyncDB.GetVariable("received"))
                {
                    cutcoeff.AddRecord(iter.ToString(), r.Keys[0]);
                    for (int i = 0; i < numThreads; i++)
                    {
                        subi[i].SyncDB.GetParameter("received").AddRecord(r.Keys).Value = r.Level;
                    }
                }

                cutconst.AddRecord(iter.ToString());
                double objsubsum = 0.0;

                // solve multiple model instances in parallel
                Object   queueMutex = new Object();
                Object   ioMutex    = new Object();
                double[] objsub     = new double[numThreads];
                Dictionary <string, double>[] coef = new Dictionary <string, double> [numThreads];
                double[] cons = new double[numThreads];

                for (int i = 0; i < numThreads; i++)
                {
                    coef[i] = new Dictionary <string, double>();
                    foreach (GAMSSetRecord j in data.OutDB.GetSet("j"))
                    {
                        coef[i].Add(j.Keys[0], 0.0);
                    }
                }

                Parallel.For(0, numThreads, delegate(int i) { ScenSolve(subi[i], ref cons[i], ref coef[i], demQueue, ref objsub[i], queueMutex, ioMutex); });

                for (int i = 0; i < numThreads; i++)
                {
                    objsubsum += objsub[i];
                    cutconst.FindRecord(iter.ToString()).Value += cons[i];
                    foreach (GAMSSetRecord j in data.OutDB.GetSet("j"))
                    {
                        cutcoeff.FindRecord(iter.ToString(), j.Keys[0]).Value += coef[i][j.Keys[0]];
                    }
                }
                lowerbound = Math.Max(lowerbound, objmaster + objsubsum);

                iter++;
                if (iter == maxiter + 1)
                {
                    throw new Exception("Benders out of iterations");
                }

                Console.WriteLine(" lowerbound: " + lowerbound + " upperbound: " + upperbound + " objmaster: " + objmaster);
            } while ((upperbound - lowerbound) >= 0.001 * (1 + Math.Abs(upperbound)));

            masteri.Dispose();
            foreach (GAMSModelInstance inst in subi)
            {
                inst.Dispose();
            }
        }