Time Complexity of the Fitness Function for GA

I am trying to calculate the time complexity of my fitness function for the genetic algorithm that I wrote.

What I did: I already read some articles and examples.

However, not all of them were really satisfactory, where I could say: now I know how to apply this in my code.

Let me show you my fitness function, where I guessed several times.

    public static List<double> calculateFitness(List<List<Point3d>> cF, Point3d startpoint)
    {
        List<double> Fitness = new List<double>(); // 1+1
        for (int i = 0; i < cF.Count; i++)  // 1 ; N+1 ; N
        {
            Point3d actual;  // N 
            Point3d next;  // N
            double distance;  // N 
            double totalDistance = startpoint.DistanceTo(cF[i][0]);  // (1+1+1+1)*N
            for (int j = 0; j < cF[i].Count - 1; j++)  // { 1 ; N ; N-1 }*N
            {
                actual = cF[i][j];  // (1+1)*(N-1)
                next = cF[i][j + 1];  // (1+1)*(N-1)

                distance = actual.DistanceTo(next);  // (1+1+1+1)*(N-1)
                totalDistance += distance;  // (1+1)*(N-1)
            }
            totalDistance += cF[i][cF[i].Count - 1].DistanceTo(startpoint);  // (1+1+1+1)*N
            Fitness.Add(totalDistance);  // N
        }
        return Fitness;  // 1
    }

- , , , , . , , - . , : double totalDistance = startpoint.DistanceTo(cF[i][0]); → (1 + 1) N? : actual = cF[i][j]; → (1 + 1) NN?

, : 1 + 1 + (1 + N + 1 + N + N + N + N + 4N + N * {1 + N + N-1 + 2 * (N-1 ) + 2 * (N-1) + 4 * (N-1) + 2 * (N-1)} + 4N + N) = 2 + (2 + 14N + N * {12N-10}) = 12N ^ 2 + 4N + 4 = O (N ^ 2)

+4
1

Big-O (.. O (1)) . , N. ,

, , .

public static List<double> calculateFitness(List<List<Point3d>> cF, Point3d startpoint)
{
    List<double> Fitness = new List<double>();
    for (int i = 0; i < cF.Count; i++)  // 1.
    {
        Point3d actual;  // 2. 
        Point3d next; 
        double distance;  
        double totalDistance = startpoint.DistanceTo(cF[i][0]);  // 3.
        for (int j = 0; j < cF[i].Count - 1; j++)  // 4.
        {
            actual = cF[i][j];  // 5.
            next = cF[i][j + 1];

            distance = actual.DistanceTo(next);
            totalDistance += distance;
        }
        totalDistance += cF[i][cF[i].Count - 1].DistanceTo(startpoint);
        Fitness.Add(totalDistance); // 6.
    }
    return Fitness;
}
  • i N , N - cF.Count. , , i < cF.Count c i++ d. N , - cdN. , , Big-O , , O (N).

  • , O (1).

  • .NET List O (1). DistanceTo, , , O (1), .

  • , N . , , cF[i].Count cF.Count. .

  • , - O (1).

  • . Add :

    Count , O (1). , O (n), n Count.

    • , O (1) , O (n), n - , Fitness . amortized O (1).

, O (1). , , O (N) O (N). , O (N) * O (N) = O (N 2).

+2

Source: https://habr.com/ru/post/1675914/


All Articles