为什么NetLogo在两个时间段内以相同的语法和逻辑运行不同

问题描述 投票:0回答:1

每个人!我正在努力使用NetLogo程序。该代码看似正确,但是该模型无法运行。我希望任何人都可以帮助我。我将此模型设计为:

Period1:day> 0和day <30 and time> 1,海龟进入某些补丁程序(补丁程序1,补丁程序2,补丁程序3 ...),并按照规则停止,每个补丁程序具有最大的存储空间乌龟,如果补丁达到最大存储量,请让该补丁上的所有乌龟都死掉。

Period2:如果day> = 31且day <60 and time> 1,请问新创建的海龟在Period1中转到相同的补丁程序,并且每个补丁程序具有不同的最大存储量,如果该补丁程序达到其最大存储量,请询问所有该补丁上的海龟死亡。

Period1和Period2中的逻辑和语法相同,唯一的区别是参数的值。但是,Period1起作用,Period2不起作用。我的意思是在Period1中,海龟可以去补丁并正确死亡。海龟甚至无法移动,在Period2中什么也没发生,但滴答声仍在运行,并且没有任何错误提示。为什么会这样呢?有人可以给我一些建议吗?

任何评论和建议将不胜感激。代码如下:

globals [time day]
breed [RiverVolumes RiverVolume]                        ;; ceate a breed to represent river volumes
breed [WaterVolumes WaterVolume]              ;; ceate a breed to represent irrigation volumes
breed [StorageVolumes StorageVolume]                    ;; ceate a breed to represent storage volumes
breed [Crops Crop]
patches-own[CurrentStorage QgateMaxR QgateMaxR1 QgateMaxL QgateMaxL1]

to setup
  clear-all
  reset-ticks
end

to go
  set time ticks mod 24                                                                  ;; there are 24 ticks per day, one tick is equal to 1 hour
  set day (floor (ticks / 24)) mod 7 + 1                                                 ;; day range from 1 to 7, means Monday to Sunday
  tick

 if day > 0 and day < 30 and time > 1
  [
    FirstProcedure
    RCropGrowth-1
    LCropGrowth-1
    DeathOfFirstWaterVolumes
    set-label
  ]

  if day >= 31 and day < 60 and time > 1
  [
    SecondProcedure
    RCropGrowth-2
    LCropGrowth-2
    DeathOfSecondWaterVolumes
    set-label
  ]
end

to FirstProcedure                                                                             
  ifelse [CurrentStorage] of patch 35 15 < FirstStorage
  [
    move-to-right-canal1-1
    RGateCapacity-1
    RGateFlow1-1
    RFieldStorage-1
    RFieldStorageOverFlow-1
  ]
  [
    ifelse [CurrentStorage] of patch 1 15 < FirstStorage
    [
      move-to-left-canal1-1
      LGateCapacity-1
      LGateFlow1-1
      LFieldStorage-1
      LFieldStorageOverFlow-1
    ]
    [
       ask patches with [pycor = 0]
       [
         if any? RiverVolumes-here
         [
           ask RiverVolumes [die]
         ]

       ]
     ]
   ]
end

to move-to-right-canal1-1
  ask patches with [pcolor = 95.1]
  [
    if pxcor = 18 and pycor = 14
    [
      ask n-of QgateMaxFixed1 RiverVolumes-here
      [
        lt 90
        fd 1
      ]
    ]
  ]
end

to RGateCapacity-1
  ask patches with [pcolor = 48.8]                                                      ;; gates at the right side of the river
  [
    ifelse pxcor = 19
    [
      set QgateMaxR1 QgateMaxFixed1                                                     ;; set the capacity of the first right gate is QgateMaxFixed1
    ]
    [
      set QgateMaxR QgateMaxFixed
    ]
  ]
end

