Ejemplo n.º 1
0
        private void ComparePermutations(
            bool expected,
            ExprNode[] setOne,
            ExprNode[] setTwo)
        {
            if (setTwo.Length == 0)
            {
                CompareSingle(expected, setOne, setTwo);
                return;
            }

            var permuter = PermutationEnumerator.Create(setTwo.Length).GetEnumerator();

            for (; permuter.MoveNext();)
            {
                var permutation = permuter.Current;
                var copy        = new ExprNode[setTwo.Length];
                for (var i = 0; i < permutation.Length; i++)
                {
                    copy[i] = setTwo[permutation[i]];
                }

                CompareSingle(expected, setOne, copy);
            }
        }
Ejemplo n.º 2
0
        public override void Start(CancellationToken token, Action <double> action)
        {
            double minPathLength         = double.MaxValue;
            var    representation        = new GeneralRepresentation <int>(_startPermutation, _startPermutation.Length, _startPermutation.Length);
            var    permutationEnumerator = new PermutationEnumerator <int>(representation);

            do
            {
                // Forcing delay for visualization
                if (_config.UseDelay)
                {
                    Thread.Sleep(_config.DelayTime);
                }

                var    curPermuation = permutationEnumerator.CurrentPermutation.Elements;
                double curMin        = _euclideanPath.GetPathLength(curPermuation, ClosedPath);

                if (curMin < minPathLength)
                {
                    minPathLength = curMin;
                    action?.Invoke(minPathLength);
                    _optimalSequence.OnNext(curPermuation);
                }
            } while (permutationEnumerator.SetNext() && !token.IsCancellationRequested);

            _optimalSequence.OnCompleted();
        }
Ejemplo n.º 3
0
 private static IEnumerable <IList <T> > PermutationsImpl <T>(IEnumerable <T> sequence)
 {
     using (var iter = new PermutationEnumerator <T>(sequence))
         while (iter.MoveNext())
         {
             yield return(iter.Current);
         }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates a sequence of lists that represent the permutations of the original sequence.
        /// </summary>
        /// <remarks>
        /// A permutation is a unique re-ordering of the elements of the sequence.<br/>
        /// This operator returns permutations in a deferred, streaming fashion; however, each
        /// permutation is materialized into a new list. There are N! permutations of a sequence,
        /// where N => sequence.Count().<br/>
        /// Be aware that the original sequence is considered one of the permutations and will be
        /// returned as one of the results.
        /// </remarks>
        /// <typeparam name="T">The type of the elements in the sequence</typeparam>
        /// <param name="sequence">The original sequence to permute</param>
        /// <returns>A sequence of lists representing permutations of the original sequence</returns>

        public static IEnumerable <IList <T> > Permutations <T>(this IEnumerable <T> sequence)
        {
            if (sequence == null)
            {
                throw new ArgumentNullException(nameof(sequence));
            }

            using (var iter = new PermutationEnumerator <T>(sequence))
                while (iter.MoveNext())
                {
                    yield return(iter.Current);
                }
        }
Ejemplo n.º 5
0
            /// <summary>
            /// Cria um cópia do enumerador.
            /// </summary>
            /// <returns>A cópia.</returns>
            public PermutationEnumerator Clone()
            {
                PermutationEnumerator resultEnumerator = new PermutationEnumerator(this.thisFastAffector as PermutationAffector);

                resultEnumerator.currentPointer  = this.currentPointer;
                resultEnumerator.isBeforeStart   = this.isBeforeStart;
                resultEnumerator.isAfterEnd      = this.isAfterEnd;
                resultEnumerator.affectedIndices = new int[this.affectedIndices.Length];
                Array.Copy(this.affectedIndices, resultEnumerator.affectedIndices, this.affectedIndices.Length);
                resultEnumerator.currentAffectationIndices = new int[this.currentAffectationIndices.Length];
                Array.Copy(this.currentAffectationIndices, resultEnumerator.currentAffectationIndices, this.currentAffectationIndices.Length);

                return(resultEnumerator);
            }
Ejemplo n.º 6
0
        public static object GetPermutations(BlockParam block, IList/*!*/ self, [DefaultProtocol, Optional]int? size) {
            var enumerator = new PermutationEnumerator(self, size);
            if (block == null) {
                return new Enumerator(enumerator);
            }

            return enumerator.Each(null, block);
        }