예제 #1
0
        /// <summary>
        /// Runs the bisection algorithm for initial phase of constraint propagation.
        /// </summary>
        /// <param name="constraints">List of constraints.</param>
        /// <param name="constraintSetsCreator">Creator of the sets of constraints.</param>
        /// <param name="lowerBoundStart">Lower bound for the bisection search.</param>
        /// <param name="upperBoundStart">Upper bound for the bisection search.</param>
        /// <returns>Result of the initial algorithm phase.</returns>
        public static PropagationResult Bisect(List <Constraint> constraints, IConstraintSetsCreator constraintSetsCreator, int lowerBoundStart, int upperBoundStart)
        {
            // create the result.
            PropagationResult result = new PropagationResult();

            //-------bisection-algorithm-for-finding-the-proper-bound-for-discret-set------
            // set default lower and upper bounds
            int lowerBound = lowerBoundStart;
            int upperBound = upperBoundStart;
            // set the boolean loop variable
            Boolean loop = true;

            int midpoint = 0;

            // loop while bounds are not crossed and loop
            while ((upperBound - lowerBound) > 1 && loop)
            {
                midpoint = (upperBound + lowerBound) / 2;

                // run rpopagation algorithm, which create constraintSet, constraintMatrix
                // and propagate it (make it stable)
                result = PropagationUtil.runPropagationAlgorithm(constraints, constraintSetsCreator, midpoint);

                // if the constraint matrix is stable (previously), and valid
                if (MatrixUtils.isValid(result.DiscreteSetMatrix))
                {
                    // change upperbound of interval, right part is thrown away
                    upperBound = midpoint;
                }
                else
                {
                    // change lowerbound of interval, left part is thrown away
                    lowerBound = midpoint;
                }
            }

            // if the current found value is not valid, move to the looser restrictions
            while (!MatrixUtils.isValid(result.DiscreteSetMatrix) && midpoint <= upperBoundStart)
            {
                result = PropagationUtil.runPropagationAlgorithm(constraints, constraintSetsCreator, ++midpoint);
            }

            // return the found result.
            return(result);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="BisectionPropagator"/> class.
 /// </summary>
 /// <param name="constraintSetsCreator">The constraint sets creator.</param>
 public BisectionPropagator(IConstraintSetsCreator constraintSetsCreator)
 {
     // initialize
     this.constraintSetsCreator = constraintSetsCreator;
 }
        // 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>
 /// Initializes a new instance of the <see cref="SimplePropagator"/> class.
 /// </summary>
 /// <param name="constraintSetsCreator">The constraint sets creator.</param>
 public SimplePropagator(IConstraintSetsCreator constraintSetsCreator)
 {
     // initialize
     this.constraintSetsCreator = constraintSetsCreator;
 }