예제 #1
0
        /// <summary>
        /// Compute the Addition operation between the specified two set.
        /// </summary>
        /// <param name="oneSet">The one set.</param>
        /// <param name="otherSet">The other set.</param>
        /// <returns>The set.</returns>
        public static Set Addition(Set oneSet, Set otherSet)
        {
            // newSet which will be as a result of this operator
            Set newSet = new Set(oneSet.Modulo);
            int newItem;
            int newFactor = 1;

            // Optimization: if |M1| + |M2| > modulo -> |M1| + |M2| = {0..modulo}
            if (oneSet.Count + otherSet.Count > oneSet.Modulo)
            {
                newSet.AddRange(GenerationAlgorithmDSAUtil.createSequenceOfNumber(oneSet.Modulo));
                return(newSet);
            }

            // for each pair item1 and item2 calculate new item
            foreach (int item1 in oneSet.DiscreteSet)
            {
                foreach (int item2 in otherSet.DiscreteSet)
                {
                    // calculate the new item
                    newItem = modifyNumberModulo((item1 + item2), oneSet.Modulo);
                    // calculate the new solutionFactor, NOT NEEDED a MinFactor

                    /*
                     * if (oneSet.MinimizationFactor.ContainsKey(item1) && otherSet.MinimizationFactor.ContainsKey(item2))
                     * {
                     *  newFactor = (int)oneSet.MinimizationFactor[item1] + (int)otherSet.MinimizationFactor[item2];
                     * }
                     * else if (oneSet.MinimizationFactor.ContainsKey(item1))
                     * {
                     *  newFactor = (int)oneSet.MinimizationFactor[item1];
                     * }
                     * else if (otherSet.MinimizationFactor.ContainsKey(item2))
                     * {
                     *  newFactor = (int)otherSet.MinimizationFactor[item2];
                     * }
                     * else
                     * {
                     *  newSet.Add(newItem);
                     *  continue;
                     * }
                     */
                    //newSet.Add(newItem, newFactor);
                    newSet.Add(newItem);
                }
            }

            return(newSet);
        }
        // TODO: move to the IPropagator Iface.

        /// <summary>
        /// Runs the propagation algorithm.
        /// Create discrete set for constraints, normalize and merge them, and propagate constraints' sets in matrix.
        /// </summary>
        /// <param name="originalConstraints">The original constraints.</param>
        /// <param name="constraintSetsCreator">The constraint sets creator.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static PropagationResult runPropagationAlgorithm(List <Constraint> originalConstraints, IConstraintSetsCreator constraintSetsCreator, int size)
        {
            // create working copy of constraints

            List <Constraint> constraints = GenerationAlgorithmDSAUtil.cloneConstraints(originalConstraints);

            //------createConstraintSet-potential-set-for-constraints-----------------------

            constraints = constraintSetsCreator.createConstraintSets(constraints, size);

            //------modification-constraints----------------------------------

            //LogUtil.printConstraintsToFile(constraints, "originalConstraints");

            // normalize constraints
            constraints = ConstraintUtil.normalizeConstraints(constraints);

            //LogUtil.printConstraintsToFile(constraints, "normalizedConstraints");

            // find equivalent constraints
            List <List <Constraint> > groupOfconstraints = ConstraintUtil.findEquivalentConstraints(constraints);

            // try to merge them
            constraints = ConstraintUtil.mergeEquivalentConstrains(groupOfconstraints);

            //LogUtil.printConstraintsToFile(constraints, "mergedConstraints");

            // createConstraintSet a hashtable only of all trainLines used in constraints
            List <TrainLine> trainLinesMap = GenerationAlgorithmDSAUtil.createTrainLineMap(constraints);

            // store constraints - into constraintCache
            ConstraintCache.getInstance().setCacheContent(constraints);
            // create matrix of discrete sets
            Set[,] setMatrix = GenerationAlgorithmDSAUtil.createDiscretSetMatrix(constraints, trainLinesMap);


            // createConstraintSet constraint's matrix
            //this.constraintMatrix = GenerationAlgorithmPESPUtil.createConstraintMatrix(constraints, trainLinesMap);



            //-------propagation-part-of-algorithm--------------------------------------------------------
            PropagationUtil.propagate(setMatrix, trainLinesMap);

            return(new PropagationResult(setMatrix, trainLinesMap));
        }
        /// <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();
        }
 /// <summary>
 /// Runs the construction timetable algorithm.
 /// Constructs timetables from solution.
 /// </summary>
 /// <param name="solutions">The solutions.</param>
 /// <param name="trainLineMap">The train line map.</param>
 /// <param name="timetables">The timetables.</param>
 /// <param name="note">The note.</param>
 public void runConstructionTimetableAlgorithm(List <Solution> solutions, List <TrainLine> trainLineMap, List <Timetable> timetables, int stepCount, String note, TimeSpan runningTime)
 {
     GenerationAlgorithmDSAUtil.constructTimetables(solutions, trainLineMap, timetables, stepCount, note, runningTime);
 }