Ants/Clone01: различия между версиями

Материал из Поле цифровой дидактики
Нет описания правки
 
(не показано 6 промежуточных версий 2 участников)
Строка 1: Строка 1:
* [[Ants]]
<netlogo model="AntsClone02" />
== Код  ==
=== Пример 1 ===
<syntaxhighlight lang="lisp">
to circle
  layout-circle turtles max-pxcor - 2
end
</syntaxhighlight>
== ВАРИАНТ 1!! ==
=== Память муравьев по координатам ===
<syntaxhighlight lang="lisp">
globals [
  food-source-number
  food-in-nest
  food-in-nest-history
  chemical-history
]
patches-own [
  chemical
  food
  food-source-number
  nest?
]
turtles-own [
turtles-own [
   has-food     ; 0 = нет еды, 1 = несёт еду
   chemical
   memory        ; список запомненных патчей (где была еда)
  food
  energy        ; энергия муравья
   memory        ; список запомненных патчей (мест с едой)
]
]
;; ============================================
;; ИНИЦИАЛИЗАЦИЯ
;; ============================================
to setup
  ca
  set-default-shape turtles "bug"
  ask patch 0 0 [ set nest? true set pcolor violet ]
  create-food-sources
  create-ants
  set food-in-nest 0
  set food-in-nest-history []
  set chemical-history []
  reset-ticks
end
to create-food-sources
  let number-of-food-sources 4
  set food-source-number number-of-food-sources
  let angle 0
  let radius (max-pxcor - 5)
  let angle-step 360 / number-of-food-sources
  repeat number-of-food-sources [
    let xcor radius * cos angle
    let ycor radius * sin angle
    ask patch xcor ycor [
      set food 1
      set pcolor green
      set food-source-number angle
    ]
    set angle angle + angle-step
  ]
end
to create-ants
  create-turtles 50 [
    setxy 0 0
    set color red
    set chemical 0
    set food 0
    set memory []
    set label ""
  ]
end
;; ============================================
;; ОСНОВНОЙ ЦИКЛ
;; ============================================
to go
  if all? turtles [food = 1] [ stop ]
  ask turtles [
    if nest? and food = 1 [ drop-food ]
    if not nest? [ move ]
    if food-here > 0 and food = 0 [ pick-up-food ]
    if not nest? [ lay-chemical ]
  ]
  diffuse chemical 0.5
  ask patches [
    set chemical chemical * 0.95
    if chemical < 0.01 [ set chemical 0 ]
    if not nest? [
      set pcolor scale-color gray chemical 0 10
    ]
  ]
  forget-stale-memory
  update-plots
  tick
end
;; ============================================
;; ПАМЯТЬ (ОСНОВНАЯ ЛОГИКА)
;; ============================================
to pick-up-food
  set food 1
  set color orange
  set label "F"
  set food-here 0
  ask patch-here [ set food 0 set pcolor gray ]
end
to drop-food
  set food 0
  set color red
  set label ""
  set food-in-nest food-in-nest + 1
end
to lay-chemical
  ask patch-here [ set chemical chemical + 60 ]
end
to move
  ; используем память, если энергии мало и есть воспоминания
  if energy < 100 and length memory > 0 [
    let closest-patch min-one-of memory [distance myself]
    if closest-patch != nobody [
      face closest-patch
      fd 1
      stop
    ]
  ]
  ; иначе обычное движение по феромону
  if chemical > 0.5 [
    uphill-chemical
  ]
  fd 1
end
to uphill-chemical
  let best-patch patch-here
  let best-value chemical
  ask neighbors [
    if chemical > best-value [
      set best-value chemical
      set best-patch self
    ]
  ]
  if best-patch != patch-here [
    face best-patch
  ]
end
to forget-stale-memory
  if length memory > 0 [
    let to-remove []
    foreach memory [ mem-patch ->
      if [food] of mem-patch = 0 [
        set to-remove lput mem-patch to-remove
      ]
    ]
    foreach to-remove [ rm-patch ->
      set memory remove rm-patch memory
    ]
  ]
end
to update-plots
  ; заглушка для графиков
end
</syntaxhighlight>
<syntaxhighlight lang="lisp">


globals [
globals [
   total-food   ; сколько еды доставлено в гнездо
   total-food-count
  food-history-list
  chemical-history-list
]
 
patches-own [
  chemical-amount
  food-amount
  is-nest?
  nest-scent-value
  food-source-id
]
]


;;;;;;;;;;;;;;;;;;;;;
turtles-own [
;; НАСТРОЙКА МИРА ;;
  chemical-carrying
;;;;;;;;;;;;;;;;;;;;;
  food-carrying
  energy-level
  memory-list
]


to setup
to setup
   clear-all
   clear-all
   set-default-shape turtles "bug"
   set-default-shape turtles "bug"
    
   create-turtles population
  ; гнездо в центре (0,0)
   [ set size 2
  ask patch 0 0 [ set pcolor violet ]
 
  ; 8 источников еды по кругу (для мира от -16 до 16)
  ask patch 16 0 [ set pcolor green set plabel "E" ]      ; право
  ask patch -16 0 [ set pcolor green set plabel "E" ]    ; лево
  ask patch 0 16 [ set pcolor green set plabel "E" ]      ; верх
  ask patch 0 -16 [ set pcolor green set plabel "E" ]    ; низ
  ask patch 16 16 [ set pcolor green set plabel "E" ]    ; верх-право
  ask patch -16 16 [ set pcolor green set plabel "E" ]    ; верх-лево
   ask patch 16 -16 [ set pcolor green set plabel "E" ]    ; низ-право
  ask patch -16 -16 [ set pcolor green set plabel "E" ]  ; низ-лево
 
  ; создаём муравьёв (40 штук)
  create-turtles 40 [
    setxy 0 0
     set color red
     set color red
     set has-food 0
     set chemical-carrying 0
     set memory []
     set food-carrying 0
     set energy 200
     set energy-level 200
  ]
    set memory-list [] ]
    
   setup-patches
   set total-food 0
   set total-food-count 0
  set food-history-list []
  set chemical-history-list []
   reset-ticks
   reset-ticks
end
end


;;;;;;;;;;;;;;;;;;;
to setup-patches
;; ОСНОВНОЙ ЦИКЛ ;;
  ask patches
;;;;;;;;;;;;;;;;;;;
  [ setup-nest
    setup-food
    recolor-patch ]
end
 
to setup-nest
  set is-nest? (distancexy 0 0) < 5
  set nest-scent-value 200 - distancexy 0 0
end
 
to setup-food
  if (distancexy (0.6 * max-pxcor) 0) < 5
  [ set food-source-id 1 ]
  if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
  [ set food-source-id 2 ]
  if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
  [ set food-source-id 3 ]
  if food-source-id > 0
  [ set food-amount one-of [1 2] ]
end
 
to recolor-patch
  ifelse is-nest?
  [ set pcolor violet ]
  [ ifelse food-amount > 0
    [ if food-source-id = 1 [ set pcolor cyan ]
      if food-source-id = 2 [ set pcolor sky  ]
      if food-source-id = 3 [ set pcolor blue ] ]
    [ set pcolor scale-color green chemical-amount 0.1 5 ] ]
end


to go
to go
   ask turtles [
   ask turtles
    ; тратим энергию
  [ if who >= ticks [ stop ]
     set energy energy - 1
     set energy-level energy-level - 1
     if energy <= 0 [ die ]
     if energy-level <= 0 [ die ]
   
    ; поведение в зависимости от того, несёт ли еду
     ifelse color = red
     ifelse color = red
      [ look-for-food ]
    [ look-for-food ]
      [ return-to-nest ]
    [ return-to-nest ]
      
     wiggle
    ; движение
     fd 1 ]
     fd 1
  diffuse chemical-amount (diffusion-rate / 100)
    right random 30
  ask patches
     left random 30
  [ set chemical-amount chemical-amount * (100 - evaporation-rate) / 100
   ]
     recolor-patch ]
   forget-stale-memory
   tick
   tick
