锁具装箱问题的C语言代码_二维装箱问题之Next-Fit Algorithm的简单实现(C语言)

e985e10b81d603b9d35c7d0aab688d6d.png

参考资料

https://www.slideshare.net/shunjiumetani/ss-17197023​www.slideshare.net https://a3a6340e-a-62cb3a1a-s-sites.googlegroups.com/site/shunjiumetani/file/2dspp_slide.pdf?attachauth=ANoY7crqG_yEvQY0dpoIam8sYgTR3Su3_SPNEO8PYdhCJO4pEvX0zuP_0FvAmoAfGGjXKVtPBtAchSLPOOV30FzD6VBIhvCy_0dZYVKVv1ioxjpxdiZ05LsikVRyeeaFM_jL-A4dzQDBQdMkBKK8-_cIQC6bz_IPRCmCGsltSS2wJcOKYbEB6cJ3IhxG9F1JkoC4Ws7JjnB3cq0ZtKBNs60n2Pq5mYtTx_Z0ZT5eYfo-rmM36aG-s3k%3D&attredirects=0​a3a6340e-a-62cb3a1a-s-sites.googlegroups.com
  • 二维装箱问题

输入

  1. ,高
    大小的长方形材料
  2. 产品的集合
    ,各个长方形
    的宽为
    ,高为

约束条件

  1. 把产品放在容器内。即满足以下约束条件,
  2. 产品相互重叠。即满足以下约束条件,

二维装箱问题可用整数规划建模求解。这里不详细介绍。

cd01be02dfecf09477e456574c538ed8.png
二维装箱问题
  • Next-Fit Algorithm

28ce2add7567b70ca945c3b45a1c37f7.png
Next-Fit Algorithm Process

Next-Fit Algorithm 是由长方形的号码顺序进行一个个的放置。如上图中,首先放置0生成红线的level,然后放置1。等到放置不下时,由2产生新的红色level,重复此过程直到所有长方形放置完毕。这个算法的计算时间为

  • Code

代码包括

  1. 数据读取
  2. 计算时间测量
  3. Next-Fit Algorithm
  4. 输出结果
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define FALSE 0
#define TRUE 1
#define MAX_N 10000  

double strip_width, strip_height; 
int n;  /* 长方形个数 */
double w[MAX_N], h[MAX_N];  /* w[i]h[i]/
double x[MAX_N], y[MAX_N];  /* 坐标(x[i],y[i])*/

void next_fit();




int main(){

  FILE *input_file, *output_file;
  double start_time, search_time;
  double area, efficiency;
  int i;
  //读取数据
  input_file = fopen("N1.rec", "r");
  fscanf(input_file, "w= %lfn", &strip_width);
  fscanf(input_file, "n= %dn",&n);
  for(i = 0; i < n; i++){
    fscanf(input_file, "%lf %lfn",&(w[i]),&(h[i]));
  }
  fclose(input_file);


  printf("w= %fn",strip_width);
  printf("n= %dn",n);
  for(i = 0; i < n; i++){
    printf("%4dt%ft%fn",i,w[i],h[i]);
  }

  //计算开始
  start_time = (double)clock()/CLOCKS_PER_SEC;


  next_fit();
  //计算结束
  search_time = (double)clock()/CLOCKS_PER_SEC - start_time;


  strip_height = 0.0;
  for(i = 0; i < n; i++){
    if(y[i]+h[i] > strip_height){
      strip_height = y[i]+h[i];
    }
  }
  //充填率
  area = 0.0;
  for(i = 0; i < n; i++){
    area += w[i] * h[i];    
  }
  efficiency = area / (strip_width * strip_height) * 100.0;

  //输出
  output_file = fopen("result.dat","w");
  fprintf(output_file,"w= %fn",strip_width);
  fprintf(output_file,"n= %dn",n);
  for(i = 0; i < n; i++){
    fprintf(output_file,"%ft%fn",w[i],h[i]);
  }
  for(i = 0; i < n; i++){
    fprintf(output_file,"%ft%fn",x[i],y[i]);
  }
  fclose(output_file);

  //确认
  printf("Coordinates of rectangles:n");
  for(i = 0; i < n; i++){
    printf("%dt%ft%fn",i,x[i],y[i]);
  }
  printf("nh= %fn",strip_height);
  printf("time= %fn",search_time);

  return(0);
}


void next_fit(){

  double level_width, level_height, max_h;
  int i;

  level_width = level_height = max_h = 0.0;
  //生成level
  for(i = 0; i < n; i++){


    if(level_width + w[i] > strip_width){
      x[i] = 0.0;
      y[i] = level_height + max_h;
      level_width = w[i];
      level_height += max_h;
      max_h = h[i];
    }else{
      x[i] = level_width;
      y[i] = level_height;
      level_width += w[i];

    //更新level
      if(h[i] > max_h){
	max_h = h[i];
      }
    }
  }
}

数据

w= 40
n= 10
7	6
40	16
5	20
24	24
7	4
4	4
7	8
4	20
5	4
7	6

大家可以以这个程序为基础,设计一些算子,提高算法的性能。另外,这个算法依赖放置的顺序,所以还可以在这个方面下功夫。

如果有什么疑问或者建议,可以给我发邮件。➡ zhaoyou728 atoutlook.com

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是三维装箱问题的 C++ 代码示例: ```cpp #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 20; int n, m; int w[N], h[N], d[N]; int v[N], ans; bool st[N]; void dfs(int u, int area, int depth) // u:当前选取的物品编号,area:当前底面积,depth:当前高度 { if (depth + v[u] > ans) return; // 剪枝:如果当前已经选取的物品总高度 + 当前物品最大高度 > 目前最优解,直接返回 if (area >= ans) return; // 剪枝:如果当前底面积已经大于等于最优解,直接返回 if (u == n) { // 如果已经遍历完所有物品 ans = depth; // 更新最优解 return; } for (int i = 0; i < m; i++) { if (st[i]) continue; // 如果已经选过,跳过 st[i] = true; // 标记已经选过 // 枚举当前物品的长、宽、高,计算底面积和体积 int nw = w[i], nh = h[i], nd = d[i]; if (nw > nh) swap(nw, nh); if (nw > nd) swap(nw, nd); if (nh > nd) swap(nh, nd); for (int j = 0; j < 3; j++) { int na = nw * nh, nv = nw * nh * nd; // 底面积和体积 dfs(u + 1, area + na, depth + nd); // 递归搜索 swap(nw, nh); // 枚举下一个方向 if (j == 2) break; // 枚举完三个方向,退出 swap(nw, nd); } st[i] = false; // 回溯 } } int main() { while (cin >> n, n) { cin >> m; for (int i = 0; i < n; i++) { cin >> w[i] >> h[i] >> d[i]; v[i] = w[i] * h[i] * d[i]; // 计算体积 } ans = 0x3f3f3f3f; memset(st, false, sizeof st); dfs(0, 0, 0); // 从第0个物品,底面积为0,高度为0开始搜索 if (ans == 0x3f3f3f3f) puts("0"); // 如果没有找到解,输出0 else printf("%d\n", ans); } return 0; } ``` 三维装箱问题是一个 NP 难问题,因此只能使用搜索算法来求解,这里使用了 DFS 来搜索所有可能的方案,并使用剪枝来优化搜索效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值