コード例 #1
0
        public static int Main(string[] args)
        {
            /*
             * // If the AMPL installation directory is not in the system search path:
             * ampl.Environment env = new ampl.Environment(
             * "full path to the AMPL installation directory");
             * // Create an AMPL instance
             * using (AMPL a = new AMPL(env)) {}
             */

            // Create an AMPL instance
            using (AMPL ampl = new AMPL())
            {
                ampl.Eval("set CITIES; set LINKS within (CITIES cross CITIES);");
                ampl.Eval("param cost {LINKS} >= 0; param capacity {LINKS} >= 0;");
                ampl.Eval("data; set CITIES := PITT NE SE BOS EWR BWI ATL MCO;");

                double[] cost      = { 2.5, 3.5, 1.7, 0.7, 1.3, 1.3, 0.8, 0.2, 2.1 };
                double[] capacity  = { 250, 250, 100, 100, 100, 100, 100, 100, 100 };
                string[] LinksFrom = { "PITT", "PITT", "NE", "NE", "NE", "SE", "SE", "SE", "SE" };
                string[] LinksTo   = { "NE", "SE", "BOS", "EWR", "BWI", "EWR", "BWI", "ATL", "MCO" };

                DataFrame df = new DataFrame(2, "LINKSFrom", "LINKSTo", "cost", "capacity");
                df.SetColumn("LINKSFrom", LinksFrom);
                df.SetColumn("LINKSTo", LinksTo);
                df.SetColumn("cost", cost);
                df.SetColumn("capacity", capacity);
                Console.WriteLine(df.ToString());

                ampl.SetData(df, "LINKS");
            }
            return(0);
        }
コード例 #2
0
        public static int Main(string[] args)
        {
            string modelDirectory = ((args != null) && (args.Length > 0)) ? args[0]
            : "../../models";
            string solver = ((args != null) && (args.Length > 1)) ? args[1] : null;
            // Create first dataframe (for data indexed over NUTR) Add data row by row
            DataFrame df1 = new DataFrame(1, "NUTR", "n_min", "n_max");

            df1.AddRow("A", 700, 20000);
            df1.AddRow("B1", 700, 20000);
            df1.AddRow("B2", 700, 20000);
            df1.AddRow("C", 700, 20000);
            df1.AddRow("CAL", 16000, 24000);
            df1.AddRow("NA", 0.0, 50000);

            // Create second dataframe (for data indexed over FOOD) Add column by column
            DataFrame df2 = new DataFrame(1, "FOOD");

            string[] foods = { "BEEF", "CHK", "FISH", "HAM",
                               "MCH",  "MTL", "SPG",  "TUR" };
            df2.SetColumn("FOOD", foods);
            double[] contents = new double[8];
            for (int j = 0; j < 8; j++)
            {
                contents[j] = 2;
            }
            df2.AddColumn("f_min", contents);
            for (int j = 0; j < 8; j++)
            {
                contents[j] = 10;
            }
            df2.AddColumn("f_max", contents);
            double[] costs = { 3.19, 2.59, 2.29, 2.89, 1.89,
                               1.99, 1.99, 2.49 };
            df2.AddColumn("cost", costs);

            // Create third dataframe, to assign data to the AMPL entity param amt{NUTR, FOOD};
            DataFrame df3 = new DataFrame(2, "NUTR", "FOOD");

            // Populate the set columns
            string[] nutrWithMultiplicity = new string[48];
            string[] foodWithMultiplicity = new string[48];
            int      i = 0;

            for (int n = 0; n < 6; n++)
            {
                for (int f = 0; f < 8; f++)
                {
                    nutrWithMultiplicity[i]   = df1.GetRowByIndex(n)[0].Str;
                    foodWithMultiplicity[i++] = foods[f];
                }
            }
            df3.SetColumn("NUTR", nutrWithMultiplicity);
            df3.SetColumn("FOOD", foodWithMultiplicity);

            // Populate with all these values
            double[] values = { 60,     8,   8,  40,   15,  70,   25,  60,  10,  20,  15,
                                35,    15,  15,  25,   15,  15,   20,  10,  10,  15,  15,  15,  10, 20, 0, 10,
                                40,    35,  30,  50,   20, 295,  770, 440, 430, 315, 400, 370, 450,
                                968, 2180, 945, 278, 1182, 896, 1329, 1397 };
            df3.AddColumn("amt", values);

            // Create an AMPL instance
            using (AMPL ampl = new AMPL())
            {
                if (solver != null)
                {
                    ampl.SetOption("solver", solver);
                }
                // Read model only
                ampl.Read(modelDirectory + "/diet/diet.mod");
                // Assign data to NUTR, n_min and n_max
                ampl.SetData(df1, "NUTR");
                // Assign data to FOOD, f_min, f_max and cost
                ampl.SetData(df2, "FOOD");
                // Assign data to amt
                ampl.SetData(df3);
                // Solve the model
                ampl.Solve();

                // Print out the result
                Console.Write("Objective function value: {0}\n",
                              ampl.GetObjective("Total_Cost").Value);

                // Get the values of the variable Buy in a dataframe
                DataFrame results = ampl.GetVariable("Buy").GetValues();
                // Print
                Console.WriteLine(results.ToString());
            }
            return(0);
        }
