因为敲符号实在是太麻烦了,所以我把约束条件写在了注释里面。
我也是在学习过程中,这不是标答,都是我自己写的,所以大家辨证看待我写的内容。
欢迎大家批评指正,别骂的太凶我都能接受的(捂脸)。

问题一

在这里插入图片描述

计算已知距离

利用matlab软件计算两个临时料场到建筑工地的距离

function NJUCM_2()

    function Distance = cal_length(a_1, b_1, a_2, b_2)
    %% 定义一个计算距离的函数;    
        temp_1 = (a_1 - a_2) ^ 2;
        temp_2 = (b_1 - b_2) ^ 2;
        Distance = (temp_1 + temp_2) ^ 0.5;
    end

%% 数据的输入与预定义;
Construction_site_location = [[1.25, 1.25]; [8.75, 0.75]; [0.5, 4.75]; [5.75, 5]; [3, 6.5]; [7.25, 1.75]];
Yard_temp_location = [[5, 1]; [2, 7]];
Distance_construction_yard = zeros(2, 6);

%% 分别计算两个料场到建筑工地的距离;
for i = 1 : 2
   for j = 1 : 6 
       Distance_construction_yard(i, j) = cal_length(Construction_site_location(j, 1), Construction_site_location(j, 2), Yard_temp_location(i, 1), Yard_temp_location(i, 2));
   end
end

disp(Distance_construction_yard)
end

得到如下结果(i 行 j 列代表第 i 个料场到第 j 个工地的距离)
在这里插入图片描述

规划约束

我最开始是这样写的,但是它告诉我这不是最优解,我尝试把它理解为一个局部最优解(?)

model:
title NJUCM 3_1;

sets:
Temp_yard/1..2/:Temp_yard_reserves; ! 临时料场的储量;
Construction_site/1..6/:Construction_site_requirement; ! 工地的需求量;
link_1(Temp_yard, Temp_yard):Yard_location, Yard_temp_distance, Goods_quantity_1, Temp_yard_location; ! 新料场的位置;
link_2(Temp_yard, Construction_site):Distance_yard_site, Goods_quantity_2; ! 临时料场到工地的距离;
endsets

data:
Temp_yard_reserves = 20, 20;
Construction_site_requirement = 3 5 4 7 6 11;
Distance_yard_site =
3.7583 3.7583 5.8577 4.0697 5.8523 2.3717
5.7987 9.1992 2.7042 4.2500 1.1180 7.4246;
Temp_yard_location =
5 1
2 7;
enddata

min = 
@sum(link_1(i, j) : Goods_quantity_1(i, j) * Yard_temp_distance(i, j)) 
+ 
@sum(link_2(i, j) : Goods_quantity_2(i, j) * Distance_yard_site(i, j));

! 给新料场与临时料场的距离赋值;
@for(link_1(i, j) :
	Yard_temp_distance(i, j) = ((Yard_location(i, 1) - Temp_yard_location(j, 1))^2 + (Yard_location(i, 2) - Temp_yard_location(j, 2))^2) ^ 0.5);

! 从料场运到临时料场的数量 > 临时料场运到建筑工地的数量;
@for(Temp_yard(i) :
	@sum(link_1(i, j) : Goods_quantity_1(i, j)) > @sum(link_2(i, j) : Goods_quantity_2(i, j)));

! 从料场运送到临时料场的货物数量 < 20;
@for(Temp_yard(i) :
	@sum(link_1(i, j) : Goods_quantity_1(i, j)) < 20);

! 从临时料场运过去的每种货物和 > 需求量;
@for(Construction_site(j) :
	@sum(link_2(i, j) : Goods_quantity_2(i, j)) > Construction_site_requirement(j));

end

这句话就是告诉我,这可能不是一个最优解
在这里插入图片描述

老师的做法

老师用到一个新的定义:

! 初始段:对集合属性定义初值(迭代算法的迭代初值);

好像使用 init 来定义的,查询资料后可知知道,就是把这两个变量定义一个初值,设置一个迭代(计算)的起点,这样可以大幅度提高计算效率,也可以尽量向全局最优解靠近。

但我在这边还是有点不理解,再学习一下吧

问题二

在这里插入图片描述

