Blog

July 30, 2020

Open Source Genetic Algorithm

I cannot say anything particular new about the Genetic Algorithm (GA) which has always been a great source for solving problems. Lots of amazing posts have been published on the internet easily accessible which makes me not try to repeat anything already explained. One of the best has been written by TutorialsPoint that was a big contribution of conceiving what happens when we run GA on a set of data with specific fitness functions. By any way you might get informed from these amazing sources since David Rutten has presented Galapagos explanatorily (which you can find the article here), this post is just a try to have the same problem solver a little bit wider but open accessed. However, I started to use GHPython to create a new adaptable GA engine, the criteria had defined the necessity of this new GA was all about the options could take part in the procedure. It tries to have sliders managed, can control the process step by step, hold the solver whenever we want, and recording the lists of generations from worst-case scenario to the best. The final part also is under development where the major focus is to get along with these sets of options. By all means, if you are interested in any kind of collaboration, you know how to find me to discuss on the development plan.

This GA definition is under development but, I am considering any comment you might have about how are Pros and Cons of any GA algorithm with Gh and what if you might define some similar one, what would be set by you as its options in get/set inputs and outputs. The GA itself is not something special but as I mentioned before, the options a designer might need was the first motivation behind this project.

__author__ = "Mahdi Soheyli"
__version__ = "2020.04.10"

import Grasshopper as gh
from Grasshopper import DataTree
from Grasshopper.Kernel.Data import GH_Path
from itertools import islice

#--------------Data Tree Defining------------#

TreeSubs = DataTree[object]()

#--------------Slider Reader------------#

#print len(ghenv.Component.Params.Input[0].Sources)

def slider_min_max():
    InputList = []

    Maxs = []
    Mins = []
    Counts = []
    for i in ghenv.Component.Params.Input:
        InputList.append(i)

    #Getting GUIDs
    GUIDList = []
    for i in InputList[0].Sources:
        GUIDList.append(str(i.InstanceGuid))
    
    #Getting Domains
    for j in range(len(InputList[0].Sources)):
        Maxs.append(InputList[0].Sources[j].Slider.Maximum)
        Mins.append(InputList[0].Sources[j].Slider.Minimum)
        Counts.append(InputList[0].Sources[j].TickCount)
        Counts_1 = [i+1 for i in Counts]

    #return zip(Mins, Maxs), GUIDList
    return Maxs, Mins, Counts_1, GUIDList

UpperLimits, LowerLimits, Counters, GUIDs = slider_min_max()

#--------------Float Range Definition------------#

def frange(start, End=None, step=None):
    
    print("start: ", start, "End: ", End, "step: ", step)
    count = 0
    while True:
        Alpha = float(start + count * step)
        if step > 0 and Alpha >= End:
            break
        elif step < 0 and Alpha <= End:
            break
        yield Alpha
        count += 1

#--------------Getting Data------------#

Selection_List = []
for i in range(len(ghenv.Component.Params.Input[0].Sources)):
    for j in frange(LowerLimits[i], UpperLimits[i], ((UpperLimits[i]-LowerLimits[i])/(Counters[i]-1))):
        Selection_List.append(j)
    Selection_List.append(UpperLimits[i])

InputList_toSlice = iter(Selection_List)
SelectedSubs = [list(islice(InputList_toSlice, elem)) for elem in Counters]

#--------------Managing Data Tree------------#

for i in range(len(SelectedSubs)):
    Subs = []
    for j in range(len(SelectedSubs[i])):
        Subs.append(SelectedSubs[i][j])
    Paths = GH_Path(i)
    TreeSubs.AddRange(Subs, Paths)


#--------------Slider Hirarechy Issues------------#

if Name_Changer:
    
    SliderObjects = ghenv.Component.Params.Input[0].Sources
    for i in range(len(SliderObjects)):
        SliderObjects[i].NickName = "GA_Num.Slider: {}".format(i)
        SliderObjects[i].ExpireSolution(True)


#--------------Data Tree Defining------------#

Randomized_Selected = DataTree[object]()

#print TreeSubs.BranchCount
#sample branch
#a = TreeSubs.Branches[0]

#-------------Selection Procedure in Class-----------#

class Random_Selection:
    
    if Seed:
        random.seed(Seed)
        
    def __init__(self, Tree, Seed = None):
        self.Tree = Tree
        self.Seed = Seed
        
    def Select_from_tree(self):
        
        Selected_ = []
        for i in range(self.Tree.BranchCount):
            selection = random.sample(self.Tree.Branches[i], 1)
            Selected_.append(selection)
        #print Selected_
        return Selected_
        
    def Listing_Tree(self):
        
        for i in range(len(self.Select_from_tree())):
            Selected_Branch_List = []
            for j in range(len(self.Select_from_tree()[i])):
                Selected_Branch_List.append(self.Select_from_tree()[i][j])
            Paths = GH_Path(i)
            Randomized_Selected.AddRange(Selected_Branch_List, Paths)
            Randomized_Selected.Flatten()
        #print Randomized_Selected
        #return Randomized_Selected
        Randomized_List = []
        for i in range(Randomized_Selected.DataCount):
            Randomized_List.append(Randomized_Selected.AllData()[i])
        print Randomized_List
        return Randomized_List


#Random_List = Random_Selection(TreeSubs).Listing_Tree()

.
.
.



####UNDER DEVELOPMENT####
Architecture, Coding, Computational Design, Data Science & Visualization, Favorites & More, Generative Design, Lectures and Presentations, OpenSource, Research & Development , , , ,
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments