Ich hab unten einen Code gepostet, der Folgendes macht:
Es gibt eine Liste mit Pheromonen (geruchsstoffe über die Ameisen kommunizieren) und aus dieser Liste soll ein Array generiert werden, das die Pheromonkonzentration an jeder Position enthält.
bsp:
Code: Alles auswählen
Liste:
position 10,10
stärke 7
position 10,20
stärke 7
position 15,15
stärke 7
position 2,2
stärke 3
=> Array
2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 2 2 2 2 2 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 2 3 3 3 3 3 2 1 1 0 0 0 0 0 0
0 0 0 0 1 2 3 3 4 4 4 3 3 2 1 0 0 0 0 0 0
0 0 0 1 2 3 3 4 5 5 5 4 3 3 2 1 0 0 0 0 0
0 0 0 1 2 3 4 5 6 6 6 5 5 4 3 2 1 0 0 0 0
0 0 0 1 2 3 4 5 6 7 7 6 6 5 4 3 2 1 1 0 0
0 0 0 1 2 3 4 5 6 7 7 7 7 6 5 4 3 2 1 1 0
0 0 0 1 2 3 3 4 5 6 7 7 6 7 6 5 3 3 2 1 0
0 0 0 0 1 2 3 3 5 6 7 6 7 7 6 5 4 3 3 2 1
0 0 0 0 1 1 2 4 5 6 7 8 7 7 7 6 5 4 3 2 1
0 0 0 0 0 2 2 4 5 6 7 8 7 8 7 6 5 4 3 2 1
0 0 0 0 1 1 2 4 5 6 7 8 7 7 7 6 5 4 3 2 1
0 0 0 0 1 2 3 3 5 6 7 6 7 7 6 5 4 3 3 2 1
0 0 0 1 2 3 3 4 5 6 7 7 6 7 6 5 3 3 2 1 0
0 0 0 1 2 3 4 5 6 7 7 7 7 6 5 4 3 2 1 1 0
0 0 0 1 2 3 4 5 6 7 7 6 6 5 4 3 2 1 1 0 0
0 0 0 1 2 3 4 5 6 6 6 5 5 4 3 2 1 0 0 0 0
0 0 0 1 2 3 3 4 5 5 5 4 3 3 2 1 0 0 0 0 0
0 0 0 0 1 2 3 3 4 4 4 3 3 2 1 0 0 0 0 0 0
0 0 0 0 1 1 2 3 3 3 3 3 2 1 1 0 0 0 0 0 0
0 0 0 0 0 1 1 2 2 2 2 2 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0
Die zeit geht in der Prozedur "generatePheromonMap2" verloren. Also könnt ihr euch mit euren Tipps auf die konzentrieren.
Ich bin für Vorschläge speziell zu diesem Code, aber auch für allgemeine zum Thema Optimierung (was man vermeiden sollte ect.) dankbar
hier nun der Code:
Code: Alles auswählen
height=500
width=500
pheromonStrength=20
Structure Source
amount.i
type.b
EndStructure
Structure ants
pos.point
age.w
type.b
load.source
pherStrength.w
*heap.heaps;pointer to the homeHeap of the ant
angel.f
damage.w
EndStructure
Structure heaps
pos.point
size.i ;size of the heap
food.i ;stored food
numberOfEggs.i
numberOfEggCareAnts.i
colony.b
List egg.w()
EndStructure
Structure pheromonList
Array strength.i(2)
pos.point
EndStructure
Structure pheromonArray
pheroList.i
Array strength.i(2)
EndStructure
Structure colony
color.i ;color of the colony
List ant.ants()
List heap.heaps()
Array pheromonArray.pheromonArray(0) ;array, that contains pointer to the pheromon List
List pheromon.pheromonList()
EndStructure
Dim CircleTemp.w(pheromonStrength*2-2,pheromonStrength*2-2)
Dim circles.w(pheromonStrength-1,pheromonStrength*2-2,pheromonStrength*2-2) ;array that contains the strengthDistribution for each strength<=strength
Procedure generatePheromonMap2(Array colony.colony(1),colony)
Shared circles()
Shared width
Shared height
ForEach colony(colony)\pheromon() ;go through each pheromon of this colony
*pher.PheromonList=colony(colony)\pheromon()
For type=0 To 2 ;go through each type
strength.w=*pher\strength(type)
If strength<>0
range=strength*2-2
For y=-strength+1 To strength-1
For x=-strength+1 To strength-1
AbsPosX=x+*pher\pos\x
AbsPosY=y+*pher\pos\y
If AbsPosX>=0 And AbsPosX<width
If AbsPosY>=0 And AbsPosY<height
colony(colony)\pheromonArray(AbsPosX+width*(AbsPosY))\strength(type)+circles(strength-1,x+strength-1,y+strength-1);sadly only 1-dimensional array possible in structure...
EndIf
EndIf
Next
Next
EndIf
Next
Next
EndProcedure
Procedure generateCircle(radius,Array ar.w(2))
For x=1 To radius*2-1
For y=1 To radius*2-1
dist=Sqr((x-radius)*(x-radius)+(y-radius)*(y-radius))
If dist<radius
ar(x-1,y-1)=radius-dist
Else
ar(x-1,y-1)=0
EndIf
Next
Next
EndProcedure
Procedure addPheromon(Array colony.colony(1),posX,posY,type,colony,strength)
Shared width
With colony(colony)\pheromonArray(width*posY+posX)
If \pheroList=0;create a new element, if it doesn't exist
\pheroList=AddElement(colony(colony)\pheromon());create a new pheromonElement and a pointer to it, which is stored in the array
colony(colony)\pheromon()\strength(type)=strength
colony(colony)\pheromon()\pos\x=posX
colony(colony)\pheromon()\pos\y=posY
Else
colony(colony)\pheromon()\strength(type)+strength
EndIf
EndWith
EndProcedure
For i=1 To pheromonStrength
generateCircle(i,CircleTemp())
For x=0 To i*2-2
For y=0 To i*2-2
circles(i-1,x,y)=CircleTemp(x,y) ;fill circles() with circles of every size <= pheromonStrength
Next
Next
Next
Dim colony.colony(0)
Dim colony(0)\pheromonArray(width*height)
For y=0 To 99
For x=0 To 99
addPheromon(colony(),x,y,0,0,pheromonStrength)
Next
Next
time=GetTickCount_()
generatePheromonMap2(colony(),0)
time=GetTickCount_()-time
; CreateFile(0,"test.txt")
; For y=0 To 110
; For x=0 To 110
; WriteString(0,Str(colony(0)\pheromonArray(x+y*width)\strength(0))+" ")
; Next
; WriteStringN(0,"")
; Next
; CloseFile(0)
MessageRequester("",Str(time))