NetLogo A-Star path finding

you should add a "setup" button and an "go" button on the GUI of the netlogo, like this:

and then you paste the code here to the code tab:

patches-own
[
  can-reach?
  parent-node
  is-open?
  is-close?
  
  g-score
  f-score
  
  temp-g-score
]

globals
[
  move-space
  open-set
  close-set
  start-node
  goal-node
  
  a-stop
  b-stop
]

to setup
  clear-all
  set a-stop 1
  set b-stop 0
  ask patches
  [
    set can-reach? true
    set is-open? false
    set is-close? false
    set parent-node -1
  ]
  ask patches
  [
    if pxcor + pycor < 40 and pxcor + pycor > -50
    [
      if pxcor = pycor
      [
        set can-reach? false
      ]
      if pxcor = pycor + 1
      [
        set can-reach? false
      ]
      if pxcor = pycor - 1
      [
        set can-reach? false
      ]
    ]
    if pxcor - pycor < 40 and pxcor - pycor > -40
    [
      if pxcor + pycor = 40
      [
        set can-reach? false
      ]
      if pxcor + pycor = 41
      [
        set can-reach? false
      ]
      if pxcor + pycor = 39
      [
        set can-reach? false
      ]
    ]
  ]
  ask patches
  [
    if-else can-reach?
    [
      set pcolor 87
    ]
    [
      set pcolor 17
    ]
  ]
  set start-node patch -40 40
  ask start-node
  [
    set is-open? true
  ]
  set goal-node patch 40 -40
  reset-ticks
end

to go
  ask patches with [is-open? = true]
  [
    set g-score 0
    set f-score g-score + sqrt(([pxcor] of self - [pxcor] of goal-node)*([pxcor] of self - [pxcor] of goal-node) + ([pycor] of self - [pycor] of goal-node)*([pycor] of self - [pycor] of goal-node))
  ]
  while [(count patches with [is-open? = true]) != 0]
  [
    set open-set patches with [is-open? = true]
    let min-f-score min [f-score] of open-set
    let current-node one-of open-set with [f-score = min-f-score]
    if current-node = goal-node
    [
      color-the-path
      stop
    ]
    ask current-node
    [
      set is-open? false
      set is-close? true
      ask neighbors
      [
        set temp-g-score [g-score] of current-node + 1.42
      ]
      ask neighbors4
      [
        set temp-g-score temp-g-score - 0.42
      ]
      ask neighbors
      [
        if can-reach?
        [
          set pcolor pcolor + -0.3
          if-else is-close?
          [
            if-else temp-g-score >= g-score
            [
            ]
            [
              if is-open? = false or temp-g-score < g-score
              [
                set parent-node current-node
;                show word self current-node
                set g-score temp-g-score
                set f-score g-score + sqrt(([pxcor] of self - [pxcor] of goal-node)*([pxcor] of self - [pxcor] of goal-node) + ([pycor] of self - [pycor] of goal-node)*([pycor] of self - [pycor] of goal-node))
                if is-open? = false
                [
                  set is-open? true
                ]
              ]
            ]
          ]
          [
            if is-open? = false or temp-g-score < g-score
            [
              set parent-node current-node
;              show word self current-node
              set g-score temp-g-score
              set f-score g-score + sqrt(([pxcor] of self - [pxcor] of goal-node)*([pxcor] of self - [pxcor] of goal-node) + ([pycor] of self - [pycor] of goal-node)*([pycor] of self - [pycor] of goal-node))
              if is-open? = false
              [
                set is-open? true
              ]
            ]
          ]
        ]
      ]
    ]
    tick
  ]
end

to get-f-score
  let h-score sqrt(([pxcor] of self - [pxcor] of goal-node)*([pxcor] of self - [pxcor] of goal-node) + ([pycor] of self - [pycor] of goal-node)*([pycor] of self - [pycor] of goal-node))
  set f-score g-score + h-score
end

to color-the-path
  let p goal-node
  while [p != -1]
  [
    ask p
    [
      set pcolor black
      set p parent-node
    ]
  ]
  show word "finded the shortest path: " [f-score] of goal-node
  tick
end

then you click on the "start" button and click on the "go" button, you will see a A-star path finding algorithm.

Sorry for that there is not much comments there, and I did have no ability for increase the speed of the algorithm, then the code is ineffiency and ugly. hope for that it's useful to some body(even this is beyond the dream of myself...).

转载于:https://www.cnblogs.com/henyihanwobushi/archive/2013/04/15/3021520.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值