end
end


;;;;;;;;;;;;;;;;;;;
to return-to-nest
;; ПОВЕДЕНИЕ      ;;
  ifelse is-nest?
;;;;;;;;;;;;;;;;;;;
  [ set color red
    set food-carrying 0
    set energy-level energy-level + 100
    set total-food-count total-food-count + 1
    rt 180 ]
  [ set chemical-carrying chemical-carrying + 60
    set chemical-amount chemical-amount + 60
    uphill-nest-scent ]
end


to look-for-food
to look-for-food
   ; если на патче есть еда (зелёный цвет)
   if food-amount > 0
   if pcolor = green [
   [ set food-carrying 1
    set has-food 1
     set color orange + 1
     set color orange
     set food-amount food-amount - 1
     set pcolor black      ; еда исчезает
     rt 180
    set plabel ""          ; убираем метку
   
     ; ЗАПОМИНАЕМ МЕСТО (если память включена)
     if use-memory? [
     if use-memory? [
       set memory lput patch-here memory
       set memory-list lput patch-here memory-list
      ; ограничиваем память 15 местами
       if length memory-list > 15 [ set memory-list but-last memory-list ]
       if length memory > 15 [ set memory but-last memory ]
     ]
     ]
   
     stop ]
    right 180              ; разворачиваемся к гнезду
   if use-memory? and energy-level < memory-threshold and length memory-list > 0 [
     stop
     let memory-agentset patch-set memory-list
  ]
    let closest-patch min-one-of memory-agentset [distance myself]
 
     if closest-patch != nobody [
  ; ЕСЛИ ЭНЕРГИЯ МАЛА И ЕСТЬ ПАМЯТЬ — ИДЁМ К ЗАПОМНЕННОМУ МЕСТУ
       face closest-patch
   if use-memory? and energy < memory-threshold and length memory > 0 [
     let target-patch min-one-of memory [distance myself]
     if target-patch != nobody [
       face target-patch
       stop
       stop
     ]
     ]
   ]
   ]
  if (chemical-amount >= 0.05) and (chemical-amount < 2)
  [ uphill-chemical ]
end
to uphill-chemical
  let scent-ahead chemical-scent-at-angle  0
  let scent-right chemical-scent-at-angle  45
  let scent-left  chemical-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end
to uphill-nest-scent
  let scent-ahead nest-scent-at-angle  0
  let scent-right nest-scent-at-angle  45
  let scent-left  nest-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end
to wiggle
  rt random 40
  lt random 40
  if not can-move? 1 [ rt 180 ]
end
end


to return-to-nest
to-report nest-scent-at-angle [angle]
   ; если в гнезде (фиолетовый патч)
   let p patch-right-and-ahead angle 1
   if pcolor = violet [
   if p = nobody [ report 0 ]
    set has-food 0
  report [nest-scent-value] of p
    set color red
    set energy energy + 100    ; награда за еду
    set total-food total-food + 1
    right 180
    stop
  ]
end
end


;; ============================================
to-report chemical-scent-at-angle [angle]
;; ЗАБЫВАНИЕ УСТАРЕВШЕЙ ПАМЯТИ
  let p patch-right-and-ahead angle 1
;; ============================================
  if p = nobody [ report 0 ]
  report [chemical-amount] of p
end


to forget-stale-memory
to forget-stale-memory
   if length memory > 0 [
   ask turtles [
    let to-forget []
    if length memory-list > 0 [
    foreach memory [ mem-patch ->
      let to-forget []
      ; если на запомненном патче больше нет еды (не зелёный)
      foreach memory-list [ mem-patch ->
      if [pcolor] of mem-patch != green [
        if [food-amount] of mem-patch = 0 [
        set to-forget lput mem-patch to-forget
          set to-forget lput mem-patch to-forget
        ]
      ]
      foreach to-forget [ forget-patch ->
        set memory-list remove forget-patch memory-list
       ]
       ]
    ]
    foreach to-forget [ forget-patch ->
      set memory remove forget-patch memory
     ]
     ]
   ]
   ]
