Exemplo n.º 1
0
        /// <summary>
        /// Runs the specified list data.
        /// </summary>
        /// <param name="listData">The list data.</param>
        /// <returns>List&lt;System.String&gt;[].</returns>
        public List <string>[] Run(List <string>[] listData)
        {
            int sheetNumber = 4;

            string[] setsTemplate = new string[2] {
                "i", "j"
            };
            string[] setsName = new string[2] {
                "所有维修员元素点", "所有任务元素点"
            };
            string[] parametersTemplate = new string[4] {
                "PL", "TL", "Tij", "Tjj"
            };
            string[] parametersName = new string[4] {
                "PLi", "TLj", "Tij", "Tjj"
            };

            GAMSWorkspace ws;

            //if (Environment.GetCommandLineArgs().Length > 1)
            //    ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
            //else
            ws = new GAMSWorkspace(workspacePath, null, DebugLevel.Off);
            GAMSDatabase db = ws.AddDatabase();

            GAMSSet[]       gSet = new GAMSSet[sheetNumber];
            GAMSParameter[] gPar = new GAMSParameter[sheetNumber];

            for (int i = 0; i < 2; i++) //前两个表设置
            {
                gSet[i] = db.AddSet(setsTemplate[i], 1, setsName[i]);
                gPar[i] = db.AddParameter(parametersTemplate[i], 1, parametersName[i]);
                for (int j = 0; j < listData[2 * i].Count; j++)
                {
                    gSet[i].AddRecord(listData[2 * i][j]);
                    gPar[i].AddRecord(listData[2 * i][j]).Value = Convert.ToDouble(listData[2 * i + 1][j]);
                }
            }
            for (int i = 2; i < 4; i++)
            {
                gPar[i] = db.AddParameter(parametersTemplate[i], 2, parametersName[i]);
                for (int j = 0; j < listData[3 * i - 2].Count; j++)
                {
                    gPar[i].AddRecord(listData[3 * i - 2][j], listData[3 * i - 1][j]).Value = Convert.ToDouble(listData[3 * i][j]);
                }
            }

            GAMSJob t = ws.AddJobFromString(GetModelText());

            using (GAMSOptions opt = ws.AddOptions())
            {
                opt.Defines.Add("gdxincname", db.Name);
                opt.AllModelTypes = "xpress";
                t.Run(opt, db);
            }
            List <string>[] resultData = GetResultData(t);

            return(resultData);
        }
Exemplo n.º 2
0
        void ReadParameter(OleDbConnection connect, GAMSDatabase db, string strAccessSelect, string parName, int parDim, string parExp = "")
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand(strAccessSelect, connect);
                connect.Open();

                OleDbDataReader reader = cmd.ExecuteReader();

                if (reader.FieldCount != parDim + 1)
                {
                    lsLog.Add("Number of fields in select statement does not match parDim+1");
                }

                GAMSParameter a = db.AddParameter(parName, parDim, parExp);

                string[] keys = new string[parDim];
                while (reader.Read())
                {
                    for (int idx = 0; idx < parDim; idx++)
                    {
                        keys[idx] = reader.GetString(idx);
                    }
                    a.AddRecord(keys).Value = Convert.ToDouble(reader.GetValue(parDim));
                }
            }
            catch (Exception ex)
            {
                lsLog.Add("Error: Failed to retrieve the required data from the database. " + ex.Message);
            }
            finally
            {
                connect.Close();
            }
        }
        public static GAMSParameter Parameter1D(GAMSDatabase db, string id, string setId, double[] values, string description = "")
        {
            var param = db.AddParameter(id, 1, description);

            for (var i = 0; i < values.Length; i++)
            {
                var rec = param.AddRecord($"{setId}{i + 1}");
                rec.Value = values[i];
            }

            return(param);
        }
        public static GAMSParameter Parameter2D(GAMSDatabase db, string id, string set1Id, string set2Id, double[,] values, string description = "")
        {
            var param = db.AddParameter(id, 2, description);

            for (var i = 0; i < values.GetUpperBound(0); i++)
            {
                for (var j = 0; j < values.GetUpperBound(1); j++)
                {
                    var rec = param.AddRecord($"{set1Id}{i + 1}", $"{set2Id}{j + 1}");
                    rec.Value = values[i, j];
                }
            }

            return(param);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            GAMSWorkspace gmsWS;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                gmsWS = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
            }
            else
            {
                gmsWS = new GAMSWorkspace();
            }

            // create a GAMSDatabase for the results
            GAMSDatabase resultDB = gmsWS.AddDatabase();

            resultDB.AddParameter("objrep", 1, "Objective value");
            resultDB.AddSet("supplyMap", 3, "Supply connection with level");

            int status = 1;

            try
            {
                // run multiple parallel jobs
                Object dbLock = new Object();
                System.Threading.Tasks.Parallel.For(10, 22, delegate(int i) { SolveWarehouse(gmsWS, i, resultDB, dbLock); });
                // export the result database to a GDX file
                resultDB.Export("\\tmp\\result.gdx");
                status = 0;
            }
            catch (GAMSException ex)
            {
                Console.WriteLine("GAMSException occured: " + ex.Message);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                resultDB.Dispose();
            }

            Environment.ExitCode = status;
        }
