예제 #1
0
        public void DataFrameDictionariesRoundtrip()
        {
            DataFrame frame = new DataFrame(
                new DataHeader <string>("name"),
                new DataHeader <double>("height"),
                new DataHeader <bool?>("male")
                );

            frame.AddRow("a", 5.0, false);
            frame.AddRow("b", 6.0, true);
            frame.AddRow("c", 5.5, null);

            List <Dictionary <string, object> > dictionaries = frame.ToDictionaries().ToList();

            Assert.IsTrue(dictionaries.Count == frame.Rows.Count);
            Assert.IsTrue(dictionaries[0].Count == frame.Columns.Count);

            DataFrame frame2 = DataFrame.FromDictionaries(dictionaries);

            Assert.IsTrue(frame2.Rows.Count == frame.Rows.Count);
            Assert.IsTrue(frame2.Columns.Count == frame.Columns.Count);
            Assert.IsTrue(frame2.Columns[0].Name == frame.Columns[0].Name);
            Assert.IsTrue(frame2.Columns[1].StorageType == frame.Columns[1].StorageType);
            Assert.IsTrue(frame2.Rows[2]["male"] == frame2.Rows[2]["male"]);
        }
예제 #2
0
        public static void Main(string[] args)
        {
            DataFrame df = new DataFrame();

            df.AddColumn("name", typeof(string));
            df.AddColumn("age", typeof(int));


            df.AddRow("John", 27);
            df.AddRow("Michael", 98);
            df.AddRow("Rue", 39);
            df.AddRow("Peeta", 78);

            df.PrimaryKey = df["name"];


            DataFrame df2 = df[df["age"] > 40];

            // DataFrame df2 = df[df["age"] > 40 & df["name"] == "Michael"];

            Console.WriteLine(df);
            Console.WriteLine("======================");
            Console.WriteLine(df2);


            Console.WriteLine();
            //  df.SaveToFile("test_df.cdf", Encoding.UTF8);
            Console.WriteLine("Finished.");
        }
예제 #3
0
        public void TestAddRow()
        {
            var d = new DataFrame();

            Assert.AreEqual(0, d.Count);

            var obj = new PSObject();

            obj.Properties.Add(new PSNoteProperty("abc", 123));
            obj.Properties.Add(new PSNoteProperty("xyz", "aaa"));
            d.AddRow(obj);

            var obj2 = new PSObject();

            obj2.Properties.Add(new PSNoteProperty("abc", 456));
            obj2.Properties.Add(new PSNoteProperty("xyz", "bbb"));
            d.AddRow(obj2);

            Assert.AreEqual(2, d.Count);

            Assert.AreEqual(123, d.GetRow(0).Properties["abc"].Value);
            Assert.AreEqual("bbb", d.GetRow(1).Properties["xyz"].Value);
        }
예제 #4
0
        private DataFrame GetTestFrame()
        {
            DataFrame frame = new DataFrame(
                new DataHeader <string>("name"),
                new DataHeader <double>("height"),
                new DataHeader <bool?>("male")
                );

            frame.AddRow("a", 7.0, false);
            frame.AddRow(null, 6.5, true);
            frame.AddRow("c", 6.0, false);
            frame.AddRow("d", 5.5, true);
            frame.AddRow("e", 5.0, null);
            frame.AddRow("f", 4.5, true);

            return(frame);
        }
예제 #5
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);
        }