public void GetFilteredValuesFrom2DFunction()
        {
            var x = new Variable <int>();
            var y = new Variable <int>();

            var z = new Variable <double> {
                Arguments = { x, y }
            };

            z[0, 0] = 1.0;
            z[0, 1] = 2.0;
            z[1, 0] = 3.0;
            z[1, 1] = 4.0;

            var xFilter = new VariableValueFilter <int>(x, 0);
            var yFilter = new VariableValueFilter <int>(y, 1);

            var values = z.GetValues(xFilter, yFilter);

            values.Count
            .Should("query values using 2 filters").Be.EqualTo(1);

            values[0]
            .Should("query values using 2 filters").Be.EqualTo(2.0);
        }
Example #2
0
 public override DataCube <double> GetValues(ITimeSeriesQueryCriteria qc, ITimeSeriesTransform provider)
 {
     if (SitesMeasured.Count > 0)
     {
         int count = SitesMeasured.Count;
         DataCube <double>[] tss = new DataCube <double> [count];
         int i = 0;
         foreach (HydroPoint hp in SitesMeasured)
         {
             Variable v = new Variable(this.VariableID);
             v.SiteID      = hp.ID;
             qc.SiteID     = hp.ID;
             qc.VariableID = this.VariableID;
             tss[i]        = v.GetValues(qc, provider);
             i++;
         }
         int        length = tss[0].Size[1];
         double[]   values = new double[length];
         DateTime[] time   = new DateTime[length];
         Array.Copy(tss[0].DateTimes, time, length);
         for (int j = 0; j < length; j++)
         {
             double sum = 0;
             for (i = 0; i < tss.Length; i++)
             {
                 sum += tss[i][0, j, 0];
             }
             values[i] = sum / tss.Length;
         }
         DataCube <double> summedts = new DataCube <double>(values, time);
         return(summedts);
     }
     else
     {
         TimeSpan          span     = qc.End - qc.Start;
         double[]          values   = new double[span.Days];
         DateTime[]        time     = new DateTime[span.Days];
         DataCube <double> summedts = new DataCube <double>(values, time);
         return(summedts);
     }
 }
        public void GetFilteredValuesFrom2DFunction()
        {
            var x = new Variable<int>();
            var y = new Variable<int>();

            var z = new Variable<double> { Arguments = { x, y } };

            z[0, 0] = 1.0;
            z[0, 1] = 2.0;
            z[1, 0] = 3.0;
            z[1, 1] = 4.0;

            var xFilter = new VariableValueFilter<int>(x, 0);
            var yFilter = new VariableValueFilter<int>(y, 1);

            var values = z.GetValues(xFilter, yFilter);

            values.Count
                .Should("query values using 2 filters").Be.EqualTo(1);

            values[0]
                .Should("query values using 2 filters").Be.EqualTo(2.0);
        }
Example #4
0
        public void Implicit1DAccess()
        {
            //m = F(x,t)
            IVariable<int> measurement = new Variable<int>();
            IVariable<int> time = new Variable<int>();
            IVariable<int> space = new Variable<int>();
            time.SetValues(new[] {1, 2, 3});
            space.SetValues(new[] {10, 20, 30});

            measurement.Arguments.Add(time);
            measurement.Arguments.Add(space);
            Assert.AreEqual(9, measurement.GetValues().Count);
            measurement[2, 10] = 200;
            //select one location and expect a singe dimensional array
            IMultiDimensionalArray<int> values = measurement.GetValues(new VariableValueFilter<int>(space, 10));
            Assert.AreEqual(3, values.Count);
            //do you really want this?
            Assert.AreEqual(200, values[1]);
        }
Example #5
0
        public void GetValuesGeneric()
        {
            IVariable<double> x = new Variable<double>("x");
            IVariable<double> y = new Variable<double>("y");
            IVariable<double> fx = new Variable<double>("fx");
            IVariable<double> fy = new Variable<double>("fy");

            IFunction f = new Function();
            f.Arguments.Add(x);
            f.Arguments.Add(y);
            f.Components.Add(fx);
            f.Components.Add(fy);

            // set (fx, fy) values to (100.0, 200.0) for a combination of x and y values.
            f.SetValues(
                new[] {100.0, 200.0},
                new VariableValueFilter<double>(x, new[] {1.0, 2.0}),
                new VariableValueFilter<double>(y, new[] { 10.0, 20.0 }));

            IMultiDimensionalArray<double> values = fx.GetValues();

            int expectedValuesCount = 4;
            Assert.AreEqual(expectedValuesCount, values.Count);
            Assert.AreEqual(expectedValuesCount, x.Values.Count*y.Values.Count);

            double expectedFxValue = 100.0;
            double expectedFyValue = 200.0;
            Assert.AreEqual(expectedFxValue, fx.Values[0, 0]);
            Assert.AreEqual(expectedFyValue, fy.Values[0, 0]);
        }