to RGateFlow1-1
  ask patches with [pcolor = 48.8 and pycor = 14]                               ;; gates at the right side of the river
  [
    ifelse pxcor = 19
    [
      ask n-of QgateMaxFixed1 RiverVolumes-here [fd 1]
    ]
    [
      ifelse CurrentStorage < FirstStorage
      [
        ifelse (count RiverVolumes-here - QgateMaxR) > 0
        [
          ask n-of QgateMaxFixed RiverVolumes-here [lt 90]
        ]
        [
          ask n-of (count RiverVolumes-here) RiverVolumes-here [lt 90]
        ]
      ]
      [
      ]
    ]
  ]
end

to RFieldStorage-1
  ask patches with [pcolor = 64.2]                                     ;; ask the fields at the right side of the river
  [
    set CurrentStorage (CurrentStorage + count RiverVolumes-here)
    sprout-WaterVolumes (count RiverVolumes-here)
    ask WaterVolumes-here
    [
      set color 97
      set size 0.8
      set heading 0
    ]
    ask RiverVolumes-here [die]
  ]
end

to RFieldStorageOverFlow-1
  ask patches with [pcolor = 64.2]
  [
    if CurrentStorage > FirstStorage
    [
      ask n-of (CurrentStorage - FirstStorage) WaterVolumes-here [die]
      set CurrentStorage FirstStorage
    ]
  ]
end

to move-to-left-canal1-1
  ask patches with [pcolor = 95.1]
  [
    if pxcor = 18 and pycor = 14
    [
        ask n-of QgateMaxFixed1 RiverVolumes-here
        [
          rt 90
          fd 1
        ]
    ]
  ]
end

to LGateCapacity-1
  ask patches with [ pcolor = 47.4]                                                     ;; gates at the left side of the river
  [
    ifelse pxcor = 17
    [
      set QgateMaxL1 QgateMaxFixed1                                                     ;; set the capacity of the first right gate is QgateMaxFixed1
    ]
    [
      set QgateMaxL QgateMaxFixed
    ]
  ]
end

to LGateFlow1-1
  ask patches with [pcolor = 47.4 and pycor = 14]                               ;; gates at the left side of the river
  [
    ifelse pxcor = 17
    [
      ask n-of QgateMaxFixed1 RiverVolumes-here [fd 1]
    ]
    [
      ifelse CurrentStorage < FirstStorage
      [
        ifelse (count RiverVolumes-here - QgateMaxL) > 0
        [
          ask n-of QgateMaxFixed RiverVolumes-here [rt 90]
        ]
        [
          ask n-of (count RiverVolumes-here) RiverVolumes-here [rt 90]
        ]
      ]
      [
      ]
    ]
  ]
end

to LFieldStorage-1
  ask patches with [pcolor = 64.2]                                     ;; ask the fields at the right side of the river
  [
    set CurrentStorage (CurrentStorage + count RiverVolumes-here)
    sprout-WaterVolumes (count RiverVolumes-here)
    ask WaterVolumes-here
    [
      set color 97
      set size 0.8
      set heading 0
    ]
    ask RiverVolumes-here [die]
  ]
end

to LFieldStorageOverFlow-1
  ask patches with [pcolor = 64.2]                                   ;; ask the one of the fields which used to store the irrigation water
  [
    if CurrentStorage > FirstStorage                                                       ;; if the current storage exceeds the maximum storage
    [
      ask n-of (CurrentStorage - FirstStorage) WaterVolumes-here [die]                ;; ask the extra storage volumes to die (so they go out the system)
      set CurrentStorage FirstStorage                                                      ;; set current storage to the FirsIrrigationDemand
    ]
  ]
end

to RCropGrowth-1
  ask patches with [pcolor = 22.6]
  [
    if pxcor > 20 and ([CurrentStorage] of patch (pxcor - 1) pycor) = FirstStorage
    [
      sprout-Crops 1
      [
        set shape "plant"
        set size 0.1
        set color green
      ]
    ]
  ]
end

to LCropGrowth-1
  ask patches with [pcolor = 22.6]
  [
    if pxcor < 16 and ([CurrentStorage] of patch (pxcor + 1) pycor) = FirstStorage
    [
      sprout-Crops 1
      [
        set shape "plant"
        set size 0.1
        set color green
      ]
    ]
  ]
end

