Jump to content

(nesting)optimize bar lengths


comcu

Recommended Posts

Hi,

 

 

Does anyone know any code that would allow me to optimize bar lengths?

 

I have bar lengths that are 6m long and I have to cut 20 allsorted lengths from the 6m lengths.

 

What code would you write to perform this task?

 

Thanks

 

Col

Link to comment
Share on other sites

Those are kinda what im looking for but the seem overally complicated.

 

All I need to be able to do is 1D optimizing and it doesnt need to be drawn out, could use Text boxes for input & output

 

any thoughts?

 

cheers

Link to comment
Share on other sites

This is something I was playing with some time back. Perhaps you can make use of it.

 

(defun c:test(/ lst maxlen)
 (setq lst '(144 35 23 86 99 12 230 12 12 14 132 189 6 3 99))
 (setq maxlen 240.0)
 (get_cutlist lst maxlen)
)
;;  result ((99) (99 132) (86 144) (12 12 12 14 23 35 189) (3 6 230))

;;  CAB 03-10-06
(defun get_cutlist (lst maxlen / cutlst itm lst ptr tl x finallst remove-at)
 (defun remove-at (lst pos / head) ; Tony Tanzillo
   (repeat pos
     (setq head (cons (car lst) head)
           lst  (cdr lst)
     )
   )
   (append (reverse head) (cdr lst))
 )

 (setq lst (mapcar '(lambda (x) (nth x lst)) (vl-sort-i lst '>)))
 ;;  step through lst
 (while lst
   (setq cutlst (list (car lst)) ; start new cutlist w/ first item
         lst    (cdr lst) ; remove first item
         ptr    (1- (length lst)) ; point to end of list
         tl     (apply '+ cutlst) ; total length so far
   )

   ;; build the cutlst
   (while (and lst cutlst)
     ;; find largest next cut
     ;; exit conditions ptr < 0 or itm length exceeds max
     (while (and (< (+ tl (setq itm (nth ptr lst))) maxlen)
                 (> ptr 0))
       (setq ptr (1- ptr))
     )

     (if (> ptr -1)
       (if (= ptr (1- (length lst)))
         ;;  no more cuts fit, go to next
         (setq finallst (cons cutlst finallst)
               cutlst   nil
         )
         (setq cutlst (cons (nth (1+ ptr) lst) cutlst)
               lst    (remove-at lst (1+ ptr))
               tl     (apply '+ cutlst) ; new total

         )
       )
       ;; else exausted pointer
       (setq finallst (cons cutlst finallst)
             cutlst   nil
       )
     ) 
   ) 
 )
 (if cutlst
   (cons cutlst finallst)
   finallst
 )
)

Link to comment
Share on other sites

  • 9 years later...

Hi Guys,

 

I was playing around with the code above and noticed that it wouldn't work in a real world situation when I make;

 

(setq maxlen 6500.0) and (setq lst '(1120 2705 2725 2715)).... I get back ((2715) (1120 2705 2725)) = 6550? this should be (2725 2715)(2705 1120)

 

(setq maxlen 6500.0) and (setq lst '(2715 1050 2725 2705)).... i get back ((2715) (1050 2705 2725)) where as this should optimize to (2725 2715 1050)(2705)

 

 

Can someone please help me fix this code?

 

Cheers

Mike

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...