public static void RunRandomly(Random rnd, int numOfNodes, ApproximationType approximation, int vectorLength, int iterations,
                                       string resultDir)
        {
            var windowSize = vectorLength * 2;
            var stepSize   = windowSize / 5;
            var resultPath =
                PathBuilder.Create(resultDir, "InnerProduct")
                .AddProperty("Dataset", "Random")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Window", windowSize.ToString())
                .AddProperty("Iterations", iterations.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
            {
                var innerProduct = new InnerProductFunction(vectorLength);
                var initVectors  = ArrayUtils.Init(numOfNodes, _ => ArrayUtils.Init(vectorLength, __ => (rnd.NextDouble() - 0.5) * numOfNodes).ToVector());
                var multiRunner  = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength,
                                                       approximation, innerProduct.MonitoredFunction);
                for (int i = 0; i < iterations; i++)
                {
                    var changes = ArrayUtils.Init(numOfNodes, index => index != 0 ? new Vector() : ArrayUtils.Init(vectorLength, _ => rnd.NextDouble() * numOfNodes / 10).ToVector());
                    multiRunner.Run(changes, rnd, true)
                    .Select(r => r.AsCsvString())
                    .ForEach((Action <string>)resultCsvFile.WriteLine);
                }
            }
        }
Esempio n. 2
0
        public static void Run(Random rnd, int size, double edgeProb, int numOfNodes, string resultDir)
        {
            var globalVectorType   = GlobalVectorType.Sum;
            var epsilon            = new ThresholdEpsilon(10);
            var amountOfIterations = 500;
            var initMatrix         = GenerateMatrix(size, edgeProb, rnd);
            var vectorLength       = initMatrix.Count;
            var fileName           = $"SpectralGap_VectorLength_{vectorLength}_Nodes_{numOfNodes}_Iterations_{amountOfIterations}.csv";
            var resultPath         = Path.Combine(resultDir, fileName);

            using (var resultCsvFile = File.CreateText(resultPath))
            {
                resultCsvFile.AutoFlush = true;
                resultCsvFile.WriteLine(AccumaltedResult.Header(numOfNodes));
                var multiRunner = MultiRunner.InitAll(SplitTo(initMatrix, size, numOfNodes, rnd), numOfNodes,
                                                      vectorLength, globalVectorType,
                                                      epsilon, SpectralGapFunction.MonitoredFunction);
                multiRunner.OnlySchemes(new MonitoringScheme.Distance(2),
                                        new MonitoringScheme.Value(),
                                        new MonitoringScheme.Vector(),
                                        new MonitoringScheme.Naive(),
                                        new MonitoringScheme.Oracle());
                var changes = GenerateChanges(initMatrix, numOfNodes, rnd).Take(amountOfIterations);
                multiRunner.RunAll(changes, rnd, false)
                .FinishAfter(multiRunner.Runners.Count, r => double.IsNegativeInfinity(r.LowerBound))
                .Select(r => r.AsCsvString())
                .ForEach((Action <string>)resultCsvFile.WriteLine);
            }

            Process.Start(resultPath);
        }
        public static void RunRandomly(Random rnd, int width, int height, int numOfNodes, int iterations, ApproximationType approximation, bool oneChanges,
                                       string resultDir)
        {
            var vectorLength = width * height;
            var resultPath   =
                PathBuilder.Create(resultDir, "AMS_F2")
                .AddProperty("Dataset", "Random")
                .AddProperty("OneChanges", oneChanges.ToString())
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Width", width.ToString())
                .AddProperty("Height", height.ToString())
                .AddProperty("Iterations", iterations.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");
            var secondMomentFunction = new SecondMoment(width, height);

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
            {
                var initVectors = ArrayUtils.Init(numOfNodes, _ => ArrayUtils.Init(vectorLength, __ => (double)rnd.Next(-4, 5)).ToVector());

                var multiRunner = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength, approximation, secondMomentFunction.MonitoredFunction);
                // multiRunner.OnlySchemes(new MonitoringScheme.Value(), new MonitoringScheme.FunctionMonitoring(), new MonitoringScheme.Oracle());
                const int OracleFullSyncs = 2;

                Func <int, Vector> ChangeGenerator() => nodeIndex =>
                {
                    if (oneChanges)
                    {
                        if (nodeIndex != 0)
                        {
                            return(new Vector());
                        }
                        return(ArrayUtils.Init(vectorLength, _ => (rnd.NextDouble() - 0.5) * numOfNodes / 5).ToVector());
                    }
                    else
                    {
                        return(ArrayUtils.Init(vectorLength, _ => (rnd.NextDouble() - 0.5) / 5).ToVector());
                    }
                };

                for (int i = 0; i < iterations; i++)
                {
                    var changes = ArrayUtils.Init(numOfNodes, ChangeGenerator());

                    var stop = new StrongBox <bool>(false);
                    multiRunner.Run(changes, rnd, false)
                    .SideEffect(r => stop.Value = stop.Value || (r.MonitoringScheme.Equals(new MonitoringScheme.Oracle()) && r.NumberOfFullSyncs > OracleFullSyncs))
                    .Select(r => r.AsCsvString())
                    .ForEach(resultCsvFile.WriteLine);
                    if (stop.Value)
                    {
                        break;
                    }
                }
            }
        }
        public static void RunCTU(Random rnd, int maxIterations, int numOfNodes, int window, int collapseDimension,
                                  ApproximationType approximation, string ctuBinaryPath, string resultDir)
        {
            var resultPath =
                PathBuilder.Create(resultDir, "EntropySketch")
                .AddProperty("Dataset", "CTU")
                .AddProperty("SketchDimension", collapseDimension.ToString())
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("Window", window.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");
            var entropySketch = new EntropySketchFunction(collapseDimension);
            var header        = string.Join(",", Enumerable.Range(1, collapseDimension).Select(i => "y" + i)) + "," +
                                string.Join(",", Enumerable.Range(1, numOfNodes).Select(i => "server_max_" + i));

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes) + ",Entropy," + header))
                using (var ctuProbabilityWindow = CtuProbabilityWindow.Init(ctuBinaryPath, numOfNodes, window))
                {
                    var initProbabilityVectors = ctuProbabilityWindow.CurrentProbabilityVector().Map(entropySketch.CollapseProbabilityVector);
                    var multiRunner            = MultiRunner.InitAll(initProbabilityVectors, numOfNodes, collapseDimension,
                                                                     approximation, entropySketch.MonitoredFunction);
                    //multiRunner.OnlySchemes(new MonitoringScheme.Oracle());
                    int i = 0;
                    while (ctuProbabilityWindow.MoveNext() && (i++ < maxIterations))
                    {
                        var entropy = Vector.AverageVector(ctuProbabilityWindow.CurrentProbabilityVector())
                                      .IndexedValues.Select(p => p.Value).Sum(v => - v * Math.Log(v));
                        var changeProbabilityVectors = ctuProbabilityWindow.CurrentChangeProbabilityVector()
                                                       .Map(entropySketch.CollapseProbabilityVector);

                        multiRunner.Run(changeProbabilityVectors, rnd, false)
                        .Select(r =>
                        {
                            return(r.AsCsvString());
                            //var oracle = multiRunner.Runners.Values.OfType<MonitoringRunner<OracleServer>>().First().Server;
                            //var nodesMax = string.Join(",", oracle.NodesVectors.Select(v => v[v.MaximumIndex()]));
                            //var res = string.Join(",",oracle.GlobalVector.Enumerate(collapseDimension).Select(y => y.ToString()));
                            //return r.AsCsvString() + "," + entropy.ToString() + "," + res + "," + nodesMax;
                        })
                        .ForEach(resultCsvFile.WriteLine);
                    }
                }
        }
        public static void RunMilanoPhoneActivity(Random rnd, int numOfNodes, int window,
                                                  ApproximationType approximation, int width,
                                                  int height, GeographicalDistributing distributingMethod,
                                                  string phoneActivityDir, string resultDir)
        {
            var vectorLength         = width * height;
            var hashFunction         = FourwiseIndepandantFunction.Init(rnd);
            var hashFunctionsTable   = HashFunctionTable.Init(numOfNodes, vectorLength, hashFunction);
            var secondMomentFunction = new SecondMoment(width, height);
            var resultPath           =
                PathBuilder.Create(resultDir, "AMS_F2")
                .AddProperty("Dataset", "MilanoPhoneActivity")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Width", width.ToString())
                .AddProperty("Height", height.ToString())
                .AddProperty("Window", window.ToString())
                .AddProperty("Distributing", distributingMethod.Name)
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes) + ",F2"))
            {
                var phonesActivityDataParser   = PhonesActivityDataParser.Create(phoneActivityDir);
                var phonesActivityWindowManger = PhonesActivityWindowManger.Init(window, numOfNodes, vectorLength, hashFunctionsTable, phonesActivityDataParser, distributingMethod);
                var initVectors = phonesActivityWindowManger.GetCurrentVectors();
                var multiRunner = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength, approximation, secondMomentFunction.MonitoredFunction);
                while (phonesActivityWindowManger.TakeStep())
                {
                    var shouldEnd     = new StrongBox <bool>(false);
                    var changeVectors = phonesActivityWindowManger.GetChangeVector();
                    // var sumVector = Vector.SumVector(phonesActivityWindowManger.Window.Value.CurrentNodesCountVectors());
                    //var f2Value = sumVector.IndexedValues.Values.Sum(x => x * x);
                    multiRunner.Run(changeVectors, rnd, false)
                    //    .SideEffect(a => shouldEnd.Value = shouldEnd.Value || (a.MonitoringScheme is MonitoringScheme.Oracle && a.NumberOfFullSyncs > 0))
                    .Select(r => r.AsCsvString())            //+ "," + f2Value)
                    .ForEach(resultCsvFile.WriteLine);
                    //if (shouldEnd.Value)
                    //  break;
                }
            }
        }
