private double CalculateTargetFunctionalValue(PartitionSettings partitionSettings, RenderTexture muRenderTexture)
        {
            if (!_calculateTargetFunctionalValue)
            {
                return(double.MinValue);
            }

            var muGridGetters         = _textureToGridConverter.GetGridValueInterpolators(muRenderTexture, partitionSettings);
            var targetFunctionalValue = new TargetFunctionalCalculator(partitionSettings).CalculateFunctionalValue(muGridGetters);

            Logger.Info($"Target functional value = {targetFunctionalValue}");
            return(targetFunctionalValue);
        }
        public void MuGridsFixedPartition8x8Test()
        {
            const string f = "Logs.txt";

            if (File.Exists(f))
            {
                File.Delete(f);
            }
            Trace.Listeners.Add(new TextWriterTraceListener(f));

            var settings = new PartitionSettings
            {
                SpaceSettings = new SpaceSettings
                {
                    MinCorner = VectorUtils.CreateVector(0, 0),
                    MaxCorner = VectorUtils.CreateVector(10, 10),
                    GridSize  = new List <int> {
                        80, 80
                    },
                    DensityType            = DensityType.Everywhere1,
                    MetricsType            = MetricsType.Euclidean,
                    CustomDensityFunction  = null,
                    CustomDistanceFunction = null
                },
                CentersSettings = new CentersSettings
                {
                    CentersCount = 2,
                    CenterDatas  = new List <CenterData>
                    {
                        new CenterData
                        {
                            A        = 0,
                            W        = 1,
                            Position = VectorUtils.CreateVector(1, 1),
                            IsFixed  = true
                        },
                        new CenterData
                        {
                            A = 0,
                            W = 1,
                            //Position = VectorUtils.CreateVector(2, 4),
                            Position = VectorUtils.CreateVector(9, 1),
                            IsFixed  = true
                        },
                    }
                },
                FuzzyPartitionFixedCentersSettings = new FuzzyPartitionFixedCentersSettings
                {
                    GradientEpsilon    = 0.001,
                    GradientStep       = 10,
                    MaxIterationsCount = 400
                },
                FuzzyPartitionPlacingCentersSettings = new FuzzyPartitionPlacingCentersSettings
                {
                    GaussLegendreIntegralOrder = 4
                }
            };

            var calculator = new FuzzyPartitionFixedCentersAlgorithm(settings);
            var partition  = calculator.BuildPartition(out var psiGrid);

            Trace.WriteLine($"PerformedIterationsCount = {calculator.PerformedIterationsCount}");
            var muValueGetters = partition.Select(v => new GridValueInterpolator(settings.SpaceSettings, new MatrixGridValueGetter(v))).ToList();

            var targetFunctionalCalculator = new TargetFunctionalCalculator(settings);
            var targetFunctionalValue      = targetFunctionalCalculator.CalculateFunctionalValue(muValueGetters);

            Trace.WriteLine($"Target functional value = {targetFunctionalValue}\n");

            var dualFunctionalCalculator = new DualFunctionalCalculator(settings, psiGrid.ToGridValueInterpolator(settings));
            var dualFunctionalValue      = dualFunctionalCalculator.CalculateFunctionalValue();

            Trace.WriteLine($"Dual functional value = {dualFunctionalValue}\n");

            Trace.WriteLine("Center #1 Mu matrix:");
            MatrixUtils.WriteMatrix(partition[0], WriteLine, 3);

            Trace.WriteLine("Center #2 Mu matrix:");
            MatrixUtils.WriteMatrix(partition[1], WriteLine, 3);

            var sum = partition.Aggregate((a, b) => a + b);

            Trace.WriteLine("Sum mu matrix:");
            MatrixUtils.WriteMatrix(sum, WriteLine, 3);
            Trace.Flush();

            Assert.AreEqual(targetFunctionalValue, dualFunctionalValue, 1d);

            var q = 5;
        }
