; function to provide inut-output - reads the file ; data from the fine p1.dat ; global variables are start with * ; function tsp takes as an argument the list and solves the TSP problem (defun skel1 () (let (data_file sym_list) (setq data_file (open "cities.dat" :direction :input)) (setq *print_max* 30) (setq *min_dist* 0) (setq *tour_list* ()) (loop (if (null (setq sym_list (read data_file nil nil ))) (return)) (solve_tsp sym_list) ) (close data_file) '****Finished**** ) ) ; Here is a dummy version of tsp fuction which you will be required ; to modify (defun solve_tsp (sym_list) (setq *city_max* (length sym_list)) (format t "~%Generating tours for following ~D cities: ~%" *city_max*) (dolist (item sym_list) (format t " ~A~%" (car item)) ) (setq *tour_cnt* 0) (setq *node_cnt* 1) (permute (rest sym_list) (list (first sym_list))) (format t "~% Tour count: ~D" *tour_cnt*) (format t "~% Node count: ~D" *node_cnt*) (format t "~% Shortest tour: ~D" *min_dist*) ) ; Function permute is invoked with two arguments ; unused symbols and partial permutation (which is at the beginning null) ; the unused symbols are added to the front of the patial permutation list ; creating a child node, and permute is called recursively do generate ; the grand children etc. (defun my_print (list) (print (reverse (pretty_list list))) ) (defun pretty_list (perm) (cond ((null perm) NIL) (t (cons (car (car perm)) (pretty_list (cdr perm)))) ) ) ; recursive function to compute the distance (defun tour_distance (perm) (+ (eucl_dist (cdr (car perm)) (cdr (car (last perm)))) (compute_distance perm) ) ) (defun compute_distance (perm) (cond ((= (length perm) 1) 0) (t (+ (compute_distance (cdr perm)) (eucl_dist (cdr (car perm)) (cdr (car (cdr perm)))))) ) ) ; euclidean distance (defun eucl_dist (a b) (sqrt (+ (* (- (first a) (first b)) (- (first a) (first b))) (* (- (second a) (second b)) (- (second a) (second b))) ) ) ) (defun permute (unused perm) (let () (cond ((null unused) (cond ( (< *tour_cnt* *print_max*) (my_print perm)) ( (= *tour_cnt* *print_max*) (format t "~% supressing printing ...") ) ) (setq *tour_cnt* (+ *tour_cnt* 1)) (cond ((= *min_dist* 0) (setq *min_dist* (tour_distance perm)) (setq *tour_list* (cons perm *tour_list*))) ((< (tour_distance perm) *min_dist*) (setq *min_dist* (tour_distance perm)) (setq *tour_list* (cons perm ()))) ((= (tour_distance perm) *min_dist*) (setq *tour_list* (cons perm *tour_list* ))) ) ) (t (dolist (item unused) (permute (remove item unused) (cons item perm)) )) ) ) )