aaa---robot_rescue_base.nlogo
来自「NETLOGO」· NLOGO 代码 · 共 458 行
NLOGO
458 行
;;;;;; =================================================================;;;;;; Definition of the simulation world;;;;;; =================================================================;;;;;;;;; Global variables and constants;;;globals [WALL ROAD BUILDING HOSPITAL CLEARED DEBRIS CARRIED FLEET LIFTERS IMAGERS MIN_VICTIMS MAX_VICTIMS SOMEONE NOT_RESCUED RESCUED NUM_STEPS BLOCK_SIZE TIME_OUT];;;;;; Initialise values of global variables: most ar used as constants;;;to set-globals set WALL -1 set ROAD 1 set BUILDING 2 set HOSPITAL -2 set CLEARED 3 set DEBRIS 4 set CARRIED [ ] set FLEET 10 set LIFTERS 4 set IMAGERS 4 set MIN_VICTIMS 15 set MAX_VICTIMS 60 set SOMEONE 5 set NOT_RESCUED 0 set RESCUED 1 set BLOCK_SIZE 5 set TIME_OUT 2880end;;;;;; Initialise the simulation;;;to reset ca ; clear all set-globals setup-patches setup-turtles set NUM_STEPS 0 ask turtles [initialise-turtles]end;;;;;; Set up the agents. Create persons, robot-lifters, robot-imagers, ambulances.;;;;;;;;; Declare the kinds of turtles in this model (robot-lifters, robot-imagers, ambulances and persons);;;breed [robot-lifters robot-lifter]breed [robot-imagers robot-imager]breed [ambulances ambulance]breed [persons person];;;;;; initialise all robots;;;to setup-turtleslocals[victims xpos ypos] ;; set ambulances up - FLEET gives number create-ambulances FLEET set-default-shape ambulances "car" foreach values-from ambulances [who] [set color-of turtle ?1 red set xcor-of turtle ?1 (-12 + ?1) set ycor-of turtle ?1 -8 set heading-of turtle ?1 270] ;; set up the robot lifters create-robot-lifters LIFTERS set-default-shape robot-lifters "van side" (foreach values-from robot-lifters [who] [set color-of robot-lifter ?1 magenta ;; initial position after ambulances set xcor-of robot-lifter ?1 (-2 + ?1 - FLEET) set ycor-of robot-lifter ?1 -8 set heading-of robot-lifter ?1 90 set passengers-of robot-lifter ?1 CARRIED]);; set up the robot imagersset-default-shape robot-imagers "truck-right"create-robot-imagers IMAGERS (foreach values-from robot-imagers [who] [set color-of robot-imager ?1 green ;; Initial position after ambulances and lifters set xcor-of robot-imager ?1 2 + ?1 - (FLEET + LIFTERS) set ycor-of robot-imager ?1 -8 set heading-of robot-imager ?1 90]) ;; put random number of victims - between MiN and MAX - on the map but only in buildings and not on top of each other set-default-shape persons "person" set victims (random (MAX_VICTIMS - MIN_VICTIMS)) + MIN_VICTIMS create-persons victims (foreach values-from persons [who] [set xpos (random 19) - 10 set ypos (random 12) - 6 ;;; loop until find unoccupied building patch for a victim while [(occupancy-of patch xpos ypos = SOMEONE) or (kind-of patch xpos ypos = ROAD)] [set xpos (random 19) - 10 set ypos (random 12) - 6] ;;; put the person on the chosen patch set xcor-of person ?1 xpos set ycor-of person ?1 ypos set occupancy-of patch xpos ypos SOMEONE])end;;;;;; Declare properties of the cells in the grid;;; kind = ROAD, BUILDING, WALL, HOSPITAL;;; occupancy = turtle on the patch;;; patches-own [kind occupancy];;;;;; Set up the environment. Create the city;;;to setup-patches ;; make everything road by default ask patches [ set kind ROAD ] ask patches [ set pcolor gray + 4] ;;; set up the surrounding wall foreach [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 1 0 1 2 3 4 5 6 7 8 9 10] [ set pcolor-of patch -13 ? black set kind-of patch -13 ? WALL set pcolor-of patch 11 ? black set kind-of patch 11 ? WALL] ;; allow way out for ambulances set kind-of patch -13 -8 ROAD set pcolor-of patch -13 -8 gray + 4 set kind-of patch -13 -7 ROAD set pcolor-of patch -13 -7 gray + 4 foreach [-14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [ set pcolor-of patch ? -10 black set kind-of patch ? -10 WALL set pcolor-of patch ? 8 black set kind-of patch ? 8 WALL] ;;; make one square the hospital set pcolor-of patch 12 -8 red set kind-of patch 12 -8 HOSPITAL ;; set up some buildings setup-buildings -10 -6 setup-buildings -3 -6 setup-buildings 4 -6 setup-buildings -10 1 setup-buildings -3 1 setup-buildings 4 1endto setup-buildings [xstart ystart]locals[xpos ypos]set xpos xstartrepeat BLOCK_SIZE[set ypos ystart repeat BLOCK_SIZE [set pcolor-of patch xpos ypos blue + 2 set kind-of patch xpos ypos BUILDING set ypos ypos + 1] set xpos xpos + 1] end;;;;;; Update the state of the simulation;;;to update set NUM_STEPS (NUM_STEPS + 1) ;; update the state of all agents ask turtles [ cycle-once ] ;; See if we are in the final state ;; Either everyone is rescued or we ran out of time if (people-rescued? = number-of-victims?) or (NUM_STEPS = TIME_OUT) [ stop ] end;;;;;; Report how many victims there were;;;to-report number-of-victims? report (count persons)end;;;;;; Count the number of people rescued;;;to-report people-rescued? report (count (persons with [status = RESCUED]))end;;;;;; Count the number of simulation steps executed;;;to-report number-of-steps report NUM_STEPSend;;;;;;;;; =================================================================;;;;;; Definition of agents;;;;;; =================================================================;;;;;;;;; Declare internal variables for the robot-lifters;;;robot-lifters-own [passengers];;; cargo: show which persons a lifter is carrying;;; ;;; ;;;;;; Initialise different robots;;;to initialise-turtles ;; use breed here to differentiate between robots etc ;; e.g if breed = robot-lifters [ ]end;;;;;; Ambulances carry passengers;;;ambulances-own[passengers] ;;;;;; Declare internal variables for people;;; Give people a status (= NOT_RESCUED or RESCUED);;; Store number of carrying-robotpersons-own [status carrying-robot];;;;;; each robot calls its behaviours in this simulation cycle: all they do;;; right now is to move forward one square. This is where other things ;;; should be called, depending on the breed;;; e.g. if breed = robot-lifters [ robot-lifter-move ];;;to cycle-once if breed = robot-lifters [move-forwards] end;;;;;; ------------------------;;; Actions;;; ------------------------;;;;;;;;; Example showing movement of robot into cell in front of it;;; As this uses passenger-of it will not work for robot-imagers!;;;to move-forwards locals [in-front] set in-front (patch-ahead 1) ;; check if the cell is free if (kind-of in-front = ROAD) and (not any? turtles-on in-front) [ fd 1 if (carrying-person? who) [move-passengers who]]end;;;;;; If turtle moves carrying person(s), then move them too ;;;to move-passengers [robot] foreach (passengers-of robot) [set xcor-of person ? (xcor-of robot) set ycor-of person ? (ycor-of robot)]end;;;;;; Rotate agent 90 degrees left;;;to rotate-left lt 90end;;;;;; Rotate agent 90 degrees right;;;to rotate-right rt 90end;;;;;; Rotate agent randomly left or right 90 degrees;;;to rotate-random ifelse (random 2 = 0) [ rotate-left ] [ rotate-right ]end;;;;;; ------------------------;;; Sensors;;; ------------------------;;;;;;;;; Check if robot is carrying a person: return boolean;;; true if is carrying;;;to-report carrying-person? [robot] report not (empty? (passengers-of (turtle robot)))end;;;;;; Check how many people a robot is carrying;;;to-report number-carried? [robot] report (count passengers-of robot)end;;;;;; Check if there is a person in front of the robot and return their id;;;to-report victim-found? locals [casualty] set casualty persons-on patch-ahead 1 ; if there is a person to be rescued in front of the robot... if any? casualty [report casualty]end@#$#@#$#@GRAPHICS-WINDOW32110911461141020.01101110111-1414-1010CC-WINDOW5475920570Command Center0BUTTON29269459NILResetNIL1TOBSERVERTNILBUTTON1302719960RunupdateT1TOBSERVERTNILBUTTON2302729860StepupdateNIL1TOBSERVERTNILMONITOR2971155120Number of victimsnumber-of-victims?01MONITOR18871308120Number rescuedpeople-rescued?01MONITOR189129307178Number of stepsnumber-of-steps01@#$#@#$#@About this model--------------------This scenario pictures - schematically - the centre of Edinburgh where an earthquake has taken place and people are trapped in the buildings. Three types of agents have been mobilised to rescue them:a) 4 Robot-imagers
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?