Beispiel #1
0
        private static INetworkCoverage CreateNetworkDiscretisation(INetwork network, double offset)
        {
            var locations = new List <INetworkLocation>();
            var chainage  = offset;

            foreach (var branch in network.Branches)
            {
                chainage = Math.Max(0, chainage);
                while (chainage < branch.Length)
                {
                    locations.Add(new NetworkLocation {
                        Branch = branch, Chainage = chainage
                    });
                    chainage += offset;
                }
                chainage -= branch.Length;
            }
            var coverage = new Discretization
            {
                Name    = "grid",
                Network = network
            };

            coverage.Locations.Values.AddRange(locations);
            return(coverage);
        }
Beispiel #2
0
        public HybridHestonHullWhiteProcess(HestonProcess hestonProcess,
                                            HullWhiteForwardProcess hullWhiteProcess,
                                            double corrEquityShortRate,
                                            Discretization discretization = Discretization.BSMHullWhite)
        {
            hestonProcess_    = hestonProcess;
            hullWhiteProcess_ = hullWhiteProcess;
            hullWhiteModel_   = new HullWhite(hestonProcess.riskFreeRate(),
                                              hullWhiteProcess.a(),
                                              hullWhiteProcess.sigma());
            corrEquityShortRate_ = corrEquityShortRate;
            disc_   = discretization;
            maxRho_ = Math.Sqrt(1 - hestonProcess.rho() * hestonProcess.rho())
                      - Math.Sqrt(Const.QL_EPSILON) /* reserve for rounding errors */;

            T_           = hullWhiteProcess.getForwardMeasureTime();
            endDiscount_ = hestonProcess.riskFreeRate().link.discount(T_);

            Utils.QL_REQUIRE(corrEquityShortRate * corrEquityShortRate
                             + hestonProcess.rho() * hestonProcess.rho() <= 1.0, () =>
                             "correlation matrix is not positive definite");

            Utils.QL_REQUIRE(hullWhiteProcess.sigma() > 0.0, () =>
                             "positive vol of Hull White process is required");
        }
Beispiel #3
0
        public ActionResult GetDiscretizations(int id)
        {
            Discretization disc     = discretizationRepository.Get(id);
            string         path     = discretizationService.getPath(datasetService.getPath(disc.DatasetID), id);
            string         fullPath = Server.MapPath(path);
            string         tmp      = fullPath + "\\tmp";

            Directory.CreateDirectory(tmp);
            string archive = fullPath + "\\Discretizations.zip";

            foreach (string file in Directory.GetFiles(fullPath))
            {
                System.IO.File.Copy(file, Path.Combine(tmp, Path.GetFileName(file)));
            }
            ZipFile.CreateFromDirectory(tmp, archive);
            Directory.Delete(tmp, true);
            FileStream files = new FileStream(archive, FileMode.Open, FileAccess.Read);
            int        len   = (int)(files.Length);

            Byte[] content = new Byte[len];
            files.Read(content, 0, len);
            files.Close();
            System.IO.File.Delete(archive);
            return(File(content, ".zip", "Discretizations" + DateTime.Now.ToShortDateString() + ".zip"));
        }
        public void ApplyTest1()
        {
            DataTable table = ProjectionFilterTest.CreateTable();

            // Show the start data
            // DataGridBox.Show(table);

            // Create a new data projection (column) filter
            var filter = new Discretization("Cost (M)");

            // Apply the filter and get the result
            DataTable result = filter.Apply(table);

            // Show it
            // DataGridBox.Show(result);

            Assert.AreEqual(5, result.Columns.Count);
            Assert.AreEqual(5, result.Rows.Count);

            Assert.AreEqual("213", result.Rows[0]["Cost (M)"]);
            Assert.AreEqual("4", result.Rows[1]["Cost (M)"]);
            Assert.AreEqual("3", result.Rows[2]["Cost (M)"]);
            Assert.AreEqual("3", result.Rows[3]["Cost (M)"]);
            Assert.AreEqual("2", result.Rows[4]["Cost (M)"]);
        }
        public void ApplyTest1()
        {
            DataTable table = ProjectionFilterTest.CreateTable();

            // Show the start data
            // DataGridBox.Show(table);

            // Create a new data projection (column) filter
            var filter = new Discretization("Cost (M)");

            // Apply the filter and get the result
            DataTable result = filter.Apply(table);

            // Show it
            // DataGridBox.Show(result);

            Assert.AreEqual(5, result.Columns.Count);
            Assert.AreEqual(5, result.Rows.Count);

            Assert.AreEqual("213", result.Rows[0]["Cost (M)"]);
            Assert.AreEqual("4", result.Rows[1]["Cost (M)"]);
            Assert.AreEqual("3", result.Rows[2]["Cost (M)"]);
            Assert.AreEqual("3", result.Rows[3]["Cost (M)"]);
            Assert.AreEqual("2", result.Rows[4]["Cost (M)"]);
        }
        public List <Discretization> GetDiscretizations(Dataset dataset, int rowCount)
        {
            var binCount = GetBinCount(rowCount);
            var ret      = new List <Discretization>();

            foreach (var measure in dataset.Measures)
            {
                var disc = new Discretization {
                    Measure = measure
                };
                var orderedValues     = _querier.GetOrderedMeasureValues(measure);
                var step              = rowCount / binCount;
                var currentLeftMargin = (long)Math.Floor(orderedValues[0]);
                for (int i = 1; i <= binCount; i++)
                {
                    var rightMargin = i == binCount ? (long)Math.Ceiling(orderedValues.Last()) : (long)Math.Floor(orderedValues[i * step]);
                    var bin         = new DiscretizeBin
                    {
                        LeftMargin  = currentLeftMargin,
                        RightMargin = rightMargin
                    };
                    currentLeftMargin = bin.RightMargin;
                    disc.Bins.Add(bin);
                }
                ret.Add(disc);
            }
            return(ret);
        }