Example #3
0
        public void Grid3x3PlacingPartitionTest()
        {
            var f = "Logs.txt";

            if (File.Exists(f))
            {
                File.Delete(f);
            }
            Trace.Listeners.Add(new TextWriterTraceListener(f));

            var settings = new PartitionSettings
            {
                IsCenterPlacingTask = false,
                SpaceSettings       = new SpaceSettings
                {
                    MinCorner = VectorUtils.CreateVector(0, 0),
                    MaxCorner = VectorUtils.CreateVector(10, 10),
                    GridSize  = new List <int> {
                        8, 8
                    },
                    DensityType            = DensityType.Everywhere1,
                    MetricsType            = MetricsType.Euclidean,
                    CustomDensityFunction  = null,
                    CustomDistanceFunction = null
                },
                CentersSettings = new CentersSettings
                {
                    CentersCount = 2,
                    CenterDatas  = new List <CenterData>
                    {
                        new CenterData
                        {
                            A        = 0,
                            W        = 1,
                            Position = VectorUtils.CreateVector(2, 5),
                            IsFixed  = true
                        },
                        new CenterData
                        {
                            A        = 0,
                            W        = 1,
                            Position = VectorUtils.CreateVector(8, 5),
                            IsFixed  = true
                        },
                    }
                },
                FuzzyPartitionFixedCentersSettings = new FuzzyPartitionFixedCentersSettings
                {
                    GradientEpsilon    = 0.01,
                    GradientStep       = 0.1,
                    MaxIterationsCount = 400
                },
                FuzzyPartitionPlacingCentersSettings = new FuzzyPartitionPlacingCentersSettings
                {
                    CentersDeltaEpsilon        = 0.1,
                    GaussLegendreIntegralOrder = 32
                },
                RAlgorithmSettings = new RAlgorithmSettings
                {
                    SpaceStretchFactor = 2,
                    H0 = 1,
                    MaxIterationsCount = 30
                }
            };

            var zeroTaus = new List <Vector <double> >
            {
                //VectorUtils.CreateVector(2, 5),
                //VectorUtils.CreateVector(8, 5)
                VectorUtils.CreateVector(1, 1),
                VectorUtils.CreateVector(10, 10)
            };

            var calculator       = new FuzzyPartitionFixedCentersAlgorithm(settings);
            var partition        = calculator.BuildPartition();
            var muValueGetters   = partition.Select(v => new GridValueInterpolator(settings.SpaceSettings, new MatrixGridValueGetter(v))).ToList();
            var placingAlgorithm = new FuzzyPartitionPlacingCentersAlgorithm(settings, zeroTaus, muValueGetters);

            while (true)
            {
                placingAlgorithm.DoIteration(muValueGetters);

                var centers = placingAlgorithm.GetCenters();

                var targetFunctionalCalculator = new TargetFunctionalCalculator(settings);
                var targetFunctionalValue      = targetFunctionalCalculator.CalculateFunctionalValue(muValueGetters);
                Trace.WriteLine($"Target functional value = {targetFunctionalValue}\n");

                for (int i = 0; i < settings.CentersSettings.CentersCount; i++)
                {
                    settings.CentersSettings.CenterDatas[i].Position = centers[i].Clone();
                    Trace.WriteLine($"Center #{i + 1}: {centers[i][0]:0.00} {centers[i][1]:0.00}");
                }

                calculator = new FuzzyPartitionFixedCentersAlgorithm(settings);
                partition  = calculator.BuildPartition();

                if (placingAlgorithm.IsStopConditionSatisfied())
                {
                    break;
                }
            }

            var list = placingAlgorithm.GetCenters();

            for (var index = 0; index < list.Count; index++)
            {
                var center = list[index];

                Trace.WriteLine($"Center #{index + 1}: {center[0]:0.00} {center[1]:0.00}");
            }

            Trace.Flush();

            var vec = VectorUtils.CreateVector(44, 4, 4, 4, 4);
            var x   = 4;
        }
