agent based protocols

agent

Option Explicit

Call Main()
Sub Main()

Dim startAgents, iterations, maxVel, maxForce, startPt, endPt, vel, arrAgents(), strobs
Dim ptCloud, vecAcc, vecAli, vecSep, vecCoh, arrPoints(), i, j, vecobs,m, k
Dim arrSrf, vecSrf
Dim arrSrfAgents, surface

‘ user input
startAgents = Rhino.GetObjects(“select agent start curves”, 4)
strobs = Rhino.GetObjects(“obstacle”, 1)

For k=0 To ubound(strobs)
ReDim Preserve arrobs(k)
arrobs(k)=rhino.PointCoordinates(strobs(k))
Next

iterations = rhino.getreal (“how many steps or iterations”, 200)

maxVel = 5
maxForce = 0.2
ptCloud = Null

‘ compile an array for all the agents

‘ (pos, vel, maxVel, maxForce)

For i = 0 To Ubound(startAgents)
startPt = rhino.CurveStartPoint(startAgents(i))
endPt = rhino.CurveEndPoint(startAgents(i))
vel = rhino.VectorCreate(endPt, startPt)  ‘go from start

ReDim Preserve arrAgents(i)   ” redimension but do not want to del anything

arrAgents(i) = Array(startPt, vel, maxVel, maxForce)

Next

‘ loop through steps/iterations

For i = 0 To iterations-1
rhino.EnableRedraw False
‘ loopo through each agent

For j = 0 To Ubound(arrAgents)

For m=0 To ubound(arrobs)
‘set acc to 0
vecAcc = Array(0,0,0)   ‘ array with no length and no direction

‘get some surface info

‘call the behavior functions – separation, alignment, cohesion
vecAli = align(arrAgents(j), arrAgents)
vecSep = separate(arrAgents(j), arrAgents)
vecCoh = cohesion(arrAgents(j), arrAgents)
vecobs = obstacle(arrobs(m), arrAgents)

‘weight the returned vecotors – scale  ‘ change the priority

vecAli = rhino.VectorScale(vecAli, 1)  ‘srf curve more, align more
vecSep = rhino.VectorScale(vecSep, 1)
vecCoh = rhino.VectorScale(vecCoh, 2)
vecobs = rhino.VectorScale(vecobs, 5)

‘add vectoers to acc
vecAcc = Rhino.VectorAdd(vecAcc, vecAli)
vecAcc = rhino.VectorAdd(vecAcc, vecSep)
vecAcc = rhino.VectorAdd(vecAcc, vecCoh)
vecAcc = rhino.VectorAdd(vecAcc, vecobs)

vel = rhino.VectorAdd(arrAgents(j)(1), vecAcc)    ”’arrAgents(j)(1)   –   old vel
‘ limit
vel = vectorLimit(vel,arrAgents(j)(2))

rhino.AddLine arrAgents(j)(0), VectorAdd(arrAgents(j)(0), vel)

‘ update vel+pos in the array
arrAgents(j)(0) = rhino.VectorAdd(arrAgents(j)(0), vel)
arrAgents(j)(1) = vel

Next

Next

‘ update the pointcloud
For j = 0 To Ubound(arrAgents)
ReDim Preserve arrPoints(j)
arrPoints(j) = arrAgents(j)(0)
Next

If Not isNull(ptCloud) Then   ””double negative
rhino.DeleteObject ptCloud  ”’first loop doesn’t exist the ptCloud
End If
ptCloud = Rhino.AddPointCloud(arrPoints)

rhino.EnableRedraw True
Next    ‘ end i loop – stips

End Sub

Function vectorLimit (vec, limit)

Dim length

length = rhino.VectorLength(vec)

If length > limit Then
vec = rhino.VectorUnitize(vec)
vectorLimit = rhino.VectorScale(vec, limit)
Else
vectorLimit = vec
End If

End Function

Function align(agent, arrAgents)

Dim count, sum, dist, i

sum = Array(0,0,0)
count = 0
‘loop all the agents
For i = 0 To Ubound(arrAgents)
‘ get distance to the agent
dist = rhino.Distance(agent(0), arrAgents(i)(0))
‘ is dist within threshold
If dist < 10 And dist > 0 Then   ”’>0, make itself out
sum = rhino.VectorAdd(sum, arrAgents(i)(1))
count = count + 1
End If

Next

If count > 0 Then   ”’in case count = 0
sum = rhino.VectorScale(sum, 1/count)
sum = vectorLimit(sum, agent(3))   ‘limit to maxforce
End If

align = sum

End Function

Function separate(agent, arrAgents)

Dim sum, count, i, dist, sepVec

sum = Array(0,0,0)
count = 0

‘loop all the agents

For i = 0 To UBound(arrAgents)
‘ distance
dist = rhino.Distance(agent(0), arrAgents(i)(0))

‘ check if within threshold
If dist < 10 And dist > 0 Then
‘ get a vector between two agents and add to sum
sepVec = rhino.VectorCreate(agent(0), arrAgents(i)(0))

sepVec = rhino.VectorScale(sepVec, 1/dist) ”’the closer to each other, the bigger the influence

sum = rhino.VectorAdd(sum, sepVec)
count = count + 1
End If

Next
If count > 0 Then
sum = rhino.VectorScale(sum, 1/count)
sum = vectorLimit(sum, agent(3))   ‘limit to maxforce
End If

separate = sum

End Function

Function obstacle(obs, arrAgents)
Dim sum, count, i, dist, sepVec

sum = Array(0,0,0)
count = 0

‘loop all the agents

For i = 0 To UBound(arrAgents)
‘ distance
dist = rhino.Distance(obs, arrAgents(i)(0))

‘ check if within threshold
If dist < 10 And dist > 0 Then
‘ get a vector between two agents and add to sum
sepVec = rhino.VectorCreate(obs, arrAgents(i)(0))

sepVec = rhino.VectorScale(sepVec, 1/dist) ”’the closer to each other, the bigger the influence

sum = rhino.VectorAdd(sum, sepVec)
count = count + 1
End If

Next
If count > 0 Then
sum = rhino.VectorScale(sum, 1/count)
sum = vectorLimit(sum, 0.2 )   ‘limit to maxforce
End If

obstacle = sum

End Function

Function cohesion(agent, arrAgents)
Dim sum, count, dist, vec, i

sum = array(0,0,0)
count = 0
‘loop through all agents

For i = 0 To Ubound(arrAgents)
dist = rhino.Distance(agent(0), arrAgents(i)(0))
If dist < 10 And dist > 0 Then
sum = rhino.VectorAdd(sum, arrAgents(i)(0))
count = count +1

End If

Next

If count > 0 Then
sum = rhino.VectorScale(sum, 1/count)
vec = rhino.VectorCreate(sum, agent(0))    ‘ sum – avarage position of the agents
vec = vectorLimit(vec, agent(3))    ‘limit
Else
vec = Array(0,0,0)

End If

cohesion = vec

End Function

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s