#Function to carry out NSGA-II's fast non dominated sort
# This non-dominated sort has three parameters: presenting three fitness functions
def fast_non_dominated_sort_new(values1, values2, values3):
    S=[[] for i in range(0,len(values1))]
    front = [[]]
    n=[0 for i in range(0,len(values1))]
    rank = [0 for i in range(0, len(values1))]

    for p in range(0,len(values1)):
        S[p]=[]
        n[p]=0
        for q in range(0, len(values1)):
            if (values1[p] < values1[q] and values2[p] < values2[q] and values3[p] < values3[q]) or (values1[p] <= values1[q] and values2[p] < values2[q] and values3[p] < values3[q]) or (values1[p] < values1[q] and values2[p] <= values2[q] and values3[p] < values3[q]) or (values1[p] < values1[q] and values2[p] < values2[q] and values3[p] <= values3[q]) or (values1[p] <= values1[q] and values2[p] <= values2[q] and values3[p] < values3[q]) or (values1[p] <= values1[q] and values2[p] < values2[q] and values3[p] <= values3[q]) or (values1[p] < values1[q] and values2[p] <= values2[q] and values3[p] <= values3[q]):
                if q not in S[p]:
                    S[p].append(q)
            elif (values1[q] < values1[p] and values2[q] < values2[p] and values3[q] < values3[p]) or (values1[q] <= values1[p] and values2[q] < values2[p] and values3[q] < values3[p]) or (values1[q] < values1[p] and values2[q] <= values2[p] and values3[q] < values3[p]) or (values1[q] < values1[p] and values2[q] < values2[p] and values3[q] <= values3[p]) or (values1[q] <= values1[p] and values2[q] <= values2[p] and values3[q] < values3[p]) or (values1[q] <= values1[p] and values2[q] < values2[p] and values3[q] <= values3[p]) or (values1[q] < values1[p] and values2[q] <= values2[p] and values3[q] <= values3[p]):
                n[p] = n[p] + 1
        if n[p]==0:
            rank[p] = 0
            if p not in front[0]:
                front[0].append(p)

    i = 0
    while(front[i] != []):
        Q=[]
        for p in front[i]:
            for q in S[p]:
                n[q] =n[q] - 1
                if( n[q]==0):
                    rank[q]=i+1
                    if q not in Q:
                        Q.append(q)
        i = i+1
        front.append(Q)

    del front[len(front)-1]
    return front