Beispiel #7
0
        public void CreateSegmentsMultipleCrossSectionAndMinimumDistance()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            // add multiple cross sections and generate calculation points at the cross section locations
            // Grid cells too smal should not be generated.
            AddTestCrossSectionAt(network, branch1, 1.0);
            AddTestCrossSectionAt(network, branch1, 2.0);
            AddTestCrossSectionAt(network, branch1, 3.0);
            AddTestCrossSectionAt(network, branch1, 4.0);
            AddTestCrossSectionAt(network, branch1, 5.0);
            AddTestCrossSectionAt(network, branch1, 6.0);

            var networkCoverage = new Discretization()
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      5.0,             // minimumDistance
                                                      false,           // gridAtStructure
                                                      0.5,             // structureDistance
                                                      true,            // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength

            Assert.AreEqual(3, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(5.0, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[2].Offset, 1.0e-6);
        }
Beispiel #8
0
        public void CreateSegments1StructureAtMinimumEndBranch()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            AddTestStructureAt(network, branch1, 99.6);

            var networkCoverage = new Discretization()
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      0.5,             // minimumDistance
                                                      true,            // gridAtStructure
                                                      0.5,             // structureDistance
                                                      false,           // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength

            // structure at less than minimumdistance; expect 1 point left out
            // [-----------------------------------------------------]
            //                                               99.6
            // x-------------------------------------------x-----(x)--- x
            // 0                                          99.1  (99.8) 100
            Assert.AreEqual(3, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(99.1, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[2].Offset, 1.0e-6);
        }
Beispiel #9
0
        public void CreateSegmentsCrossSectionAndMinimumDistanceNearEnd()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            AddTestCrossSectionAt(network, branch1, 99.0);

            var networkCoverage = new Discretization()
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      5.0,             // minimumDistance
                                                      false,           // gridAtStructure
                                                      0.5,             // structureDistance
                                                      true,            // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength

            Assert.AreEqual(2, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
        }
Beispiel #10
0
        public void CreateSegments1StructureAtNearMinimumBeginBranch()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            AddTestStructureAt(network, branch1, 0.8);

            var networkCoverage = new Discretization
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      0.5,             // minimumDistance
                                                      true,            // gridAtStructure
                                                      0.5,             // structureDistance
                                                      false,           // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength

            // structure at near minimumdistance; expect point centered at 0.8 - 0.5 = 0.3 not created
            // [----------------------
            //                0.8
            // x       x             x ----------------------------- x
            // 0    (0.3)           1.3                             100
            //        ^

            Assert.AreEqual(3, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(1.3, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[2].Offset, 1.0e-6);
        }
Beispiel #11
0
        public void CreateSegmentsMultipleStructures()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            AddTestStructureAt(network, branch1, 20);
            AddTestStructureAt(network, branch1, 40);
            AddTestStructureAt(network, branch1, 60);

            var networkCoverage = new Discretization
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      0,               // minimumDistance
                                                      true,            // gridAtStructure
                                                      0.5,             // structureDistance
                                                      false,           // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength
            Assert.AreEqual(8, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(19.5, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(20.5, networkCoverage.Locations.Values[2].Offset, 1.0e-6);
            Assert.AreEqual(39.5, networkCoverage.Locations.Values[3].Offset, 1.0e-6);
            Assert.AreEqual(40.5, networkCoverage.Locations.Values[4].Offset, 1.0e-6);
            Assert.AreEqual(59.5, networkCoverage.Locations.Values[5].Offset, 1.0e-6);
            Assert.AreEqual(60.5, networkCoverage.Locations.Values[6].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[7].Offset, 1.0e-6);
        }
        public void thresholds()
        {
            // Example for https://github.com/accord-net/framework/issues/737

            // Let's say we have a dataset of US birds:
            string[] names = { "State", "Bird", "Percentage" };

            object[][] inputData =
            {
                new object[] { "Kansas", "Crow",    0.1 },
                new object[] { "Ohio",   "Pardal",  0.5 },
                new object[] { "Hawaii", "Penguim", 0.7 }
            };

            // Discretize the continous data from a doubles to a string representation
            var discretization = new Discretization <double, string>(names, inputData);

            discretization["Percentage"].Mapping[x => x >= 0.00 && x < 0.25] = x => "Q1";
            discretization["Percentage"].Mapping[x => x >= 0.25 && x < 0.50] = x => "Q2";
            discretization["Percentage"].Mapping[x => x >= 0.50 && x < 0.75] = x => "Q3";
            discretization["Percentage"].Mapping[x => x >= 0.75 && x < 1.09] = x => "Q4";

            // Transform the data into discrete categories
            string[][] discreteData = discretization.Transform(inputData);

            // Codify the discrete data from strings to integers
            var codebook = new Codification <string>(names, discreteData);

            // Transform the data into integer symbols
            int[][] values = codebook.Transform(discreteData);

            // Transform the symbols into 1-of-K vectors
            double[][] states = Jagged.OneHot(values.GetColumn(0));
            double[][] birds  = Jagged.OneHot(values.GetColumn(1));
            double[][] colors = Jagged.OneHot(values.GetColumn(2));

            // Normalize each variable separately if needed
            states = states.Divide(codebook["State"].NumberOfSymbols);
            birds  = birds.Divide(codebook["Bird"].NumberOfSymbols);
            colors = colors.Divide(codebook["Percentage"].NumberOfSymbols);

            // Create final feature vectors
            double[][] features = Matrix.Concatenate(states, birds, colors);

            Assert.AreEqual(new[] { 3, 3 }, states.GetLength());
            Assert.AreEqual(new[] { 3, 3 }, birds.GetLength());
            Assert.AreEqual(new[] { 3, 2 }, colors.GetLength());
            Assert.AreEqual(new[] { 3, 8 }, features.GetLength());

            // string t = features.ToCSharp();
            var expected = new double[][]
            {
                new double[] { 0.333333333333333, 0, 0, 0.333333333333333, 0, 0, 0.5, 0 },
                new double[] { 0, 0.333333333333333, 0, 0, 0.333333333333333, 0, 0, 0.5 },
                new double[] { 0, 0, 0.333333333333333, 0, 0, 0.333333333333333, 0, 0.5 }
            };

            Assert.IsTrue(features.IsEqual(expected, rtol: 1e-10));
        }
Beispiel #13
0
 public cdf_nu_ds(HestonProcess _process, double _nu_0, double _nu_t, double _dt,
                  Discretization _discretization)
 {
     process        = _process;
     nu_0           = _nu_0;
     nu_t           = _nu_t;
     dt             = _dt;
     discretization = _discretization;
 }
Beispiel #14
0
        public void TestGetPath()
        {
            List <Discretization> discretizations = new List <Discretization>();
            Discretization        discretization  = new Discretization();

            discretization.DownloadPath = "this is download path";
            discretizations.Add(discretization);
            string downloadPaths = discretizationService.GetDownloadPath(discretizations);

            Assert.IsNotNull(downloadPaths);
            Assert.AreEqual(downloadPaths, "this is download path");
        }
Beispiel #15
0
        public void CreateSegmentsMultipleCrossSectionsAndFixedPoint()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            var discretization = new Discretization()
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(discretization, // networkCoverage
                                                      branch1,        // branch
                                                      5.0,            // minimumDistance
                                                      false,          // gridAtStructure
                                                      0.5,            // structureDistance
                                                      false,          // gridAtCrossSection
                                                      true,           // gridAtFixedLength
                                                      2);             // fixedLength
            Assert.AreEqual(51, discretization.Locations.Values.Count);


            INetworkLocation networkLocation = discretization.Locations.Values.Where(nl => nl.Offset == 8).First();

            //DiscretizationHelper.SetUserDefinedGridPoint(networkLocation, true);
            discretization.ToggleFixedPoint(networkLocation);
            networkLocation = discretization.Locations.Values.Where(nl => nl.Offset == 32).First();
            discretization.ToggleFixedPoint(networkLocation);

            AddTestCrossSectionAt(network, branch1, 10.0);
            AddTestCrossSectionAt(network, branch1, 20.0);
            AddTestCrossSectionAt(network, branch1, 30.0);

            HydroNetworkHelper.GenerateDiscretization(discretization, // networkCoverage
                                                      branch1,        // branch
                                                      5.0,            // minimumDistance
                                                      false,          // gridAtStructure
                                                      0.5,            // structureDistance
                                                      true,           // gridAtCrossSection
                                                      false,          // gridAtFixedLength
                                                      -1);            // fixedLength
            // expect gridpoints at:
            // begin and end 0 and 100
            // fixed locations 8 and 32.
            // 20 for the cross section, 10 and 30 should not be generated due to existing
            // fixed points and minimium distance 0f 5.
            Assert.AreEqual(5, discretization.Locations.Values.Count);
            Assert.AreEqual(0.0, discretization.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(8.0, discretization.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(20.0, discretization.Locations.Values[2].Offset, 1.0e-6);
            Assert.AreEqual(32.0, discretization.Locations.Values[3].Offset, 1.0e-6);
            Assert.AreEqual(100.0, discretization.Locations.Values[4].Offset, 1.0e-6);
        }
Beispiel #16
0
        public void CreateSegments2StructureAtNearMinimumBeginBranch()
        {
            IHydroNetwork network = CreateSegmentTestNetwork();
            var           branch1 = network.Channels.First();

            AddTestStructureAt(network, branch1, 0.8);
            AddTestStructureAt(network, branch1, 1.2);

            var networkCoverage = new Discretization()
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      0.001,           // minimumDistance
                                                      true,            // gridAtStructure
                                                      0.5,             // structureDistance
                                                      false,           // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength

            // structure at near minimumdistance; expect 1 point centered at first segment
            // [----------------------
            //                0.8   1.2
            // x       x          x             x ------------------ x
            // 0      0.3        1.0           1.7                  100
            //         ^

            Assert.AreEqual(5, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(0.3, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(1.0, networkCoverage.Locations.Values[2].Offset, 1.0e-6);
            Assert.AreEqual(1.7, networkCoverage.Locations.Values[3].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[4].Offset, 1.0e-6);

            // repeat with minimumDistance set to 0.5
            HydroNetworkHelper.GenerateDiscretization(networkCoverage, // networkCoverage
                                                      branch1,         // branch
                                                      0.5,             // minimumDistance
                                                      true,            // gridAtStructure
                                                      0.5,             // structureDistance
                                                      false,           // gridAtCrossSection
                                                      false,           // gridAtFixedLength
                                                      -1);             // fixedLength
            // expect gridpoints at 0.3 eliminated
            Assert.AreEqual(4, networkCoverage.Locations.Values.Count);
            Assert.AreEqual(0.0, networkCoverage.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(1.0, networkCoverage.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(1.7, networkCoverage.Locations.Values[2].Offset, 1.0e-6);
            Assert.AreEqual(100.0, networkCoverage.Locations.Values[3].Offset, 1.0e-6);
        }
Beispiel #17
0
        //TODO
        public string SendToDiscretization(SendDiscretizationViewModel sendDiscretizationViewModel)
        {
            string[]        methodsList           = Request.Form["Methods"].Split(',');
            var             knowledgeBasedMethods = Request.Form["KnowledgedBasedMethods"].Split(',');
            FileTransferrer fileTransferrer       = new FileTransferrer();
            Dataset         dataset       = db.Datasets.Find(sendDiscretizationViewModel.id);
            string          currentUserId = User.Identity.GetUserId();
            ApplicationUser currentUser   = db.Users.FirstOrDefault(y => y.Id == currentUserId);

            // Check that there are *not* knowledge based methods in the request
            if (methodsList[0] != "")
            {
                discretizationService.Discretize(methodsList, datasetService.getPath(dataset.DatasetID), id);
                List <Discretization> discretizations = discretizationService.CreateDiscretizations(dataset, methodsList, id, datasetService.getPath(dataset.DatasetID));
                for (int i = 0; i < discretizations.Count; i++)
                {
                    discretizations[i].Owner = currentUser;
                    discretizationRepository.Add(discretizations[i]);
                }
            }

            // Knowledge based methods with files uploaded
            if (Request.Files.Count > 0)
            {
                foreach (var knowledgeBasedMethod in knowledgeBasedMethods)
                {
                    var knowledgeBasedMethodSplit = knowledgeBasedMethod.ToString().Split('_');
                    var methodName         = knowledgeBasedMethodSplit[0];
                    var fileName           = knowledgeBasedMethodSplit[knowledgeBasedMethodSplit.Length - 1];
                    var maxGap             = knowledgeBasedMethodSplit[1];
                    var windowSize         = knowledgeBasedMethodSplit[2];
                    var statesFilyBytes    = fileTransferrer.GetBytesFromFile(Request.Files[fileName]);
                    var methodDownloadPath = fileTransferrer.ExpertDataset(dataset.Path, methodName + "/" + maxGap + "_" + windowSize, statesFilyBytes);

                    Discretization d = new Discretization()
                    {
                        Dataset           = dataset,
                        DownloadPath      = methodDownloadPath,
                        Visibility        = "",
                        Type              = "Discretized",
                        Owner             = currentUser,
                        FullName          = MethodEncodingToMethodName[methodName],
                        ParametersIsReady = "In Progress"
                    };
                    db.Discretizations.Add(d);
                }
            }
            discretizationRepository.SaveChanges();
            return("Discretizing...");
        }
Beispiel #18
0
        public void rule_matching_test()
        {
            DataTable input = new DataTable("Sample data");

            input.Columns.Add("x", typeof(double));
            input.Columns.Add("y", typeof(double));
            input.Columns.Add("z", typeof(double));

            input.Rows.Add(0.02, 60.6, 24.2);
            input.Rows.Add(0.92, 50.2, 21.1);
            input.Rows.Add(0.32, 60.9, 19.8);
            input.Rows.Add(2.02, 61.8, 92.4);


            // Create a discretization filter to operate on the first 2 columns
            var target = new Discretization <double, int>("x", "y");

            target.Columns["x"].Mapping[x => true] = x => (int)System.Math.Round(x, MidpointRounding.AwayFromZero);
            target.Columns["y"].Mapping[x => true] = x => ((x - (int)x) >= 0.7999999999999) ? ((int)x + 1) : (int)x;

            DataTable expected = new DataTable("Sample data");

            expected.Columns.Add("x", typeof(double));
            expected.Columns.Add("y", typeof(double));
            expected.Columns.Add("z", typeof(double));

            expected.Rows.Add(0, 60, 24.2);
            expected.Rows.Add(1, 50, 21.1);
            expected.Rows.Add(0, 61, 19.8);
            expected.Rows.Add(2, 62, 92.4);


            DataTable actual = target.Apply(input);

            for (int i = 0; i < actual.Rows.Count; i++)
            {
                double ex = (double)expected.Rows[i][0];
                double ey = (double)expected.Rows[i][1];
                double ez = (double)expected.Rows[i][2];

                double ax = (int)actual.Rows[i][0];
                double ay = (int)actual.Rows[i][1];
                double az = (double)actual.Rows[i][2];

                Assert.AreEqual(ex, ax);
                Assert.AreEqual(ey, ay);
                Assert.AreEqual(ez, az);
            }
        }
        public void ApplyTest()
        {
            

            DataTable input = new DataTable("Sample data");
            input.Columns.Add("x", typeof(double));
            input.Columns.Add("y", typeof(double));
            input.Columns.Add("z", typeof(double));

            input.Rows.Add(0.02, 60.6, 24.2);
            input.Rows.Add(0.92, 50.2, 21.1);
            input.Rows.Add(0.32, 60.9, 19.8);
            input.Rows.Add(2.02, 61.8, 92.4);


            // Create a discretization filter to operate on the first 2 columns
            Discretization target = new Discretization("x","y");
            target.Columns["y"].Threshold = 0.8;

            DataTable expected = new DataTable("Sample data");
            expected.Columns.Add("x", typeof(double));
            expected.Columns.Add("y", typeof(double));
            expected.Columns.Add("z", typeof(double));

            expected.Rows.Add(0, 60, 24.2);
            expected.Rows.Add(1, 50, 21.1);
            expected.Rows.Add(0, 61, 19.8);
            expected.Rows.Add(2, 62, 92.4);


            DataTable actual = target.Apply(input);

            for (int i = 0; i < actual.Rows.Count; i++)
            {
                    double ex = (double)expected.Rows[i][0];
                    double ey = (double)expected.Rows[i][1];
                    double ez = (double)expected.Rows[i][2];

                    double ax = (double)actual.Rows[i][0];
                    double ay = (double)actual.Rows[i][1];
                    double az = (double)actual.Rows[i][2];

                    Assert.AreEqual(ex, ax);
                    Assert.AreEqual(ey, ay);
                    Assert.AreEqual(ez, az);
            }
            
        }
Beispiel #20
0
        public void CreateSegmentsFixedLocations()
        {
            IHydroNetwork network        = CreateSegmentTestNetwork();
            var           branch1        = network.Channels.First();
            var           discretization = new Discretization()
            {
                Network = network,
                SegmentGenerationMethod = SegmentGenerationMethod.SegmentBetweenLocations
            };

            HydroNetworkHelper.GenerateDiscretization(discretization, // networkCoverage
                                                      branch1,        // branch
                                                      0.5,            // minimumDistance
                                                      false,          // gridAtStructure
                                                      0.5,            // structureDistance
                                                      false,          // gridAtCrossSection
                                                      true,           // gridAtFixedLength
                                                      10);            // fixedLength
            Assert.AreEqual(11, discretization.Locations.Values.Count);
            Assert.AreEqual(0.0, discretization.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(50.0, discretization.Locations.Values[5].Offset, 1.0e-6);
            Assert.AreEqual(100.0, discretization.Locations.Values[10].Offset, 1.0e-6);

            INetworkLocation networkLocation = discretization.Locations.Values[7];

            Assert.AreEqual(70.0, networkLocation.Offset, 1.0e-6);
            discretization.ToggleFixedPoint(networkLocation);
            //DiscretizationHelper.SetUserDefinedGridPoint(networkLocation, true);

            HydroNetworkHelper.GenerateDiscretization(discretization, // networkCoverage
                                                      branch1,        // branch
                                                      0.5,            // minimumDistance
                                                      false,          // gridAtStructure
                                                      0.5,            // structureDistance
                                                      false,          // gridAtCrossSection
                                                      true,           // gridAtFixedLength
                                                      40);            // fixedLength
            // expect values at
            // - 0 and 100 start and end
            // - 70 for fixed location
            // - none between 70 and 100
            // - (0 - 70) > 40, divide in equal parts -> 35
            Assert.AreEqual(4, discretization.Locations.Values.Count);
            Assert.AreEqual(0.0, discretization.Locations.Values[0].Offset, 1.0e-6);
            Assert.AreEqual(35.0, discretization.Locations.Values[1].Offset, 1.0e-6);
            Assert.AreEqual(70.0, discretization.Locations.Values[2].Offset, 1.0e-6);
            Assert.AreEqual(100.0, discretization.Locations.Values[3].Offset, 1.0e-6);
        }
Beispiel #21
0
        public void ApplyTest()
        {
            DataTable input = new DataTable("Sample data");

            input.Columns.Add("x", typeof(double));
            input.Columns.Add("y", typeof(double));
            input.Columns.Add("z", typeof(double));

            input.Rows.Add(0.02, 60.6, 24.2);
            input.Rows.Add(0.92, 50.2, 21.1);
            input.Rows.Add(0.32, 60.9, 19.8);
            input.Rows.Add(2.02, 61.8, 92.4);


            // Create a discretization filter to operate on the first 2 columns
            Discretization target = new Discretization("x", "y");

            target.Columns["y"].Threshold = 0.8;

            DataTable expected = new DataTable("Sample data");

            expected.Columns.Add("x", typeof(double));
            expected.Columns.Add("y", typeof(double));
            expected.Columns.Add("z", typeof(double));

            expected.Rows.Add(0, 60, 24.2);
            expected.Rows.Add(1, 50, 21.1);
            expected.Rows.Add(0, 61, 19.8);
            expected.Rows.Add(2, 62, 92.4);


            DataTable actual = target.Apply(input);

            for (int i = 0; i < actual.Rows.Count; i++)
            {
                double ex = (double)expected.Rows[i][0];
                double ey = (double)expected.Rows[i][1];
                double ez = (double)expected.Rows[i][2];

                double ax = (double)actual.Rows[i][0];
                double ay = (double)actual.Rows[i][1];
                double az = (double)actual.Rows[i][2];

                Assert.AreEqual(ex, ax);
                Assert.AreEqual(ey, ay);
                Assert.AreEqual(ez, az);
            }
        }
        private void SaveButton_Discret_Click(object sender, RoutedEventArgs e)
        {
            int countOfIntervals = Convert.ToInt32(IntervalsText_Discret.Text);
            int numOfColumn      = ColumnCombo_Discret.SelectedIndex;

            if (numOfColumn < 0)
            {
                MessageBox.Show("Wybierz kolumnę");
            }
            else
            {
                Discretization.DoDiscretization(numOfColumn, countOfIntervals);
                //odświeżenie widoku - wyświetlenie zmian
                DisplayNewDataInMenu(numOfColumn + 1);
            }
        }
Beispiel #23
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Discretization discretization = db.Discretizations.Find(id);

            //Dataset dataset = await db.Datasets.FindAsync(id);
            if (discretization == null)
            {
                return(HttpNotFound());
            }
            return(View(new KarmaLegoExistingViewModel()
            {
                DiscretizationID = (int)id
            }));
        }
Beispiel #24
0
        public void LinkAndUnlinkTwoDiscretizationItems()
        {
            var dataObject1 = new Discretization();
            var dataObject2 = new Discretization();

            var dataItem1 = new DataItem(dataObject1, "a1");
            var dataItem2 = new DataItem(dataObject2, "a2");

            dataItem2.LinkTo(dataItem1);

            Assert.IsTrue(dataItem2.IsLinked);
            Assert.AreSame(dataItem1, dataItem2.LinkedTo);
            Assert.AreSame(dataItem2, dataItem1.LinkedBy[0]);

            dataItem2.Unlink();

            Assert.AreEqual(null, dataItem2.LinkedTo);
            Assert.AreEqual(0, dataItem1.LinkedBy.Count);
        }
Beispiel #25
0
        public HestonProcess(Handle <YieldTermStructure> riskFreeRate,
                             Handle <YieldTermStructure> dividendYield,
                             Handle <Quote> s0,
                             double v0, double kappa,
                             double theta, double sigma, double rho,
                             Discretization d = Discretization.QuadraticExponentialMartingale)
            : base(new EulerDiscretization())
        {
            riskFreeRate_   = riskFreeRate;
            dividendYield_  = dividendYield;
            s0_             = s0;
            v0_             = v0;
            kappa_          = kappa;
            theta_          = theta;
            sigma_          = sigma;
            rho_            = rho;
            discretization_ = d;

            riskFreeRate_.registerWith(update);
            dividendYield_.registerWith(update);
            s0_.registerWith(update);
        }
Beispiel #26
0
        public void thresholds()
        {
            // Example for https://github.com/accord-net/framework/issues/737

            #region doc_percentage
            // Let's say we have some data representing the probability
            // of seeing some particular species of birds across the U.S.:
            string[] names = { "State", "Bird", "Percentage" };

            object[][] data =
            {
                new object[] { "Kansas", "Crow",    0.1 },
                new object[] { "Ohio",   "Pardal",  0.5 },
                new object[] { "Hawaii", "Penguim", 0.7 }
            };

            // Create a new discretization filter for the given dataset:
            var discretization = new Discretization <double, string>(names)
            {
                { "Percentage", x => x >= 0.00 && x < 0.25, "lowest" },
                { "Percentage", x => x >= 0.25 && x < 0.50, "low" },
                { "Percentage", x => x >= 0.50 && x < 0.75, "medium" },
                { "Percentage", x => x >= 0.75 && x < 1.00, "likely" },
            };

            // Convert the data using the above conversion rules:
            object[][] output = discretization.Transform(data);

            // The output should be:
            object[][] expected =
            {
                new object[] { "Kansas", "Crow",    "lowest" },
                new object[] { "Ohio",   "Pardal",  "medium" },
                new object[] { "Hawaii", "Penguim", "medium" }
            };
            #endregion

            Assert.IsTrue(output.IsEqual(expected));
        }
Beispiel #27
0
        public void LinkAndUnlinkTwoDiscretizationItemsCheckValueAndName()
        {
            var grid1 = new Discretization {
                Name = "grid1"
            };
            var grid2 = new Discretization {
                Name = "grid2"
            };

            var dataItem1 = new DataItem(grid1, "a1");
            var dataItem2 = new DataItem(grid2, "a2");

            dataItem2.LinkTo(dataItem1);

            dataItem2.Unlink();

            Assert.AreEqual(typeof(Discretization), dataItem2.Value.GetType());
            Assert.AreEqual(typeof(Discretization), dataItem1.Value.GetType());

            var grid2X = (Discretization)dataItem2.Value;

            Assert.AreEqual("computational grid", grid2X.Name);
        }
        public void DiscretizationBinding()
        {
            var network        = RouteHelperTest.GetSnakeNetwork(false, new Point(0, 0), new Point(100, 0), new Point(100, 100));
            var discretization = new Discretization {
                Network = network
            };

            var discretizationBindingList = new DiscretizationBindingList(discretization);

            Assert.AreEqual(4, discretizationBindingList.ColumnNames.Count());
            Assert.AreEqual(DiscretizationBindingList.ColumnNameLocationName, discretizationBindingList.ColumnNames[2]);

            Assert.AreEqual(0, discretizationBindingList.Count());

            discretization[new NetworkLocation(network.Branches.First(), 0.0)
                           {
                               Name = "haha"
                           }] = 1.0;

            Assert.AreEqual(1, discretizationBindingList.Count());

            Assert.AreEqual("haha", discretizationBindingList.First()[2].ToString());
        }
Beispiel #29
0
 public cdf_nu_ds_minus_x(double _x0, HestonProcess _process, double _nu_0, double _nu_t, double _dt,
                          Discretization _discretization)
 {
     cdf_nu = new cdf_nu_ds(_process, _nu_0, _nu_t, _dt, _discretization);
     x0     = _x0;
 }
Beispiel #30
0
        public void missing_values_thresholds_test()
        {
            DataTable input = new DataTable("Tennis Example with Missing Values");

            input.Columns.Add("Day", typeof(string));
            input.Columns.Add("Outlook", typeof(string));
            input.Columns.Add("Temperature", typeof(int));
            input.Columns.Add("Humidity", typeof(string));
            input.Columns.Add("Wind", typeof(string));
            input.Columns.Add("PlayTennis", typeof(string));
            input.Rows.Add("D1", "Sunny", 35, "High", "Weak", "No");
            input.Rows.Add("D2", null, 32, "High", "Strong", "No");
            input.Rows.Add("D3", null, null, "High", null, "Yes");
            input.Rows.Add("D4", "Rain", 25, "High", "Weak", "Yes");
            input.Rows.Add("D5", "Rain", 16, null, "Weak", "Yes");
            input.Rows.Add("D6", "Rain", 12, "Normal", "Strong", "No");
            input.Rows.Add("D7", "Overcast", "18", "Normal", "Strong", "Yes");
            input.Rows.Add("D8", null, 27, "High", null, "No");
            input.Rows.Add("D9", null, 17, "Normal", "Weak", "Yes");
            input.Rows.Add("D10", null, null, "Normal", null, "Yes");
            input.Rows.Add("D11", null, 23, "Normal", null, "Yes");
            input.Rows.Add("D12", "Overcast", 25, null, "Strong", "Yes");
            input.Rows.Add("D13", "Overcast", 33, null, "Weak", "Yes");
            input.Rows.Add("D14", "Rain", 24, "High", "Strong", "No");

            Assert.AreEqual(14, input.Rows.Count);
            Assert.AreEqual(6, input.Columns.Count);

            var discretization = new Discretization <double, string>()
            {
                { "Temperature", x => x >= 30 && x < 50, "Hot" },
                { "Temperature", x => x >= 20 && x < 30, "Mild" },
                { "Temperature", x => x >= 00 && x < 20, "Cool" },
            };

            DataTable actual = discretization.Apply(input);

            Assert.AreEqual(14, actual.Rows.Count);
            Assert.AreEqual(6, actual.Columns.Count);


            DataTable expected = new DataTable("Tennis Example with Missing Values");

            expected.Columns.Add("Day", typeof(string));
            expected.Columns.Add("Outlook", typeof(string));
            expected.Columns.Add("Temperature", typeof(string));
            expected.Columns.Add("Humidity", typeof(string));
            expected.Columns.Add("Wind", typeof(string));
            expected.Columns.Add("PlayTennis", typeof(string));
            expected.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
            expected.Rows.Add("D2", null, "Hot", "High", "Strong", "No");
            expected.Rows.Add("D3", null, null, "High", null, "Yes");
            expected.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
            expected.Rows.Add("D5", "Rain", "Cool", null, "Weak", "Yes");
            expected.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
            expected.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
            expected.Rows.Add("D8", null, "Mild", "High", null, "No");
            expected.Rows.Add("D9", null, "Cool", "Normal", "Weak", "Yes");
            expected.Rows.Add("D10", null, null, "Normal", null, "Yes");
            expected.Rows.Add("D11", null, "Mild", "Normal", null, "Yes");
            expected.Rows.Add("D12", "Overcast", "Mild", null, "Strong", "Yes");
            expected.Rows.Add("D13", "Overcast", "Hot", null, "Weak", "Yes");
            expected.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");


            for (int j = 0; j < expected.Rows.Count; j++)
            {
                var erow = expected.Rows[j];
                var arow = actual.Rows[j];

                for (int i = 0; i < expected.Columns.Count; i++)
                {
                    object e = erow[i];
                    object a = arow[i];
                    Assert.AreEqual(e, a);
                }
            }
        }
Beispiel #31
0
        public string DiscoverPatterns()
        {
            var karmaLegoConfigs = Request.Form["Configs"].Split(',');

            //  List<int> discretizatonsIds = new List<int>();
            //  List<int> currentDiscretizationIds = new List<int>();
            foreach (var klc in karmaLegoConfigs)
            {
                var configs  = klc.Split('_');
                var configId = configs[0];
                //  currentDiscretizationIds.Add(Int32.Parse(configId));
            }
            // var configsToSendList = new List<string>();
            if (karmaLegoConfigs[0] != "")
            {
                foreach (var config in karmaLegoConfigs)
                {
                    var            configs      = config.Split('_');
                    var            configId     = configs[0];
                    var            fold         = configs[configs.Length - 1];
                    var            configParams = configs.Skip(1).Take(configs.Length - 2).Select(x => x).ToArray();
                    Discretization d            = db.Discretizations.Find(Int32.Parse(configId));
                    id = klRepository.GetNextId();
                    var path          = discretizationService.getPath(datasetService.getPath(d.DatasetID), d.DiscretizationID);
                    var inputPath     = Server.MapPath(path);
                    var karmaLegoPath = inputPath + @"\KARMALEGO\" + id.ToString();
                    inputPath += @"\KL.txt";

                    //if(fold == "1")
                    //{
                    //     karmaLegoPath = d.DownloadPath + "/KARMALEGO/" + String.Join("_", configParams);

                    //}
                    //configsToSendList.Add(karmaLegoPath);
                    string          currentUserId = User.Identity.GetUserId();
                    ApplicationUser currentUser   = db.Users.FirstOrDefault(y => y.Id == currentUserId);
                    if (!Int32.TryParse(configParams[0], out int tempInt))
                    {
                        return("Epslion value is not ok");
                    }
                    else
                    {
                        if (tempInt < 0)
                        {
                            return("Epslion value is not ok");
                        }
                    }
                    if (!Int32.TryParse(configParams[0], out tempInt))
                    {
                        return("Maximum gap value is not ok");
                    }
                    else
                    {
                        if (tempInt < 0)
                        {
                            return("Maximum gap value is not ok");
                        }
                    }
                    if (!Double.TryParse(configParams[2], out double tempDouble))
                    {
                        return("Vertical support value is not ok");
                    }
                    else
                    {
                        if (tempDouble < 0 || tempDouble > 100)
                        {
                            return("Vertical support value is not ok");
                        }
                    }
                    var epsilon            = Double.Parse(configParams[0]);
                    var maxGap             = Int32.Parse(configParams[1]);
                    var minVerticalSupport = Double.Parse(configParams[2]) / 100;
                    karmaLegoService.sendToKL(inputPath, karmaLegoPath, epsilon, minVerticalSupport, maxGap);
                    KarmaLego kl = new KarmaLego()
                    {
                        Discretization         = d,
                        Epsilon                = epsilon,
                        DownloadPath           = karmaLegoPath,
                        IsReady                = "In Progress",
                        MaximumGap             = maxGap,
                        MinimumVerticalSupport = minVerticalSupport,
                        Owner = currentUser,
                        Fold  = Int32.Parse(fold)
                    };
                    //discretizatonsIds.Add(d.DiscretizationID);

                    db.KarmaLegos.Add(kl);
                }
                //var llll = String.Join(" ", configsToSendList);
                db.SaveChanges();
                //List<string> klIds = new List<string>();
                //foreach (var kl in db.KarmaLegos)
                //{
                //    if(discretizatonsIds.IndexOf(kl.DiscretizationID) != -1 )
                //        klIds.Add(kl.KarmaLegoID.ToString());
                //}
                return("Success");
                //return Json(new
                //{
                //    Ids = String.Join("_", klIds),
                //    Classes = "Class1,Class2_Class3,Class4",
                //    Errors = ""
                //});
            }
            return("Error");
            //return Json(new
            //{
            //    Errors = "ERRORS"

            //});
        }
Beispiel #32
0
        public override void Initialize(System.Collections.Hashtable properties)
        {
            string config    = null;
            string elevation = null;
            string fdr       = null;

            //read input arguments
            foreach (DictionaryEntry arg in properties)
            {
                switch (arg.Key.ToString())
                {
                case "ConfigFile":
                    config = arg.Value.ToString();
                    break;

                case "SurfaceElevation":
                    elevation = arg.Value.ToString();
                    break;

                case "FlowDirection":
                    fdr = arg.Value.ToString();
                    break;
                }
            }

            //make sure that Surface Elevation and Config path have been specified
            if (config == null || elevation == null || fdr == null)
            {
                throw new Exception("Exception Occured in Diffusive Wave Wrapper: User Must Supply config path, elevation, and fdr in *.omi");
            }

            //setup model properties
            this.SetValuesTableFields();
            this.SetVariablesFromConfigFile(config);

            //get model information
            InputExchangeItem input = this.GetInputExchangeItem(0);

            _inExcessElementSet = input.ElementSet.ID;
            _inExcessQuantity   = input.Quantity.ID;

            input = this.GetInputExchangeItem(1);
            _inStageElementSet = input.ElementSet.ID;
            _inStageQuantity   = input.Quantity.ID;

            OutputExchangeItem output = this.GetOutputExchangeItem(0);

            _outExcessElementSet = output.ElementSet.ID;
            _outExcessQuantity   = output.Quantity.ID;

            _dt = this.GetTimeStep();

            //Define element set
            ElementSet eset;

            _engine.BuildElementSet(elevation, fdr, this.GetModelID(), this.GetModelDescription(), out eset, out _sox, out _soy);

            //save cell size
            this._cellsize = _engine._cellsize;

            //save elevation
            this._elevation = _engine.elevations;

            //TODO:  Add more discretization schemes to the class, and allow the user to choose which one
            //       to use by passing an argument in the *.omi file.
            //Initialize the Discretization class

            //Try using Differend Discretizations
            //_discretization = new ForwardDifferencing(_engine._cellsize, this.GetTimeStep(), _engine._elementCount);
            _discretization       = new Euler(_engine._cellsize, this.GetTimeStep(), _engine._elementCount, _engine.elevations);
            _discretization.Datum = _engine.datum;

            //set Sox and Soy
            _discretization.Sox = _sox;
            _discretization.Soy = _soy;

            //set Nx and Ny
            _discretization.Nx = nx;
            _discretization.Ny = ny;

            ////set Sox and Soy
            //_discretization._sox = _sox;
            //_discretization._soy = _soy;

            ////set Nx and Ny
            //_discretization.nx = nx;
            //_discretization.ny = ny;

            //STARTHERE: !!!!!  Read land cover data and set Nx Ny
            //TODO:  Read land cover data and set Nx Ny
            //_discretization._nx = _nx;
            //_discretization._ny = _ny;

            //create array to hold head
            _h = new double[_engine._elementCount];
        }