end
end
; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.
</syntaxhighlight>

Текущая версия от 13:43, 14 мая 2026

* Ants

Код

Пример 1

to circle
  layout-circle turtles max-pxcor - 2
end


ВАРИАНТ 1!!

Память муравьев по координатам

globals [
  food-source-number
  food-in-nest
  food-in-nest-history
  chemical-history
]

patches-own [
  chemical
  food
  food-source-number
  nest?
]

turtles-own [
  chemical
  food
  memory        ; список запомненных патчей (мест с едой)
]

;; ============================================
;; ИНИЦИАЛИЗАЦИЯ
;; ============================================

to setup
  ca
  set-default-shape turtles "bug"
  ask patch 0 0 [ set nest? true set pcolor violet ]
  create-food-sources
  create-ants
  set food-in-nest 0
  set food-in-nest-history []
  set chemical-history []
  reset-ticks
end

to create-food-sources
  let number-of-food-sources 4
  set food-source-number number-of-food-sources
  let angle 0
  let radius (max-pxcor - 5)
  let angle-step 360 / number-of-food-sources
  repeat number-of-food-sources [
    let xcor radius * cos angle
    let ycor radius * sin angle
    ask patch xcor ycor [
      set food 1
      set pcolor green
      set food-source-number angle
    ]
    set angle angle + angle-step
  ]
end

to create-ants
  create-turtles 50 [
    setxy 0 0
    set color red
    set chemical 0
    set food 0
    set memory []
    set label ""
  ]
end

;; ============================================
;; ОСНОВНОЙ ЦИКЛ
;; ============================================

to go
  if all? turtles [food = 1] [ stop ]
  ask turtles [
    if nest? and food = 1 [ drop-food ]
    if not nest? [ move ]
    if food-here > 0 and food = 0 [ pick-up-food ]
    if not nest? [ lay-chemical ]
  ]
  diffuse chemical 0.5
  ask patches [
    set chemical chemical * 0.95
    if chemical < 0.01 [ set chemical 0 ]
    if not nest? [
      set pcolor scale-color gray chemical 0 10
    ]
  ]
  forget-stale-memory
  update-plots
  tick
end

;; ============================================
;; ПАМЯТЬ (ОСНОВНАЯ ЛОГИКА)
;; ============================================

to pick-up-food
  set food 1
  set color orange
  set label "F"
  set food-here 0
  ask patch-here [ set food 0 set pcolor gray ]
end

to drop-food
  set food 0
  set color red
  set label ""
  set food-in-nest food-in-nest + 1
end

to lay-chemical
  ask patch-here [ set chemical chemical + 60 ]
end

to move
  ; используем память, если энергии мало и есть воспоминания
  if energy < 100 and length memory > 0 [
    let closest-patch min-one-of memory [distance myself]
    if closest-patch != nobody [
      face closest-patch
      fd 1
      stop
    ]
  ]
  ; иначе обычное движение по феромону
  if chemical > 0.5 [
    uphill-chemical
  ]
  fd 1
end

to uphill-chemical
  let best-patch patch-here
  let best-value chemical
  ask neighbors [
    if chemical > best-value [
      set best-value chemical
      set best-patch self
    ]
  ]
  if best-patch != patch-here [
    face best-patch
  ]
end

to forget-stale-memory
  if length memory > 0 [
    let to-remove []
    foreach memory [ mem-patch ->
      if [food] of mem-patch = 0 [
        set to-remove lput mem-patch to-remove
      ]
    ]
    foreach to-remove [ rm-patch ->
      set memory remove rm-patch memory
    ]
  ]
end

to update-plots
  ; заглушка для графиков
end


