Beispiel #1
0
        /// <summary>
        /// Gets a new Decision Vector with elements which have potentially been mutated.
        /// Uses <see cref="IVariable"/> to wrap the added number, ensuring a valid Decision Vector is always created.
        /// </summary>
        /// <param name="decisionVector">The existing Decision Vector.</param>
        /// <returns>A new Decision Vector.</returns>
        public DecisionVector Operate(DecisionVector decisionVector)
        {
            var locationsToMutate = rngManager.GetLocations(
                decisionVector.Count, maximumNumberOfMutations,
                true, mutationProbability);

            var newDv = decisionVector.Select(i => (double)i).ToArray();
            var newDs = decisionVector.GetDecisionSpace();

            foreach (var location in locationsToMutate)
            {
                var mutation = rngManager.Rng.Next(
                    minimum, includeZero ? maximum : maximum - 1);
                if (!includeZero)
                {
                    if (mutation >= 0)
                    {
                        mutation += 1;
                    }
                }

                newDv[location] = Convert.ToDouble(newDs.ElementAt(location).AddOrWrap(newDv[location], mutation));
            }

            return(DecisionVector.CreateFromArray(newDs, newDv));
        }
Beispiel #2
0
        public override IEnumerable <double> Evaluate(DecisionVector definition)
        {
            if (definition.Count != GetGlobalOptimum().Count)
            {
                throw new System.ArgumentOutOfRangeException(nameof(definition),
                                                             $"Route should be a complete tour of {GetGlobalOptimum().Count} stops.");
            }

            return(new[] { CalculateTotalTravelDistance(definition.Select(l => (int)l)) });
        }
Beispiel #3
0
            private void updateDecisionVector(bool add = true)
            {
                var elements = decisionVector.Select(d => (double)d).ToList();

                elements[0] += add ? 0.01 : -0.01;
                var space = decisionVector.GetDecisionSpace();

                decisionVector = DecisionVector.CreateFromArray(
                    space, elements);
            }
Beispiel #4
0
        /// <summary>
        /// Gets a new Decision Vector where elements have potentially been mutated.
        /// Uses <see cref="IVariable"/> to wrap the added integer, ensuring a valid Decision Vector is always created.
        /// </summary>
        /// <param name="decisionVector">The existing Decision Vector.</param>
        /// <returns>A new Decision Vector.</returns>
        public DecisionVector Operate(DecisionVector decisionVector)
        {
            var locationsToMutate = rngManager.GetLocations(
                decisionVector.Count, maximumNumberOfMutations,
                true, mutationProbability);

            var newDv = decisionVector.Select(i => (double)i).ToArray();
            var newDs = decisionVector.GetDecisionSpace();

            foreach (var location in locationsToMutate)
            {
                var mutationLocation = rngManager.Rng.Next(0, numberSet.Length);
                newDv[location] = Convert.ToDouble(newDs.ElementAt(location).AddOrWrap(newDv[location], numberSet[mutationLocation]));
            }

            return(DecisionVector.CreateFromArray(newDs, newDv));
        }
Beispiel #5
0
        /// <summary>
        /// Gets a new Decision Vector where elements which pass the conditions have potentially been mutated.
        /// </summary>
        /// <param name="decisionVector"><see cref="DecisionVector"/> to mutate.</param>
        /// <returns>A mutated Decision Vector.</returns>
        public DecisionVector Operate(DecisionVector decisionVector)
        {
            var elementsToMutate = decisionVector.Select(e => conditions.All(c => c(e)));
            var tempDs           = new DecisionSpace(decisionVector.GetDecisionSpace().Where((d, i) => elementsToMutate.ElementAt(i)));
            var tempDv           = DecisionVector.CreateFromArray(tempDs, decisionVector.Where((e, i) => elementsToMutate.ElementAt(i)));
            var newElements      = mutation.Operate(tempDv);

            var newVector = decisionVector.ToArray();
            var item      = 0;

            for (var i = 0; i < newVector.Length; i++)
            {
                if (elementsToMutate.ElementAt(i))
                {
                    newVector[i] = newElements.ElementAt(item);
                    item++;
                }
            }

            return(DecisionVector.CreateFromArray(decisionVector.GetDecisionSpace(), newVector));
        }
Beispiel #6
0
        /// <summary>
        /// Creates a set of Decision Vectors, based on a starting location, incrementing each dimension by a fixed amount.
        /// </summary>
        /// <param name="initialLocation">The <see cref="DecisionVector"/> representing the starting location.</param>
        /// <param name="stepSize">
        /// The distance from the starting location at which each further location will be created
        /// (same in all dimensions).
        /// </param>
        /// <returns>List of <see cref="DecisionVector"/>s, the same length as the number of dimensions.</returns>
        public static List <DecisionVector> CreateNearLocation(DecisionVector initialLocation, double stepSize)
        {
            if (Math.Abs(stepSize) < double.Epsilon)
            {
                throw new ArgumentOutOfRangeException(nameof(stepSize), "Step size cannot be zero.");
            }

            var newDVs = new List <DecisionVector>();

            var startDv = initialLocation.Select(d => (double)d).ToArray();

            for (var i = 2; i <= startDv.Length + 1; i++)
            {
                // Create D+1 total vertices.
                var newDv = new double[startDv.Length];
                startDv.CopyTo(newDv, 0);

                // Each vertex has one of its dimensions offset by an amount equal to stepsize.
                newDv[i - 2] += stepSize;

                try
                {
                    newDVs.Add(DecisionVector.CreateFromArray(
                                   initialLocation.GetDecisionSpace(),
                                   newDv));
                }
                catch (ArgumentOutOfRangeException)
                {
                    // Can't go this way, as we are not in the acceptable region.
                    // Try to go the other way...
                    newDv[i - 2] -= 2 * stepSize;

                    newDVs.Add(DecisionVector.CreateFromArray(
                                   initialLocation.GetDecisionSpace(),
                                   newDv));
                }
            }
            return(newDVs);
        }
 public override IEnumerable <double> Evaluate(DecisionVector definition)
 {
     return(new[] { CalculateTotalTravelDistance(definition.Select(x => (int)x)) });
 }