Exemplo n.º 6
0
        static void ReadParameter(OleDbConnection connect, GAMSDatabase db, string strAccessSelect, string parName, int parDim, string parExp = "")
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand(strAccessSelect, connect);
                connect.Open();

                OleDbDataReader reader = cmd.ExecuteReader();

                if (reader.FieldCount != parDim+1)
                {
                    Console.WriteLine("Number of fields in select statement does not match parDim+1");
                    Environment.Exit(1);
                }

                GAMSParameter a = db.AddParameter(parName, parDim, parExp);

                string[] keys = new string[parDim];
                while (reader.Read())
                {
                    for (int idx = 0; idx < parDim; idx++)
                        keys[idx] = reader.GetString(idx);

                    a.AddRecord(keys).Value = Convert.ToDouble(reader.GetValue(parDim));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
                Environment.Exit(1);
            }
            finally
            {
                connect.Close();
            }
        }
Exemplo n.º 7
0
        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);
            }
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            // Reading input data from workbook
            var excelApp = new Excel.Application();

            Excel.Workbook wb = excelApp.Workbooks.Open(Directory.GetCurrentDirectory() + @"\..\..\..\..\Data\transport.xls");

            Excel.Range range;

            Excel.Worksheet capacity = (Excel.Worksheet)wb.Worksheets.get_Item("capacity");
            range = capacity.UsedRange;
            Array capacityData = (Array)range.Cells.Value;
            int   iCount       = capacity.UsedRange.Columns.Count;

            Excel.Worksheet demand = (Excel.Worksheet)wb.Worksheets.get_Item("demand");
            range = demand.UsedRange;
            Array demandData = (Array)range.Cells.Value;
            int   jCount     = range.Columns.Count;

            Excel.Worksheet distance = (Excel.Worksheet)wb.Worksheets.get_Item("distance");
            range = distance.UsedRange;
            Array distanceData = (Array)range.Cells.Value;

            // number of markets/plants have to be the same in all spreadsheets
            Debug.Assert((range.Columns.Count - 1) == jCount && (range.Rows.Count - 1) == iCount,
                         "Size of the spreadsheets doesn't match");
            wb.Close();

            // Creating the GAMSDatabase and fill with the workbook data
            GAMSWorkspace ws;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
            }
            else
            {
                ws = new GAMSWorkspace();
            }
            GAMSDatabase db = ws.AddDatabase();

            GAMSSet       i             = db.AddSet("i", 1, "Plants");
            GAMSSet       j             = db.AddSet("j", 1, "Markets");
            GAMSParameter capacityParam = db.AddParameter("a", 1, "Capacity");
            GAMSParameter demandParam   = db.AddParameter("b", 1, "Demand");
            GAMSParameter distanceParam = db.AddParameter("d", 2, "Distance");

            for (int ic = 1; ic <= iCount; ic++)
            {
                i.AddRecord((string)capacityData.GetValue(1, ic));
                capacityParam.AddRecord((string)capacityData.GetValue(1, ic)).Value = (double)capacityData.GetValue(2, ic);
            }
            for (int jc = 1; jc <= jCount; jc++)
            {
                j.AddRecord((string)demandData.GetValue(1, jc));
                demandParam.AddRecord((string)demandData.GetValue(1, jc)).Value = (double)demandData.GetValue(2, jc);
                for (int ic = 1; ic <= iCount; ic++)
                {
                    distanceParam.AddRecord((string)distanceData.GetValue(ic + 1, 1), (string)distanceData.GetValue(1, jc + 1)).Value = (double)distanceData.GetValue(ic + 1, jc + 1);
                }
            }

            // Create and run the GAMSJob
            using (GAMSOptions opt = ws.AddOptions())
            {
                GAMSJob t10 = ws.AddJobFromString(GetModelText());
                opt.Defines.Add("gdxincname", db.Name);
                opt.AllModelTypes = "xpress";
                t10.Run(opt, db);
                foreach (GAMSVariableRecord rec in t10.OutDB.GetVariable("x"))
                {
                    Console.WriteLine("x(" + rec.Keys[0] + "," + rec.Keys[1] + "): level=" + rec.Level + " marginal=" + rec.Marginal);
                }
            }
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            // Create a save/restart file usually supplied by an application provider
            // We create it for demonstration purpose
            string wDir = Path.Combine(".", "tmp");

            CreateSaveRestart(Path.Combine(wDir, "tbase"));

            // define some data by using C# data structures
            List <string> plants = new List <string>()
            {
                "Seattle", "San-Diego"
            };
            List <string> markets = new List <string>()
            {
                "New-York", "Chicago", "Topeka"
            };
            Dictionary <string, double> capacity = new Dictionary <string, double>()
            {
                { "Seattle", 350.0 }, { "San-Diego", 600.0 }
            };
            Dictionary <string, double> demand = new Dictionary <string, double>()
            {
                { "New-York", 325.0 }, { "Chicago", 300.0 }, { "Topeka", 275.0 }
            };
            Dictionary <Tuple <string, string>, double> distance = new Dictionary <Tuple <string, string>, double>()
            {
                { new Tuple <string, string> ("Seattle", "New-York"), 2.5 },
                { new Tuple <string, string> ("Seattle", "Chicago"), 1.7 },
                { new Tuple <string, string> ("Seattle", "Topeka"), 1.8 },
                { new Tuple <string, string> ("San-Diego", "New-York"), 2.5 },
                { new Tuple <string, string> ("San-Diego", "Chicago"), 1.8 },
                { new Tuple <string, string> ("San-Diego", "Topeka"), 1.4 }
            };

            GAMSWorkspace ws;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                ws = new GAMSWorkspace(workingDirectory: wDir, systemDirectory: Environment.GetCommandLineArgs()[1]);
            }
            else
            {
                ws = new GAMSWorkspace(workingDirectory: wDir);
            }
            // prepare a GAMSDatabase with data from the C# data structures
            GAMSDatabase db = ws.AddDatabase();

            GAMSSet i = db.AddSet("i", 1, "canning plants");

            foreach (string p in plants)
            {
                i.AddRecord(p);
            }

            GAMSSet j = db.AddSet("j", 1, "markets");

            foreach (string m in markets)
            {
                j.AddRecord(m);
            }

            GAMSParameter a = db.AddParameter("a", 1, "capacity of plant i in cases");

            foreach (string p in plants)
            {
                a.AddRecord(p).Value = capacity[p];
            }

            GAMSParameter b = db.AddParameter("b", 1, "demand at market j in cases");

            foreach (string m in markets)
            {
                b.AddRecord(m).Value = demand[m];
            }

            GAMSParameter d = db.AddParameter("d", 2, "distance in thousands of miles");

            foreach (Tuple <string, string> t in distance.Keys)
            {
                d.AddRecord(t.Item1, t.Item2).Value = distance[t];
            }

            GAMSParameter f = db.AddParameter("f", 0, "freight in dollars per case per thousand miles");

            f.AddRecord().Value = 90;

            // run a job using data from the created GAMSDatabase
            GAMSCheckpoint cpBase = ws.AddCheckpoint("tbase");

            using (GAMSOptions opt = ws.AddOptions())
            {
                GAMSJob t4 = ws.AddJobFromString(GetModelText(), cpBase);
                opt.Defines.Add("gdxincname", db.Name);
                opt.AllModelTypes = "xpress";
                t4.Run(opt, db);
                foreach (GAMSVariableRecord rec in t4.OutDB.GetVariable("x"))
                {
                    Console.WriteLine("x(" + rec.Keys[0] + "," + rec.Keys[1] + "): level=" + rec.Level + " marginal=" + rec.Marginal);
                }
            }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            GAMSWorkspace ws;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
            }
            else
            {
                ws = new GAMSWorkspace();
            }
            // instantiate GAMSOptions and define parameters
            GAMSOptions  opt          = ws.AddOptions();
            GAMSDatabase cutstockData = ws.AddDatabase("csdata");

            opt.AllModelTypes = "Cplex";
            opt.OptCR         = 0.0; // Solve to optimality
            int maxpattern = 35;

            opt.Defines.Add("pmax", maxpattern.ToString());
            opt.Defines.Add("solveMasterAs", "RMIP");

            // define input data
            Dictionary <string, double> d = new Dictionary <string, double>()
            {
                { "i1", 97 }, { "i2", 610 }, { "i3", 395 }, { "i4", 211 }
            };
            Dictionary <string, double> w = new Dictionary <string, double>()
            {
                { "i1", 47 }, { "i2", 36 }, { "i3", 31 }, { "i4", 14 }
            };
            int r = 100; // raw width

            // cutstockData.AddSet("i", 1, "widths").AddRecords(d.Keys);
            // cutstockData.AddParameter("d", 1, "demand").AddRecords(d);
            // cutstockData.AddParameter("w", 1, "width").AddRecords(w);
            // cutstockData.AddParameter("r", 0, "raw width").AddRecord().Value = r;

            GAMSSet       widths   = cutstockData.AddSet("i", 1, "widths");
            GAMSParameter rawWidth = cutstockData.AddParameter("r", 0, "raw width");
            GAMSParameter demand   = cutstockData.AddParameter("d", 1, "demand");
            GAMSParameter width    = cutstockData.AddParameter("w", 1, "width");

            rawWidth.AddRecord().Value = r;
            foreach (string i in d.Keys)
            {
                widths.AddRecord(i);
            }
            foreach (KeyValuePair <string, double> t in d)
            {
                demand.AddRecord(t.Key).Value = t.Value;
            }
            foreach (KeyValuePair <string, double> t in w)
            {
                width.AddRecord(t.Key).Value = t.Value;
            }

            // create initial checkpoint
            GAMSCheckpoint masterCP      = ws.AddCheckpoint();
            GAMSJob        masterInitJob = ws.AddJobFromString(GetMasterModel());

            masterInitJob.Run(opt, masterCP, cutstockData);

            GAMSJob masterJob = ws.AddJobFromString("execute_load 'csdata', aip, pp; solve master min z using %solveMasterAs%;", masterCP);

            GAMSSet       pattern     = cutstockData.AddSet("pp", 1, "pattern index");
            GAMSParameter patternData = cutstockData.AddParameter("aip", 2, "pattern data");

            // Initial pattern: pattern i hold width i
            int patternCount = 0;

            foreach (KeyValuePair <string, double> t in w)
            {
                patternData.AddRecord(t.Key, pattern.AddRecord((++patternCount).ToString()).Keys[0]).Value = (int)(r / t.Value);
            }

            // create model instance for sub job
            GAMSCheckpoint subCP = ws.AddCheckpoint();

            ws.AddJobFromString(GetSubModel()).Run(opt, subCP, cutstockData);
            GAMSModelInstance subMI = subCP.AddModelInstance();

            // define modifier demdual
            GAMSParameter demandDual = subMI.SyncDB.AddParameter("demdual", 1, "dual of demand from master");

            subMI.Instantiate("pricing min z using mip", opt, new GAMSModifier(demandDual));

            // find new pattern
            bool patternAdded = true;

            do
            {
                masterJob.Run(opt, masterCP, cutstockData);
                // Copy duals into gmssubMI.SyncDB DB
                demandDual.Clear();
                foreach (GAMSEquationRecord dem in masterJob.OutDB.GetEquation("demand"))
                {
                    demandDual.AddRecord(dem.Keys[0]).Value = dem.Marginal;
                }

                subMI.Solve();
                if (subMI.SyncDB.GetVariable("z").FindRecord().Level < -0.00001)
                {
                    if (patternCount == maxpattern)
                    {
                        Console.Out.WriteLine("Out of pattern. Increase maxpattern (currently {0}).", maxpattern);
                        patternAdded = false;
                    }
                    else
                    {
                        Console.WriteLine("New patter! Value: " + subMI.SyncDB.GetVariable("z").FindRecord().Level);
                        GAMSSetRecord s = pattern.AddRecord((++patternCount).ToString());
                        foreach (GAMSVariableRecord y in subMI.SyncDB.GetVariable("y"))
                        {
                            if (y.Level > 0.5)
                            {
                                patternData.AddRecord(y.Keys[0], s.Keys[0]).Value = Math.Round(y.Level);
                            }
                        }
                    }
                }
                else
                {
                    patternAdded = false;
                }
            } while (patternAdded);

            // solve final MIP
            opt.Defines["solveMasterAs"] = "MIP";
            masterJob.Run(opt, cutstockData);
            Console.WriteLine("Optimal Solution: {0}", masterJob.OutDB.GetVariable("z").FindRecord().Level);
            foreach (GAMSVariableRecord xp in masterJob.OutDB.GetVariable("xp"))
            {
                if (xp.Level > 0.5)
                {
                    Console.Out.Write("  pattern {0} {1} times: ", xp.Keys[0], xp.Level);
                    GAMSParameterRecord aip = masterJob.OutDB.GetParameter("aip").FirstRecord(" ", xp.Keys[0]);
                    do
                    {
                        Console.Out.Write(" {0}: {1}", aip.Keys[0], aip.Value);
                    } while (aip.MoveNext());
                    Console.Out.WriteLine();
                }
            }
            // clean up of unmanaged ressources
            cutstockData.Dispose();
            subMI.Dispose();
            opt.Dispose();
        }