Esempio n. 6
0
        public static void RunDatabaseAccesses(Random rnd, int numOfNodes,
                                               int window,
                                               ApproximationType approximation, int vectorLength,
                                               UsersDistributing distributing,
                                               string databaseAccessesPath, string resultDir)
        {
            var resultPath =
                PathBuilder.Create(resultDir, "Entropy")
                .AddProperty("Dataset", "DatabaseAccesses")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("Window", window.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .AddProperty("DistributingMethod", distributing.Name)
                .ToPath("csv");
            var hashUser = new Func <int, int>(userId => userId % numOfNodes);
            var entropy  = new EntropyFunction(vectorLength);

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
                using (var databaseAccessesStatistics = DatabaseAccessesStatistics.Init(databaseAccessesPath, numOfNodes, window, distributing.DistributeFunc))
                {
                    var initProbabilityVectors = databaseAccessesStatistics.InitProbabilityVectors();
                    if (!initProbabilityVectors.All(v => v.Sum().AlmostEqual(1.0, 0.000001)))
                    {
                        throw new Exception();
                    }
                    var multiRunner = MultiRunner.InitAll(initProbabilityVectors, numOfNodes, vectorLength,
                                                          approximation, entropy.MonitoredFunction);
                    while (databaseAccessesStatistics.TakeStep())
                    {
                        var changeProbabilityVectors = databaseAccessesStatistics.GetChangeProbabilityVectors();

                        if (!changeProbabilityVectors.All(v => v.Sum().AlmostEqual(0.0, 0.000001)))
                        {
                            throw new Exception();
                        }
                        multiRunner.Run(changeProbabilityVectors, rnd, true)
                        .Select(r => r.AsCsvString())
                        .ForEach(resultCsvFile.WriteLine);
                    }
                }
        }
Esempio n. 7
0
        public static void RunStocks(Random rnd, MonitoredFunction function, string cbName, int iterations, Tree <long> closestValueQuery, int numOfNodes, int window, DateTime startingDateTime,
                                     int minAmountAtDay, ApproximationType approximation,
                                     string stocksDirPath, string resultDir)
        {
            var vectorLength = closestValueQuery.Data.Length;
            var resultPath   =
                PathBuilder.Create(resultDir, "Entropy_" + cbName)
                .AddProperty("Dataset", "Stocks")
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("Window", window.ToString())
                .AddProperty("StartingTime", startingDateTime.ToShortDateString().Replace('/', '-'))
                .AddProperty("MinAmountAtDay", minAmountAtDay.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
                using (var stocksProbabilityWindow = StocksProbabilityWindow.Init(stocksDirPath, startingDateTime, minAmountAtDay, numOfNodes, window, closestValueQuery))
                {
                    var initProbabilityVectors = stocksProbabilityWindow.CurrentProbabilityVector();
                    if (!initProbabilityVectors.All(v => v.Sum().AlmostEqual(1.0, 0.000001)))
                    {
                        throw new Exception();
                    }
                    var multiRunner = MultiRunner.InitAll(initProbabilityVectors, numOfNodes, vectorLength,
                                                          approximation, function);
                    int i = 0;
                    while (stocksProbabilityWindow.MoveNext() && (i++ < iterations))
                    {
                        var changeProbabilityVectors = stocksProbabilityWindow.CurrentChangeProbabilityVector();

                        if (!changeProbabilityVectors.All(v => v.Sum().AlmostEqual(0.0, 0.000001)))
                        {
                            throw new Exception();
                        }
                        multiRunner.Run(changeProbabilityVectors, rnd, false)
                        .Select(r => r.AsCsvString())
                        .ForEach(resultCsvFile.WriteLine);
                    }
                }
        }
        public static void RunBagOfWords(Random rnd, int vectorLength, string wordsPath,
                                         ApproximationType approximation,
                                         string resultDir,
                                         Func <int, bool> isLeft, string[] textFilesPathes)
        {
            var numOfNodes         = textFilesPathes.Length;
            var windowSize         = 20000;
            var amountOfIterations = 2000;
            var stepSize           = 1000;
            var halfVectorLength   = vectorLength / 2;
            var optionalWords      = File.ReadLines(wordsPath).Take(halfVectorLength).ToArray();
            var optionalStrings    = new SortedSet <string>(optionalWords, StringComparer.OrdinalIgnoreCase);
            var resultPath         =
                PathBuilder.Create(resultDir, "InnerProduct")
                .AddProperty("Dataset", "BagOfWords")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Window", windowSize.ToString())
                .AddProperty("Iterations", amountOfIterations.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
                using (var stringDataParser =
                           TextParser <string> .Init(StreamReaderUtils.EnumarateWords, windowSize, optionalStrings,
                                                     textFilesPathes))
                {
                    var innerProduct = new InnerProductFunction(vectorLength);
                    var initVectors  = stringDataParser.Histograms.Map(h => h.CountVector())
                                       .PadWithZeros(halfVectorLength, isLeft);
                    var multiRunner = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength,
                                                          approximation, innerProduct.MonitoredFunction);
                    var changes = stringDataParser.AllCountVectors(stepSize)
                                  .Select(ch => ch.PadWithZeros(halfVectorLength, isLeft))
                                  .Take(amountOfIterations);
                    multiRunner.RunAll(changes, rnd, true)
                    .Select(r => r.AsCsvString())
                    .ForEach((Action <string>)resultCsvFile.WriteLine);
                }
        }
        public static void RunDatabaseAccesses(Random rnd, int numOfNodes, int window,
                                               ApproximationType approximation, int width,
                                               int height, UsersDistributing distributing,
                                               string databaseAccessesPath, string resultDir)
        {
            var vectorLength         = width * height;
            var hashFunction         = FourwiseIndepandantFunction.Init(rnd);
            var hashFunctionsTable   = HashFunctionTable.Init(numOfNodes, vectorLength, hashFunction);
            var secondMomentFunction = new SecondMoment(width, height);
            var resultPath           =
                PathBuilder.Create(resultDir, "AMS_F2")
                .AddProperty("Dataset", "DatabaseAccesses")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Width", width.ToString())
                .AddProperty("Height", height.ToString())
                .AddProperty("Window", window.ToString())
                .AddProperty("Distributing", distributing.Name)
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
                using (var databaseAccessesStatistics = DatabaseAccessesStatistics.Init(databaseAccessesPath, numOfNodes, window, distributing.DistributeFunc))
                {
                    var initCountVectors = databaseAccessesStatistics.InitCountVectors();
                    var initVectors      = hashFunction.TransformToAMSSketch(initCountVectors, vectorLength, hashFunctionsTable);
                    var multiRunner      = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength,
                                                               approximation, secondMomentFunction.MonitoredFunction);
                    while (databaseAccessesStatistics.TakeStep())
                    {
                        var changeCountVectors = databaseAccessesStatistics.GetChangeCountVectors();
                        var changeVectors      = hashFunction.TransformToAMSSketch(changeCountVectors, vectorLength, hashFunctionsTable);
                        multiRunner.Run(changeVectors, rnd, true)
                        .Select(r => r.AsCsvString())
                        .ForEach(resultCsvFile.WriteLine);
                    }
                }
        }
        public static void RunStocks(Random rnd, int iterations, Tree <long> closestValueQuery, int numOfNodes, int window, int collapseDimension, DateTime startingDateTime,
                                     int minAmountAtDay, ApproximationType approximation,
                                     string stocksDirPath, string resultDir)
        {
            var vectorLength = closestValueQuery.Data.Length;
            var resultPath   =
                PathBuilder.Create(resultDir, "EntropySketch")
                .AddProperty("Dataset", "Stocks")
                .AddProperty("BucketLength", vectorLength.ToString())
                .AddProperty("SketchDimension", collapseDimension.ToString())
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("Window", window.ToString())
                .AddProperty("StartingTime", startingDateTime.ToShortDateString().Replace('/', '-'))
                .AddProperty("MinAmountAtDay", minAmountAtDay.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");
            var entropySketch = new EntropySketchFunction(collapseDimension);

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes) + ",Entropy"))
                using (var stocksProbabilityWindow = StocksProbabilityWindow.Init(stocksDirPath, startingDateTime, minAmountAtDay, numOfNodes, window, closestValueQuery))
                {
                    var initProbabilityVectors = stocksProbabilityWindow.CurrentProbabilityVector().Map(entropySketch.CollapseProbabilityVector);
                    var multiRunner            = MultiRunner.InitAll(initProbabilityVectors, numOfNodes, collapseDimension,
                                                                     approximation, entropySketch.MonitoredFunction);
                    int i = 0;
                    while (stocksProbabilityWindow.MoveNext() && (i++ < iterations))
                    {
                        var changeProbabilityVectors = stocksProbabilityWindow.CurrentChangeProbabilityVector()
                                                       .Map(entropySketch.CollapseProbabilityVector);
                        var entropy = Vector.AverageVector(stocksProbabilityWindow.CurrentProbabilityVector())
                                      .IndexedValues.Values.Sum(p => - p * Math.Log(p));
                        multiRunner.Run(changeProbabilityVectors, rnd, false)
                        .Select(r => r.AsCsvString() + "," + entropy)
                        .ForEach(resultCsvFile.WriteLine);
                    }
                }
        }
        public static void Run(Random rnd, int numOfNodes, int vectorLength, int iterations, ApproximationType approximation, string resultDir)
        {
            var resultPath =
                PathBuilder.Create(resultDir, "Sphere")
                .AddProperty("Data", "Random")
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Iterations", iterations.ToString())
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            Vector[] GetChange()
            {
                Vector GenerateChange()
                {
                    return(ArrayUtils.Init(vectorLength, i => (rnd.NextDouble() - 0.5) / 5).ToVector());
                }

                //  return ArrayUtils.Init(numOfNodes, GenerateChange() : ArrayUtils.Init(vectorLength, _ => 0.0).ToVector());
                return(ArrayUtils.Init(numOfNodes, _ => GenerateChange()));
            }

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
            {
                var sphereFunction = new SphereFunction(vectorLength);
                var initVectors    = Vector.Init(numOfNodes);
                var multiRunner    = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength,
                                                         approximation, sphereFunction.MonitoredFunction);
                multiRunner.OnlySchemes(new MonitoringScheme.Value(), new MonitoringScheme.Distance(2), new MonitoringScheme.Oracle(), new MonitoringScheme.Vector());
                for (int i = 0; i < iterations; i++)
                {
                    multiRunner.Run(GetChange(), rnd, false)
                    .Select(r => r.AsCsvString())
                    .ForEach((Action <string>)resultCsvFile.WriteLine);
                }
            }
        }
        public static void RunTaxiTrips(Random random, int iterations, int sqrtNumOfNodes, int hoursInWindow, ApproximationType approximation, int sqrtVectorLength, DataSplitter <TaxiTripEntry> splitter, CityRegion cityRegion, string taxiBinDataPath, string resultDir)
        {
            var vectorLength = 2 * sqrtVectorLength * sqrtVectorLength;
            var numOfNodes   = sqrtNumOfNodes * sqrtNumOfNodes;
            var resultPath   =
                PathBuilder.Create(resultDir, "InnerProduct")
                .AddProperty("Dataset", "TaxiTrips")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("Window", hoursInWindow.ToString())
                .AddProperty("Iterations", iterations.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .AddProperty("DataSplit", splitter.Name)
                .ToPath("csv");

            using (var resultCsvFile = AutoFlushedTextFile.Create(resultPath, AccumaltedResult.Header(numOfNodes)))
                using (var binaryReader = new BinaryReader(File.OpenRead(taxiBinDataPath)))
                {
                    var innerProductFunction = new InnerProductFunction(vectorLength);
                    var taxiTrips            = TaxiTripEntry.FromBinary(binaryReader);
                    var windowManager        = TaxiTripsWindowManger.Init(hoursInWindow, sqrtNumOfNodes, sqrtVectorLength, splitter, cityRegion, taxiTrips);
                    var initVectors          = windowManager.GetCurrentVectors();
                    var multiRunner          = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength, approximation, innerProductFunction.MonitoredFunction);
                    for (int i = 0; i < iterations; i++)
                    {
                        if (!windowManager.TakeStep())
                        {
                            break;
                        }
                        var changeVectors = windowManager.GetChangeVector();
                        multiRunner.Run(changeVectors, random, false)
                        .Select(r => r.AsCsvString())
                        .ForEach(resultCsvFile.WriteLine);
                    }
                }
        }
