学号:16340008
目录
Question:
Suppose there are n facilities and m customers. We wish to choose:
- which of the n facilities to open
- the assignment of customers to facilities
The objective is to minimize the sum of the opening cost and the assignment cost.
The total demand assigned to a facility must not exceed its capacity.
Answer:
为了简化报告,blog中只描述算法,代码保存在github中,可直接运行两个算法中的Main.py检验。语言为Python3,因此运行效率可能不及C++等。
算法①:模拟退火算法
首先考虑的是模拟退火算法。
模拟退火算法是从一个状态,不断生成一个新解,并以一定的概率考虑是否接受这个新解,以不断逼近最优解。
典型的模拟退火算法步骤如下:
- 初始化温度,降温策略,一个随机状态作为当前状态A
- 重复以下步骤(外循环)
- 声明集合S
- 重复以下步骤(内循环)
- 从当前状态A生成一个邻域新解B
- 将B加入集合S
- 重复次数到达C(C一般为参数,或参数与问题规模的函数),则退出内循环
- 获得集合S中最优的解C,作以下判断
- 若C比A好,则将A更改为C
- 若C比A差,则根据C与A的差距以及温度得到P,并随机生成一个[0,1)的小数R,若R<P,则将A更改为C,否则不作改变。一般P需要满足:C与A差距越大,P越小;T越小,P越小。
- 按照降温策略降温
- 若温度低于某参数,则退出外循环
我使用的得到邻居解的方法是将一个用户从一个设备选择另一个设备(在不超出容量的前提)。我设定的邻域大小为10(即内循环执行10次),就跳出内循环。同时我没有使用S,而是每次内循环与一个之前内循环中得到的最优解作对比,若更优则保存所作操作以及解的值。同时,因为不同问题,规模大小不一样,因此降温策略增加了与问题规模的匹配。同时概率函数P的得到也复杂化,在考虑与当前状态之余,还考虑与曾经得到的最优解的差距。当然也是差距越大P越小。以上都是参考了论文的DSA-CE&MAP算法[1](一个对经典SA的优化):
在该题目中我的初温选择为测例随机后的初状态消耗的1/2,于是T能随着初状态的复杂程度而变化。降温率我选择0.001,而coolingEnhancer在客户数不大于50,不大于100,不大于150,大于150分别为1,0.5,0.33,0.25。我对每个测例执行了30次SA,以下结果挑最优者,时间保留两位小数:
Result | Time(s) | |
---|---|---|
p1 | 8958 | 0.99 |
p2 | 7920 | 1.05 |
p3 | 9521 | 1.01 |
p4 | 10852 | 1.18 |
p5 | 8993 | 1.08 |
p6 | 7855 | 1.01 |
p7 | 9601 | 1.08 |
p8 | 11499 | 1.12 |
p9 | 8576 | 0.93 |
p10 | 7648 | 0.93 |
p11 | 8967 | 0.93 |
p12 | 10427 | 0.94 |
p13 | 8365 | 0.96 |
p14 | 7137 | 0.98 |
p15 | 8808 | 0.99 |
p16 | 10730 | 0.97 |
p17 | 8240 | 1.07 |
p18 | 7214 | 0.98 |
p19 | 9015 | 0.96 |
p20 | 10779 | 0.98 |
p21 | 8114 | 0.96 |
p22 | 7120 | 0.94 |
p23 | 8754 | 0.94 |
p24 | 10547 | 0.94 |
p25 | 12261 | 3.19 |
p26 | 11033 | 3.18 |
p27 | 13041 | 3.28 |
p28 | 15033 | 3.25 |
p29 | 12790 | 3.28 |
p30 | 11565 | 3.29 |
p31 | 13996 | 3.24 |
p32 | 15922 | 3.27 |
p33 | 12221 | 3.18 |
p34 | 11025 | 3.17 |
p35 | 13009 | 3.18 |
p36 | 15011 | 3.17 |
p37 | 12200 | 3.17 |
p38 | 10984 | 3.13 |
p39 | 12984 | 3.19 |
p40 | 14349 | 3.18 |
p41 | 6874 | 1.93 |
p42 | 6213 | 1.79 |
p43 | 5944 | 1.74 |
p44 | 7130 | 1.94 |
p45 | 6549 | 1.90 |
p46 | 6036 | 1.78 |
p47 | 6452 | 1.88 |
p48 | 5858 | 1.83 |
p49 | 5378 | 1.80 |
p50 | 9219 | 1.94 |
p51 | 8007 | 1.87 |
p52 | 9289 | 2.06 |
p53 | 8996 | 2.01 |
p54 | 9075 | 2.00 |
p55 | 8260 | 1.88 |
p56 | 21879 | 4.23 |
p57 | 27819 | 4.38 |
p58 | 41031 | 4.38 |
p59 | 31301 | 4.35 |
p60 | 21602 | 4.17 |
p61 | 26469 | 4.46 |
p62 | 37830 | 4.33 |
p63 | 29332 | 4.26 |
p64 | 21750 | 4.19 |
p65 | 27001 | 4.22 |
p66 | 36629 | 4.26 |
p67 | 29477 | 4.48 |
p68 | 21861 | 4.27 |
p69 | 27103 | 4.25 |
p70 | 37164 | 4.28 |
p71 | 29979 | 4.83 |
p1:
Result: 8958
Status of facilities: 1 1 1 1 1 1 0 0 1 1
The assignment of customers to facilities: 8 2 1 5 3 8 2 4 4 1 9 0 3 2 8 3 4 0 9 4 3 4 9 4 2 5 1 5 0 5 2 5 0 3 9 4 4 3 0 4 1 8 1 5 4 0 2 0 4 0
Time cost: 0.9896676540374756s
p2:
Result: 7920
Status of facilities: 1 1 1 1 1 1 1 0 1 1
The assignment of customers to facilities: 8 2 1 6 3 8 2 4 4 1 9 0 3 2 8 3 4 0 9 4 3 4 6 4 2 5 1 5 0 5 2 6 0 3 9 4 4 3 0 4 1 8 1 5 4 0 2 0 4 0
Time cost: 1.0549333095550537s
p3:
Result: 9521
Status of facilities: 1 1 1 1 1 1 1 0 1 0
The assignment of customers to facilities: 8 2 1 6 3 8 2 4 4 1 4 0 3 2 8 3 2 0 6 4 3 4 6 4 2 5 1 5 0 5 2 6 0 3 4 4 4 3 0 4 1 8 1 5 4 0 2 0 4 0
Time cost: 1.0146305561065674s
p4:
Result: 10852
Status of facilities: 1 1 1 1 1 0 1 0 1 0
The assignment of customers to facilities: 8 2 1 6 3 8 2 4 4 1 4 0 3 2 8 3 2 0 6 4 3 4 6 4 2 6 3 1 0 3 2 6 0 3 4 4 4 3 0 4 1 8 1 6 4 0 2 0 4 0
Time cost: 1.1778690814971924s
p5:
Result: 8993
Status of facilities: 1 1 1 1 1 0 1 1 1 1
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 2 1 9 8 3 2 8 3 4 0 9 7 3 8 6 4 2 6 1 1 0 1 2 6 0 3 9 4 4 3 0 4 1 8 1 6 7 0 2 3 4 0
Time cost: 1.0822694301605225s
p6:
Result: 7855
Status of facilities: 1 1 1 1 1 1 1 1 1 1
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 4 1 9 8 3 2 8 3 4 0 9 7 3 8 6 4 2 5 1 5 0 5 2 6 0 3 9 4 4 3 0 4 1 8 1 5 7 0 2 3 7 0
Time cost: 1.0116400718688965s
p7:
Result: 9601
Status of facilities: 1 1 1 1 1 0 1 0 1 1
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 4 1 9 8 3 2 8 3 2 0 9 4 3 8 6 4 2 6 1 1 0 1 9 6 0 3 9 9 4 3 0 4 1 8 1 6 4 0 2 3 4 0
Time cost: 1.0775701999664307s
p8:
Result: 11499
Status of facilities: 1 1 1 1 1 1 1 1 1 0
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 4 1 4 8 3 2 8 3 4 0 6 7 3 8 6 4 2 5 1 5 0 5 2 6 0 3 4 4 7 3 0 4 1 8 1 5 7 0 2 3 7 0
Time cost: 1.1194796562194824s
p9:
Result: 8576
Status of facilities: 1 1 1 1 1 0 1 0 1 1
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 4 1 9 8 3 2 8 3 4 0 9 4 3 8 6 4 2 6 1 1 0 1 2 6 0 3 9 4 4 3 0 4 1 8 1 6 4 0 2 0 4 0
Time cost: 0.932659387588501s
p10:
Result: 7648
Status of facilities: 1 1 1 1 1 0 1 1 1 1
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 4 1 9 8 3 2 8 3 4 0 9 7 3 8 6 4 2 6 1 1 0 1 2 6 0 3 9 4 4 3 0 4 1 8 1 6 7 0 4 0 4 0
Time cost: 0.9273645877838135s
p11:
Result: 8967
Status of facilities: 1 1 1 0 1 0 1 0 1 0
The assignment of customers to facilities: 8 8 1 6 1 8 2 4 2 1 4 8 0 2 8 0 4 0 6 4 1 8 6 4 2 6 1 1 0 1 2 6 0 2 4 4 4 0 0 4 1 8 1 6 4 0 2 0 4 0
Time cost: 0.9326126575469971s
p12:
Result: 10427
Status of facilities: 1 1 1 1 1 0 1 0 1 0
The assignment of customers to facilities: 8 8 1 6 3 8 2 4 4 1 4 8 3 2 8 3 2 0 6 4 3 8 6 4 2 6 1 1 0 1 2 6 0 3 4 4 4 3 0 4 1 8 1 6 4 0 2 0 4 0
Time cost: 0.94134521484375s
p13:
Result: 8365
Status of facilities: 0 1 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1 1 0 1
The assignment of customers to facilities: 16 1 10 10 1 13 17 13 14 17 15 19 1 10 13 14 15 10 17 9 14 15 15 19 9 1 16 17 1 17 13 15 13 16 1 10 17 19 19 13 9 16 15 16 9 13 14 10 19 9
Time cost: 0.9643852710723877s
p14:
Result: 7137
Status of facilities: 0 0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 0 1
The assignment of customers to facilities: 16 3 10 10 19 13 17 13 14 17 15 19 19 10 13 3 15 10 17 9 14 15 15 19 9 17 16 17 3 17 13 15 13 16 3 10 3 19 19 13 9 16 15 16 3 13 14 10 19 9
Time cost: 0.9760012626647949s
p15:
Result: 8808
Status of facilities: 0 0 0 1 0 0 0 0 0 1 1 0 0 1 0 1 1 1 0 1
The assignment of customers to facilities: 16 3 10 10 19 13 17 13 3 17 15 19 19 10 13 3 15 10 17 9 15 15 15 19 9 17 16 17 3 17 13 15 13 16 3 10 3 19 19 13 9 16 16 16 3 13 15 10 19 9
Time cost: 0.9880332946777344s
p16:
Result: 10730
Status of facilities: 1 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 1
The assignment of customers to facilities: 0 3 10 10 19 13 17 13 3 17 7 19 19 6 13 3 7 10 17 6 7 7 7 19 6 17 0 17 3 17 13 7 13 7 3 10 3 19 19 13 6 0 0 0 3 13 7 10 19 6
Time cost: 0.9731912612915039s
p17:
Result: 8240
Status of facilities: 1 1 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1
The assignment of customers to facilities: 0 1 10 10 1 15 17 13 14 17 15 19 1 10 13 14 15 10 17 9 14 15 15 19 9 1 0 17 1 17 13 15 13 0 1 10 17 19 19 13 9 0 15 0 9 13 14 10 19 9
Time cost: 1.0709400177001953s
p18:
Result: 7214
Status of facilities: 1 0 0 1 0 0 1 0 0 0 1 0 0 1 1 1 0 1 0 1
The assignment of customers to facilities: 0 3 10 10 19 15 17 13 14 17 15 19 19 6 13 3 15 10 17 6 14 15 15 19 6 17 0 17 3 17 13 15 13 0 3 10 3 19 19 13 6 0 0 15 3 13 14 10 19 6
Time cost: 0.9833917617797852s
p19:
Result: 9015
Status of facilities: 0 0 0 1 0 0 1 0 0 0 1 0 1 1 0 1 1 1 0 1
The assignment of customers to facilities: 16 12 10 10 19 12 17 13 12 17 15 19 19 6 13 3 15 10 17 6 12 15 15 19 6 17 16 17 3 17 13 15 13 16 3 10 3 19 19 13 6 16 15 16 3 13 12 10 19 6
Time cost: 0.9604361057281494s
p20:
Result: 10779
Status of facilities: 1 0 0 1 0 0 1 0 0 0 1 0 0 1 1 1 0 1 0 1
The assignment of customers to facilities: 0 3 10 10 19 15 17 13 14 17 15 19 19 6 13 3 15 10 17 6 14 15 15 19 6 17 0 17 3 17 13 15 13 0 3 10 3 19 19 13 6 0 15 0 3 13 14 10 19 6
Time cost: 0.9773945808410645s
p21:
Result: 8114
Status of facilities: 1 1 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 0 0
The assignment of customers to facilities: 0 1 10 10 1 15 17 13 14 17 15 1 1 10 13 14 15 10 17 9 14 15 15 13 9 17 0 17 1 17 13 15 13 0 1 10 17 1 1 13 10 0 0 0 9 13 14 10 1 9
Time cost: 0.9609119892120361s
p22:
Result: 7120
Status of facilities: 0 0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 0 1
The assignment of customers to facilities: 16 3 10 10 19 15 17 13 14 17 15 19 19 10 13 3 15 10 17 9 14 15 15 19 9 17 16 17 3 17 13 15 13 16 3 10 3 19 19 13 10 16 15 16 3 13 14 10 19 9
Time cost: 0.9359855651855469s
p23:
Result: 8754
Status of facilities: 1 0 0 0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 0 1
The assignment of customers to facilities: 0 12 10 10 19 12 17 13 12 17 15 19 19 10 13 12 15 10 17 9 12 15 15 19 9 17 0 17 12 17 13 15 13 0 12 10 17 19 19 13 10 0 0 0 9 13 12 10 19 9
Time cost: 0.9379396438598633s
p24:
Result: 10547
Status of facilities: 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 0
The assignment of customers to facilities: 16 1 10 10 1 15 17 2 10 17 15 1 1 6 2 1 15 10 17 6 15 15 15 2 6 17 16 17 1 17 2 15 2 16 1 10 17 1 1 2 6 16 16 16 6 15 15 10 2 6
Time cost: 0.9380884170532227s
p25:
Result: 12261
Status of facilities: 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0
The assignment of customers to facilities: 20 11 20 14 20 5 2 24 14 17 24 8 8 20 0 14 14 17 5 11 2 0 5 2 5 8 2 24 5 24 5 25 20 25 20 20 24 24 0 14 0 14 24 8 24 14 0 20 11 8 5 14 14 5 20 20 17 25 11 14 0 5 20 11 11 11 24 0 24 24 20 20 11 17 24 8 24 20 14 24 20 0 20 17 17 17 17 0 20 5 2 24 17 11 0 24 2 14 25 8 8 17 14 25 2 25 0 11 14 8 14 14 2 11 11 25 24 24 24 24 5 8 25 20 8 0 5 24 0 25 0 11 24 5 8 14 0 5 20 17 0 8 20 14 8 25 25 5 2 5
Time cost: 3.189701557159424s
p26:
Result: 11033
Status of facilities: 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0
The assignment of customers to facilities: 20 11 20 14 20 5 14 24 14 17 24 8 8 20 2 14 14 17 5 11 2 0 5 2 5 8 2 24 5 24 5 25 20 25 20 20 24 24 0 14 0 14 24 8 24 14 0 20 11 8 5 14 14 5 20 20 17 11 11 14 0 5 20 11 11 11 24 0 5 24 20 20 11 17 24 8 24 20 14 24 20 0 20 17 2 17 17 0 20 5 2 24 0 11 0 5 2 14 25 20 8 17 17 25 2 25 0 11 14 8 14 14 2 11 11 25 24 24 24 24 5 8 25 20 8 0 5 24 0 25 0 11 24 5 8 14 0 5 20 17 0 8 20 14 8 25 25 5 2 5
Time cost: 3.176814556121826s
p27:
Result: 13041
Status of facilities: 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0
The assignment of customers to facilities: 20 11 20 14 20 5 2 24 14 17 24 8 8 20 0 14 14 17 5 11 14 0 5 2 5 8 2 24 5 24 5 25 20 25 20 20 24 24 0 14 0 14 24 8 24 14 0 20 11 8 5 14 14 5 20 20 17 11 0 14 0 5 20 11 11 11 24 0 5 24 20 20 11 0 24 8 24 20 14 24 20 0 20 17 17 17 17 0 20 5 2 24 17 11 0 5 2 2 25 20 8 17 17 25 2 25 0 11 14 8 14 14 2 11 11 25 24 24 24 24 5 8 25 20 8 0 5 24 0 25 0 11 24 5 8 14 0 5 20 17 0 8 20 14 8 25 25 5 2 5
Time cost: 3.2794647216796875s
p28:
Result: 15033
Status of facilities: 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0
The assignment of customers to facilities: 20 11 20 14 20 5 2 24 14 17 24 8 8 20 0 14 14 17 5 11 14 0 5 2 5 8 2 24 5 24 5 25 20 25 20 20 24 24 0 14 0 14 24 8 24 14 0 20 11 8 5 14 14 5 20 20 17 11 0 14 0 5 20 11 11 11 24 0 24 24 20 20 11 17 24 8 24 20 14 24 20 0 20 17 2 17 17 0 20 5 2 24 0 11 0 5 2 14 25 20 8 17 17 25 2 25 0 11 14 8 14 14 2 11 11 11 24 24 24 24 5 8 25 20 8 0 5 24 0 25 0 11 24 5 8 14 0 5 20 17 0 8 20 14 8 25 25 5 2 5
Time cost: 3.2464394569396973s
p29:
Result: 12790
Status of facilities: 1 0 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 0 1 0 0 0 1 1 0 0 0 0
The assignment of customers to facilities: 20 11 20 2 20 5 2 24 14 17 5 8 8 20 17 14 14 17 5 11 2 0 5 17 5 8 2 24 5 15 25 25 6 25 20 20 24 15 0 14 0 14 15 14 24 14 0 20 11 8 25 14 14 5 6 20 17 11 11 14 0 5 20 11 11 11 24 0 5 24 20 6 11 17 24 8 24 20 2 24 20 0 8 17 2 17 17 0 20 5 2 24 17 11 17 24 2 2 25 8 8 17 17 25 2 25 17 11 14 8 14 14 2 11 11 25 24 15 24 24 5 8 25 20 8 0 5 15 0 25 0 11 24 25 8 14 0 5 8 17 0 8 20 14 8 25 25 5 2 5
Time cost: 3.2839627265930176s
p30:
Result: 11565
Status of facilities: 1 0 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0
The assignment of customers to facilities: 20 11 13 2 13 5 2 24 14 17 25 8 8 20 2 14 14 17 5 11 2 0 24 17 25 8 2 24 25 25 5 25 13 25 20 20 24 5 0 14 0 14 24 8 24 14 0 13 11 8 25 2 14 5 13 20 17 11 0 14 0 5 20 11 11 11 24 0 5 25 20 13 11 17 24 8 24 20 14 24 20 0 20 17 17 17 17 0 20 5 2 24 17 11 17 5 2 2 25 13 8 17 17 25 2 25 0 11 14