to DeathOfFirstWaterVolumes
  ask patches with [pcolor = 64.2 and pxcor > 20 and pycor = 15]
  [
    if any? Crops-on patch 36 15
    [
      ask WaterVolumes-here [die]
    ]
  ]

  ask patches with [pcolor = 64.2 and pxcor < 16  and pycor = 15]
  [
    if any? Crops-on patch 0 15
    [
      ask WaterVolumes-here [die]
    ]
  ]
end

to SecondProcedure                                                                            
  ifelse [CurrentStorage] of patch 35 15 < SecondStorage
  [
    move-to-right-canal1-2
    RGateCapacity-2
    RGateFlow1-2
    RFieldStorage-2
    RFieldStorageOverFlow-2
  ]
  [
    ifelse [CurrentStorage] of patch 1 15 < SecondStorage
    [
      move-to-left-canal1-2
      LGateCapacity-2
      LGateFlow1-2
      LFieldStorage-2
      LFieldStorageOverFlow-2
    ]
    [
      ask patches with [pycor = 0]
      [
        if any? RiverVolumes-here
        [
          ask RiverVolumes [die]
        ]
      ]
    ]
  ]
end

to move-to-right-canal1-2
  ask patches with [pcolor = 95.1]
  [
    if pxcor = 18 and pycor = 14
    [
      ask n-of QgateMaxFixed1 RiverVolumes-here
      [
        lt 90
        fd 1
      ]
    ]
  ]
end

to RGateCapacity-2
  ask patches with [pcolor = 48.8]                                                      ;; gates at the right side of the river
  [
    ifelse pxcor = 19
    [
      set QgateMaxR1 QgateMaxFixed1                                                     ;; set the capacity of the first right gate is QgateMaxFixed1
    ]
    [
      set QgateMaxR QgateMaxFixed
    ]
  ]
end

to RGateFlow1-2
  ask patches with [pcolor = 48.8 and pycor = 14]                               ;; gates at the right side of the river
  [
    ifelse pxcor = 19
    [
      ask n-of QgateMaxFixed1 RiverVolumes-here [fd 1]
    ]
    [
      ifelse CurrentStorage < SecondStorage
      [
        ifelse (count RiverVolumes-here - QgateMaxR) > 0
        [
          ask n-of QgateMaxFixed RiverVolumes-here [lt 90]
        ]
        [
          ask n-of (count RiverVolumes-here) RiverVolumes-here [lt 90]
        ]
      ]
      [
      ]
    ]
  ]
end

to RFieldStorage-2
  ask patches with [pcolor = 64.2]                                     ;; ask the fields at the right side of the river
  [
    set CurrentStorage (CurrentStorage + count RiverVolumes-here)
    sprout-WaterVolumes (count RiverVolumes-here)
    ask WaterVolumes-here
    [
      set color 97
      set size 0.8
      set heading 0
    ]
    ask RiverVolumes-here [die]
  ]
end

to RFieldStorageOverFlow-2
  ask patches with [pcolor = 64.2]
  [
    if CurrentStorage > SecondStorage                                                       ;; if the current storage exceeds the maximum storage
    [
      ask n-of (CurrentStorage - SecondStorage) WaterVolumes-here [die]                ;; ask the extra storage volumes to die (so they go out the system)
      set CurrentStorage SecondStorage
    ]
  ]
end

to move-to-left-canal1-2
  ask patches with [pcolor = 95.1]
  [
    if pxcor = 18 and pycor = 14
    [
        ask n-of QgateMaxFixed1 RiverVolumes-here
        [
          rt 90
          fd 1
        ]
    ]
  ]
end

to LGateCapacity-2
  ask patches with [ pcolor = 47.4]                                                     ;; gates at the left side of the river
  [
    ifelse pxcor = 17
    [
      set QgateMaxL1 QgateMaxFixed1                                                     ;; set the capacity of the first right gate is QgateMaxFixed1
    ]
    [
      set QgateMaxL QgateMaxFixed
    ]
  ]
end

