Clocklink

Sunday, 29 January 2012

MY RHINO-SCRIPTS [Simple GA]

The following code demonstrates a basic Genetic Algorithm that I had created whilst working on my thesis program. The programming language used is Microsoft's VBScript implemented in Rhinoceros 4. It has been further enhanced and modified as my thesis develops.  Working drafts and snippets of the final code will be posted in turn. 

Option Explicit
'Script written by Walid nazih | MSC10'11 | 2011
'(c) Walid Nazih. All rights reserved

Call GA()
Sub GA()

Randomize
Dim generation,Maxgens, pop
Dim Chromosome(),gene_length , words, word_length
Dim objects(),values
Dim Mom,Dad,DNAMOM,DNADAD
Dim mutationRate, successCount
Dim FitnessObj()
Dim Populationfitness ,GenerationFitness,Offspring1,Offspring2
Dim i,j
Dim FP,LP
Dim ALL
' -------- Get Integers ----------------------------
Maxgens = Rhino.Integerbox("How Many Generations", 6)
pop = Rhino.integerbox("How Many populations" , 6)
mutationRate = Rhino.Integerbox("Mutation Rate - Percentage %", 10 )
gene_length = 30 ' words * word_length
words = 6 ' give 6 integers between 0 and 31
word_length = 5
FP = 1 ' First population
LP = pop ' last population
'---------- build DNA population---------------------
For i = 1 To pop
ReDim Preserve Chromosome(pop)
Chromosome(i) = BuildDNA(gene_length)
Next
'---------loop through number of generations----------
For generation = 1 To Maxgens
ReDim Preserve objects(Maxgens,pop)
For i = 1 To pop
'--------- Decode genes ------------------------------
Values = Decode(Chromosome,words,word_length,i)
'---------- build objects / Get each population Fitness --------------
objects(generation,i) = build(Values, generation,i ,pop, FitnessObj)
Next
'----------- Fitness Population -------------------------------
Populationfitness = 0
For i = 1 To pop
Populationfitness = Populationfitness + FitnessObj(i)
Next
' --------------------Selection ---------------------
If generation <> Maxgens Then
mom = NaturalSelection(FitnessObj, pop)
dad = NaturalSelection(FitnessObj, pop)
DNADAD = chromosome(Dad)
DNAMOM = chromosome(mom)
'-------------------- Crossover -----------------------
For j = 1 To pop - 1 Step 2
Offspring1 = D_crossover ( DNADAD , DNAMOM , gene_length)
Offspring2 = M_crossover ( DNADAD , DNAMOM , gene_length)
'---------------- Mutation ------------------------
If Rnd() > mutationRate Then
chromosome (j) = Mutate (offspring1,gene_length)
chromosome (j+1) = Mutate (offspring2,gene_length)
Else
chromosome (j) = offspring1
chromosome (j+1) = offspring2
End If
Next
End If
Next
End Sub
'------------------------------------------------- MAKE DNA -----------------------------
Function BuildDNA(gene_length)
Dim gene , j
gene = ""
For j = 1 To gene_length
If Rnd < 0.5 Then
gene = gene + "1"
Else
gene = gene + "0"
End If
Next 
buildDNA = gene
End Function
'-----------------------------------------------Dcode Chromosome -------------------------
Function Decode(Chromosome,words,word_length,i)
Dim values()
Dim temp , pos , endpos
Dim j , k
For j = 1 To words
ReDim Preserve values(j)
temp = 0
endpos = j * word_length
'work backwards through string
For k = 0 To word_length - 1
pos = endpos - k
If Mid(Chromosome(i), pos, 1) = "1" Then temp = temp + 2 ^ k
Next
If temp = 0 Then temp = 1
values(j) = temp
Next
Decode = values
End Function
'---------------------------------------------Build populations ---------------------------
Function build(Values, generation,i,pop,ByRef FitnessObj)
Dim object
Dim Array01
Dim objFit
Dim c
ReDim Preserve FitnessObj(i)
Array01 = Array (Array(0,0,0), Array(values(1),0,0),Array(values(1),values(2),0),Array(0,values(2),0),Array(0,0,values(3)),Array(values(1),0,values(4)),Array(values(1),values(2),values(5)),Array(0,values(2),values(6)))
object = Rhino.AddBox(Array01)
Rhino.MoveObject object, Array(0,0,0), Array (100*i, 100*generation , 0)
objFit = Rhino.SurfaceVolume(object)
FitnessObj(i) = objFit(0) 'the bigger the fittest
build = object
End Function
'------------------------------------------------------- roulette--------------------------------
Function roulette (FP,LP)
Dim Random
Random = Int((LP - FP + 1) * Rnd + FP)
roulette = Random
End Function
'--------------------------------------------------------- Crossover -------------------------------
Function D_crossover ( DNADAD , DNAMOM , gene_length)
Dim start , newdad, NLow,NHigh
NLow = 1
NHigh = gene_length
Randomize
start= Int((NHigh - NLow + 1) * Rnd + NLow)
newdad = left(DNADAD, start) + Mid(DNAMOM, start + 1)
D_crossover = newdad
End Function