Esempio n. 13
0
        public static void RunRandomly(Random rnd, int numOfNodes, ApproximationType approximation, int vectorLength, int collapeDimension, int iterations, bool oneChanges,
                                       string resultDir)
        {
            var entropyResultPath =
                PathBuilder.Create(resultDir, "Entropy")
                .AddProperty("Dataset", "Random")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", vectorLength.ToString())
                .AddProperty("OneChanges", oneChanges.ToString())
                .AddProperty("Iterations", iterations.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");
            var sketchEntropyResultPath =
                PathBuilder.Create(resultDir, "SketchEntropy")
                .AddProperty("Dataset", "Random")
                .AddProperty("Nodes", numOfNodes.ToString())
                .AddProperty("VectorLength", collapeDimension.ToString())
                .AddProperty("OneChanges", oneChanges.ToString())
                .AddProperty("Iterations", iterations.ToString())
                .AddProperty("Approximation", approximation.AsString())
                .ToPath("csv");

            using (var entropyResultCsvFile = AutoFlushedTextFile.Create(entropyResultPath, AccumaltedResult.Header(numOfNodes)))
                using (var sketchEntropyResultCsvFile = AutoFlushedTextFile.Create(sketchEntropyResultPath, AccumaltedResult.Header(numOfNodes)))
                {
                    var sqrt                  = (int)Math.Sqrt(vectorLength);
                    var entropyFunction       = new EntropyFunction(vectorLength);
                    var entropySketchFunction = new EntropySketchFunction(collapeDimension);

                    Func <int, Vector> genVector = node => ArrayUtils.Init(vectorLength, rnd.NextDouble())
                                                   .ToVector().NormalizeInPlaceToSum(1.0);

                    var initVectors       = ArrayUtils.Init(numOfNodes, genVector);
                    var sketchInitVectors = initVectors.Map(entropySketchFunction.CollapseProbabilityVector);
                    var globalVector      = Vector.AverageVector(initVectors);

                    Vector getChangeVector(int node)
                    {
                        if (oneChanges && node >= 1)
                        {
                            return(new Vector());
                        }

                        var    someVector   = genVector(node);
                        Vector changeVector = someVector - globalVector;

                        if (!oneChanges)
                        {
                            changeVector.DivideInPlace(numOfNodes);
                        }

                        globalVector.AddInPlace(changeVector / numOfNodes);
                        return(changeVector);
                    }

                    var entropyMultiRunner = MultiRunner.InitAll(initVectors, numOfNodes, vectorLength,
                                                                 approximation, entropyFunction.MonitoredFunction);
                    var sketchEntropyMultiRunner = MultiRunner.InitAll(sketchInitVectors, numOfNodes, vectorLength,
                                                                       approximation, entropySketchFunction.MonitoredFunction);
                    var shouldStop = new StrongBox <bool>(false);
                    for (int i = 0; i < iterations; i++)
                    {
                        var changes       = ArrayUtils.Init(numOfNodes, getChangeVector);
                        var sketchChanges = changes.Map(entropySketchFunction.CollapseProbabilityVector);

//entropyMultiRunner.Run(changes, rnd, false)
//.SideEffect(r => shouldStop.Value = shouldStop.Value || (r.MonitoringScheme is MonitoringScheme.Oracle && r.NumberOfFullSyncs >= 8))
//.Select(r => r.AsCsvString())
//.ForEach((Action<string>)entropyResultCsvFile.WriteLine);

                        sketchEntropyMultiRunner.Run(sketchChanges, rnd, false)
                        .SideEffect(r => shouldStop.Value = shouldStop.Value || (r.MonitoringScheme is MonitoringScheme.Oracle && r.NumberOfFullSyncs >= 8))
                        .Select(r => r.AsCsvString())
                        .ForEach((Action <string>)sketchEntropyResultCsvFile.WriteLine);
                        if (shouldStop.Value)
                        {
                            break;
                        }
                    }
                }
        }