Author Topic: (Challenge) To draw the shortest lwpolyline  (Read 44094 times)

0 Members and 1 Guest are viewing this topic.

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #120 on: January 04, 2019, 04:59:41 AM »
Hi again, my latest revision... I returned back to slow but perhaps more reliable coding... Although its maximum is about 20 points for factor 2, that's also better than brute force... For 3, 4, ... don't even think you'll see the result - its computing large lists and may never finish... So I suggest default 1 and hope that result will be optimal as much as possible, if not then you are practically doomed like with brute force... I'll also input version for start-end in the topic by user @handasa - there is link for it when I compete with @nullptr - Daniel - I post it somewhere in the middle when it was started...

Code - Auto/Visual Lisp: [Select]
1. (defun c:TSP-2D-MR ( / LM:ConvexHull-ptsonHull LM:Clockwise-p car-sort plstdiff processrs ss ti i pl pln k plp pld pll d r rr ppp lil lii1 lii2 lil1 lil2 ip f kk n )
2.
3.   ;; Convex Hull  -  Lee Mac
4.   ;; Implements the Graham Scan Algorithm to return the Convex Hull of a list of points.
5.
6.   (defun LM:ConvexHull-ptsonHull ( lst / ch p0 lstl )
7.       (cond
8.           (   (< (length lst) 4) lst)
9.           (   (setq p0 (car lst))
10.               (foreach p1 (cdr lst)
12.                           (and (= (cadr p1) (cadr p0)) (< (car p1) (car p0)))
13.                       )
14.                       (setq p0 p1)
15.                   )
16.               )
17.               (setq lst (vl-remove p0 lst))
18.               (setq lst (append (list p0) lst))
19.               (setq lst
20.                   (vl-sort lst
21.                           (lambda ( a b / c d )
22.                               (if (or (equal (setq c (angle p0 a)) (setq d (angle p0 b)) 1e-8) (and (or (equal c 0.0 1e-8) (equal c (* 2 pi) 1e-8)) (or (equal d 0.0 1e-8) (equal d (* 2 pi) 1e-8))))
23.                                   (< (distance (list (car p0) (cadr p0)) a) (distance (list (car p0) (cadr p0)) b))
24.                                   (< c d)
25.                               )
26.                           )
27.                       )
28.                   )
29.               )
30.               (setq lstl (vl-remove-if-not (function (lambda ( x ) (equal (angle p0 (last lst)) (angle p0 x) 1e-8))) lst))
31.               (setq lst (vl-remove-if (function (lambda ( x ) (equal (angle p0 (last lst)) (angle p0 x) 1e-8))) lst))
32.               (setq lstl (vl-sort lstl (function (lambda ( a b ) (> (distance (list (car p0) (cadr p0)) a) (distance (list (car p0) (cadr p0)) b))))))
33.               (setq lst (append lst lstl))
34.               (setq ch (list (cadr lst) (car lst)))
35.               (foreach pt (cddr lst)
36.                   (setq ch (cons pt ch))
38.                       (setq ch (cons pt (cddr ch)))
39.                   )
40.               )
41.               (reverse ch)
42.           )
43.       )
44.   )
45.
46.   ;; Clockwise-p  -  Lee Mac
47.   ;; Returns T if p1,p2,p3 are clockwise oriented or [s]collinear[/s]
48.
49.   (defun LM:Clockwise-p ( p1 p2 p3 )
50.       (<  (-  (* (- (car  p2) (car  p1)) (- (cadr p3) (cadr p1)))
51.               (* (- (cadr p2) (cadr p1)) (- (car  p3) (car  p1)))
52.           )
53.           0.0
54.       )
55.   )
56.
57.   ;;; (car-sort '(2 4 1 3 5 1) '<) => nil
58.   ;;; (car-sort '(2 4 1 3 5 1) '<=) => 1
59.   (defun car-sort ( l f / removenth r k )
60.
61.     (defun removenth ( l n / k )
62.       (setq k -1)
63.       (vl-remove-if (function (lambda ( x ) (= (setq k (1+ k)) n))) l)
64.     )
65.
66.     (setq k -1)
67.     (vl-some (function (lambda ( a ) (setq k (1+ k)) (if (vl-every (function (lambda ( x ) (apply f (list a x)))) (removenth l k)) (setq r a)))) l)
68.     r
69.   )
70.
71.   (defun plstdiff ( l1 l2 )
72.     (foreach p l1
73.       (setq l2 (vl-remove p l2))
74.     )
75.     l2
76.   )
77.
78.   (defun processrs ( r / rr )
79.     (foreach xx r
80.       (if (and (null f) (if (equal xx pln) (setq ppp pl) (setq ppp (plstdiff xx pl))))
81.         (foreach p ppp
82.           (setq k -1)
83.           (repeat (length xx)
84.             (setq k (1+ k))
85.             (setq plp (reverse (member (nth k xx) (reverse xx))))
86.             (setq pls (cdr (member (nth k xx) xx)))
87.             (setq pll (append plp (list p) pls))
88.             (setq rr (cons pll rr))
89.           )
90.         )
91.         (setq f t)
92.       )
93.     )
94.     (if f
95.         (setq pl nil)
96.         r
97.       )
98.       (if (= kk n)
99.           (setq kk 0)
100.           (setq rr (mapcar (function (lambda ( x ) (list (apply (function +) (mapcar (function (lambda ( a b ) (distance a b))) x (append (cdr x) (list (car x))))) x))) rr))
101.           (setq rr (list (cadr (car-sort rr (function (lambda ( a b ) (<= (car a) (car b))))))))
102.         )
103.         rr
104.       )
105.     )
106.   )
107.
108.   (setq ss (ssget '((0 . "POINT"))))
109.   (repeat (setq i (sslength ss))
110.     (setq pl (cons (mapcar (function +) '(0 0) (cdr (assoc 10 (entget (ssname ss (setq i (1- i))))))) pl))
111.   )
112.   (initget 6)
113.   (setq n (getint "\nSpecify speed factor - reliability - [1-fast/2-slow] <1> : "))
114.   (if (null n)
115.     (setq n 1)
116.   )
117.   (setq ti (car (_vl-times)))
118.   (setq pln (LM:ConvexHull-ptsonHull pl))
119.   (foreach p pln
120.     (setq pl (vl-remove p pl))
121.   )
122.   (setq kk 0)
123.   (while pl
124.     (setq kk (1+ kk))
125.     (if (null rr)
126.       (setq rr (processrs (list pln)))
127.       (setq rr (processrs rr))
128.     )
129.   )
130.   (setq rr (mapcar (function (lambda ( x ) (list (apply (function +) (mapcar (function (lambda ( a b ) (distance a b))) x (append (cdr x) (list (car x))))) x))) rr))
131.   (setq pln (cadr (car-sort rr (function (lambda ( a b ) (<= (car a) (car b)))))))
132.   (setq lil (mapcar (function (lambda ( a b ) (list a b))) pln (append (cdr pln) (list (car pln)))))
133.   (while (vl-some (function (lambda ( li1 ) (vl-some (function (lambda ( li2 ) (if (and (setq ip (inters (car li1) (cadr li1) (car li2) (cadr li2))) (not (equal ip (car li1) 1e-8)) (not (equal ip (cadr li1) 1e-8)) (not (equal ip (car li2) 1e-8)) (not (equal ip (cadr li2) 1e-8))) (setq lii1 li1 lii2 li2)))) (vl-remove li1 lil)))) lil)
134.     (setq lil1 (if (member lii2 (cdr (member lii2 (reverse (cdr (member lii1 (append lil lil))))))) (reverse (cdr (member lii2 (cdr (member lii2 (reverse (cdr (member lii1 (append lil lil))))))))) (reverse (cdr (member lii2 (reverse (cdr (member lii1 (append lil lil)))))))))
135.     (setq lil2 (if (member lii1 (cdr (member lii1 (reverse (cdr (member lii2 (append lil lil))))))) (cdr (member lii1 (cdr (member lii1 (reverse (cdr (member lii2 (append lil lil)))))))) (cdr (member lii1 (reverse (cdr (member lii2 (append lil lil))))))))
136.     (setq lil (append lil1 (list (list (cadr (last lil1)) (cadr (car lil2)))) (mapcar (function reverse) lil2) (list (list (car (last lil2)) (car (car lil1))))))
137.   )
138.   (setq pln (mapcar (function car) lil))
139.   (setq d (apply (function +) (mapcar (function (lambda ( a b ) (distance a b))) pln (append (cdr pln) (list (car pln))))))
140.       (list
141.         '(0 . "LWPOLYLINE")
142.         '(100 . "AcDbEntity")
143.         '(100 . "AcDbPolyline")
144.         (cons 90 (length pln))
145.         (cons 70 (1+ (* (getvar 'plinegen) 128)))
146.         '(38 . 0.0)
147.       )
148.       (mapcar (function (lambda ( x ) (cons 10 x))) pln)
149.       (list
150.         '(210 0.0 0.0 1.0)
151.         '(62 . 1)
152.       )
153.     )
154.   )
155.   (prompt "\nDistance : ") (princ (rtos d 2 50))
156.   (prompt "\nElapsed time : ") (princ (rtos (/ (- (car (_vl-times)) ti) 1000.0) 2 50)) (prompt " seconds...")
157.   (princ)
158. )
159.

P.S. If you are lucky enough that your points almost all form shape of ConvexHull then it will be fast and reliable, but if not, then you'll have to wait and only hope for correct result... So this is actually my remark to remove doubts that TSP can be solved, but never give up... chlh_jd's code is fast and superior to my versions, but still not full reliable answer, I mean, so what if it can finish in 1 second, when you don't get correct result, you can get only close to correct...

Regards, happy New Year holidays...
M.R.
« Last Edit: June 19, 2019, 12:47:03 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

mailmaverick

• Bull Frog
• Posts: 465
Re: (Challenge) To draw the shortest lwpolyline
« Reply #121 on: March 26, 2019, 07:43:15 AM »
Hi All

Out of the various routines given by various people, which one is the latest / fastest / best ?

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #122 on: March 26, 2019, 08:20:25 AM »
Hi All

Out of the various routines given by various people, which one is the latest / fastest / best ?

Hi, I am finally using CADSTUDIO's SHORTPATH.VLX with my additional LISP that calls it and then checks for intersecting lines with Evgeniy's method and prompts total length of path in 15 decimal places precision...
Marko Ribar, d.i.a. (graduated engineer of architecture)

ronjonp

• Needs a day job
• Posts: 7181
Re: (Challenge) To draw the shortest lwpolyline
« Reply #123 on: March 26, 2019, 08:23:38 AM »
Hi All

Out of the various routines given by various people, which one is the latest / fastest / best ?
The latest is right before your post  .. the solutions in here vary based on the length of the list being processed. You'll have to test to see what works for you.

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #124 on: May 22, 2020, 03:17:16 PM »
Hi there...
Long time I haven't played with TSP...
I decided to post my latest version to see if someone will reply and give me any comments... For now it proved the most comprehensive for me... Only problem I see is that it's slow with minimal input options... For example if you use depth 1 and 1 solution attempt per depth iteration it is still very slow... The point now is that if you input depth smaller than maximum (number of free points inside convex hull), you can watch lwpolyline changing its shape until routine finishes task... So for speed computation and with best quality/speed I still recommend shortpath.vlx... This is only if you have specific task to do and you must be sure result is perfect - so you specify maximal input options (greatest depth allowed and all solution attempts <all> and then <100%> as percentage of attempts per depth iterations)... If you have more than 10 free points inside convex hull, you'll have to wait for a very long time, but if you have less than 10, then I suggest depending on PC that you input maximal values and still wait until it finishes... This is very complex problem and it requires very long computations, so just for fun and experimentation you may use this code, but at your own risk that you'll loose el. energy and time if PC is with low performances like my PC (old over 20 years)...

Code - Auto/Visual Lisp: [Select]
1. (defun c:TSP-2D-MR-LATEST-NEW ( / LM:ConvexHull-ptsonHull LM:Clockwise-p sort processpt depth _do-events cmde ss n m j i p ppp pl pll plll pllll lw lwx lil k ilil pre mid suf dmin d ti )
2.
3.   ;; Convex Hull  -  Lee Mac
4.   ;; Implements the Graham Scan Algorithm to return the Convex Hull of a list of points.
5.
6.   (defun LM:ConvexHull-ptsonHull ( lst / ch p0 lstl )
7.     (cond
8.       ( (< (length lst) 4) lst)
9.       ( (setq p0 (car lst))
10.         (foreach p1 (cdr lst)
12.                   (and (= (cadr p1) (cadr p0)) (< (car p1) (car p0)))
13.               )
14.               (setq p0 p1)
15.           )
16.         )
17.         (setq lst (vl-remove p0 lst))
18.         (setq lst (append (list p0) lst))
19.         (setq lst
20.           (vl-sort lst
21.               (lambda ( a b / c d )
22.                 (if (or (equal (setq c (angle p0 a)) (setq d (angle p0 b)) 1e-8) (and (or (equal c 0.0 1e-8) (equal c (* 2 pi) 1e-8)) (or (equal d 0.0 1e-8) (equal d (* 2 pi) 1e-8))))
23.                   (< (distance (list (car p0) (cadr p0)) a) (distance (list (car p0) (cadr p0)) b))
24.                   (< c d)
25.                 )
26.               )
27.             )
28.           )
29.         )
30.         (setq lstl (vl-remove-if-not (function (lambda ( x ) (equal (angle p0 (last lst)) (angle p0 x) 1e-8))) lst))
31.         (setq lst (vl-remove-if (function (lambda ( x ) (equal (angle p0 (last lst)) (angle p0 x) 1e-8))) lst))
32.         (setq lstl (vl-sort lstl (function (lambda ( a b ) (> (distance p0 a) (distance p0 b))))))
33.         (setq lst (append lst lstl))
34.         (setq ch (list (cadr lst) (car lst)))
35.         (foreach pt (cddr lst)
36.           (setq ch (cons pt ch))
38.             (setq ch (cons pt (cddr ch)))
39.           )
40.         )
41.         (reverse ch)
42.       )
43.     )
44.   )
45.
46.   ;; Clockwise-p  -  Lee Mac
47.   ;; Returns T if p1,p2,p3 are clockwise oriented or [s]collinear[/s]
48.
49.   (defun LM:Clockwise-p ( p1 p2 p3 )
50.     (<  (-  (* (- (car  p2) (car  p1)) (- (cadr p3) (cadr p1)))
51.             (* (- (cadr p2) (cadr p1)) (- (car  p3) (car  p1)))
52.         )
53.         0.0
54.     )
55.   )
56.
57.   (defun sort ( l )
58.     (vl-sort l (function (lambda ( a b ) (< (apply (function +) (mapcar (function (lambda ( c d ) (distance c d))) a (append (cdr a) (list (car a))))) (apply (function +) (mapcar (function (lambda ( c d ) (distance c d))) b (append (cdr b) (list (car b)))))))))
59.   )
60.
61.   (defun processpt ( ppp p j / ppl pll plll pllll )
62.     (gc)
63.     (if j
64.         (foreach pp ppp
65.           (setq ppl (append (member pp ppp) (reverse (cdr (member pp (reverse ppp))))))
66.           (setq pll (append (list pp) (list p) (cdr ppl)))
67.           (setq plll (cons pll plll))
68.         )
69.         (setq plll (sort plll))
70.         (repeat j
71.           (if (car plll)
72.             (setq pllll (cons (car plll) pllll))
73.           )
74.           (setq plll (cdr plll))
75.         )
76.       )
77.         (foreach pp ppp
78.           (setq ppl (append (member pp ppp) (reverse (cdr (member pp (reverse ppp))))))
79.           (setq pll (append (list pp) (list p) (cdr ppl)))
80.           (setq plll (cons pll plll))
81.         )
82.         (setq pllll plll)
83.       )
84.     )
85.     pllll
86.   )
87.
88.   (defun depth ( plll m / unique trimbynum trimbyperc ff plr pllll )
89.
90.     (defun unique ( l / x ll f1 f2 )
91.
92.       (defun f1 ( a b ) (equal a b 1e-6))
93.
94.       (defun f2 ( y ) (vl-every (function f1) y (member (car y) x)))
95.
96.       (gc)
97.       (while (setq x (car l))
98.         (setq ll (cons x ll)
99.               x  (append x x)
100.               l  (vl-remove-if (function f2) (cdr l))
101.         )
102.       )
103.       ll
104.     )
105.
106.     (defun trimbynum ( l m / ll )
107.       (if (< m (length l))
108.           (repeat m
109.             (setq ll (cons (car l) ll))
110.             (setq l (cdr l))
111.           )
112.           (setq ll (reverse ll))
113.         )
114.         (setq ll l)
115.       )
116.       ll
117.     )
118.
119.     (defun trimbyperc ( l m / ll )
120.       (if (< m 100)
121.           (repeat (fix (* (length l) (/ m 100.0)))
122.             (setq ll (cons (car l) ll))
123.             (setq l (cdr l))
124.           )
125.           (setq ll (reverse ll))
126.         )
127.         (setq ll l)
128.       )
129.       ll
130.     )
131.
132.     (defun ff ( x )
133.       (vl-position x lww)
134.     )
135.
136.     (if m
137.         (setq plll (unique plll))
138.         (if (= (type m) 'INT)
139.           (foreach lww (trimbynum (if (< m (length plll)) (sort plll) plll) m)
140.             (setq plr (vl-remove-if (function ff) pl))
141.             (foreach p plr
142.               (setq pllll (append (processpt lww p j) pllll))
143.             )
144.           )
145.           (foreach lww (trimbyperc (if (< m 100) (sort plll) plll) m)
146.             (setq plr (vl-remove-if (function ff) pl))
147.             (foreach p plr
148.               (setq pllll (append (processpt lww p j) pllll))
149.             )
150.           )
151.         )
152.       )
153.       (foreach lww (unique plll)
154.         (setq plr (vl-remove-if (function ff) pl))
155.         (foreach p plr
156.           (setq pllll (append (processpt lww p j) pllll))
157.         )
158.       )
159.     )
160.     pllll
161.   )
162.
163.   (defun _do-events nil
164.     (gc)
165.     (repeat 2 (vl-cmdf "_.DELAY" 0) (princ ""))
166.   )
167.
168.   (setq cmde (getvar 'cmdecho))
169.   (setvar 'cmdecho 0)
170.   (if
171.     (and
172.       (princ "\nSelect points, blocks or circles in WCS...")
173.       (setq ss (ssget '((0 . "POINT,CIRCLE,INSERT"))))
174.     )
175.       (repeat (setq i (sslength ss))
176.         (setq p (cdr (assoc 10 (entget (ssname ss (setq i (1- i)))))))
177.         (setq pl (cons (mapcar (function +) '(0 0) p) pl))
178.       )
179.       (setq pll (LM:ConvexHull-ptsonHull pl))
180.       (setq lw
181.             (list
182.               '(0 . "LWPOLYLINE")
183.               '(100 . "AcDbEntity")
184.               '(100 . "AcDbPolyline")
185.               (cons 90 (length pll))
186.               (cons 70 (1+ (* 128 (getvar 'plinegen))))
187.               '(38 . 0.0)
188.             )
189.             (mapcar (function (lambda ( x ) (cons 10 x))) pll)
190.             '((210 0.0 0.0 1.0))
191.           )
192.         )
193.       )
194.       (setq lwx (entget lw))
195.       (setq pl (vl-remove-if (function (lambda ( x ) (vl-position x pll))) pl))
196.       (initget 6)
197.       (setq n (getint (strcat "\nSpecify depth number - positive integer - from 1 to " (itoa (length pl)) " - preferable 3 <" (itoa (length pl)) "> : ")))
198.       (if (null n)
199.         (setq n (length pl))
200.       )
201.       (while (> n (length pl))
202.         (prompt "\nYou specified number greater than : ") (princ (length pl))
203.         (initget 6)
204.         (setq n (getint (strcat "\nSpecify depth number - positive integer - from 1 to " (itoa (length pl)) " - preferable 3 <" (itoa (length pl)) "> : ")))
205.         (if (null n)
206.           (setq n (length pl))
207.         )
208.       )
209.       (if (/= n (length pl))
210.           (initget 4)
211.           (setq i (getreal "\nIncremental depth - preferable 0 <1> - you can specify 1.5, 0.5, 0.33333334, 0.25 : "))
212.           (if (null i)
213.             (setq i 1)
214.           )
215.           (while (or (and (minusp (- (length pl) n n)) (> i 0)) (> i (- (length pl) n n) 0))
216.             (prompt "\nYou specified number greater than : ") (if (minusp (- (length pl) n n)) (prompt "0 - you must specify \"0\"") (princ (- (length pl) n n)))
217.             (initget 4)
218.             (setq i (getreal "\nIncremental depth - preferable 0 <1> - you can specify 1.5, 0.5, 0.33333334, 0.25 : "))
219.             (if (null i)
220.               (setq i 1)
221.             )
222.           )
223.         )
224.       )
225.       (initget 6)
226.       (setq m (getint (strcat "\nSpecify number of solution attempts per depth iteration - preferable " (itoa (fix (/ 1600.0 (length pl)))) " <all> : ")))
227.       (if (null m)
228.           (initget 6)
229.           (setq m (getreal "\nSpecify percentage of solution attempts per depth iteration <100%> : "))
230.           (while (and m (> m 100))
231.             (prompt "\nYou specified number greater than 100...")
232.             (initget 6)
233.             (setq m (getreal "\nSpecify percentage of solution attempts per depth iteration <100%> : "))
234.           )
235.         )
236.       )
237.       (initget 6)
238.       (setq j (getint "\nSpecify number of list length processed by (processpt) sub function - preferable 2 <all> : "))
239.       (setq ti (car (_vl-times)))
240.       (setq ppp pll)
241.       (while pl
242.         (if (not (equal ppp pll))
243.           (setq n (+ n i))
244.         )
245.         (foreach p pl
246.           (setq plll (append (processpt ppp p j) plll))
247.         )
248.         (setq pllll plll)
249.         (repeat (if (<= (fix n) (length pl)) (1- (fix n)) (1- (length pl)))
250.           (setq pllll (depth pllll m))
251.         )
252.         (setq dmin 1e+308)
253.         (foreach xxx pllll
254.           (if (and xxx (< (setq d (apply (function +) (mapcar (function (lambda ( a b ) (distance a b))) xxx (append (cdr xxx) (list (car xxx)))))) dmin))
255.             (setq dmin d pllll xxx)
256.           )
257.         )
258.           (cdr
259.             (assoc -1
260.                 (setq lwx
261.                     (subst (cons 90 (length pllll)) (assoc 90 lwx) (reverse (cdr (member (assoc 10 lwx) (reverse lwx)))))
262.                     (mapcar (function (lambda ( x ) (cons 10 x))) pllll)
263.                   )
264.                 )
265.               )
266.             )
267.           )
268.         )
269.         (setq pl (vl-remove-if (function (lambda ( x ) (vl-position x pllll))) pl))
270.         (setq ppp pllll)
271.         (_do-events)
272.         (redraw lw)
273.         (setq plll nil pllll nil)
274.       )
275.       (setq pl (mapcar (function cdr) (vl-remove-if (function (lambda ( x ) (/= (car x) 10))) lwx)))
276.       (setq lil (mapcar (function list) pl (append (cdr pl) (list (car pl)))))
277.       (setq k -1)
278.       (while (and (< (setq k (1+ k)) (length lil)) (setq a (nth k lil)))
279.         (setq ilil (vl-some (function (lambda ( b / ip ) (setq ip (inters (car a) (cadr a) (car b) (cadr b))) (if (and ip (setq ip (mapcar (function +) '(0 0) ip)) (or (and (or (equal ip (car a) 1e-6) (equal ip (cadr a) 1e-6)) (not (equal ip (car b) 1e-6)) (not (equal ip (cadr b) 1e-6))) (and (or (equal ip (car b) 1e-6) (equal ip (cadr b) 1e-6)) (not (equal ip (car a) 1e-6)) (not (equal ip (cadr a) 1e-6))) (and (not (equal ip (car a) 1e-6)) (not (equal ip (cadr a) 1e-6)) (not (equal ip (car b) 1e-6)) (not (equal ip (cadr b) 1e-6))))) (list a b)))) (vl-remove a lil)))
280.         (if ilil
281.             (setq pre (reverse (cdr (member (car ilil) (reverse lil)))))
282.             (setq mid (cdr (member (car ilil) lil)))
283.             (setq mid (cdr (member (cadr ilil) (reverse mid))))
284.             (setq mid (mapcar (function reverse) mid))
285.             (setq suf (cdr (member (cadr ilil) lil)))
286.             (setq lil (append pre (list (list (car (car ilil)) (car (cadr ilil)))) mid (list (list (cadr (car ilil)) (cadr (cadr ilil)))) suf))
287.             (setq pre nil mid nil suf nil)
288.             (setq pl (mapcar (function car) lil))
289.               (cdr
290.                 (assoc -1
291.                       (reverse (cdr (member (assoc 10 lwx) (reverse lwx))))
292.                       (mapcar (function (lambda ( x ) (cons 10 x))) pl)
293.                     )
294.                   )
295.                 )
296.               )
297.             )
298.             (_do-events)
299.             (redraw lw)
300.             (setq ilil nil k -1)
301.           )
302.         )
303.       )
304.     )
305.   )
306.   (setvar 'cmdecho cmde)
307.   (prompt "\nDistance : ") (princ (rtos (apply (function +) (mapcar (function distance) pl (append (cdr pl) (list (car pl))))) 2 20))
308.   (prompt "\nElapsed time : ") (princ (rtos (- (car (_vl-times)) ti) 2 20)) (prompt " milliseconds...")
309.   (princ)
310. )
311.

Stay well and be healthy my friends...
M.R.

« Last Edit: June 24, 2020, 10:03:40 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #125 on: May 26, 2020, 07:39:03 AM »
I see there is no comment for my code...
Just a thought, wouldn't be nice if someone would convert LISP into some faster language... I mean - this is TSP problem, so it needs boosting in speed of calculations... However I am not educated in other than LISP... Maybe if Daniel could see it, or Gilles (.NET)... But I need it for A2018, so maybe I am old with my wishes, but that's the problem with other languages (ObjectARX, C#, ... )... It would be the best if it could work faster than LISP, but compatible with all AutoCAD releases...

This is just my thought, no one replied, so I had to step in again...
Thanks for attention, M.R.

[EDIT : If someone succeds to translate LISP, just don't give some stupid command names for execution like : "DOIT", "TEST", ... It would be nice something like : "TSP-2D-LSP-CONVERTED" or similar...]
« Last Edit: May 26, 2020, 07:59:04 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

VovKa

• Swamp Rat
• Posts: 1273
• Ukraine
Re: (Challenge) To draw the shortest lwpolyline
« Reply #126 on: May 26, 2020, 08:16:34 AM »
I see there is no comment for my code...
because the code is long and rather difficult to read and understand

i'm pretty sure it could be optimized more
at least some simple things as
Code: [Select]
`(setq mid (reverse (cdr (member (cadr ilil) (reverse mid)))))(setq mid (reverse mid))`

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #127 on: May 26, 2020, 11:29:10 AM »
i'm pretty sure it could be optimized more
at least some simple things as
Code: [Select]
`(setq mid (reverse (cdr (member (cadr ilil) (reverse mid)))))(setq mid (reverse mid))`

I see...
So instead of those 2 lines, just replace them in my code (lastly portion - lines 298 and 299) with :
Code: [Select]
`(setq mid (cdr (member (cadr ilil) (reverse mid))))`
But this is really nothing in overall performance... I mean that we need boost in speed with *.arx or maybe *.dll that will replicate functionality of above posted LISP...

[EDIT : Corrected my above posted code...]
« Last Edit: June 17, 2020, 07:51:21 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #128 on: May 26, 2020, 04:57:12 PM »
Just to inform...

I've changed (processpt) sub function to be more efficient... To me the code looks now without evident lacks and ready for further process of translating/converting into faster programming language...

Regards...
Marko Ribar, d.i.a. (graduated engineer of architecture)

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #129 on: May 28, 2020, 09:59:30 AM »
I have a problem LISP crashed in BricsCAD with this message :

Code: [Select]
`; ----- LISP : Call Stack -----; [0]...C:TSP-2D-MR-LATEST-NEW; [1].....DEPTH; [2].......UNIQUE # 1108 <<--;; ----- Error around expression -----; (EQUAL A B 1.0e-06);; error : out of LISP 'Heap' memory at [gc]`
Then I've changed this portion :

Code - Auto/Visual Lisp: [Select]
1. ...
2.   (defun depth ( plll m / unique sort trimbynum trimbyperc plr pllll )
3.
4.     (defun unique ( l )
5.       (gc)
6.       (if l (cons (car l) (unique (vl-remove-if (function (lambda ( x ) (vl-every (function (lambda ( a b ) (equal a b 1e-6))) x (member (car x) (append (car l) (car l)))))) l))))
7.     )
8. ...
9.

As you can see I've added (gc) in recursive sub function...
But still I run it and it crashes at about the same place with this error message :

Code: [Select]
`: TSP-2D-MR-LATEST-NEWSelect points, blocks or circles in WCS...Select entities:Opposite Corner: Entities in set: 40Select entities:Specify depth number - positive integer - from 0 to 32 <32> : Specify number of solution attempts per depth iteration - preferable 50 <all> : 100Specify number of list length processed by (processpt) sub function - preferable 2 <all> : 4; ----- LISP : Call Stack -----; [0]...C:TSP-2D-MR-LATEST-NEW; [1].....DEPTH; [2].......UNIQUE # 1130 <<--;; ----- Error around expression -----; (CAR L);; error : out of LISP 'Heap' memory at [gc]`
So how can I fix it?
Marko Ribar, d.i.a. (graduated engineer of architecture)

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #130 on: May 28, 2020, 10:13:13 AM »
Sorry it was my mistake, I forgot to owerwrite correct LISP which has (gc) added with old one which was loaded...
I am testing it again, we'll see how will it go...
Marko Ribar, d.i.a. (graduated engineer of architecture)

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #131 on: May 28, 2020, 10:31:36 AM »
No it won't pass...
Now I have this :

Code: [Select]
`: TSP-2D-MR-LATEST-NEWSelect points, blocks or circles in WCS...Select entities:Opposite Corner: Entities in set: 40Select entities:Specify depth number - positive integer - from 0 to 32 <32> : Specify number of solution attempts per depth iteration - preferable 50 <all> : 100Specify number of list length processed by (processpt) sub function - preferable 2 <all> : 4; ----- LISP : Call Stack -----; [0]...C:TSP-2D-MR-LATEST-NEW; [1].....DEPTH; [2].......UNIQUE # 897 <<--;; ----- Error around expression -----; (GC);; error : out of LISP 'Heap' memory at [gc]`
Can someone fix it?
Marko Ribar, d.i.a. (graduated engineer of architecture)

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #132 on: May 28, 2020, 01:43:53 PM »
It worked and in BricsCAD with those inputs... It looks like BricsCAD don't like recursions... So I've replaced (unique) with iterative version and it worked well...

Code - Auto/Visual Lisp: [Select]
1. ...
2.   (defun depth ( plll m / unique sort trimbynum trimbyperc plr pllll )
3.
4.     (defun unique ( l / x ll )
5.       (while (setq x (car l))
6.         (setq ll (cons x ll))
7.         (setq l (vl-remove-if (function (lambda ( y ) (vl-every (function (lambda ( a b ) (equal a b 1e-6))) y (member (car y) (append x x))))) l))
8.       )
9.       ll
10.     )
11. ...
12.

Regards, M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

VovKa

• Swamp Rat
• Posts: 1273
• Ukraine
Re: (Challenge) To draw the shortest lwpolyline
« Reply #133 on: May 28, 2020, 07:40:27 PM »
But this is really nothing in overall performance...
this is true
but the devil is in the detail
i think you should spend more time on optimizing
Code: [Select]
`(defun unique3 (l / x ll f1 f2)  (defun f1 (a b) (equal a b 1e-6))  (defun f2 (y) (vl-every 'f1 y (member (car y) x)))  (while (setq x (car l))    (setq ll (cons x ll)   x  (append x x)   l  (vl-remove-if 'f2 (cdr l))    )  )  ll)`your algorithm is unchanged but it is written in a more 'optimized' way
and i believe it will run a bit faster

ribarm

• Water Moccasin
• Posts: 2401
• Marko Ribar, architect
Re: (Challenge) To draw the shortest lwpolyline
« Reply #134 on: May 29, 2020, 01:16:24 AM »
But those are minimal improvements...

Code - Auto/Visual Lisp: [Select]
1. (defun c:test ( / l t0 t1 k f )
2.   (repeat 10
3.     (setq l (append l (atoms-family 1)))
4.   )
5.   (setq t0 (car (_vl-times)))
6.   (foreach a l
7.     (princ "")
8.   )
9.   (setq t1 (car (_vl-times)))
10.   (prompt "\nElapsed time for (foreach a l) : ") (princ (rtos (- t1 t0) 2 20)) (prompt " milliseconds...")
11.   (setq t0 (car (_vl-times)))
12.   (while (setq a (car l))
13.     (princ "")
14.     (setq l (cdr l))
15.   )
16.   (setq t1 (car (_vl-times)))
17.   (prompt "\nElapsed time for (while (setq a (car l))) : ") (princ (rtos (- t1 t0) 2 20)) (prompt " milliseconds...")
18.   (repeat 10
19.     (setq l (append l (atoms-family 1)))
20.   )
21.   (setq k -1)
22.   (setq t0 (car (_vl-times)))
23.   (while (and (setq k (1+ k)) (< k (length l)) (setq a (nth k l)))
24.     (princ "")
25.   )
26.   (setq t1 (car (_vl-times)))
27.   (prompt "\nElapsed time for (while (and (setq k (1+ k)) (< k (length l)) (setq a (nth k l)))) : ") (princ (rtos (- t1 t0) 2 20)) (prompt " milliseconds...")
28.   (setq t0 (car (_vl-times)))
29.   (mapcar (function (lambda ( x ) (princ ""))) l)
30.   (setq t1 (car (_vl-times)))
31.   (prompt "\nElapsed time for (mapcar (function (lambda ( x ) (princ \"\"))) l) : ") (princ (rtos (- t1 t0) 2 20)) (prompt " milliseconds...")
32.   (setq t0 (car (_vl-times)))
33.   (defun f ( x ) (princ ""))
34.   (mapcar (function f) l)
35.   (setq t1 (car (_vl-times)))
36.   (prompt "\nElapsed time for (mapcar (function f) l) : ") (princ (rtos (- t1 t0) 2 20)) (prompt " milliseconds...")
37.   (princ)
38. )
39.
41. ;|
42. : TEST
43.
44. Elapsed time for (foreach a l) : 2187 milliseconds...
45. Elapsed time for (while (setq a (car l))) : 2312 milliseconds...
46. Elapsed time for (while (and (setq k (1+ k)) (< k (length l)) (setq a (nth k l)))) : 8219 milliseconds...
47. Elapsed time for (mapcar (function (lambda ( x ) (princ ""))) l) : 2235 milliseconds...
48. Elapsed time for (mapcar (function f) l) : 2219 milliseconds...
49. |;
50.
Marko Ribar, d.i.a. (graduated engineer of architecture)