普通线性规划

! 2022/03/09;
! 略小()于 范围均限制在 2;
! 需要原料 约为 范围限制在 正负2;
! 原料总数 约为 范围限制在 正负200;

model:
title NJUCM第三次作业;

sets:
Medicine/1..2/:Profits_medicine, Number_production;
Material/1..2/:Total_material;
link(Medicine, Material):Consuption;
endsets

data:
Profits_medicine = 3 4;
Total_material = 4600 4800;
Consuption = 
4 12
20 6.4;
enddata

max = @sum(Medicine(i) : Number_production(i) * Profits_medicine(i));

@for(Material(j) :
	@sum(Medicine(i) : Number_production(i) * Consuption(i, j)) < Total_material(j));

end

带有伸缩指标的线性规划

!   2022/03/09
!   略小()于 范围均限制在 2;
! 需要原料 约为 范围限制在 正负2;
! 原料总数 约为 范围限制在 正负200;

model:
title NJUCM第三次作业;

sets:
Medicine/1..2/:Profits_medicine, Number_production;
Material/1..2/:Total_material;
link(Medicine, Material):Consuption;
endsets

data:
Profits_medicine = 3 4;
Total_material = 4600 4800;
enddata

max = @sum(Medicine(i) : Number_production(i) * Profits_medicine(i));

2 < Consuption(1, 1);
Consuption(1, 1) < 4;
20 < Consuption(2, 1);
Consuption(2, 1) < 22;
10 < Consuption(1, 2);
Consuption(1, 2)  < 14;
4.4 < Consuption(2, 2);
Consuption(2, 2) < 8.4;
Consuption(1, 1) * Number_production(1) + Consuption(2, 1) * Number_production(2) < 4800;
Consuption(1, 1) * Number_production(1) + Consuption(2, 1) * Number_production(2) > 4400;
Consuption(1, 2) * Number_production(1) + Consuption(2, 2) * Number_production(2) < 5000;
Consuption(1, 2) * Number_production(1) + Consuption(2, 2) * Number_production(2) > 4600;

end

模糊线性规划

这个emmmm让我想一想,思路有点乱的赶脚。等明天另外写一篇文章

问题三

在这里插入图片描述

model:
title NJUCM第三次作业;

sets:
! Demand_number : 每种产品需求的数量;
! Demand_length : 每种产品需求的长度;
! Cutting_price : 每种切割方式的价格(按照降序排列);
Demand/1..4/:Demand_number, Demand_length, Cutting_price;
! Material_consuption_number : 按照一种切割方式切割的原料数量;
Material/1..4/:Material_consuption_number;
! Cutting_pattern(i, j) : 切割模式,按照第 i 种切割方式获得第 j 种产品的数量;
link(Material, Demand):Cutting_pattern;
endsets

data:
Material_length = 1850;
Material_price = 1;
Demand_number = 15 28 21 30;
Demand_length = 290 315 350 455;
Cutting_price = 1.1 1.2 1.3 1.4;
Max_number = 5;
Max_remain_material = 100;
enddata

min = @sum(Material(i) : Material_consuption_number(i) * Cutting_price(i));

! 每种产品数量 > 需求数量;
@for(Demand(j) :
	@sum(Material(i) :
		Cutting_pattern(i, j) * Material_consuption_number(i)) > Demand_number(j));

! 每种原料 > 原长 - 100;
@for(Material(i) :
	@sum(Demand(j) :
		Cutting_pattern(i, j) * Demand_length(j)) > Material_length - Max_remain_material);

! 每种切割方式获得长度 < 原料长度;
@for(Material(i) :
	@sum(Demand(j) :
		Cutting_pattern(i, j) * Demand_length(j)) < Material_length);

! 每根原料获得的产品数量 < 5;
@for(Material(i) :
	@sum(Demand(j) :
		Cutting_pattern(i, j)) < Max_number);

! 限制在整数;
@for(link(i, j) : @gin(Cutting_pattern(i, j)));

@for(Material(i) : @gin(Material_consuption_number(i)));

! 按照降序排列;
@for(Material(i)|i#lt#@size(Material) :
	Material_consuption_number(i) > Material_consuption_number(i + 1));

end
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