to LGateFlow1-2
  ask patches with [pcolor = 47.4 and pycor = 14]                               ;; gates at the left side of the river
  [
    ifelse pxcor = 17
    [
      ask n-of QgateMaxFixed1 RiverVolumes-here [fd 1]
    ]
    [
      ifelse CurrentStorage < SecondStorage
      [
        ifelse (count RiverVolumes-here - QgateMaxL) > 0
        [
          ask n-of QgateMaxFixed RiverVolumes-here [rt 90]
        ]
        [
          ask n-of (count RiverVolumes-here) RiverVolumes-here [rt 90]
        ]
      ]
      [
      ]
    ]
  ]
end

to LFieldStorage-2
  ask patches with [pcolor = 64.2]                                     ;; ask the fields at the right side of the river
  [
    set CurrentStorage (CurrentStorage + count RiverVolumes-here)
    sprout-WaterVolumes (count RiverVolumes-here)
    ask WaterVolumes-here
    [
      set color 97
      set size 0.8
      set heading 0
    ]
    ask RiverVolumes-here [die]
  ]
end

to LFieldStorageOverFlow-2
  ask patches with [pcolor = 64.2]
  [
    if CurrentStorage > SecondStorage                                                       ;; if the current storage exceeds the maximum storage
    [
      ask n-of (CurrentStorage - SecondStorage) WaterVolumes-here [die]                ;; ask the extra storage volumes to die (so they go out the system)
      set CurrentStorage SecondStorage
    ]
  ]
end

to RCropGrowth-2
  ask patches with [pcolor = 22.6]
  [
    if pxcor > 20 and ([CurrentStorage] of patch (pxcor - 1) pycor) = FirstStorage
    [
      sprout-Crops 1
      [
        set shape "plant"
        set size 0.2
        set color green
      ]
    ]
  ]
end

to LCropGrowth-2
  ask patches with [pcolor = 22.6]
  [
    if pxcor < 16 and ([CurrentStorage] of patch (pxcor + 1) pycor) = FirstStorage
    [
      sprout-Crops 1
      [
        set shape "plant"
        set size 0.2
        set color green
      ]
    ]
  ]
end

to DeathOfSecondWaterVolumes
  ask patches with [pcolor = 64.2 and pxcor > 20 and pycor = 15]
  [
    if any? Crops-on patch 36 15
    [
      ask WaterVolumes-here [die]
    ]
  ]

  ask patches with [pcolor = 64.2 and pxcor < 16  and pycor = 15]
  [
    if any? Crops-on patch 0 15
    [
      ask WaterVolumes-here [die]
    ]
  ]
end

to set-label
  ask patches with [pcolor = 64.2 or pcolor = 98.4 or pcolor = 47.4 or pcolor = 48.8 or pcolor = 94.5 or pcolor = 44.9 or pcolor = 95.1]
  [                                                                       
    ifelse (count turtles-here) > 0                 
    [
      set plabel (count turtles-here)         
      set plabel-color black
    ]
    [set plabel ""]                                                        
  ]
end
netlogo
1个回答
0
投票

因此,我将在这里进行猜测,因为如果没有您的正确设置程序,我们将无法运行模型。 (我们还需要您的接口变量的值。)但是SecondProcedure的第一行是

ifelse [CurrentStorage] of patch 35 15 < SecondStorage

您的setup或仅使用NetLogo默认值,就可能将此补丁的CurrentStorage初始化为零。在您的第一个过程中,是否可能将此补丁的CurrentStorage填充到大于或等于SecondStorage的水平,并且与补丁1 15的填充水平相同?如果是这样,您的SecondProcedure可能不会给您想要的结果。您可以通过输入

来检查此设置(如Seth所建议的)
show [CurrentStorage] of patch 35 15

ifelse之前先查看其实际值是多少。如果我是正确的话,那不是您期望的那样,您将需要重新初始化它,并且可能需要在第一和第二期间之间重新初始化其他变量。

show命令对调试非常有用。我建议您自由使用它!

希望这会有所帮助,查尔斯

© www.soinside.com 2019 - 2024. All rights reserved.