コード例 #3
0
        public static int Main(string[] args)
        {
            string modelDirectory = ((args != null) && (args.Length > 0)) ? args[0]
           : "../../models";
            string solver = ((args != null) && (args.Length > 1)) ? args[1] : null;

            /*
             * // If the AMPL installation directory is not in the system search path:
             * ampl.Environment env = new ampl.Environment(
             * "full path to the AMPL installation directory");
             * // Create an AMPL instance
             * using (AMPL a = new AMPL(env)) {}
             */

            // Create an AMPL instance
            using (var ampl = new AMPL())
            {
                if (solver != null)
                {
                    ampl.SetOption("solver", solver);
                }
                // Read the model file
                ampl.Read(modelDirectory + "/diet/diet.mod");

                string[] foods = { "BEEF", "CHK", "FISH", "HAM",
                                   "MCH",  "MTL", "SPG",  "TUR" };
                double[] costs = { 3.59, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49 };
                double[] fmin  = { 2, 2, 2, 2, 2, 2, 2, 2 };
                double[] fmax  = { 10, 10, 10, 10, 10, 10, 10, 10 };

                DataFrame df = new DataFrame(1, "FOOD");
                df.SetColumn("FOOD", foods);
                df.AddColumn("cost", costs);
                df.AddColumn("f_min", fmin);
                df.AddColumn("f_max", fmax);
                ampl.SetData(df, "FOOD");

                string[] nutrients = { "A", "C", "B1", "B2", "NA", "CAL" };
                double[] nmin      = { 700, 700, 700, 700, 0, 16000 };
                double[] nmax      = { 20000, 20000, 20000, 20000, 50000, 24000 };
                df = new DataFrame(1, "NUTR");
                df.SetColumn("NUTR", nutrients);
                df.AddColumn("n_min", nmin);
                df.AddColumn("n_max", nmax);
                ampl.SetData(df, "NUTR");

                double[,] amounts =
                {
                    {  60,    8,   8,  40,   15,  70,   25,   60 },
                    {  20,    0,  10,  40,   35,  30,   50,   20 },
                    {  10,   20,  15,  35,   15,  15,   25,   15 },
                    {  15,   20,  10,  10,   15,  15,   15,   10 },
                    { 928, 2180, 945, 278, 1182, 896, 1329, 1397 },
                    { 295,  770, 440, 430,  315, 400,  379,  450 }
                };
                df = new DataFrame(2, "NUTR", "FOOD", "amt");
                df.SetMatrix(nutrients, foods, amounts);
                ampl.SetData(df);

                ampl.Solve();

                Console.WriteLine(string.Format("Objective: {0}",
                                                ampl.GetObjective("Total_Cost").Value));
            }
            return(0);
        }