
function [ fitness ] = calculateFitness(Nc, times, center, data, m)
    
    % This function calculate the fitness function of OLYMPUS

    % ------------------------------------------------------------------- %
    % Input : Nc     = Number of Clusters
    %         times  = time points
    %         center = Centers of clusters
    %         data   = Data Set
    %         m      = fuzziness
    %         
    % Output: fitness = Value of fitness function (as mentioned in Eq. 8)    
    % ------------------------------------------------------------------- %

    % ------------------------------------------------------------------- %
    global GLOBAL_d;
    global GLOBAL_e;
    global GLOBAL_f;
    global GLOBAL_a;
    global GLOBAL_c;       
    global GLOBAL_b;       
    
    % DImensions of Data Set
    N = size(data, 1);
    Nt = size(data, 2);    
    % Calculate partition matrix U 
    [~, U] = calculatePartition(Nc, times, center, data, m, 1);
    % Convert vector V (Clusters Centers) to a matrix (time series X Number of Clusters)
    V = reshape(center, Nt, Nc)';
    pd = zeros(Nc, Nt);
    % Calculate fitness function (Eq. 8)
    for i=1:Nc
        for k=2:Nt-1
            term1 = GLOBAL_a(k)*V(i, k-1)+GLOBAL_b(k)*V(i,k)+GLOBAL_c(k)*V(i,k+1);
            denominator = ((times(k)-times(k+1)).^2) * ((times(k)-times(k-1)).^2);
            S = 0;
            for j=1:N
                S = S + (U(i, j)^m) * (term1 + GLOBAL_d(k)*data(j, k-1) + GLOBAL_e(k)*data(j,k) + GLOBAL_f(k)*data(j,k+1));
            end
            pd(i,k) = S/denominator;
        end
    end
    fitness = abs(sum(sum(pd)));
    % ------------------------------------------------------------------- %
end

