private void ModifyWeights(
            AggregatedClusterStatistic aggregatedClusterStatistic,
            IRelativeWeightCalculator relativeWeightCalculator,
            IWeightsNormalizer weightsNormalizer,
            IWeights weights)
        {
            var newWeights = new Dictionary <Uri, Weight>(aggregatedClusterStatistic.Replicas.Count);
            var statisticCollectedTimestamp = aggregatedClusterStatistic.Cluster.Timestamp;
            var relativeMaxWeight           = 0d;

            foreach (var(replica, replicaStatistic) in aggregatedClusterStatistic.Replicas)
            {
                var previousWeight = weights.Get(replica, settings.WeightsTTL) ??
                                     new Weight(settings.InitialWeight, statisticCollectedTimestamp - settings.WeightUpdatePeriod);

                var newReplicaWeight = relativeWeightCalculator
                                       .Calculate(aggregatedClusterStatistic.Cluster, replicaStatistic, previousWeight, settings);

                newWeights.Add(replica, newReplicaWeight);

                if (relativeMaxWeight < newReplicaWeight.Value)
                {
                    relativeMaxWeight = newReplicaWeight.Value;
                }
            }

            weightsNormalizer.Normalize(newWeights, relativeMaxWeight);

            LogWeights(weights, newWeights);

            weights.Update(newWeights, settings);
        }
Пример #2
0
 /// <summary>
 /// Implements the +.
 /// </summary>
 /// <param name="a">A.</param>
 /// <param name="b">The b.</param>
 /// <returns>The result of the operator.</returns>
 public static IWeights AddScaledInPlace(this IWeights a, IWeights b, double s)
 {
     for (var i = 0; i < a.Length; ++i)
     {
         a[i] += b[i] * s;
     }
     return(a);
 }
Пример #3
0
 public static IWeights AddInPlace([NotNull] this IWeights a, [NotNull] IWeights b)
 {
     for (var i = 0; i < a.Length; ++i)
     {
         a[i] += b[i];
     }
     return(a);
 }
