Finding Lowest Elevation Path Between Two Points












1












$begingroup$


Let's say I have a matrix of values that represent heights with function $f(x,y)$ and I am trying to find the "lowest value path" beween two points. So this would be the reverse of hill climbing, as in optimization, taking gradient and following that etc, but in a way, I guess I need the anti-gradient - not the direction of steepest climb but the non-steepest walk.



I need this for a mapping application, I have elevation values on a grid and I am trying to find a path between those points that require minimum amt of climbing.



I guess I could create a cost function that gives highest values for high elevation + furthest points to destination + non-smoothness of paths, and do optimization on that. I was just wondering if anyone worked with such cost functions before, or there is another calculus trick I have to utilize.



Keywords: flattest route










share|cite|improve this question











$endgroup$












  • $begingroup$
    Just curious: if you took the largest value, say $M_{mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt?
    $endgroup$
    – postmortes
    Jan 26 at 14:39
















1












$begingroup$


Let's say I have a matrix of values that represent heights with function $f(x,y)$ and I am trying to find the "lowest value path" beween two points. So this would be the reverse of hill climbing, as in optimization, taking gradient and following that etc, but in a way, I guess I need the anti-gradient - not the direction of steepest climb but the non-steepest walk.



I need this for a mapping application, I have elevation values on a grid and I am trying to find a path between those points that require minimum amt of climbing.



I guess I could create a cost function that gives highest values for high elevation + furthest points to destination + non-smoothness of paths, and do optimization on that. I was just wondering if anyone worked with such cost functions before, or there is another calculus trick I have to utilize.



Keywords: flattest route










share|cite|improve this question











$endgroup$












  • $begingroup$
    Just curious: if you took the largest value, say $M_{mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt?
    $endgroup$
    – postmortes
    Jan 26 at 14:39














1












1








1


1



$begingroup$


Let's say I have a matrix of values that represent heights with function $f(x,y)$ and I am trying to find the "lowest value path" beween two points. So this would be the reverse of hill climbing, as in optimization, taking gradient and following that etc, but in a way, I guess I need the anti-gradient - not the direction of steepest climb but the non-steepest walk.



I need this for a mapping application, I have elevation values on a grid and I am trying to find a path between those points that require minimum amt of climbing.



I guess I could create a cost function that gives highest values for high elevation + furthest points to destination + non-smoothness of paths, and do optimization on that. I was just wondering if anyone worked with such cost functions before, or there is another calculus trick I have to utilize.



Keywords: flattest route










share|cite|improve this question











$endgroup$




Let's say I have a matrix of values that represent heights with function $f(x,y)$ and I am trying to find the "lowest value path" beween two points. So this would be the reverse of hill climbing, as in optimization, taking gradient and following that etc, but in a way, I guess I need the anti-gradient - not the direction of steepest climb but the non-steepest walk.



I need this for a mapping application, I have elevation values on a grid and I am trying to find a path between those points that require minimum amt of climbing.



I guess I could create a cost function that gives highest values for high elevation + furthest points to destination + non-smoothness of paths, and do optimization on that. I was just wondering if anyone worked with such cost functions before, or there is another calculus trick I have to utilize.



Keywords: flattest route







optimization discrete-optimization gradient-descent searching






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Jan 29 at 12:00







BB_ML

















asked Jan 26 at 14:11









BB_MLBB_ML

6,05152544




6,05152544












  • $begingroup$
    Just curious: if you took the largest value, say $M_{mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt?
    $endgroup$
    – postmortes
    Jan 26 at 14:39


















  • $begingroup$
    Just curious: if you took the largest value, say $M_{mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt?
    $endgroup$
    – postmortes
    Jan 26 at 14:39
















$begingroup$
Just curious: if you took the largest value, say $M_{mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt?
$endgroup$
– postmortes
Jan 26 at 14:39




$begingroup$
Just curious: if you took the largest value, say $M_{mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt?
$endgroup$
– postmortes
Jan 26 at 14:39










2 Answers
2






active

oldest

votes


















1












$begingroup$

This is an instance of a "shortest path" problem. Create a directed graph with vertex set equal to your grid points, and with a directed edge from $a$ to $b$ if $a$ and $b$ are adjacent, and give that edge weight $w(a,b)=min(0,h(b)-h(a))$ where $h(a)$ is the elevation of $a$, and so on. (That is, the vertices are cells in your matrix, where the typical vertex has 4 neighbors: the cell to the North, the one to the South, etc. The edges represent things like "go North from this cell to its neighbor" and the weight is the climb associated with such a move.) Use one of the algorithms described in the link. Warning: some of these algorithms require intricate coding. (This weight function measures only uphill climb, which, as an occasional hiker I know is not necessarily the whole story about weariness of foot.)






share|cite|improve this answer











$endgroup$





















    0












    $begingroup$

    This problem can indeed be seen as shortest path problem. Let's say matrix has elevation data, a cells neighbors can be retrieved using Queen's pattern (8 of them), then code is



    from pqdict import pqdict
    import numpy as np

    def get_neighbor_idx(x,y,dims):
    res =
    for i in ([0,-1,1]):
    for j in ([0,-1,1]):
    if i==0 and j==0: continue
    if x+i<(dims[0]) and x+i>-1 and y+j<(dims[1]) and y+j>-1:
    res.append((x+i,y+j))
    return res

    def dijkstra(C,s,e):
    D = {}
    P = {}
    Q = pqdict()
    Q[s] = 0

    while len(Q)>0:
    (v,vv) = Q.popitem()
    D[v] = vv
    neighs = get_neighbor_idx(v[0],v[1],C.shape)
    for w in neighs:
    vwLength = D[v] + np.abs(C[v[0],v[1]] - C[w[0],w[1]])
    if w in D:
    if vwLength < D[v]:
    raise ValueError
    elif w not in Q or vwLength < Q[w]:
    Q[w] = vwLength
    P[w] = v

    path =
    while 1:
    path.append(e)
    if e == s: break
    e = P[e]
    path.reverse()
    return path

    m = np.array([[999.9, 999.9, 999.9, 0. ],
    [999.9, 999.9, 999.9, 0. ],
    [999.9, 999.9, 999.9, 0. ],
    [ 0., 0., 0., 0. ]])

    res = dijkstra(m,(3,0),(0,3))

    print (res)


    This will print



    [[999.9 999.9 999.9   0. ]
    [999.9 999.9 999.9 0. ]
    [999.9 999.9 999.9 0. ]
    [ 0. 0. 0. 0. ]]
    [(3, 0), (3, 1), (3, 2), (2, 3), (1, 3), (0, 3)]


    So starting from lower left corner, to the target upper right corner the flattest route was reported. The 999's are 'hills' the 0's are ground.






    share|cite|improve this answer











    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "69"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3088292%2ffinding-lowest-elevation-path-between-two-points%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1












      $begingroup$

      This is an instance of a "shortest path" problem. Create a directed graph with vertex set equal to your grid points, and with a directed edge from $a$ to $b$ if $a$ and $b$ are adjacent, and give that edge weight $w(a,b)=min(0,h(b)-h(a))$ where $h(a)$ is the elevation of $a$, and so on. (That is, the vertices are cells in your matrix, where the typical vertex has 4 neighbors: the cell to the North, the one to the South, etc. The edges represent things like "go North from this cell to its neighbor" and the weight is the climb associated with such a move.) Use one of the algorithms described in the link. Warning: some of these algorithms require intricate coding. (This weight function measures only uphill climb, which, as an occasional hiker I know is not necessarily the whole story about weariness of foot.)






      share|cite|improve this answer











      $endgroup$


















        1












        $begingroup$

        This is an instance of a "shortest path" problem. Create a directed graph with vertex set equal to your grid points, and with a directed edge from $a$ to $b$ if $a$ and $b$ are adjacent, and give that edge weight $w(a,b)=min(0,h(b)-h(a))$ where $h(a)$ is the elevation of $a$, and so on. (That is, the vertices are cells in your matrix, where the typical vertex has 4 neighbors: the cell to the North, the one to the South, etc. The edges represent things like "go North from this cell to its neighbor" and the weight is the climb associated with such a move.) Use one of the algorithms described in the link. Warning: some of these algorithms require intricate coding. (This weight function measures only uphill climb, which, as an occasional hiker I know is not necessarily the whole story about weariness of foot.)






        share|cite|improve this answer











        $endgroup$
















          1












          1








          1





          $begingroup$

          This is an instance of a "shortest path" problem. Create a directed graph with vertex set equal to your grid points, and with a directed edge from $a$ to $b$ if $a$ and $b$ are adjacent, and give that edge weight $w(a,b)=min(0,h(b)-h(a))$ where $h(a)$ is the elevation of $a$, and so on. (That is, the vertices are cells in your matrix, where the typical vertex has 4 neighbors: the cell to the North, the one to the South, etc. The edges represent things like "go North from this cell to its neighbor" and the weight is the climb associated with such a move.) Use one of the algorithms described in the link. Warning: some of these algorithms require intricate coding. (This weight function measures only uphill climb, which, as an occasional hiker I know is not necessarily the whole story about weariness of foot.)






          share|cite|improve this answer











          $endgroup$



          This is an instance of a "shortest path" problem. Create a directed graph with vertex set equal to your grid points, and with a directed edge from $a$ to $b$ if $a$ and $b$ are adjacent, and give that edge weight $w(a,b)=min(0,h(b)-h(a))$ where $h(a)$ is the elevation of $a$, and so on. (That is, the vertices are cells in your matrix, where the typical vertex has 4 neighbors: the cell to the North, the one to the South, etc. The edges represent things like "go North from this cell to its neighbor" and the weight is the climb associated with such a move.) Use one of the algorithms described in the link. Warning: some of these algorithms require intricate coding. (This weight function measures only uphill climb, which, as an occasional hiker I know is not necessarily the whole story about weariness of foot.)







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Jan 26 at 15:00

























          answered Jan 26 at 14:37









          kimchi loverkimchi lover

          11.4k31229




          11.4k31229























              0












              $begingroup$

              This problem can indeed be seen as shortest path problem. Let's say matrix has elevation data, a cells neighbors can be retrieved using Queen's pattern (8 of them), then code is



              from pqdict import pqdict
              import numpy as np

              def get_neighbor_idx(x,y,dims):
              res =
              for i in ([0,-1,1]):
              for j in ([0,-1,1]):
              if i==0 and j==0: continue
              if x+i<(dims[0]) and x+i>-1 and y+j<(dims[1]) and y+j>-1:
              res.append((x+i,y+j))
              return res

              def dijkstra(C,s,e):
              D = {}
              P = {}
              Q = pqdict()
              Q[s] = 0

              while len(Q)>0:
              (v,vv) = Q.popitem()
              D[v] = vv
              neighs = get_neighbor_idx(v[0],v[1],C.shape)
              for w in neighs:
              vwLength = D[v] + np.abs(C[v[0],v[1]] - C[w[0],w[1]])
              if w in D:
              if vwLength < D[v]:
              raise ValueError
              elif w not in Q or vwLength < Q[w]:
              Q[w] = vwLength
              P[w] = v

              path =
              while 1:
              path.append(e)
              if e == s: break
              e = P[e]
              path.reverse()
              return path

              m = np.array([[999.9, 999.9, 999.9, 0. ],
              [999.9, 999.9, 999.9, 0. ],
              [999.9, 999.9, 999.9, 0. ],
              [ 0., 0., 0., 0. ]])

              res = dijkstra(m,(3,0),(0,3))

              print (res)


              This will print



              [[999.9 999.9 999.9   0. ]
              [999.9 999.9 999.9 0. ]
              [999.9 999.9 999.9 0. ]
              [ 0. 0. 0. 0. ]]
              [(3, 0), (3, 1), (3, 2), (2, 3), (1, 3), (0, 3)]


              So starting from lower left corner, to the target upper right corner the flattest route was reported. The 999's are 'hills' the 0's are ground.






              share|cite|improve this answer











              $endgroup$


















                0












                $begingroup$

                This problem can indeed be seen as shortest path problem. Let's say matrix has elevation data, a cells neighbors can be retrieved using Queen's pattern (8 of them), then code is



                from pqdict import pqdict
                import numpy as np

                def get_neighbor_idx(x,y,dims):
                res =
                for i in ([0,-1,1]):
                for j in ([0,-1,1]):
                if i==0 and j==0: continue
                if x+i<(dims[0]) and x+i>-1 and y+j<(dims[1]) and y+j>-1:
                res.append((x+i,y+j))
                return res

                def dijkstra(C,s,e):
                D = {}
                P = {}
                Q = pqdict()
                Q[s] = 0

                while len(Q)>0:
                (v,vv) = Q.popitem()
                D[v] = vv
                neighs = get_neighbor_idx(v[0],v[1],C.shape)
                for w in neighs:
                vwLength = D[v] + np.abs(C[v[0],v[1]] - C[w[0],w[1]])
                if w in D:
                if vwLength < D[v]:
                raise ValueError
                elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

                path =
                while 1:
                path.append(e)
                if e == s: break
                e = P[e]
                path.reverse()
                return path

                m = np.array([[999.9, 999.9, 999.9, 0. ],
                [999.9, 999.9, 999.9, 0. ],
                [999.9, 999.9, 999.9, 0. ],
                [ 0., 0., 0., 0. ]])

                res = dijkstra(m,(3,0),(0,3))

                print (res)


                This will print



                [[999.9 999.9 999.9   0. ]
                [999.9 999.9 999.9 0. ]
                [999.9 999.9 999.9 0. ]
                [ 0. 0. 0. 0. ]]
                [(3, 0), (3, 1), (3, 2), (2, 3), (1, 3), (0, 3)]


                So starting from lower left corner, to the target upper right corner the flattest route was reported. The 999's are 'hills' the 0's are ground.






                share|cite|improve this answer











                $endgroup$
















                  0












                  0








                  0





                  $begingroup$

                  This problem can indeed be seen as shortest path problem. Let's say matrix has elevation data, a cells neighbors can be retrieved using Queen's pattern (8 of them), then code is



                  from pqdict import pqdict
                  import numpy as np

                  def get_neighbor_idx(x,y,dims):
                  res =
                  for i in ([0,-1,1]):
                  for j in ([0,-1,1]):
                  if i==0 and j==0: continue
                  if x+i<(dims[0]) and x+i>-1 and y+j<(dims[1]) and y+j>-1:
                  res.append((x+i,y+j))
                  return res

                  def dijkstra(C,s,e):
                  D = {}
                  P = {}
                  Q = pqdict()
                  Q[s] = 0

                  while len(Q)>0:
                  (v,vv) = Q.popitem()
                  D[v] = vv
                  neighs = get_neighbor_idx(v[0],v[1],C.shape)
                  for w in neighs:
                  vwLength = D[v] + np.abs(C[v[0],v[1]] - C[w[0],w[1]])
                  if w in D:
                  if vwLength < D[v]:
                  raise ValueError
                  elif w not in Q or vwLength < Q[w]:
                  Q[w] = vwLength
                  P[w] = v

                  path =
                  while 1:
                  path.append(e)
                  if e == s: break
                  e = P[e]
                  path.reverse()
                  return path

                  m = np.array([[999.9, 999.9, 999.9, 0. ],
                  [999.9, 999.9, 999.9, 0. ],
                  [999.9, 999.9, 999.9, 0. ],
                  [ 0., 0., 0., 0. ]])

                  res = dijkstra(m,(3,0),(0,3))

                  print (res)


                  This will print



                  [[999.9 999.9 999.9   0. ]
                  [999.9 999.9 999.9 0. ]
                  [999.9 999.9 999.9 0. ]
                  [ 0. 0. 0. 0. ]]
                  [(3, 0), (3, 1), (3, 2), (2, 3), (1, 3), (0, 3)]


                  So starting from lower left corner, to the target upper right corner the flattest route was reported. The 999's are 'hills' the 0's are ground.






                  share|cite|improve this answer











                  $endgroup$



                  This problem can indeed be seen as shortest path problem. Let's say matrix has elevation data, a cells neighbors can be retrieved using Queen's pattern (8 of them), then code is



                  from pqdict import pqdict
                  import numpy as np

                  def get_neighbor_idx(x,y,dims):
                  res =
                  for i in ([0,-1,1]):
                  for j in ([0,-1,1]):
                  if i==0 and j==0: continue
                  if x+i<(dims[0]) and x+i>-1 and y+j<(dims[1]) and y+j>-1:
                  res.append((x+i,y+j))
                  return res

                  def dijkstra(C,s,e):
                  D = {}
                  P = {}
                  Q = pqdict()
                  Q[s] = 0

                  while len(Q)>0:
                  (v,vv) = Q.popitem()
                  D[v] = vv
                  neighs = get_neighbor_idx(v[0],v[1],C.shape)
                  for w in neighs:
                  vwLength = D[v] + np.abs(C[v[0],v[1]] - C[w[0],w[1]])
                  if w in D:
                  if vwLength < D[v]:
                  raise ValueError
                  elif w not in Q or vwLength < Q[w]:
                  Q[w] = vwLength
                  P[w] = v

                  path =
                  while 1:
                  path.append(e)
                  if e == s: break
                  e = P[e]
                  path.reverse()
                  return path

                  m = np.array([[999.9, 999.9, 999.9, 0. ],
                  [999.9, 999.9, 999.9, 0. ],
                  [999.9, 999.9, 999.9, 0. ],
                  [ 0., 0., 0., 0. ]])

                  res = dijkstra(m,(3,0),(0,3))

                  print (res)


                  This will print



                  [[999.9 999.9 999.9   0. ]
                  [999.9 999.9 999.9 0. ]
                  [999.9 999.9 999.9 0. ]
                  [ 0. 0. 0. 0. ]]
                  [(3, 0), (3, 1), (3, 2), (2, 3), (1, 3), (0, 3)]


                  So starting from lower left corner, to the target upper right corner the flattest route was reported. The 999's are 'hills' the 0's are ground.







                  share|cite|improve this answer














                  share|cite|improve this answer



                  share|cite|improve this answer








                  edited Jan 31 at 8:10

























                  answered Jan 29 at 12:00









                  BB_MLBB_ML

                  6,05152544




                  6,05152544






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Mathematics Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3088292%2ffinding-lowest-elevation-path-between-two-points%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory