/// <summary> /// EQUATION 3 /// </summary> private Vector <double> MotionDirection(Krill krill, int currentIteration) { Vector <double> alpha_local = AlphaLocal(krill); // i-th Krill Vector <double> alpha_target = AlphaTarget(krill, currentIteration); // i-th Krill return(alpha_local + alpha_target); }
/// <summary> /// EQUATION 15 /// </summary> /// <returns></returns> private Vector <double> Beta_i_best(Krill krill) { if (krill.BestFitness > krill.Fitness) { return(X_i_j(krill.Coordinates, krill.BestCoordinates).Multiply(K_i_j(krill.Fitness, krill.BestFitness))); } else { return(Vector <double> .Build.Dense(krill.Coordinates.Count)); } }
/// <summary> /// EQUATION 10 /// </summary> public Vector <double> GetForagingMotion(Krill krill, int currentIteration, Vector <double> vf_position) { int lastIteration = currentIteration - 1; Vector <double> F_i_old = ForagingSpeedHistory.ContainsKey(new Tuple <int, int>(lastIteration, krill.KrillNumber)) ? ForagingSpeedHistory[new Tuple <int, int>(lastIteration, krill.KrillNumber)] : Vector <double> .Build.Dense(krill.Coordinates.Count); double Omega_f = (0.1 + (0.8 * (1 - currentIteration / MaxIteration))); Vector <double> B_i = (Beta_i_best(krill) + Beta_i_food(krill, lastIteration, vf_position)); // EQUATION 11 Vector <double> F_i = B_i.Multiply(V_f) + F_i_old.Multiply(Omega_f); // EQUATION 10 ForagingSpeedHistory.Add(new Tuple <int, int>(currentIteration, krill.KrillNumber), F_i); return(F_i); }
/// <summary> /// EQUATION 7 /// </summary> private double SensingDistance(Krill krill) { double N = krillPopulation.Population.Count; double firstPart = (1 / (5 * N)); double secondPart = 0; foreach (var otherKrill in krillPopulation.Population) { secondPart += Distance.Euclidean(krill.Coordinates, otherKrill.Coordinates); } double result = firstPart * secondPart; return(Math.Round(result, 2)); }
private void PopulationInitialization() { // Population initialization List <Krill> Krills = new List <Krill>(); for (int i = 1; i <= Parameters.Population; i++) { Krill krill = new Krill(i) { Coordinates = Vector <double> .Build.Random(Parameters.Dimensions, new ContinuousUniform(Parameters.LB, Parameters.UB)) }; Krills.Add(krill); } Population = new KrillPopulation(Krills); }
/// <summary> /// EQUATION 8 /// </summary> private Vector <double> AlphaTarget(Krill krill, int currentIteration) { Krill bestKrill = KrillPopulation.GetBestKrill(); if (bestKrill.Fitness > krill.Fitness) { double K_i_best = K_i_j(krill.Fitness, bestKrill.Fitness); Vector <double> X_i_best = X_i_j(krill.Coordinates, bestKrill.Coordinates); double C_best = EffectiveCoefficient(currentIteration); return(X_i_best.Multiply(C_best * K_i_best)); } else { return(Vector <double> .Build.Dense(krill.Coordinates.Count)); } }
/// <summary> /// EQUATION 2 /// </summary> public Vector <double> GetInducedMotion(Krill krill, int currentIteration) { int lastIteration = currentIteration - 1; Vector <double> Alpha_i = MotionDirection(krill, currentIteration); Vector <double> N_old = InducedSpeedHistory.ContainsKey(new Tuple <int, int>(lastIteration, krill.KrillNumber)) ? InducedSpeedHistory[new Tuple <int, int>(lastIteration, krill.KrillNumber)] : Vector <double> .Build.Dense(krill.Coordinates.Count); double Omega_n = (0.1 + (0.8 * (1 - currentIteration / MaxIteration))); Vector <double> N_new = N_max * Alpha_i + Omega_n * N_old; // We add a krill to the history to know what the value of N_old is in later iterations InducedSpeedHistory.Add(new Tuple <int, int>(currentIteration, krill.KrillNumber), N_new); return(N_new); }
/// <summary> /// EQUATION 13 /// </summary> private Vector <double> Beta_i_food(Krill krill, int currentIteration, Vector <double> vf_position) { Vector <double> result = Vector <double> .Build.Dense(krill.Coordinates.Count); var K_food = FitnessFunction.Invoke(vf_position.ToArray()); if (K_food > krill.Fitness) { double C_food = EffectiveFoodCoefficient(currentIteration); double K_i_food = K_i_j(krill.Fitness, K_food); Vector <double> X_i_food = X_i_j(krill.Coordinates, vf_position); result = X_i_food.Multiply(K_i_food * C_food); } return(result); }
/// <summary> /// EQUATION 4 /// </summary> private Vector <double> AlphaLocal(Krill krill) { Vector <double> alphaLocal = Vector <double> .Build.Dense(krill.Coordinates.Count); var neighbourhood = Neighbourhood.GetNeighbourhood(krill); if (neighbourhood.Count == 0) { return(alphaLocal); } foreach (var neighbour in neighbourhood) // NN { var K_i_j_value = K_i_j(krill.Fitness, neighbour.Fitness); alphaLocal = alphaLocal.Add(X_i_j(krill.Coordinates, neighbour.Coordinates).Multiply(K_i_j_value)); // i-th Krill, j-th Krill Neighbour } return(alphaLocal); }
/// <summary> /// Creates a neighborhood for a given krill /// </summary> public List <Krill> GetNeighbourhood(Krill krill) { List <Krill> neighbourhood = new List <Krill>(); double sensingDistance = SensingDistance(krill); foreach (var neighbour in krillPopulation.Population) { if (neighbour.KrillNumber != krill.KrillNumber) { // calculate the distance between one individual and the other double distance = Distance.Euclidean(krill.Coordinates, neighbour.Coordinates); // If the distance between two krills is less than "sensingDistance", then we add this krill to the neighborhood if (distance < sensingDistance) { neighbourhood.Add(neighbour); } } } return(neighbourhood); }