Example #6
0
        public void GetValues()
        {
            IVariable x = new Variable<double>("x");
            IVariable y = new Variable<double>("y");
            IVariable fx = new Variable<double>("fx");
            IVariable fy = new Variable<double>("fy");

            IFunction f = new Function();
            f.Arguments.Add(x);
            f.Arguments.Add(y);
            f.Components.Add(fx);
            f.Components.Add(fy);

            Assert.AreEqual(2,fy.Values.Rank);

            // set values of the x, y arguments, fx, fy components will be set to their default values.
            x.SetValues(new[] {1.0, 2.0});
            y.SetValues(new[] {10.0, 20.0});

            // get values, all values equal DefaultValue
            IMultiDimensionalArray values = fx.GetValues();
            Assert.AreEqual(4, values.Count, "number of values after argument values are set");
            Assert.AreEqual(fx.DefaultValue, values[0, 0], "default value");

            // set value using indexes.
            int i = 1;
            int j = 1;

            fx.Values[i, j] = 111.0;
            fy.Values[i, j] = 222.0;

            // ... and it is also the same as (accessing component variables directly):
            f.Components[0].Values[i, j] = 111.0;
            f.Components[1].Values[i, j] = 222.0;

            // ... you can also assign *all* values in the following way
            var array2d = new[,] {{fx.DefaultValue, fx.DefaultValue}, {111.0, 111.0}};
            fx.SetValues(array2d);

            array2d = new[,] {{fy.DefaultValue, fy.DefaultValue}, {222.0, 222.0}};
            fy.SetValues(array2d);

            fx[2.0, 10.0] = 111.0;
            fy[2.0, 20.0] = 222.0;

            // we can also get a single value in this way
            Assert.AreEqual(111.0, (double) fx[2.0, 10.0]);
            Assert.AreEqual(222.0, (double) fy[2.0, 20.0]);

            // a single value can not be asked using indexes of the arguments. 
            // now you have to find the values via the arguments
            var value = (double) fx.Values[i, j];
            Assert.AreEqual(111.0, value);
        }
Example #7
0
        public void SetValuesUsingJaggerdArrayAndCheckToStringResults()
        {
            var values = new[,]
                             {
                                 {1, 2, 3},
                                 {4, 5, 6}
                             };

            var x = new Variable<int>("x");
            var y = new Variable<int>("y");
            var f = new Variable<int>("f");

            // TODO: arguments are added in a different way compare to array, refactor!
            f.Arguments.Add(y);
            f.Arguments.Add(x);

            x.SetValues(new[] {0, 1, 2});
            y.SetValues(new[] {0, 1});
            f.SetValues(values);

            f.GetValues().ToString().Should().Be.EqualTo("{{1, 2, 3}, {4, 5, 6}}");
        }
        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 (AMPL a = new AMPL())
            {
                if (solver != null)
                {
                    a.SetOption("solver", solver);
                }
                // Interpret the two files
                a.Read(System.IO.Path.Combine(modelDirectory, "diet/diet.mod"));
                a.ReadData(System.IO.Path.Combine(modelDirectory, "diet/diet.dat"));

                // Solve
                a.Solve();

                // Get objective entity by AMPL name
                Objective totalcost = a.GetObjective("Total_Cost");
                // Print it
                Console.WriteLine("ObjectiveInstance is: {0}", totalcost.Value);
                // Reassign data - specific instances
                Parameter cost = a.GetParameter("cost");
                cost.SetValues(ampl.Tuple.FromArray("BEEF", "HAM"),
                               new double[] { 5.01, 4.55 });
                Console.WriteLine("Increased costs of beef and ham.");

                // ReSolve and display objective
                a.Solve();
                Console.WriteLine("Objective value: {0}", totalcost.Value);

                // Reassign data - all instances
                cost.SetValues(new double[] { 3, 5, 5, 6, 1, 2, 5.01, 4.55 });
                Console.WriteLine("Updated all costs");
                // ReSolve and display objective
                a.Solve();
                Console.WriteLine("New objective value: {0}", totalcost.Value);

                // Get the values of the variable Buy in a dataframe object
                Variable Buy = a.GetVariable("Buy");
                // Access a specific instance (method 1)
                Console.WriteLine(Buy.Get("FISH").ToString());
                // Access a specific instance (method 2)
                Console.WriteLine(Buy[new ampl.Tuple("FISH")].ToString());
                DataFrame df = Buy.GetValues();
                // Print them
                Console.WriteLine(df);

                // Get the values of an expression into a DataFrame object
                DataFrame df2 = a.GetData("{j in FOOD} 100*Buy[j]/Buy[j].ub");
                // Print them
                Console.WriteLine(df2);
            }
            return(0);
        }