Exemplo n.º 11
0
        public void ProcessTask(TaskManager.TaskDetails oTask, out List <string> lsLogs, out string sOutput, out string sStatus)
        {
            // cast input values
            CutStockInput oInput = new CutStockInput();

            oInput = (CutStockInput)oDF.XmlStringToObject(oTask.InputValues, oInput);

            CutStockOutput oOutput = new CutStockOutput();

            // call processing algorithm

            GAMSWorkspace ws = new GAMSWorkspace();

            lsLog.Add("working directory : " + ws.WorkingDirectory);
            // instantiate GAMSOptions and define parameters
            GAMSOptions  opt          = ws.AddOptions();
            GAMSDatabase cutstockData = ws.AddDatabase("csdata");

            opt.AllModelTypes = "Cplex";
            opt.OptCR         = 0.0; // Solve to optimality
            opt.Defines.Add("pmax", oInput.MaxPattern.ToString());
            opt.Defines.Add("solveMasterAs", "RMIP");

            GAMSSet       widths   = cutstockData.AddSet("i", 1, "widths");
            GAMSParameter rawWidth = cutstockData.AddParameter("r", 0, "raw width");
            GAMSParameter demand   = cutstockData.AddParameter("d", 1, "demand");
            GAMSParameter width    = cutstockData.AddParameter("w", 1, "width");

            rawWidth.AddRecord().Value = oInput.RawWidth;

            foreach (CutItem oI in oInput.Items)
            {
                widths.AddRecord(oI.Name);
                demand.AddRecord(oI.Name).Value = oI.Demand;
                width.AddRecord(oI.Name).Value  = oI.Width;
            }

            // create initial checkpoint
            GAMSCheckpoint masterCP      = ws.AddCheckpoint();
            GAMSJob        masterInitJob = ws.AddJobFromString(GetMasterModel());

            masterInitJob.Run(opt, masterCP, cutstockData);

            GAMSJob masterJob = ws.AddJobFromString("execute_load 'csdata', aip, pp; solve master min z using %solveMasterAs%;", masterCP);

            GAMSSet       pattern     = cutstockData.AddSet("pp", 1, "pattern index");
            GAMSParameter patternData = cutstockData.AddParameter("aip", 2, "pattern data");

            // Initial pattern: pattern i hold width i
            int patternCount = 0;

            foreach (GAMSParameterRecord rec in width)
            {
                patternData.AddRecord(rec.Keys[0], pattern.AddRecord((++patternCount).ToString()).Keys[0]).Value = (int)(oInput.RawWidth / rec.Value);
            }

            // create model instance for sub job
            GAMSCheckpoint subCP = ws.AddCheckpoint();

            ws.AddJobFromString(GetSubModel()).Run(opt, subCP, cutstockData);
            GAMSModelInstance subMI = subCP.AddModelInstance();

            // define modifier demdual
            GAMSParameter demandDual = subMI.SyncDB.AddParameter("demdual", 1, "dual of demand from master");

            subMI.Instantiate("pricing min z using mip", opt, new GAMSModifier(demandDual));

            // find new pattern
            bool patternAdded = true;

            do
            {
                masterJob.Run(opt, masterCP, cutstockData);
                // Copy duals into gmssubMI.SyncDB DB
                demandDual.Clear();
                foreach (GAMSEquationRecord dem in masterJob.OutDB.GetEquation("demand"))
                {
                    demandDual.AddRecord(dem.Keys[0]).Value = dem.Marginal;
                }

                subMI.Solve();
                if (subMI.SyncDB.GetVariable("z").FindRecord().Level < -0.00001)
                {
                    if (patternCount == oInput.MaxPattern)
                    {
                        lsLog.Add("Out of pattern. Increase maxpattern (currently " + oInput.MaxPattern + ")." + Environment.NewLine);
                        patternAdded = false;
                    }
                    else
                    {
                        lsLog.Add("New pattern! Value: " + subMI.SyncDB.GetVariable("z").FindRecord().Level + Environment.NewLine);
                        GAMSSetRecord s = pattern.AddRecord((++patternCount).ToString());
                        foreach (GAMSVariableRecord y in subMI.SyncDB.GetVariable("y"))
                        {
                            if (y.Level > 0.5)
                            {
                                patternData.AddRecord(y.Keys[0], s.Keys[0]).Value = Math.Round(y.Level);
                            }
                        }
                    }
                }
                else
                {
                    patternAdded = false;
                }
            } while (patternAdded);

            // solve final MIP
            opt.Defines["solveMasterAs"] = "MIP";
            masterJob.Run(opt, cutstockData);
            var dlevel = masterJob.OutDB.GetVariable("z").FindRecord().Level;

            oOutput.OptimalSolution = (int)dlevel;
            lsLog.Add("Optimal Solution: " + dlevel + Environment.NewLine);
            List <Pattern> lP = new List <Pattern>();

            foreach (GAMSVariableRecord xp in masterJob.OutDB.GetVariable("xp"))
            {
                string sPatternLog = "";
                if (xp.Level > 0.5)
                {
                    sPatternLog = string.Format("  pattern {0,2} {1,4} times: ", xp.Keys[0], Math.Round(xp.Level));
                    GAMSParameterRecord aip = masterJob.OutDB.GetParameter("aip").FirstRecord(" ", xp.Keys[0]);

                    Pattern oP = new Pattern()
                    {
                        Name = "pattern " + xp.Keys[0].ToString(), Count = (int)Math.Round(xp.Level)
                    };
                    List <PatternPiece> lPP = new List <PatternPiece>();
                    int    x     = 0;
                    double scale = 100 / masterJob.OutDB.GetParameter("r").FirstRecord().Value;
                    do
                    {
                        sPatternLog += " " + aip.Keys[0] + ": " + aip.Value.ToString();
                        // draw cuts
                        for (int i = 0; i < aip.Value; i++)
                        {
                            PatternPiece oPP = new PatternPiece()
                            {
                                Name  = aip.Keys[0],
                                Width = (int)(width.FindRecord(aip.Keys[0]).Value *scale),
                                Left  = x,
                                Color = oInput.Items.Where(xi => xi.Name == aip.Keys[0]).FirstOrDefault().Color
                            };
                            lPP.Add(oPP);
                            // x += oPP.Width - 1;
                            x += oPP.Width;
                        }
                    } while (aip.MoveNext());

                    if (x < 100)
                    {
                        PatternPiece oPExcess = new PatternPiece()
                        {
                            Name  = "E",
                            Width = (int)(rawWidth.FirstRecord().Value *scale - x),
                            Left  = x,
                            Color = "CCCCCC"
                        };
                        lPP.Add(oPExcess);
                    }
                    lsLog.Add(sPatternLog);
                    oP.Pieces = lPP.ToArray();
                    lP.Add(oP);
                }
            }

            oOutput.Patterns = lP.ToArray();
            // clean up of unmanaged resources
            cutstockData.Dispose();
            subMI.Dispose();
            opt.Dispose();


            sOutput = oDF.ObjectToXmlString(oOutput);
            sStatus = "completed";
            lsLogs  = lsLog;
        }