Ants/Clone01: различия между версиями
Материал из Поле цифровой дидактики
CristCas (обсуждение | вклад) |
CristCas (обсуждение | вклад) |
||
| Строка 15: | Строка 15: | ||
ВАРИАНТ 1!! | |||
=== Память муравьев по координатам === | === Память муравьев по координатам === | ||
| Строка 184: | Строка 184: | ||
ВАРИАНТ 2!! | |||
=== Память как второй тип феромона === | |||
<syntaxhighlight lang="netlogo"> | |||
;; ============================================ | ;; ============================================ | ||
;; ANTS WITH MEMORY (Вариант 2) | |||
;; Память как второй тип феромона (индивидуальный след) | ;; Память как второй тип феромона (индивидуальный след) | ||
;; ============================================ | ;; ============================================ | ||
| Строка 251: | Строка 254: | ||
set chemical 0 | set chemical 0 | ||
set food 0 | set food 0 | ||
set has-memory? false | set has-memory? false | ||
set label "" | set label "" | ||
] | ] | ||
| Строка 268: | Строка 271: | ||
if not nest? [ lay-chemicals ] | if not nest? [ lay-chemicals ] | ||
] | ] | ||
;; Обычный феромон | ;; Обычный феромон | ||
diffuse chemical 0.5 | diffuse chemical 0.5 | ||
| Строка 275: | Строка 278: | ||
if chemical < 0.01 [ set chemical 0 ] | if chemical < 0.01 [ set chemical 0 ] | ||
] | ] | ||
;; Память-феромон (испаряется и диффундирует) | ;; Память-феромон (испаряется и диффундирует) | ||
if use-memory? [ | if use-memory? [ | ||
| Строка 284: | Строка 287: | ||
] | ] | ||
] | ] | ||
;; Визуализация | ;; Визуализация | ||
ask patches [ | ask patches [ | ||
if not nest? [ | if not nest? [ | ||
ifelse memory-pheromone > 0.1 | ifelse memory-pheromone > 0.1 | ||
[ set pcolor scale-color blue memory-pheromone 0 5 ] | [ set pcolor scale-color blue memory-pheromone 0 5 ] | ||
[ set pcolor scale-color gray chemical 0 10 ] | [ set pcolor scale-color gray chemical 0 10 ] | ||
] | ] | ||
] | ] | ||
update-plots | update-plots | ||
tick | tick | ||
| Строка 308: | Строка 311: | ||
set food-here 0 | set food-here 0 | ||
ask patch-here [ set food 0 ] | ask patch-here [ set food 0 ] | ||
set has-memory? true | set has-memory? true | ||
end | end | ||
| Строка 320: | Строка 322: | ||
to lay-chemicals | to lay-chemicals | ||
ask patch-here [ set chemical chemical + 60 ] | ask patch-here [ set chemical chemical + 60 ] | ||
if use-memory? and has-memory? [ | if use-memory? and has-memory? [ | ||
ask patch-here [ | ask patch-here [ | ||
| Строка 333: | Строка 332: | ||
to move | to move | ||
if use-memory? and has-memory? [ | if use-memory? and has-memory? [ | ||
let mem-here memory-pheromone | let mem-here memory-pheromone | ||
| Строка 342: | Строка 340: | ||
] | ] | ||
] | ] | ||
if chemical > 0.5 [ | if chemical > 0.5 [ | ||
uphill-chemical | uphill-chemical | ||
| Строка 349: | Строка 345: | ||
stop | stop | ||
] | ] | ||
fd 1 | fd 1 | ||
end | end | ||
| Строка 385: | Строка 379: | ||
; заглушка для графиков | ; заглушка для графиков | ||
end | end | ||
</syntaxhighlight> | |||
Версия от 12:05, 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
ВАРИАНТ 2!!
Память как второй тип феромона
;; ============================================
;; ANTS WITH MEMORY (Вариант 2)
;; Память как второй тип феромона (индивидуальный след)
;; ============================================
globals [
food-source-number
food-in-nest
food-in-nest-history
chemical-history
]
patches-own [
chemical ; коллективный феромон
memory-pheromone ; индивидуальный феромон (память)
food
food-source-number
nest?
]
turtles-own [
chemical
food
has-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 has-memory? false
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-chemicals ]
]
;; Обычный феромон
diffuse chemical 0.5
ask patches [
set chemical chemical * 0.95
if chemical < 0.01 [ set chemical 0 ]
]
;; Память-феромон (испаряется и диффундирует)
if use-memory? [
diffuse memory-pheromone 0.5
ask patches [
set memory-pheromone memory-pheromone * (100 - memory-evaporation) / 100
if memory-pheromone < 0.05 [ set memory-pheromone 0 ]
]
]
;; Визуализация
ask patches [
if not nest? [
ifelse memory-pheromone > 0.1
[ set pcolor scale-color blue memory-pheromone 0 5 ]
[ set pcolor scale-color gray chemical 0 10 ]
]
]
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 has-memory? true
end
to drop-food
set food 0
set color red
set label ""
set food-in-nest food-in-nest + 1
end
to lay-chemicals
ask patch-here [ set chemical chemical + 60 ]
if use-memory? and has-memory? [
ask patch-here [
set memory-pheromone memory-pheromone + memory-deposit
if memory-pheromone > 200 [ set memory-pheromone 200 ]
]
]
end
to move
if use-memory? and has-memory? [
let mem-here memory-pheromone
if mem-here > 0.5 [
uphill-memory-pheromone
fd 1
stop
]
]
if chemical > 0.5 [
uphill-chemical
fd 1
stop
]
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 uphill-memory-pheromone
let best-patch patch-here
let best-value memory-pheromone
ask neighbors [
if memory-pheromone > best-value [
set best-value memory-pheromone
set best-patch self
]
]
if best-patch != patch-here [
face best-patch
]
end
to update-plots
; заглушка для графиков
end