明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 331|回复: 4

[讨论] 关于一维下料算法探讨

[复制链接]
发表于 2024-8-31 11:35:28 | 显示全部楼层 |阅读模式
  1. ;探讨一下算法,目的:把一个列表,分割成和是【接近且小于等于15】的多个表。结果追求【总数n少,组合i少】。
  2. ;目前最优答案是:"共计生成了16个表,过滤重复后,共有9种组合"
  3. ;(((9 4 2) 2) ((10 5) 2) ((12 3) 2) ((7 4 4) 1) ((6 4 4 1) 1) ((13 1) 1) ((8 6) 4) ((14) 2) ((15) 1))
  4. ;抛砖引玉。
  5. (defun c:ff(/ ass asslst b i k lst n newlst sum);最直接的办法:"共计生成了16个表,过滤重复后,共有10种组合"
  6.   (setq lst '(14 10 9 4 6 8 4 14 10 9 4 6 8 4 5 6 3 2 1 12 4 6 8 4 5 6 3 2 1 12 13 15 8 7))
  7.   (setq newlst(vl-sort(mapcar '(lambda(x)(+ x 0.0))lst)'>))
  8.   (setq sum 0 ass nil asslst nil);---临时总和,元素结果表,元素组合表
  9.   (while newlst
  10.     (if(setq b(vl-some '(lambda(x / a)(if(<=(setq a(+ x sum))15)(list x a)))newlst));---查找表里元素有没有符合要求的
  11.       (progn
  12.         (setq k(car b)sum(cadr b)ass(cons k ass));---有的话就加入到新表
  13.         (setq newlst(vl-remove nil(mapcar '(lambda(x)(if(= x k)(setq x nil k nil)x))newlst)));---删掉第一个指定元素
  14.       )
  15.       (setq asslst(cons(reverse ass)asslst)sum 0 ass nil);---把组合写入到asslst中
  16.     )
  17.   )
  18.   (setq asslst(cons(reverse ass)asslst));---处理最后一个元素
  19.   (setq asslst(reverse asslst));((4 4 4 3) (4 4 4 3) (6 6 2 1) (6 6 2 1) (6 8) (8 5) (8 5) (8 7) (14) (14) (12) (12) (10) (10) (9) (9) (15) (13))
  20.   (setq n(length asslst))
  21.   (setq i(length(dup-Counts asslst)));(((4 4 4 3) 2) ((6 6 2 1) 2) ((6 8) 1) ((8 5) 2) ((8 7) 1) ((14) 2) ((12) 2) ((10) 2) ((9) 2) ((15) 1) ((13) 1))
  22.   (print (strcat "共计生成了" (rtos n) "个表,过滤重复后,共有"(rtos i)"种组合"))
  23.   (princ)
  24. )
  25. (defun c:gg(/ ass asslst b i k lst n newlst sum);第二种办法:"共计生成了18个表,过滤重复后,共有11种组合",很显然,这种更不合理了
  26.   (setq lst '(14 10 9 4 6 8 4 14 10 9 4 6 8 4 5 6 3 2 1 12 4 6 8 4 5 6 3 2 1 12 13 15 8 7))
  27.   (setq lst(dup-Counts lst));((14 2) (10 2) (9 2) (4 6) (6 5) (8 4) (5 2) (3 2) (2 2) (1 2) (12 2) (13 1) (15 1) (7 1))
  28.   (setq lst(vl-sort lst '(lambda(x1 x2)
  29.                            (if(=(cadr x1)(cadr x2));首选数量排序,次选大小排序
  30.                              (>(car x1)(car x2))
  31.                              (>(cadr x1)(cadr x2))
  32.                            )
  33.                          )));((4 6) (6 5) (8 4) (14 2) (12 2) (10 2) (9 2) (5 2) (3 2) (2 2) (1 2) (15 1) (13 1) (7 1))
  34.   (setq newlst nil)
  35.   (foreach x lst
  36.     (repeat(cadr x)
  37.       (setq newlst(cons(car x)newlst));---拍平
  38.     )
  39.   )
  40.   (setq newlst(reverse newlst));(4 4 4 4 4 4 6 6 6 6 6 8 8 8 8 14 14 12 12 10 10 9 9 5 5 3 3 2 2 1 1 15 13 7)
  41.   (setq sum 0 ass nil asslst nil);---临时总和,元素结果表,元素组合表
  42.   (while newlst
  43.     (if(setq b(vl-some '(lambda(x / a)(if(<=(setq a(+ x sum))15)(list x a)))newlst));---查找表里元素有没有符合要求的
  44.       (progn
  45.         (setq k(car b)sum(cadr b)ass(cons k ass));---有的话就加入到新表
  46.         (setq newlst(vl-remove nil(mapcar '(lambda(x)(if(= x k)(setq x nil k nil)x))newlst)));---删掉第一个指定元素
  47.       )
  48.       (setq asslst(cons(reverse ass)asslst)sum 0 ass nil);---把组合写入到asslst中
  49.     )
  50.   )
  51.   (setq asslst(cons(reverse ass)asslst));---处理最后一个元素
  52.   (setq asslst(reverse asslst));((4 4 4 3) (4 4 4 3) (6 6 2 1) (6 6 2 1) (6 8) (8 5) (8 5) (8 7) (14) (14) (12) (12) (10) (10) (9) (9) (15) (13))
  53.   (setq n(length asslst))
  54.   (setq i(length(dup-Counts asslst)));(((4 4 4 3) 2) ((6 6 2 1) 2) ((6 8) 1) ((8 5) 2) ((8 7) 1) ((14) 2) ((12) 2) ((10) 2) ((9) 2) ((15) 1) ((13) 1))
  55.   (print (strcat "共计生成了" (rtos n) "个表,过滤重复后,共有"(rtos i)"种组合"))
  56.   (princ)
  57. )
  58. ;---重复元素及其出现次数
  59. (defun dup-Counts(lst / n newlst x)
  60.   (setq newlst nil)
  61.   (while(setq x(car lst))
  62.     (setq n(length lst))
  63.     (setq lst(vl-remove x lst))
  64.     (setq newlst(cons(list x(- n(length lst)))newlst))
  65.   )
  66.   (reverse newlst)
  67. )
欢迎大佬们参与。
"觉得好,就打赏"
还没有人打赏,支持一下
 楼主| 发表于 2024-8-31 16:32:08 | 显示全部楼层

挑战一下咯
发表于 2024-9-1 09:39:48 | 显示全部楼层
运行速度是不是太慢了?
发表于 2024-9-1 10:54:23 | 显示全部楼层
了解一下 遗传算法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-9-23 20:10 , Processed in 0.185071 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表