Pertama..
Fokuskan pada pembuatan GUI di tabulasi Interface (untuk mempermudah bayangan anda, bisa anda lihat gambar di atas ato dipostingan terdahulu.
Langkah 1, kita buat slider untuk mengatur jumlah semut (Slider yang pertama). Caranya, klik tombol button -> pilih slider -> tempatkan (dengan cara klik) dilayar putih sebelah kiri.
Ya, seharusnya muncul wizard untuk konfigurasi Slider. Global variable ini untuk memberi nama slider. Karena tujuan kita ingin membuat slider untuk mengatur jumlah semut, kita isi saja dengan NumberOfAnt.
Minimum, untuk menandakan value (jumlah semut) minimal, ane isikan saja 1. Increment juga biarkan saja terisi 1. Maximum, mempunyai fungsi yang berkebalikan dengan Minimum, ane isikan saja 100.
Value, artinya nilai default yang akan diset. Ane isikan nilai value : 50.
Langkah 2, kita buat tombol setup. Langkah membuat tombol setup, sama seperti membuat slider.
Tombol setup disini nantinya akan digabungkan dengan coding (akan dijelaskan dibagian procedures nanti). Tombol ini saya buat sebagai perintah untuk inisiasi dan menyiapkan lokasi sarang juga makanan. Apa saja yang harus dipersiapkan ?
- Pertama, kita atur agar button ini dapat dikendalikan oleh external agent/observer.
- Kedua, pada isian commands, kita isi setup. Maksudnya tombol ini nantinya dikaitkan dengan procedures setup.
- Berikutnya kita isi Display name : "Setup". Display name berfungsi untuk memberi nama button.
- Klik OK untuk mengakhiri.
Langkah 3, buat tombol go. Pada tombol go,kita persiapkan seperti berikut :
- Pertama, kita atur agar button ini dapat dikendalikan oleh external agent/observer.
- Kedua, check list forever diberi tanda centang. Mengapa? Karena saat button go dipencet. Button ini nantinya akan menjalankan procedur terkait secara loop hingga selesai, karenanya check list forever harus diberi tanda centang. Jika tidak berarti tidak ada proses loop saat button ini dieksekusi.
- Berikutkan, command diisi go.
- Display name juga diisi go.
- Klik OK untuk mengakhiri
Slider ini dibuat untuk mengatur besaran sudut head wiggle (goyangan kepala) pada setiap langkah si semut pemimpin. Jadi jika besaran sudutnya dibuat 0 derajat, maka si semut akan berjalan lurus menuju makanan. Sudut terbesar adalah 90 derajat. Dari cerita ini, bisa kan mengatur isiannya? Gak bisa? Oke deh saya beri gambarnya.
Langkah 5, membuat slider delay.
Seperti namanya, slider ini bertujuan untuk mengatur jeda antar semut. Kita atur jeda minimum adalah 0 detik dan maksimum jeda adalah 60 detik. Bisa kan membuat konfigurasi slider ini ya..
Langkah 6, membuat monitor ants-released.
Ini fungsinya untuk monitor berapa jumlah semut yang telah meninggalkan sarangnya. Cara membuatnya :
- Klik pada button -> pilih monitor -> klik pada area putih yang kosong
- Pada kotak isian reporter, kita isi : count turtles with [xcor > nest-x]. Maksudnya menghitung jumlah turtles (ingat kan arti dari istilah turtles?) dimana koordinat x-nya lebih besar daripada koordinat x nest (sarang)
- Display name, diisi ant-released, kemudian klik OK.
Kedua..
Kita fokuskan perhatian kita ke tabulasi procedures. Bagian ini penting, karena disinilah kita coding bagaimana si agent tersebut bekerja.
breed [ leaders leader ]
breed [ followers follower ]
globals [
nest-x nest-y ;; location of center of nest
food-x food-y ;; location of center of food
leader-heading ;; heading of the leader ant
]
to setup ;; set up nest and food locations
ca
set-default-shape turtles "bug"
set nest-x 10 + min-pxcor
set nest-y 0
set food-x max-pxcor - 10
set food-y 0
;; draw the nest in brown by stamping a circular
;; brown turtle
ask patch nest-x nest-y [
sprout 1 [
set color brown
set shape "circle"
set size 10
stamp
die
]
]
;; draw the nest in orange by stamping a circular
;; orange turtle
ask patch food-x food-y [
sprout 1 [
set color orange
set shape "circle"
set size 10
stamp
die
]
]
create-leaders 1
[ set color red ;; leader ant is red
set size 2
wiggle 50 ] ;; ...and starts out with a random heading
create-followers (num-ants - 1)
[ set size 2
set color yellow ] ;; middle ants are yellow
ask turtles
[ setxy nest-x nest-y ;; start the ants out at the nest
set heading 90 ]
ask turtle (num-ants - 1)
[ set color blue ;; last ant is blue
set pen-size 2
pd ] ;; ...and leaves a trail
ask leaders
[ set pen-size 2
pd ] ;; the leader also leaves a trail
set leader-heading [heading] of one-of leaders
to go
if all? turtles [xcor >= food-x]
[ stop ]
ask leaders ;; the leader ant wiggles and moves
[ wiggle leader-wiggle-angle
correct-path
if (xcor > (food-x - 5 )) ;; leader heads straight for food, if it is close
[ facexy food-x food-y ]
if xcor < food-x ;; do nothing if you're at or past the food [ fd 0.5 ] ] ask followers [ face turtle (who - 1) ;; follower ants follow the ant ahead of them if time-to-start? and (xcor < food-x) ;; followers wait a bit before leaving nest [ fd 0.5 ] ] set leader-heading [heading] of one-of leaders tick end ;; turtle procedure; wiggle a random amount, averaging zero turn to wiggle [angle] rt random-float angle lt random-float angle end ;; turtle procedure to correct-path ifelse heading > 180
[ rt 180 ]
[ if patch-at 0 -5 = nobody
[ rt 100 ]
if patch-at 0 5 = nobody
[ lt 100 ] ]
end
;; turtle reporter; if true, then the ant is authorized to move out of the nest
to-report time-to-start?
report ([xcor] of (turtle (who - 1))) > (nest-x + start-delay + random start-delay )
end
; Copyright 1997 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.
breed [ followers follower ]
globals [
nest-x nest-y ;; location of center of nest
food-x food-y ;; location of center of food
leader-heading ;; heading of the leader ant
]
to setup ;; set up nest and food locations
ca
set-default-shape turtles "bug"
set nest-x 10 + min-pxcor
set nest-y 0
set food-x max-pxcor - 10
set food-y 0
;; draw the nest in brown by stamping a circular
;; brown turtle
ask patch nest-x nest-y [
sprout 1 [
set color brown
set shape "circle"
set size 10
stamp
die
]
]
;; draw the nest in orange by stamping a circular
;; orange turtle
ask patch food-x food-y [
sprout 1 [
set color orange
set shape "circle"
set size 10
stamp
die
]
]
create-leaders 1
[ set color red ;; leader ant is red
set size 2
wiggle 50 ] ;; ...and starts out with a random heading
create-followers (num-ants - 1)
[ set size 2
set color yellow ] ;; middle ants are yellow
ask turtles
[ setxy nest-x nest-y ;; start the ants out at the nest
set heading 90 ]
ask turtle (num-ants - 1)
[ set color blue ;; last ant is blue
set pen-size 2
pd ] ;; ...and leaves a trail
ask leaders
[ set pen-size 2
pd ] ;; the leader also leaves a trail
set leader-heading [heading] of one-of leaders
end
to go
if all? turtles [xcor >= food-x]
[ stop ]
ask leaders ;; the leader ant wiggles and moves
[ wiggle leader-wiggle-angle
correct-path
if (xcor > (food-x - 5 )) ;; leader heads straight for food, if it is close
[ facexy food-x food-y ]
if xcor < food-x ;; do nothing if you're at or past the food [ fd 0.5 ] ] ask followers [ face turtle (who - 1) ;; follower ants follow the ant ahead of them if time-to-start? and (xcor < food-x) ;; followers wait a bit before leaving nest [ fd 0.5 ] ] set leader-heading [heading] of one-of leaders tick end ;; turtle procedure; wiggle a random amount, averaging zero turn to wiggle [angle] rt random-float angle lt random-float angle end ;; turtle procedure to correct-path ifelse heading > 180
[ rt 180 ]
[ if patch-at 0 -5 = nobody
[ rt 100 ]
if patch-at 0 5 = nobody
[ lt 100 ] ]
end
;; turtle reporter; if true, then the ant is authorized to move out of the nest
to-report time-to-start?
report ([xcor] of (turtle (who - 1))) > (nest-x + start-delay + random start-delay )
end
; Copyright 1997 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.
Baris potongan perintah (procedures) diawali kalimat to diakhiri end
to correct-path
ifelse heading > 180
[ rt 180 ]
[ if patch-at 0 -5 = nobody
[ rt 100 ]
if patch-at 0 5 = nobody
[ lt 100 ] ]
ifelse heading > 180
[ rt 180 ]
[ if patch-at 0 -5 = nobody
[ rt 100 ]
if patch-at 0 5 = nobody
[ lt 100 ] ]
end
Referensi..
Wilensky, U. (1997). NetLogo Ant Lines model.
wow blognya keren...maju terus gan...^_^
ReplyDelete