Ejemplo n.º 1
0
        private static void AnalyseRowValue(MatrixRowSummary summary, int i, int j, ushort percent, SortedDictionary <ushort, ValueCluster> lookup)
        {
            if (j == i && percent > 0)
            {
                summary.SelfPercent = percent;
            }

            if (percent == 0)
            {
                summary.NoOfZeroPercents += 1;
            }
            else
            {
                if (lookup.TryGetValue(percent, out ValueCluster found))
                {
                    found.NoOfTimes += 1;
                }
                else
                {
                    found = new ValueCluster
                    {
                        Value     = percent,
                        First     = j,
                        NoOfTimes = 1,
                    };

                    lookup.Add(percent, found);
                }


                summary.NoOfNonZeroPercents += 1;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="summary"></param>
        /// <param name="groupIndex">Always 0 or 1 if true; -1 if false </param>
        /// <returns></returns>
        public static bool MaybeSecondaryOptimization(MatrixRowSummary summary, out int outIndex)
        {
            if (summary.Clusters.Length != 2)
            {
                outIndex = -1;
                return(false);
            }

            if (!DetrimineGroupIndexInPair(summary, out int groupIndex))
            {
                outIndex = -1;
                return(false);
            }

            var otherGroup = summary.Clusters[GetOtherGroupIndex(groupIndex)];

            if (summary.NoOfNonZeroPercents != (otherGroup.NoOfTimes + 1))
            {
                outIndex = groupIndex;
                return(false);
            }
            else
            {
                outIndex = -1;
                return(true);
            }
        }
Ejemplo n.º 3
0
        public static SolutionType FindPrimarySolution(ushort rowDenominator, MatrixRowSummary summary)
        {
            if (summary.NoOfStates == summary.NoOfZeroPercents)
            {
                return(SolutionType.NoOperation);
            }

            if (summary.Clusters.Length == 1)
            {
                if (summary.Clusters[0].Value == rowDenominator && summary.Clusters[0].NoOfTimes == 1)
                {
                    return(summary.Row == summary.Clusters[0].First ? SolutionType.DeadEnd : SolutionType.Redirect);
                }

                if (summary.NoOfNonZeroPercents == summary.NoOfStates)
                {
                    return(SolutionType.EvenAll);
                }

                if (!summary.SelfPercent.HasValue &&
                    summary.NoOfNonZeroPercents == summary.NoOfStates - 1)
                {
                    return(SolutionType.EvenOut);
                }
            }

            if (summary.NoOfNonZeroPercents < summary.NoOfStates)
            {
                return(SolutionType.Sparse);
            }

            return(SolutionType.Unoptimized);
        }
Ejemplo n.º 4
0
        private int GetLeft(SolutionType approach, MatrixRowSummary summary)
        {
            switch (approach)
            {
            case SolutionType.EvenOut:
                return(summary.Row + 1);

            default:
                return(default(int));
            }
        }
Ejemplo n.º 5
0
        private static IDictionary <ushort, ValueCluster> GetClustersVia2DArray(MatrixRowSummary summary, int noOfStates, int i, ushort[,] rowValues)
        {
            var lookup = new SortedDictionary <ushort, ValueCluster>(
                new HigherPercentComparer());

            for (int j = 0; j < noOfStates; j += 1)
            {
                AnalyseRowValue(summary, i, j, rowValues[i, j], lookup);
            }

            return(lookup);
        }
Ejemplo n.º 6
0
        public MatrixRowSolution Evaluate(ushort rowDenominator, MatrixRowSummary summary)
        {
            var solutionType = FindPrimarySolution(rowDenominator, summary);

            return(new MatrixRowSolution
            {
                Approach = solutionType,
                RowDenominator = rowDenominator,
                Branch = GetBranch(solutionType, summary),
                Left = GetLeft(solutionType, summary),
                Domain = GetDomain(solutionType, summary),
            });
        }
Ejemplo n.º 7
0
 public static bool DetrimineGroupIndexInPair(MatrixRowSummary summary, out int groupIndex)
 {
     groupIndex = 0;
     if (summary.Clusters[groupIndex].NoOfTimes != 1)
     {
         groupIndex = 1;
         if (summary.Clusters[groupIndex].NoOfTimes != 1)
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 8
0
        private static IDictionary <ushort, ValueCluster> GetClustersVia1DArray(MatrixRowSummary summary, int i, ushort[] rowValues)
        {
            var lookup = new SortedDictionary <ushort, ValueCluster>(
                new HigherPercentComparer());
            int noOfValues = rowValues.Length;

            for (int j = 0; j < noOfValues; j += 1)
            {
                ushort value = rowValues[j];
                AnalyseRowValue(summary, i, j, value, lookup);
            }

            return(lookup);
        }
Ejemplo n.º 9
0
        private MatrixRowSummary Investigate2DArray(int i, int noOfStates, ushort[,] matrix)
        {
            var summary = new MatrixRowSummary
            {
                Row        = i,
                NoOfStates = noOfStates,
            };

            var clusters = GetClustersVia2DArray(summary, noOfStates, i, matrix);

            CopyClusters(clusters, summary);

            return(summary);
        }
Ejemplo n.º 10
0
        public MatrixRowSummary Investigate1DArray(int i, ushort[] rowValues)
        {
            // Check if all zeros

            var summary = new MatrixRowSummary
            {
                Row        = i,
                NoOfStates = rowValues.Length,
            };

            var clusters = GetClustersVia1DArray(summary, i, rowValues);

            CopyClusters(clusters, summary);

            return(summary);
        }
Ejemplo n.º 11
0
 public MatrixRowSolution SetupPrimaryLevel(
     SolutionType solutionType,
     MatrixRowSummary summary,
     ushort rowDenominator,
     int treeIndex)
 {
     return(new MatrixRowSolution
     {
         Approach = solutionType,
         RowDenominator = rowDenominator,
         Branch = GetBranch(solutionType, summary),
         Left = GetLeft(solutionType, summary),
         Domain = GetDomain(solutionType, summary),
         Tree = treeIndex,
     });
 }
Ejemplo n.º 12
0
        private static int GetBranch(SolutionType approach, MatrixRowSummary summary)
        {
            switch (approach)
            {
            case SolutionType.DeadEnd:
            case SolutionType.Redirect:
                return(summary.Clusters[0].First);

            case SolutionType.EvenAll:
            case SolutionType.EvenOut:
                return(summary.Row);

            default:
                return(default(int));
            }
        }
Ejemplo n.º 13
0
        private int GetDomain(SolutionType approach, MatrixRowSummary summary)
        {
            switch (approach)
            {
            case SolutionType.EvenAll:
            {
                return(Solver.WrapRange(0, summary.NoOfNonZeroPercents - 1, summary.NoOfStates));
            }

            case SolutionType.EvenOut:
                return(Solver.WrapRange(
                           summary.Row + 1,
                           summary.Row - 1,
                           summary.NoOfStates));

            default:
                return(default(int));
            }
        }
Ejemplo n.º 14
0
        private bool InsertSecondarySolution(List <MatrixRowSolution> dest, MatrixRowSummary summary, ushort rowDenominator, bool[] checks, ushort percentValue)
        {
            if (mSecondary.IsOptimizable(summary, checks, out IslandResult result))
            {
                var secondary = new MatrixRowSolution
                {
                    Approach       = SolutionType.SecondaryOptimization,
                    Branch         = result.Peak,
                    Left           = result.Left,
                    Domain         = result.Right,
                    RowDenominator = rowDenominator,
                    Cutoff         = percentValue,
                };

                dest.Add(secondary);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 15
0
        public bool IsOptimizable(MatrixRowSummary summary, bool[] queries, out IslandResult result)
        {
            if (summary.Clusters.Length != 2)
            {
                result = new IslandResult
                {
                    Status = IslandOptimizationStatus.Invalid,
                };
                return(false);
            }

            var peakGroupIndex  = summary.Clusters[0].NoOfTimes == 1 ? 0 : 1;
            var otherGroupIndex = (peakGroupIndex == 0) ? 1 : 0;

            var otherGroup = summary.Clusters[otherGroupIndex];

            if (summary.NoOfNonZeroPercents != (otherGroup.NoOfTimes + 1))
            {
                result = new IslandResult
                {
                    Status = IslandOptimizationStatus.Invalid,
                };
                return(false);
            }

            //var queries = new bool[summary.NoOfStates];

            //// SETUP UP CHECKS
            //for (var i = 0; i < summary.NoOfStates; i += 1)
            //{
            //    queries[i] = (source[i] == otherGroup.Value);
            //}

            result = Examine(queries, summary.Clusters[peakGroupIndex].First);
            return(result.Status == IslandOptimizationStatus.EvenAll);
        }
Ejemplo n.º 16
0
        private void InsertSolutionVia2DMatrix(SolutionType solutionType, List <MatrixRowSolution> dest, List <RowTree> trees, ushort[,] matrix, int i, MatrixRowSummary summary, ushort rowDenominator)
        {
            var treeIndex = GenerateTreeFromMatrix(solutionType, trees, i, matrix);
            var item      = SetupPrimaryLevel(solutionType, summary, rowDenominator, treeIndex);

            dest.Add(item);
        }
Ejemplo n.º 17
0
        private void InsertSolutionVia1DArray(SolutionType solutionType, List <MatrixRowSolution> dest, List <RowTree> trees, ushort[] input, MatrixRowSummary summary, ushort rowDenominator)
        {
            var treeIndex = GenerateTreeFromArray(solutionType, trees, input);
            var primary   = SetupPrimaryLevel(solutionType, summary, rowDenominator, treeIndex);

            dest.Add(primary);
        }
Ejemplo n.º 18
0
 private static void CopyClusters(IDictionary <ushort, ValueCluster> src, MatrixRowSummary dest)
 {
     dest.Clusters = new ValueCluster[src.Count];
     src.Values.CopyTo(dest.Clusters, 0);
 }