|
| 1 | +import numpy |
| 2 | + |
| 3 | +population = numpy.array([[20, 2.2], |
| 4 | + [60, 4.4], |
| 5 | + [65, 3.5], |
| 6 | + [15, 4.4], |
| 7 | + [55, 4.5], |
| 8 | + [50, 1.8], |
| 9 | + [80, 4.0], |
| 10 | + [25, 4.6]]) |
| 11 | + |
| 12 | +def non_dominant_sorting(curr_set): |
| 13 | + # List of the members of the current dominant front/set. |
| 14 | + dominant_set = [] |
| 15 | + # List of the non-members of the current dominant front/set. |
| 16 | + non_dominant_set = [] |
| 17 | + for idx1, sol1 in enumerate(curr_set): |
| 18 | + # Flag indicates whether the solution is a member of the current dominant set. |
| 19 | + is_dominant = True |
| 20 | + for idx2, sol2 in enumerate(curr_set): |
| 21 | + if idx1 == idx2: |
| 22 | + continue |
| 23 | + # Zipping the 2 solutions so the corresponding genes are in the same list. |
| 24 | + # The returned array is of size (N, 2) where N is the number of genes. |
| 25 | + b = numpy.array(list(zip(sol1, sol2))) |
| 26 | + |
| 27 | + #TODO Consider repacing < by > for maximization problems. |
| 28 | + # Checking for if any solution dominates the current solution by applying the 2 conditions. |
| 29 | + # le_eq: All elements must be True. |
| 30 | + # le: Only 1 element must be True. |
| 31 | + le_eq = b[:, 1] <= b[:, 0] |
| 32 | + le = b[:, 1] < b[:, 0] |
| 33 | + |
| 34 | + # If the 2 conditions hold, then a solution dominates the current solution. |
| 35 | + # The current solution is not considered a member of the dominant set. |
| 36 | + if le_eq.all() and le.any(): |
| 37 | + # print(f"{sol2} dominates {sol1}") |
| 38 | + # Set the is_dominant flag to False to not insert the current solution in the current dominant set. |
| 39 | + # Instead, insert it into the non-dominant set. |
| 40 | + is_dominant = False |
| 41 | + non_dominant_set.append(sol1) |
| 42 | + break |
| 43 | + else: |
| 44 | + # Reaching here means the solution does not dominant the current solution. |
| 45 | + # print(f"{sol2} does not dominate {sol1}") |
| 46 | + pass |
| 47 | + |
| 48 | + # If the flag is True, then no solution dominates the current solution. |
| 49 | + if is_dominant: |
| 50 | + dominant_set.append(sol1) |
| 51 | + |
| 52 | + # Return the dominant and non-dominant sets. |
| 53 | + return dominant_set, non_dominant_set |
| 54 | + |
| 55 | +dominant_set = [] |
| 56 | +non_dominant_set = population.copy() |
| 57 | +while len(non_dominant_set) > 0: |
| 58 | + d1, non_dominant_set = non_dominant_sorting(non_dominant_set) |
| 59 | + dominant_set.append(d1) |
| 60 | + |
| 61 | +for i, s in enumerate(dominant_set): |
| 62 | + print(f'Dominant Front Set {i+1}:\n{s}') |
0 commit comments