-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmisc.py
More file actions
100 lines (82 loc) · 3.1 KB
/
misc.py
File metadata and controls
100 lines (82 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 24 16:49:00 2019
@author: user
"""
import math
import os
import numpy as np
import matplotlib.pyplot as plt
#Function to sort by values
def sort_by_values(aList, values):
'''
Sorting each individuals by their objective values
Acending order sorting (lowesr value first) and gradualy increasing
reutrns a list of index
'''
sorted_list = []
while(len(sorted_list)!=len(aList)):
if values.index(min(values)) in aList:
sorted_list.append(values.index(min(values)))
values[values.index(min(values))] = math.inf # Initilize min to infinit to avoid computing it
return sorted_list
def sortList(aList, order='ascending'):
'''
Sorting each individuals by their
Acending order sorting (lowesr value first) and gradualy increasing
reutrns a sorted list and sorted index
'''
temp = [val for val in aList]
sorted_list = []
sorted_index = []
if order == 'ascending':
while(len(sorted_list)!=len(temp)):
index = temp.index(min(temp))
value = temp[index]
if value in aList:
sorted_list.append(value)
sorted_index.append(index)
temp[index] = math.inf # Initilize min to infinit to avoid computing it
else:
while(len(sorted_list)!=len(temp)):
index = temp.index(max(temp))
value = temp[index]
if value in aList:
sorted_list.append(value)
sorted_index.append(index)
temp[index] = -math.inf # Initilize min to negative infinit to avoid computing it
return sorted_list, sorted_index
#Function to find index of list
def index_of(val, inList):
for i in range(0,len(inList)):
if inList[i] == val:
return i
return -1
def unique_elements(plist, printList = False):
# intilize a null list
unique_list = []
# traverse for all elements
for x in plist:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
if printList:
for x in unique_list:
print ('Uniques list:',x)
return unique_list
def index_in_list(plistElemennt, pList):
index_list = []
for x in plistElemennt:
index_list.append(pList.index(x))
return index_list
def preserve_diversity(pPopulation, pop_size):
'''
Preserve Elit and Diverser Individuals
args:
param: itrPopulation populaiton of individuslas,
param: pop_size number of individuals
return: a sorted and diverse population based on tree fitness and error
'''
nSortedPopulation = [pPopulation[i] for i in sort_by_values([i for i in range(0,len(pPopulation))], [pop.mCost[0] for pop in pPopulation])]
nSortedPopulationFitness = [[round(pop.mCost[0], 2), pop.mTree.getTreeSize()] for pop in nSortedPopulation]
return [nSortedPopulation[i] for i in index_in_list(unique_elements(nSortedPopulationFitness),nSortedPopulationFitness)]