Example #4
0
        private void ShowMuGridsInfo()//IList<IGridCellValueGetter> muGrids, IGridCellValueGetter psiGrid)
        {
            if (!_showMuGridsInfo)
            {
                return;
            }

            List <Matrix <double> > muGridsCPU = null;

            if (_compareWithCpuMuGrid)
            {
                muGridsCPU = new FuzzyPartitionFixedCentersAlgorithm(Settings).BuildPartition(out var psiGrid);
                //var muGrids2 = algorithm.BuildPartition().Select(v => new MatrixGridValueGetter(v)).ToList();rg

                var targetFunctionalValueFromCpu = new TargetFunctionalCalculator(Settings)
                                                   .CalculateFunctionalValue(muGridsCPU.Select(v => new GridValueInterpolator(Settings.SpaceSettings, new MatrixGridValueGetter(v))).ToList());
                Logger.Trace($"Target functional value by CPU: {targetFunctionalValueFromCpu}");

                var dualFunctionalValueFromCpu = new DualFunctionalCalculator(Settings, new GridValueInterpolator(Settings.SpaceSettings, new MatrixGridValueGetter(psiGrid))).CalculateFunctionalValue();
                Logger.Trace($"Dual functional value by CPU: {dualFunctionalValueFromCpu}");
            }

            var muGrids = _textureToGridConverter.GetGridCellsGetters(_muGridsTexture, Settings);

            for (var index = 0; index < muGrids.Count; index++)
            {
                if (_compareWithCpuMuGrid)
                {
                    var muGridCPU = muGridsCPU[index];
                    Logger.Trace($"Mu Matrix by CPU for the center #{index + 1}");
                    MatrixUtils.WriteMatrix(
                        Settings.SpaceSettings.GridSize[0],
                        Settings.SpaceSettings.GridSize[1],
                        (i, i1) => muGridCPU[i, i1],
                        Logger.Trace,
                        3);
                }

                var muGrid = muGrids[index];

                Logger.Trace($"Mu Matrix by GPU for the center #{index + 1}");
                MatrixUtils.WriteMatrix(
                    Settings.SpaceSettings.GridSize[0],
                    Settings.SpaceSettings.GridSize[1],
                    (i, i1) => muGrid.GetValue(i, i1),
                    Logger.Trace,
                    3);
            }

            Logger.Trace("Sum mu matrix:");
            MatrixUtils.WriteMatrix(
                Settings.SpaceSettings.GridSize[0],
                Settings.SpaceSettings.GridSize[1],
                (i, i1) =>
            {
                var v = 0d;
                foreach (var t in muGrids)
                {
                    v += t.GetValue(i, i1);
                }
                return(v);
            },
                Logger.Trace,
                3);

            var psiGridCalculator = _textureToGridConverter.GetGridValueTextureCalculator(_psiGridTexture, Settings);

            Logger.Trace("Psi matrix:");
            MatrixUtils.WriteMatrix(
                Settings.SpaceSettings.GridSize[0],
                Settings.SpaceSettings.GridSize[1],
                (i, i1) => psiGridCalculator.GetValue(i, i1),
                Logger.Trace,
                3);

            for (var i = 0; i < Settings.CentersSettings.CentersCount; i++)
            {
                Logger.Trace($"Computed Mu grid #{i + 1} from Psi grid:");
                MatrixUtils.WriteMatrix(
                    Settings.SpaceSettings.GridSize[0],
                    Settings.SpaceSettings.GridSize[1],
                    (xIndex, yIndex) =>
                {
                    var psiValue     = psiGridCalculator.GetValue(xIndex, yIndex);
                    var xRatio       = (double)xIndex / (Settings.SpaceSettings.GridSize[0] - 1d);
                    var yRatio       = (double)yIndex / (Settings.SpaceSettings.GridSize[1] - 1d);
                    var ratioPoint   = VectorUtils.CreateVector(xRatio, yRatio);
                    var point        = Settings.SpaceSettings.MinCorner + ratioPoint.PointwiseMultiply(Settings.SpaceSettings.MaxCorner - Settings.SpaceSettings.MinCorner);
                    var densityValue = 1d;
                    var distance     = (Settings.CentersSettings.CenterDatas[i].Position - point).L2Norm();
                    var newMuValue   = -psiValue / (2d * densityValue * (distance / 1d + 0d));
                    return(newMuValue);
                },
                    Logger.Trace,
                    3);
            }
        }