/// <summary>
        /// Creates the solutionFactor record when optimizing the order of the backtracking variable labeling.
        /// </summary>
        /// <param name="set">Set of constraints.</param>
        /// <param name="record">Record describing how the @c set of constraints is suitable for the labeling.</param>
        /// <returns>True iff the record has been created.</returns>
        public static bool createFactorRecord(Set set, ref FactorRangeRecord record)
        {
            if (set.MinimizationFactor.Count <= 1)
            {
                return(false);
            }

            int maxVal = int.MinValue;
            int minVal = int.MaxValue;

            foreach (int factor in set.MinimizationFactor.Values)
            {
                if (maxVal < factor)
                {
                    maxVal = factor;
                }

                if (minVal > factor)
                {
                    minVal = factor;
                }
            }

            record.RangeFactor = maxVal - minVal;
            return(true);
        }
Esempio n. 2
0
        public static Boolean searchForBestRecord(Set[,] discreteSetMatrix, out FactorRangeRecord resultFactorRangeRecord, SelectBestRecord selectBestRecord)
        {
            Boolean bestRecordsFound = true;

            // Find the top list of possible choices of Set with maximum minFactor range.
            List <FactorRangeRecord> bestRecords = searchForTheBestRecords(discreteSetMatrix);

            // If no best record found
            if (bestRecords.Count.Equals(0))
            {
                // set boolean
                bestRecordsFound = false;
                // return empty record
                resultFactorRangeRecord = new FactorRangeRecord();
            }
            else
            {
                // select one of the best record
                FactorRangeRecord bestRecord = selectBestRecord(bestRecords);
                Set currentSet = bestRecord.Set;

                // from selected set choose pair with min value
                KeyValuePair <int, int> minKeyValuePair = currentSet.Min();

                // selected solutionFactor record set as result
                resultFactorRangeRecord = bestRecord;
                // set item of set with min solutionFactor
                resultFactorRangeRecord.MinItemOfSet = minKeyValuePair.Key;
            }

            return(bestRecordsFound);
        }
Esempio n. 3
0
        public static List <FactorRangeRecord> searchForTheBestRecords(Set[,] s)
        {
            // Create the list of MAX_RULETTE_COUNT best records.
            List <FactorRangeRecord> bestRecords = new List <FactorRangeRecord>();
            int currentWorst = 0;

            // Search for the best records.
            for (int i = 0, rows = s.GetLength(0); i != rows; ++i)
            {
                for (int j = i + 1, cols = s.GetLength(1); j != cols; ++j)
                {
                    FactorRangeRecord currentRecord = new FactorRangeRecord
                    {
                        Row = i,
                        Col = j,
                        Set = s[i, j]
                    };
                    if (!FactorRangeRecord.createFactorRecord(s[i, j], ref currentRecord))
                    {
                        continue;
                    }

                    if (currentRecord.RangeFactor >= currentWorst)
                    {
                        // Insert - create a space.
                        bestRecords.Add(currentRecord);
                        int k = bestRecords.Count - 1;
                        // Shift them - so that they are sorted.
                        while (k > 0 && bestRecords[k - 1].RangeFactor < currentRecord.RangeFactor)
                        {
                            bestRecords[k] = bestRecords[k - 1];
                            --k;
                        }
                        // Last assignment places the current record at the right position.
                        bestRecords[k] = currentRecord;

                        // If we have too many elements remove them.
                        if (bestRecords.Count > MAX_RULETTE_COUNT)
                        {
                            bestRecords.RemoveAt(MAX_RULETTE_COUNT);
                        }

                        // Remember the current worst possibility.
                        currentWorst = bestRecords[bestRecords.Count - 1].RangeFactor;
                    }
                }
            }

            return(bestRecords);
        }
        /// <summary>
        /// Fixes the one potential of set in matrix according the founded best record.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="bestRecord">The best record.</param>
        /// <returns>The changed matrix.</returns>
        private Set[,] fixOnePotentialOfSetInMatrix(Set[,] matrix, FactorRangeRecord bestRecord)
        {
            // copy the matrix
            Set[,] newMatrix = GenerationAlgorithmDSAUtil.cloneDiscreteSetMatrix(matrix);

            // create a singleton set. Fix minute which corresponds with minimal factor value
            Set newSet = new Set(bestRecord.Set.Modulo);

            // add only one item with facotr, fixed item
            newSet.Add(bestRecord.MinItemOfSet, bestRecord.Set.MinimizationFactor[bestRecord.MinItemOfSet]);

            // replace changed Set also in matrix
            newMatrix[bestRecord.Row, bestRecord.Col] = newSet;
            newMatrix[bestRecord.Col, bestRecord.Row] = new Set(newSet);
            newMatrix[bestRecord.Col, bestRecord.Row].Reverse();

            return(newMatrix);

            //// Current set to be shortened
            //Set currentSet = best.Set;

            //// Item of Set which will be fixed.
            //int minute = best.MinItemOfSet;

            //// TODO: Use the branch and bound prunning.

            //// Copy the matrix.
            //Set[,] newMatrix = GenerationAlgorithmDSAUtil.cloneDiscreteSetMatrix(discreteSetMatrix);

            //// Create a singleton set. Fix minute which corresponds with minimal factor value.
            //Set newSet = new Set(currentSet.Modulo);
            //// Add only one item, fixed item.
            //newSet.Add(minute, currentSet.MinimizationFactor[minute]);

            //// Remeber to the new matrix.
            //newMatrix[best.Row, best.Col] = newSet;
            //newMatrix[best.Col, best.Row] = new Set(newSet);
            //newMatrix[best.Col, best.Row].Reverse();
        }
Esempio n. 5
0
        public static FactorRangeRecord probabilisticChoice(List <FactorRangeRecord> bestRecords)
        {
            FactorRangeRecord selectedRecord = new FactorRangeRecord();

            // calculate sum of range factors
            int sumOfRangeFactors = bestRecords.Sum(bestRecord => bestRecord.RangeFactor);
            // generate random number from {0..sumOfRangeFactors}
            int randomNumber = random.Next(sumOfRangeFactors + 1);

            // current sum
            int currentSum = 0;
            int index      = 0;

            // current sum is less then random number and index in range
            while (currentSum <= randomNumber && index < bestRecords.Count)
            {
                currentSum    += bestRecords[index].RangeFactor;
                selectedRecord = bestRecords[index];
                index++;
            }

            return(selectedRecord);
        }
Esempio n. 6
0
 /// <summary>
 /// Finds the best record used probabilistic method.
 /// Firstly choose randomly with certain probability the Set with respect to range of minFactr, the pick the min value.
 /// </summary>
 /// <returns></returns>
 public static Boolean probableSearchForBestRecord(Set[,] discreteSetMatrix, out FactorRangeRecord resultFactorRangeRecord)
 {
     return(searchForBestRecord(discreteSetMatrix, out resultFactorRangeRecord, probabilisticChoice));
 }
Esempio n. 7
0
 /// <summary>
 /// Finds the best record used deterministic method.
 /// Firstly choose the biggest range of minFactor in Set, then pick the Min value from that range.
 /// </summary>
 /// <param name="discreteSetMatrix">The discrete set matrix.</param>
 /// <returns></returns>
 public static Boolean deterministicSearchForBestRecord(Set[,] discreteSetMatrix, out FactorRangeRecord resultFactorRangeRecord)
 {
     return(searchForBestRecord(discreteSetMatrix, out resultFactorRangeRecord, deterministicChoice));
 }