├── README.md ├── LICENSE.md └── NSGA II.py /README.md: -------------------------------------------------------------------------------- 1 | # NSGA-II 2 | NSGA is a popular non-domination based genetic algorithm for multi-objective optimization. 3 | It is a very effective algorithm but has been generally criticized for its computational complexity, lack of elitism and for choosing the optimal parameter value for sharing parameter σshare. 4 | A modified version, NSGA II was developed, which has a better sorting algorithm , incorporates elitism and no sharing parameter needs to be chosen a priori. 5 | This is a python implementation of NSGA-II algorithm 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2017 NSGA-II.py Authors. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /NSGA II.py: -------------------------------------------------------------------------------- 1 | # Program Name: NSGA-II.py 2 | # Description: This is a python implementation of Prof. Kalyanmoy Deb's popular NSGA-II algorithm 3 | # Author: Haris Ali Khan 4 | # Supervisor: Prof. Manoj Kumar Tiwari 5 | 6 | #Importing required modules 7 | import math 8 | import random 9 | import matplotlib.pyplot as plt 10 | 11 | #First function to optimize 12 | def function1(x): 13 | value = -x**2 14 | return value 15 | 16 | #Second function to optimize 17 | def function2(x): 18 | value = -(x-2)**2 19 | return value 20 | 21 | #Function to find index of list 22 | def index_of(a,list): 23 | for i in range(0,len(list)): 24 | if list[i] == a: 25 | return i 26 | return -1 27 | 28 | #Function to sort by values 29 | def sort_by_values(list1, values): 30 | sorted_list = [] 31 | while(len(sorted_list)!=len(list1)): 32 | if index_of(min(values),values) in list1: 33 | sorted_list.append(index_of(min(values),values)) 34 | values[index_of(min(values),values)] = math.inf 35 | return sorted_list 36 | 37 | #Function to carry out NSGA-II's fast non dominated sort 38 | def fast_non_dominated_sort(values1, values2): 39 | S=[[] for i in range(0,len(values1))] 40 | front = [[]] 41 | n=[0 for i in range(0,len(values1))] 42 | rank = [0 for i in range(0, len(values1))] 43 | 44 | for p in range(0,len(values1)): 45 | S[p]=[] 46 | n[p]=0 47 | for q in range(0, len(values1)): 48 | if (values1[p] > values1[q] and values2[p] > values2[q]) or (values1[p] >= values1[q] and values2[p] > values2[q]) or (values1[p] > values1[q] and values2[p] >= values2[q]): 49 | if q not in S[p]: 50 | S[p].append(q) 51 | elif (values1[q] > values1[p] and values2[q] > values2[p]) or (values1[q] >= values1[p] and values2[q] > values2[p]) or (values1[q] > values1[p] and values2[q] >= values2[p]): 52 | n[p] = n[p] + 1 53 | if n[p]==0: 54 | rank[p] = 0 55 | if p not in front[0]: 56 | front[0].append(p) 57 | 58 | i = 0 59 | while(front[i] != []): 60 | Q=[] 61 | for p in front[i]: 62 | for q in S[p]: 63 | n[q] =n[q] - 1 64 | if( n[q]==0): 65 | rank[q]=i+1 66 | if q not in Q: 67 | Q.append(q) 68 | i = i+1 69 | front.append(Q) 70 | 71 | del front[len(front)-1] 72 | return front 73 | 74 | #Function to calculate crowding distance 75 | def crowding_distance(values1, values2, front): 76 | distance = [0 for i in range(0,len(front))] 77 | sorted1 = sort_by_values(front, values1[:]) 78 | sorted2 = sort_by_values(front, values2[:]) 79 | distance[0] = 4444444444444444 80 | distance[len(front) - 1] = 4444444444444444 81 | for k in range(1,len(front)-1): 82 | distance[k] = distance[k]+ (values1[sorted1[k+1]] - values2[sorted1[k-1]])/(max(values1)-min(values1)) 83 | for k in range(1,len(front)-1): 84 | distance[k] = distance[k]+ (values1[sorted2[k+1]] - values2[sorted2[k-1]])/(max(values2)-min(values2)) 85 | return distance 86 | 87 | #Function to carry out the crossover 88 | def crossover(a,b): 89 | r=random.random() 90 | if r>0.5: 91 | return mutation((a+b)/2) 92 | else: 93 | return mutation((a-b)/2) 94 | 95 | #Function to carry out the mutation operator 96 | def mutation(solution): 97 | mutation_prob = random.random() 98 | if mutation_prob <1: 99 | solution = min_x+(max_x-min_x)*random.random() 100 | return solution 101 | 102 | #Main program starts here 103 | pop_size = 20 104 | max_gen = 921 105 | 106 | #Initialization 107 | min_x=-55 108 | max_x=55 109 | solution=[min_x+(max_x-min_x)*random.random() for i in range(0,pop_size)] 110 | gen_no=0 111 | while(gen_no