Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:24:24+00:00 2026-05-26T04:24:24+00:00

I am trying to modify an existing Hill-climb function, which takes two node names

  • 0

I am trying to modify an existing Hill-climb function, which takes two node names (such as A and E), and has an optional parameter which is used recursively (a queue). I’m trying to define a function ‘cheaper’ that evaluates if one path is cheaper than another. Also, instead of one goal node, I’m trying to pass a list of goal nodes, which the function, upon reaching one of those nodes, stops evaluating.

The problem is my function won’t return anything except the start node I’ve input and an empty list.

Here is my network/graph and associated costs:

(setf (get 's 'coordinates) '(0 3)
      (get 'a 'coordinates) '(4 6)
      (get 'b 'coordinates) '(7 6)
      (get 'c 'coordinates) '(11 9)
      (get 'd 'coordinates) '(2 0)
      (get 'e 'coordinates) '(9 2)
      (get 'f 'coordinates) '(11 3))


(setf (get 's 'cost) 0
      (get 'a 'cost) 16
      (get 'b 'cost) 4
      (get 'c 'cost) 10
      (get 'd 'cost) 5
      (get 'e 'cost) 12
      (get 'f 'cost) 14)

And here is my modified Hill-climb function:

(defun hill-climb (start finish &optional (queue (list (list start))))
  (cond ((endp queue) nil)
        ((member (first (first queue)) finish)
         (reverse (first queue)))
        (t (hill-climb start finish (append (sort (extend (first queue))
                                                  #'(lambda (p1 p2)
                                                      (cheaper p1 p2 
                                                               finish)))
                                            (rest queue))))))

Finally, here are the ‘cost’ and ‘cheaper’ functions:

(defun cost (path)
  (apply '+ (mapcar #'(lambda (x) (get x 'cost)) path)))


(defun cheaper (p1 p2)
  (< (cost p1)
     (cost p2)))  

EDIT: Sorry, and here’s ‘extend’:

(defun extend (path)
  (print (reverse path))
  (mapcar #'(lambda (new-node) (cons new-node path))
          (remove-if #'(lambda (neighbor) (member neighbor path))
                     (get (first path) 'neighbors))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:24:25+00:00Added an answer on May 26, 2026 at 4:24 am

    I’m not really sure, what the problem is here. In your expand, a neighbor property is used which is not given in your question. If this property is defined for every node, your code works.

    Assuming every node that is next to another without another in between (which is the only option that seems to make sense for your data, since the alternative, namely to make only tangent nodes (i.e. nodes which are +/-1 for one or both coordinates) neighbors, would yield no neighbors at all in your example):

    (setf (get 's 'neighbors) '(a d)
          (get 'a 'neighbors) '(s b d)
          (get 'b 'neighbors) '(a c e)
          (get 'c 'neighbors) '(b)
          (get 'd 'neighbors) '(s a e)
          (get 'e 'neighbors) '(b d f)
          (get 'f 'neighbors) '(e))
    
    (defun hill-climb (start finish &optional (queue (list (list start))))
      (cond ((endp queue) nil)
            ((member (first (first queue)) finish)
             (reverse (first queue)))
            (t (hill-climb start finish (append (sort (extend (first queue))
                                                      #'cheaper)
                                                (rest queue))))))
    

    (Missing parts stay the same as in your post. Only minor adjustments, like dropping the lambda around, and the extra argument to, cheaper.)

    Will give the correct results:

    CL-USER> (hill-climb 's '(b))
    
    (S) 
    (S D) 
    (S D E) 
    (S D E B)
    CL-USER> (hill-climb 's '(b d))
    
    (S) 
    (S D)
    

    If you may not introduce the new property, you will have to check for neighbors in your expand function (which would also mean that you’d have to pass a list of nodes).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to modify an existing WPF application for localization. One of my
I'm trying to modify an existing NSIS install script to allow for different licence
I am trying to modify an existing MySQL database for use in a new
I'm trying to modify and existing Access application to use MySQL as a database
I am new to Ruby On Rails, and currently trying to modify an existing
I am trying to modify an existing line of JavaScript that I use often
I'm trying to use django-evolution to modify some models from an existing project. Now
I'm trying to modify an existing Django Mezzanine setup to allow me to blog
I'm trying to modify existing perl script to support geocoding. Found this module for
I am trying to modify existing MimeMessage body part. I would like to filter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.