globals [
  total-food-count
  food-history-list
  chemical-history-list
]

patches-own [
  chemical-amount
  food-amount
  is-nest?
  nest-scent-value
  food-source-id
]

turtles-own [
  chemical-carrying
  food-carrying
  energy-level
  memory-list
]

to setup
  clear-all
  set-default-shape turtles "bug"
  create-turtles population
  [ set size 2
    set color red
    set chemical-carrying 0
    set food-carrying 0
    set energy-level 200
    set memory-list [] ]
  setup-patches
  set total-food-count 0
  set food-history-list []
  set chemical-history-list []
  reset-ticks
end

to setup-patches
  ask patches
  [ setup-nest
    setup-food
    recolor-patch ]
end

to setup-nest
  set is-nest? (distancexy 0 0) < 5
  set nest-scent-value 200 - distancexy 0 0
end

to setup-food
  if (distancexy (0.6 * max-pxcor) 0) < 5
  [ set food-source-id 1 ]
  if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
  [ set food-source-id 2 ]
  if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
  [ set food-source-id 3 ]
  if food-source-id > 0
  [ set food-amount one-of [1 2] ]
end

to recolor-patch
  ifelse is-nest?
  [ set pcolor violet ]
  [ ifelse food-amount > 0
    [ if food-source-id = 1 [ set pcolor cyan ]
      if food-source-id = 2 [ set pcolor sky  ]
      if food-source-id = 3 [ set pcolor blue ] ]
    [ set pcolor scale-color green chemical-amount 0.1 5 ] ]
end

to go
  ask turtles
  [ if who >= ticks [ stop ]
    set energy-level energy-level - 1
    if energy-level <= 0 [ die ]
    ifelse color = red
    [ look-for-food  ]
    [ return-to-nest ]
    wiggle
    fd 1 ]
  diffuse chemical-amount (diffusion-rate / 100)
  ask patches
  [ set chemical-amount chemical-amount * (100 - evaporation-rate) / 100
    recolor-patch ]
  forget-stale-memory
  tick
end

to return-to-nest
  ifelse is-nest?
  [ set color red
    set food-carrying 0
    set energy-level energy-level + 100
    set total-food-count total-food-count + 1
    rt 180 ]
  [ set chemical-carrying chemical-carrying + 60
    set chemical-amount chemical-amount + 60
    uphill-nest-scent ]
end

to look-for-food
  if food-amount > 0
  [ set food-carrying 1
    set color orange + 1
    set food-amount food-amount - 1
    rt 180
    if use-memory? [
      set memory-list lput patch-here memory-list
      if length memory-list > 15 [ set memory-list but-last memory-list ]
    ]
    stop ]
  if use-memory? and energy-level < memory-threshold and length memory-list > 0 [
    let memory-agentset patch-set memory-list
    let closest-patch min-one-of memory-agentset [distance myself]
    if closest-patch != nobody [
      face closest-patch
      stop
    ]
  ]
  if (chemical-amount >= 0.05) and (chemical-amount < 2)
  [ uphill-chemical ]
end

to uphill-chemical
  let scent-ahead chemical-scent-at-angle   0
  let scent-right chemical-scent-at-angle  45
  let scent-left  chemical-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end

to uphill-nest-scent
  let scent-ahead nest-scent-at-angle   0
  let scent-right nest-scent-at-angle  45
  let scent-left  nest-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end

to wiggle
  rt random 40
  lt random 40
  if not can-move? 1 [ rt 180 ]
end

to-report nest-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [nest-scent-value] of p
end

to-report chemical-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [chemical-amount] of p
end

to forget-stale-memory
  ask turtles [
    if length memory-list > 0 [
      let to-forget []
      foreach memory-list [ mem-patch ->
        if [food-amount] of mem-patch = 0 [
          set to-forget lput mem-patch to-forget
        ]
      ]
      foreach to-forget [ forget-patch ->
        set memory-list remove forget-patch memory-list
      ]
    ]
  ]
end

; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.