Пример #4
0
 /// <summary>
 /// Implements the *.
 /// </summary>
 /// <param name="a">A.</param>
 /// <param name="s">The s.</param>
 /// <returns>The result of the operator.</returns>
 public static IWeights ScaleInPlace(IWeights a, double s)
 {
     for (var i = 0; i < a.Length; ++i)
     {
         a[i] *= s;
     }
     return(a);
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WeightVector"/> class.
 /// </summary>
 /// <param name="weights">The weights.</param>
 public WeightVector([NotNull] IWeights weights)
     : this(weights.Length)
 {
     for (var i = 0; i < weights.Length; ++i)
     {
         this[i] = weights[i];
     }
 }
Пример #6
0
 /// <summary>
 /// Implements the -.
 /// </summary>
 /// <param name="a">A.</param>
 /// <param name="b">The b.</param>
 /// <returns>The result of the operator.</returns>
 public static IWeights SubtractInPlace(this IWeights a, IWeights b)
 {
     for (var i = 0; i < a.Length; ++i)
     {
         a[i] -= b[i];
     }
     return(a);
 }
Пример #7
0
        /// <summary>
        /// Returns the absolute values
        /// </summary>
        /// <param name="a">A.</param>
        /// <returns>The result of the operator.</returns>
        public static IWeights Absolute(this IWeights a)
        {
            var weights = new WeightVector(a);

            for (var i = 0; i < a.Length; ++i)
            {
                weights[i] = Math.Abs(weights[i]);
            }
            return(weights);
        }
Пример #8
0
        /// <summary>
        /// Calculates the distance between two weight vectors.
        /// </summary>
        /// <param name="a">A.</param>
        /// <param name="b">The b.</param>
        /// <returns>System.Double.</returns>
        /// <exception cref="System.ArgumentException">Lengths of weight vectors differ.</exception>
        public double CalculateDistance(IWeights a, IWeights b)
        {
            var length = a.Length;

            if (length != b.Length)
            {
                throw new ArgumentException("Lengths of weight vectors differ.");
            }

            return(CalculateDistance(a.AsReadOnlyList, b.AsReadOnlyList));
        }
Пример #9
0
 /// <summary>
 /// Updates the weights.
 /// </summary>
 /// <param name="newWeights">The new weights.</param>
 /// <exception cref="System.ArgumentException">Lengths of weight vectors differ.</exception>
 public void Update(IWeights newWeights)
 {
     if (newWeights.Length != _weights.Length)
     {
         throw new ArgumentException("Lengths of weight vectors differ.");
     }
     for (var i = 0; i < newWeights.Length; ++i)
     {
         this[i] = newWeights[i];
     }
 }
Пример #10
0
        /// <summary>
        /// Calculates the new weights.
        /// </summary>
        /// <param name="iteration">The iteration.</param>
        /// <param name="trainingVector">The training vector.</param>
        /// <param name="currentWeights">The current weights.</param>
        /// <param name="distance">The distance.</param>
        /// <returns>IWeights.</returns>
        public IWeights CalculateNewWeights(int iteration, IWeights trainingVector, IWeights currentWeights, double distance)
        {
            var radius  = _radiusFunction.CalculateRadius(iteration);
            var h       = _neighboorFunction.CalculateFactor(distance, radius);
            var epsilon = _learningRate.CalculateLearningRate(iteration);
            var factor  = h * epsilon;

            // Ritter et al. (1991)

            // calculate V(t)-W(t)
            var delta = trainingVector.Subtract(currentWeights);

            // calculate W(t) + N(t)*L(t) * (V(t)-W(t))
            var weights = new WeightVector(currentWeights);

            return(weights.AddScaledInPlace(delta, factor));
        }
Пример #11
0
        public ClusterState(
            IRelativeWeightCalculator relativeWeightCalculator = null,
            IRawClusterStatistic rawClusterStatistic           = null,
            ITimeProvider timeProvider           = null,
            IStatisticHistory statisticHistory   = null,
            IWeightsNormalizer weightsNormalizer = null,
            IWeights weights = null)
        {
            IsUpdatingNow            = new AtomicBoolean(false);
            TimeProvider             = timeProvider ?? new TimeProvider();
            RelativeWeightCalculator = relativeWeightCalculator ?? new RelativeWeightCalculator();
            Weights           = weights ?? new Weights();
            WeightsNormalizer = weightsNormalizer ?? new WeightsNormalizer();
            CurrentStatistic  = rawClusterStatistic ?? new RawClusterStatistic();
            StatisticHistory  = statisticHistory ?? new StatisticsHistory();

            LastUpdateTimestamp = TimeProvider.GetCurrentTime();
        }
Пример #12
0
        /// <summary>
        /// Implements the +.
        /// </summary>
        /// <param name="a">A.</param>
        /// <param name="b">The b.</param>
        /// <returns>The result of the operator.</returns>
        public static IWeights AddScaled(this IWeights a, IWeights b, double s)
        {
            var weights = new WeightVector(a);

            return(AddScaledInPlace(weights, b, s));
        }
Пример #13
0
        public static IWeights Add([NotNull] this IWeights a, [NotNull] IWeights b)
        {
            var weights = new WeightVector(a);

            return(AddInPlace(weights, b));
        }
Пример #14
0
        /// <summary>
        /// Implements the *.
        /// </summary>
        /// <param name="a">A.</param>
        /// <param name="s">The s.</param>
        /// <returns>The result of the operator.</returns>
        public static IWeights Scale(IWeights a, double s)
        {
            var weights = new WeightVector(a);

            return(ScaleInPlace(weights, s));
        }
Пример #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Neuron" /> class.
 /// </summary>
 /// <param name="weights">The Weights.</param>
 public Neuron(IWeights weights)
 {
     Weights = weights;
 }
Пример #16
0
        /// <summary>
        /// Implements the -.
        /// </summary>
        /// <param name="a">A.</param>
        /// <param name="b">The b.</param>
        /// <returns>The result of the operator.</returns>
        public static IWeights Subtract(this IWeights a, IWeights b)
        {
            var weights = new WeightVector(a);

            return(SubtractInPlace(weights, b));
        }
Пример #17
0
 /// <summary>
 /// Updates the weights.
 /// </summary>
 /// <param name="newWeights">The new weights.</param>
 public void UpdateWeights(IWeights newWeights)
 {
     Weights.Update(newWeights);
 }