Example #9
0
        public void DeleteTimeStepTest()
        {
            //waterLevel = f(cell,time)
            IFeatureCoverage waterLevelCoverage = new FeatureCoverage("WaterLevelCoverage");

            IVariable <int>           timeVariable       = new Variable <int>("timestep");
            IVariable <SimpleFeature> boundariesVariable = new Variable <SimpleFeature>("cell");
            IVariable <float>         waterLevelVariable = new Variable <float>("Level");

            waterLevelCoverage.Arguments.Add(timeVariable);
            waterLevelCoverage.Arguments.Add(boundariesVariable);
            waterLevelCoverage.Components.Add(waterLevelVariable);

            waterLevelCoverage.Features = (IList)boundaries;

            for (int i = 0; i < 12; i++)
            {
                waterLevelCoverage.SetValues(new[] { (i * 10) + 1.0f, (i * 10) + 2.0f, (i * 10) + 3.0f,
                                                     (i * 10) + 4.0f, (i * 10) + 5.0f, (i * 10) + 6.0f },
                                             new VariableValueFilter <int>(timeVariable, i));
            }
            // content should be now:
            // t=0   :  1.0   2.0   3.0   4.0   5.0   6.0
            // t=1   : 11.0  12.0  13.0  14.0  15.0  16.0
            // ..
            // ..
            // t=11  :111.0 112.0 113.0 114.0 115.0 116.0

            IList <float> values = waterLevelVariable.GetValues();

            Assert.AreEqual(6 * 12, values.Count);
            Assert.AreEqual(14.0, values[9]);
            Assert.AreEqual(114.0, values[69]);

            values = waterLevelVariable.GetValues(new VariableValueFilter <int>(timeVariable, 5));
            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(51, values[0]);
            Assert.AreEqual(56, values[5]);

            // Remove values at t=2
            timeVariable.Values.Remove(2);
            values = waterLevelVariable.GetValues();
            Assert.AreEqual(6 * 11, values.Count);
            timeVariable.Values.Remove(3);
            timeVariable.Values.Remove(4);
            IList argumentValues = waterLevelCoverage.Arguments[0].Values;

            Assert.AreEqual(9, argumentValues.Count);
            Assert.AreEqual(11, argumentValues[8]);

            timeVariable.Values.Remove(5);
            timeVariable.Values.Remove(6);
            timeVariable.Values.Remove(7);
            argumentValues = waterLevelCoverage.Arguments[0].Values;
            Assert.AreEqual(6, argumentValues.Count);
            Assert.AreEqual(11, argumentValues[5]);

            waterLevelCoverage.RemoveValues(new VariableValueFilter <int>(timeVariable, new [] { 0, 1, 8, 9, 10, 11 }));

            values = waterLevelVariable.GetValues();
            Assert.AreEqual(0, values.Count);
            argumentValues = waterLevelCoverage.Arguments[0].Values;
            Assert.AreEqual(0, argumentValues.Count);
        }
        /// <summary>
        /// This example shows a simple AMPL iterative procedure implemented in AMPL.
        /// Must be executed with a solver supporting the suffix dunbdd
        /// </summary>
        /// <param name="args">
        /// The first string, if present, should point to the models directory
        /// </param>
        public static int Main(string[] args)
        {
            string modelDirectory = ((args != null) && (args.Length > 0)) ? args[0]
         : "../../models";

            /*
             * // 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())
            {
                // Must be solved with a solver supporting the suffix dunbdd
                ampl.SetOption("solver", "cplex");

                modelDirectory += "/locationtransportation";
                ampl.SetOption("presolve", false);
                ampl.SetOption("omit_zero_rows", true);

                // Read the model.
                ampl.Read(modelDirectory + "/trnloc2.mod");
                ampl.ReadData(modelDirectory + "/trnloc.dat"); // TODO: set data
                                                               // programmatically

                // Get references to AMPL's model entities for easy access.
                Objective  shipCost    = ampl.GetObjective("Ship_Cost");
                Variable   maxShipCost = ampl.GetVariable("Max_Ship_Cost");
                Variable   buildVar    = ampl.GetVariable("Build");
                Constraint supply      = ampl.GetConstraint("Supply");
                Constraint demand      = ampl.GetConstraint("Demand");
                Parameter  numCutParam = ampl.GetParameter("nCUT");
                Parameter  cutType     = ampl.GetParameter("cut_type");
                Parameter  buildParam  = ampl.GetParameter("build");
                Parameter  supplyPrice = ampl.GetParameter("supply_price");
                Parameter  demandPrice = ampl.GetParameter("demand_price");

                numCutParam.Set(0);
                maxShipCost.Value = 0;
                double[] initialBuild = new double[ampl.GetSet("ORIG").Size];
                for (int i = 0; i < initialBuild.Length; i++)
                {
                    initialBuild[i] = 1;
                }
                buildParam.SetValues(initialBuild);
                int numCuts;
                for (numCuts = 1; ; numCuts++)
                {
                    Console.WriteLine("Iteration {0}", numCuts);
                    ampl.Display("build");
                    // Solve the subproblem.
                    ampl.Eval("solve Sub;");
                    string result = shipCost.Result;

                    if (result.Equals("infeasible"))
                    {
                        // Add a feasibility cut.
                        numCutParam.Set(numCuts);
                        cutType.Set(new ampl.Tuple(numCuts), "ray");
                        DataFrame dunbdd = supply.GetValues("dunbdd");
                        foreach (var row in dunbdd)
                        {
                            supplyPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl);
                        }
                        dunbdd = demand.GetValues("dunbdd");
                        foreach (var row in dunbdd)
                        {
                            demandPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl);
                        }
                    }
                    else if (shipCost.Value > maxShipCost.Value + 0.00001)
                    {
                        // Add an optimality cut.
                        numCutParam.Set(numCuts);
                        cutType.Set(new ampl.Tuple(numCuts), "point");
                        ampl.Display("Ship");
                        DataFrame duals = supply.GetValues();
                        foreach (var row in duals)
                        {
                            supplyPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl);
                        }
                        duals = demand.GetValues();
                        foreach (var row in duals)
                        {
                            demandPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl);
                        }
                    }
                    else
                    {
                        break;
                    }

                    // Re-solve the master problem.
                    Console.WriteLine("RE-SOLVING MASTER PROBLEM");
                    ampl.Eval("solve Master;");

                    // Copy the data from the Build variable used in the master problem
                    // to the build parameter used in the subproblem.
                    DataFrame data = buildVar.GetValues();
                    buildParam.SetValues(data);
                }
                ampl.Display("Ship");
            }
            return(0);
        }
Example #11
0
        public void DeleteTimeStepTest()
        {
            //waterLevel = f(cell,time)
            IFeatureCoverage waterLevelCoverage = new FeatureCoverage("WaterLevelCoverage");

            IVariable<int> timeVariable = new Variable<int>("timestep");
            IVariable<SimpleFeature> boundariesVariable = new Variable<SimpleFeature>("cell");
            IVariable<float> waterLevelVariable = new Variable<float>("Level");  
         
            waterLevelCoverage.Arguments.Add(timeVariable);
            waterLevelCoverage.Arguments.Add(boundariesVariable);
            waterLevelCoverage.Components.Add(waterLevelVariable);

            waterLevelCoverage.Features = (IList)boundaries;

            for (int i = 0; i < 12; i++)
            {
                waterLevelCoverage.SetValues(new[] { (i * 10) + 1.0f, (i * 10) + 2.0f, (i * 10) + 3.0f, 
                                                           (i * 10) + 4.0f, (i * 10) + 5.0f, (i * 10) + 6.0f } ,
                                             new VariableValueFilter<int>(timeVariable, i));
            }
            // content should be now:
            // t=0   :  1.0   2.0   3.0   4.0   5.0   6.0   
            // t=1   : 11.0  12.0  13.0  14.0  15.0  16.0   
            // ..
            // ..
            // t=11  :111.0 112.0 113.0 114.0 115.0 116.0   

            IList<float> values = waterLevelVariable.GetValues();
            Assert.AreEqual(6 * 12, values.Count);
            Assert.AreEqual(14.0, values[9]);
            Assert.AreEqual(114.0, values[69]);

            values = waterLevelVariable.GetValues(new VariableValueFilter<int>(timeVariable, 5));
            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(51, values[0]);
            Assert.AreEqual(56, values[5]);

            // Remove values at t=2
            timeVariable.Values.Remove(2);
            values = waterLevelVariable.GetValues();
            Assert.AreEqual(6 * 11, values.Count);
            timeVariable.Values.Remove(3);
            timeVariable.Values.Remove(4);
            IList argumentValues = waterLevelCoverage.Arguments[0].Values;
            Assert.AreEqual(9, argumentValues.Count);
            Assert.AreEqual(11, argumentValues[8]);

            timeVariable.Values.Remove(5);
            timeVariable.Values.Remove(6);
            timeVariable.Values.Remove(7);
            argumentValues = waterLevelCoverage.Arguments[0].Values;
            Assert.AreEqual(6, argumentValues.Count);
            Assert.AreEqual(11, argumentValues[5]);

            waterLevelCoverage.RemoveValues(new VariableValueFilter<int>(timeVariable, new [] { 0, 1, 8, 9, 10, 11 }));

            values = waterLevelVariable.GetValues();
            Assert.AreEqual(0, values.Count);
            argumentValues = waterLevelCoverage.Arguments[0].Values;
            Assert.AreEqual(0, argumentValues.Count);
        }