Function M_crossover ( DNADAD , DNAMOM , gene_length)
Dim start,newmom , NLow,NHigh
NLow = 1
NHigh = gene_length
Randomize
start= Int((NHigh - NLow + 1) * Rnd + NLow)
newmom = left(DNAMOM, start) + Mid(DNADAD, start + 1)
M_crossover = newmom
End Function

'--------------------------------------------------- Mutation ---------------------------------------
Function Mutate (offspring,gene_length)
Dim pos , NLow , NHigh
NLow = 1
NHigh = gene_length
Randomize
pos= Int((NHigh - NLow + 1) * Rnd + NLow)
If Mid(offspring1, pos, 1) = "0" Then
Mid(offspring1, pos, 1) = "1"
Else
Mid(offspring1, pos, 1) = "0"
End If
mutate1= offspring1
End Function

'--------------------------------------------- Natural Selection ------------------------------------

Function NaturalSelection(FitnessObj, pop)
' select a single individual via weighted roulette wheel selection
Dim slice , counter , j
counter = 0
j = 0
slice = FitnessObj ( Int(Rnd()* (pop-1)) )
Do
j = j + 1
counter = counter + FitnessObj(j)
Loop Until ((counter > slice) Or (j = pop-1))
NaturalSelection = j
End Function

TOWARDS AN INTEGRATED APPLICATION OF GENETIC ALGORITHM IN ARCHITECTURAL DESIGN




'This Thesis is dedicated to Laila, my beloved little daughter and to Laila, my deceased Mom whose generosity and spirituality continues to give me the strength to carry on throughout hard times'.


ACKNOWLEDGEMENTS

I am deeply thankful to Paul S.Coates for his supportive discussions, insightful direction and ponderable comments throughout more than a year of study in UEL. I would also like to thank Emmanouil Zaroukas whose enthusiastic support and contribution have been essential for the development of this work. 
Dad, Mo', Yasmin and Monty for their undivided love and continuous support during the year.


ABSTRACT

Genetic algorithms have been increasingly utilized, currently with a dual application, within architectural design. Firstly, as a form-generative tool that embraces the concept of Emergence through the use of evolution and morphogenesis as a method of producing innovative forms. Secondly as a potent optimization tool for a well-defined building problem such as structural and thermal performance, due to its ability to solve multi-objective optimization problems. However, the use of ‘local’ Genetic Algorithms in architecture, where one or the other of the above identified uses are applied, has been subjugated to wide criticism. Additionally, concerns have been raised about GA’s capability to evolve forms of greater complexity due to the nature of simple Genotype-to-phenotype mapping embedded in traditional GA. It is my conviction that an integrated genetic algorithm paradigm can be accomplished through firstly, the coordination of GA’s dual operation of optimization and form generative properties, in other words, incorporating optimisation properties of GA into a form generative machine comprising a set of morphological processes. Secondly, by modifying the simple genotype-to-phenotype mapping in a way that borrows from nature to enhance GA’s capacity to evolve forms. 



Such a paradigm would represent a powerful generative - performative design methodology to be adopted in early design phases, permitting the emergence of architectural forms in a bottom-up process whilst addressing the notion of enhancing evolvability, which will in turn convey the aesthetic complexity of nature into architectural design. In addition it will assist architects in finding optimal solutions for performance problems associated with these emergent complex forms that surpass the architects’ ability to solve. In other words, architects will obtain a system that creates designs and takes into consideration their performance feedback, such a system will present the architect with various tested design possibilities that are steered by the exploration of form and performance satisfaction early in the design process. 

A previous work carried out on Genetic Algorithm will be expanded whereby a generative system will be embedded in the GA body plan through a set of conditional rule-based processes that vigorously respond to genetic information received arising through the genome’s decoding process. Here, genes translate into values and conditions for the morphological rules located in the system’s generative mechanism. The process of activating and idling these rules according to the inherited genetic materials will subsequently fluctuate the development of the phenotype, in which various design solutions could emerge. The evolutionary dynamics of mutation will be modified by mutating a specific number of genes selected at random rather than applying a global mutation rate for the genome which eventually will result in enhancing the system’s ‘evolvability’. 

The thesis will attempt to demonstrate that such a paradigm has validity by computing the incremental improvement of prescribed design performances and inspecting the system’s evolutionary sequences and the emerging levels of complexity. The phenotype in this examination is a high-rise building envelope guided by environmental and zoning code criteria.