Imagin a family of worms sorted on a path. Every worm move randomly from previous one. This worm recive a vote based on approach to the target specified.
Coding this script I follow a very simple rule: worms that gain better vote may proliferate replacing themselves in next generations.
Every generation manifest mutation like in Darwin's theory of evolution, and environment rules define if this mutations are good or not for species.
Like human body and ant colony, later generations reduce scattering for a more accurate configuration. Studying countless generations, the family should reach the target specified.
Specifying different rules for worms should be possible to study emergent systems whose behavior is not predictable from the study of individual elements, like organic complexity L-systems.
Option Explicit
' Script written by Alessandro Zomparelli
' alessandro.zomparelli@gmail.com
' http://alessandrozompa.altervista.org/
' Script version venerdì 30 ottobre 2009 16.59.03
Call Main()
Sub Main()
Dim strTarget, arrFamily, arrTarget, n
n=Rhino.GetInteger("set number elements",20,2,100)
ReDim arrFamily(n), arrTarget(2)
arrFamily(0)=Rhino.GetObject("pick starting point")
'check for starting point
If Rhino.IsPoint(arrFamily(0))=False Then
Exit Sub
End If
strTarget=Rhino.GetObject("pick target point")
'check for target point
If Rhino.IsPoint(strTarget)=False Then
Exit Sub
End If
arrTarget=Rhino.PointCoordinates(strTarget)
Dim dblDist
dblDist=Rhino.Distance(Rhino.PointCoordinates(arrFamily(0)),arrTarget)
'defining number of generations and amount of mutation between following generations
Dim dblGen, dblMut
dblGen=Rhino.GetInteger("set number of generations",100,2,1000)
dblMut=Rhino.GetReal("set mutation value",2,0.01,100)
'defining an array to collect data about different family
Dim arrData
ReDim arrData(n)
'defining a value for better step-element of different family
Dim dblVote
'defining counters, array for data recording and an array for scattering direction
Dim i, j, arrVect, arrVect0, arrRec
ReDim arrVect(2), arrVect0(2), arrRec(1)
'defining random data for first generation
For j=1 To n
arrVect=array(rnd-rnd,rnd-rnd,rnd-rnd)
dblVote=0
arrData(j)=array(dblVote,arrVect)
Next
Dim strLine
'starting evolution system
For i=0 To dblGen-1
Call Rhino.EnableRedraw(False)
'starting family growning
For j=1 To n
arrRec=arrData(j)
arrVect0=arrRec(1)
arrVect=array(arrVect0(0)+dblMut*(dblGen-i)/dblGen*(rnd-rnd),arrVect0(1)+dblMut*(dblGen-i)/dblGen*(rnd-rnd),arrVect0(2)+dblMut*(dblGen-i)/dblGen*(rnd-rnd))
arrVect0=Rhino.VectorUnitize(arrVect)
arrVect=Rhino.VectorScale(arrVect0,dblDist/n)
'generation of new element of the family
arrFamily(j)=Rhino.CopyObject(arrFamily(j-1),arrVect)
strLine=Rhino.AddLine(Rhino.PointCoordinates(arrFamily(j-1)),Rhino.PointCoordinates(arrFamily(j)))
Call Rhino.ObjectColor(strLine, RGB(255-255/dblGen*i,0,255/dblGen*i))
If Rhino.Distance(Rhino.PointCoordinates(arrFamily(j)),arrTarget)
arrData(j)=array(dblVote,arrVect)
End If
End If
Next
For j=1 To n
Call Rhino.DeleteObject(arrFamily(j))
Next
Call Rhino.EnableRedraw(True)
Next
End Sub
Hi Alessandro! Nice to see you're keeping up posting!
ReplyDeleteCareful of 2 things, one theorical and one technical:
. evolution does not happen towards a target, nor it is a linear process; environmental pressure plays a crucial part. Yours is more likely to a noise-reduction algorithm (which is good!) but, besides the first random generation, results are predictable since you just blend the original configuration towards the point (your algorithm will never diverge, for example). Try to rely more onto and explore local (part-to-part) interaction.
. clean your scripts from unuseful repetitions: putting in a loop instructions that can be run only one time (Rhino.Enableredraw or dblVote=0) is just a waste in speed and resources. This kind of scripting optimization is useful when a script grows in number of lines or when you start exploring recursive algorithms.
Cheers!
Alessio
.I totally agree with first point. I would like to explore local interaction for an unpredictable result, like in emergent systems. It was only a test about some base principles.
ReplyDelete.surely the script is not optimized (I also need to clean the management of nested arrays), but I put Rhino.Enableredraw in loop for display "evolutionary progression" step by step. But it's also true that working with a lot of "generations" it takes a long time... :(
Thanks
Alessandro