zoukankan      html  css  js  c++  java
  • NetLogo AStar 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...).

  • 相关阅读:
    mongodb笔记一
    mysql的备份和恢复
    explain的type列
    Debian下apache2设置并发
    nginx基本调优
    c语言struct
    Centos 安装nginx + php + mysql
    Debian下系统启动时执行脚本
    centos5.5服务器基本篇
    分治法
  • 原文地址:https://www.cnblogs.com/henyihanwobushi/p/3021520.html
Copyright © 2011-2022 走看看