Capacitated Facility Location Problem
文章目录Capacitated Facility Location Problem[更多技术博客 http://vilins.top/](http://vilins.top/)题目分析实验工具解决算法模拟退火法禁忌搜索算法局部设计细节模拟退火法禁忌搜索结果整理两种做法的对比原始输出结果程序运行说明源码github[更多技术博客 http://vilins.top/](http://vilins.top/)Capacitated Facility Location Problem更多技术博客 http://vilins.top/题目要求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.输入格式结果要求分析选址问题在生产生活,物流,甚至军事中都有这非常广泛的应用,以成为运筹学中经典的问题之一,其目的是确定一个或者多个设施的地址,使得判断标准获得优化的同时,及时向顾客提供其所需的产品和服务。工厂选址问题不仅影响到工厂的利润和市场竞争力,甚至决定了工厂的生死存亡。所以,工厂选址的研究具有重要意义。这类问题在论文中出现得比较多,而网上现成的以实现的办法几乎没有,很多都是从理论层面上分析解决这类问题的可行性,而没有实操去证明其可行性。由于这类问题属于优化类问题,在线性时间很难求得其最优解,所以我们选择算法的时候一般选择优化类算法,动态规划之类的算法是行不通的,一般用于解决这类问题的办法有蚁群算法,遗传算法,贪婪算法,模拟退火法,禁忌搜索算法,近似算法等。具体到这个题目,我们首先分析其限制的条件,这是一类有容量限制的工厂问题,具体限制如下:每个工厂开放有一定的花费opening cost。有一定的顾客数量,顾客对工厂具有需求,也就是demand,任何工厂都可以向顾客提供需求,但这里的限制是每个顾客的需求只需一个工厂来提供,这也是对问题的简化。每个工厂具有一定的容量限制,工厂向顾客提供的需求不能超过该工厂的容量限制。每个顾客到工厂有一定的路程,称为assignment cost。花费要尽可能少总花费(total cost) = 工厂开放花费(opening cost) + 顾客分配到某个工厂以后的路程花费(assginment cost)实验工具语言:python工具:Pycharm解决算法模拟退火法算法框架细节实现模拟退火法的设计有几个关键的因素:初始解这里模拟退火法目的就是为了跳出局部最优解的,所以初始解重要,但又没有那么重要,初始解设计得好的话,模拟退火法可以很快得到收敛,使用的时间相对较短一点;但初始解很差,模拟退火法求得的解也没什么差距,只不过需要的时间需要的长一些。这里我尝试过几种不同的初始化的方法,包括一下几种,但测试后发现,加入贪婪因素后求解的效率表现得很差,反而最后两种初始化的效果不错,而最后一种表现得最好,个人猜测是这个解的形式与理论上最优解类似。用全贪婪获得初始解用部分贪婪获得初始解(百分比)简单的填满前面几个工厂完全随机化选择部分工厂开放,但不让所有的工厂开放新解的产生新解的产生设计到我们搜索范围的大小,这里有两个关键因素,一个是重复产生新解的次数,一个是新解产生的方法;有足够多的次数来产生邻居,那么我们找到邻居的最优解的概率就越高,这里设置为200次就可以了;而产生邻居的方式要根据具体问题而定,这个问题我采取随机产生一个顾客和工厂的下标,将该顾客分配给该工厂,有一下情况发生:工厂没有开放的情况并且工厂的容量满足顾客需求,将该顾客分配给该工厂。如果工厂已经开放,并且其剩下的容量足够提供顾客的需求,那么将顾客分配给该工厂。如果工厂容量不足以提供顾客需求,重复生成随机数操作,直到找到一个新解为止。初温初温的设置与模拟退火跳出局部有关系,在温度较高的时候,解跳出局部最优的可能性就越大,所以初温应该有一定的高度,让解跳出局部,这里设置的初温是120度。降温速率降温速率与邻居的效率有关,降温幅度底,那么找到邻居最优解的邻居解的产生可能性就越大,参考降温速率为0.95-0.99。终止温度终止温度与最终找到的最优解有关,温度足够低,那么当前解接受差解的概率就会很低,终止温度底,那么保证最终获得的解是一个不错的解。禁忌搜索算法框架细节实现同样,禁忌搜索也有几个关键的因素初始解这里的初始解的产生很模拟退火采用的是同一种初始化的方法,效果表现得还不错,具体描述见上。重复次数这里重复的次数是保证在足够多的解空间中对结果进行搜索,实验证明,这里我们需要对解空间的大小以及搜索需要的时间进行权衡;解空间大的时候,搜索需要的时间也必然会变得长一些,找出的解也可能会好一些;而解空间小,搜索的花费没有那么大,找出的最优解可能没有那么好,权衡之下,重复20000次是比较好的选择。新解的产生新解产生根模拟退火新解产生一致,具体见上。禁忌表的设计原理的示意图如下禁忌表的引入可以有效避免环的产生,避免那些冗余得搜索,根据这样的原理,那么很自然要设计的一个参数就是在几步以内不可以搜索同样得解呢?也就是在几步以内这个解要保存在禁忌表中,当有同样的解在禁忌表发现的时候,我们直接跳过这个解,不去搜索它。当搜索了足够长的步数以后再将这个解从禁忌表中移除,那么这个禁忌表的长度如何设计呢?有下面两种策略:固定长度20根据解空间而变化可能是由于数据集的适应度比较好,所以固定长度20的时候,算法的表现已经足够好了。当如果根据上述方式来设计的话,会存在一个明显的问题,那就是时间的花费,禁忌搜索本来需要的时间就比较长了,如果中间还要加上搜索禁忌表的花费的话,那么所用的时间复杂度将会变得异常高,对于这样的情况,我们可以采用这样的方法进行优化,这样只需要O(1)的时间就可以查询到该解是否在禁忌表中,当当前维护的count表中对应元素的值的时候,表示我们可以重新搜索这个解,否则这个解在禁忌的状态。算法局部设计细节模拟退火法初始解的产生,采用完全随机化选择部分工厂开放,但不让所有的工厂开放,符合一般最优化表现形式,表现最好。def init_solution(self): for i in range(self.customer_num): self.current_state_of_customer.append(-1) for i in self.demand: self.total_demand += i self.current_capacity = self.capacity.copy() total_cost = 0 temp = [] while total_cost self.total_demand: r = random.randint(0, self.facility_num - 1) if r not in temp: temp.append(r) total_cost += self.capacity[r] index = 0 for i in range(self.customer_num): if self.current_capacity[temp[index]] - self.demand[i] = 0: self.current_capacity[temp[index]] -= self.demand[i] self.current_state_of_customer[i] = temp[index] else: index += 1 i -= 1 # self.current_state_of_customer[i] = index # self.current_capacity[index] -= self.demand[i] self.current_cost = self.calculate_cost(self.current_state_of_customer) self.new_capacity = self.current_capacity.copy() self.new_state_of_customer = self.current_state_of_customer.copy()初始化解,利用局部贪婪,可以调节贪婪的比例,但效果不太好def init_solution(self): for i in range(self.customer_num): self.current_state_of_customer.append(-1) for i in self.demand: self.total_demand += i self.current_capacity = self.capacity.copy() for i in range(self.customer_num): ran_select = random.random() if ran_select 0.2: min = 100000 index = 0 for j in range(self.facility_num): if min self.assignment[j][i]: min = self.assignment[j][i] index = j if self.current_capacity[index] = self.demand[i]: self.current_capacity[index] -= self.demand[i] self.current_state_of_customer[i] = index else: tag = True while tag: ran = random.randint(0, self.facility_num - 1) if self.current_capacity[ran] = self.demand[i]: self.current_capacity[ran] -= self.demand[i] self.current_state_of_customer[i] = ran tag = False else: tag_out = True while tag_out: ran = random.randint(0, self.facility_num - 1) if self.current_capacity[ran] = self.demand[i]: self.current_capacity[ran] -= self.demand[i] self.current_state_of_customer[i] = ran tag_out = False self.current_cost = self.calculate_cost(self.current_state_of_customer) self.new_capacity = self.current_capacity.copy() self.new_state_of_customer = self.current_state_of_customer.copy() print('cost ', self.current_cost) print('current cap ', self.current_capacity) print('current cu ', self.current_state_of_customer)产生新解的方式def gen_new_solution(self): tag = True self.new_capacity = self.current_capacity.copy() self.new_state_of_customer = self.current_state_of_customer.copy() while tag: ran_customer = random.randint(0, self.customer_num - 1) ran_facility = random.randint(0, self.facility_num - 1) # print("ran ", ran_facility, ran_customer) if self.new_capacity[ran_facility] = self.demand[ran_customer]: tag = False self.new_capacity[ran_facility] -= self.demand[ran_customer] origin = self.new_state_of_customer[ran_customer] self.new_state_of_customer[ran_customer] = ran_facility if origin == -1: break self.new_capacity[origin] += self.demand[ran_customer] else: continue退火过程def simulated_annealing(self): global fp_anneal time_start = time.time() while self.T 0.01: i = 0 while i self.repeat: i += 1 self.gen_new_solution() self.current_cost = self.calculate_cost(self.current_state_of_customer) new_cost = self.calculate_cost(self.new_state_of_customer) if new_cost self.current_cost: self.current_capacity = self.new_capacity.copy() self.current_state_of_customer = self.new_state_of_customer.copy() # print("current cost ", new_cost) else: num = math.exp((self.current_cost - new_cost) / self.T) ran = random.random() # print("num ", num) # print("ran ", ran) if num = ran: self.current_capacity = self.new_capacity.copy() self.current_state_of_customer = self.new_state_of_customer.copy() # print("current cost ", new_cost) self.T *= self.cooling_rate time_end = time.time() used_time = time_end - time_start fp_anneal.write('time ' + str(used_time) + '\n') fp_anneal.write('cost ' + str(self.current_cost) + '\n') temp = [] result_state = [] for i in range(self.customer_num): if self.current_state_of_customer[i] not in temp: temp.append(self.new_state_of_customer[i]) temp.sort() for i in range(self.facility_num): result_state.append(0) for i in temp: result_state[i] = 1 # print('cost ' + str(self.current_cost)) # print(result_state) # print(self.current_state_of_customer) # print(self.current_capacity) fp_anneal.write('facility state ' + str(result_state) + '\n') fp_anneal.write('current state of customer ' + str(self.current_state_of_customer) + '\n\n')禁忌搜索新解产生与模拟退火类似邻居的产生def gen_nei_solution(self): tag = True self.nei_capacity = self.current_capacity.copy() self.nei_state_of_customer = self.current_state_of_customer.copy() ran_customer = 0 origin = 0 while tag: ran_customer = random.randint(0, self.customer_num - 1) ran_facility = random.randint(0, self.facility_num - 1) # print("ran ", ran_facility, ran_customer) if self.nei_capacity[ran_facility] = self.demand[ran_customer]: tag = False self.nei_capacity[ran_facility] -= self.demand[ran_customer] origin = self.nei_state_of_customer[ran_customer] self.nei_state_of_customer[ran_customer] = ran_facility if origin == -1: break self.nei_capacity[origin] += self.demand[ran_customer] else: continue return ran_customer, origin禁忌搜索过程def tabu_search(self): global fp_tabu self.nei_state_of_customer = self.current_state_of_customer.copy() self.nei_capacity = self.current_capacity.copy() # print(self.current_state_of_customer) # print(self.nei_capacity) self.tabu_list = [[0 for i in range(self.customer_num)]for i in range(self.facility_num)] # print(tabu_list) repeat = 30000 count = 0 nei_count = int(self.customer_num/4) best_solution_customer = self.current_state_of_customer.copy() best_solution_capacity = self.current_capacity.copy() best_cost = self.calculate_cost(best_solution_customer) time_start = time.time() while count repeat: count += 1 i = nei_count self.gen_new_solution() new_cost = self.calculate_cost(self.new_state_of_customer) ran_customer = 0 origin = 0 while i 1: i -= 1 ran_customer, origin = self.gen_nei_solution() nei_cost = self.calculate_cost(self.nei_state_of_customer) # print("nei ", nei_cost) # print("new ", new_cost) if nei_cost new_cost: self.new_state_of_customer = self.nei_state_of_customer.copy() self.new_capacity = self.nei_capacity.copy() new_cost = nei_cost if new_cost best_cost: # print("new ", new_cost) best_solution_customer = self.new_state_of_customer.copy() best_solution_capacity = self.new_capacity.copy() best_cost = new_cost if self.tabu_list[origin][ran_customer] count: self.tabu_list[origin][ran_customer] = count + 20 self.current_state_of_customer = self.new_state_of_customer.copy() self.current_capacity = self.new_capacity.copy() time_end = time.time() used_time = time_end - time_start temp = [] result_state = [] for i in range(self.customer_num): if best_solution_customer[i] not in temp: temp.append(best_solution_customer[i]) temp.sort() for i in range(self.facility_num): result_state.append(0) for i in temp: result_state[i] = 1 fp_tabu.write('time ' + str(used_time) + '\n') fp_tabu.write('cost ' + str(best_cost) + '\n') fp_tabu.write('facility state ' + str(result_state) + '\n') fp_tabu.write('customer state ' + str(best_solution_customer) + '\n\n') print(best_cost) print(result_state) print(best_solution_customer) print(best_solution_capacity)结果整理fileresulttimeresulttime模拟退火法模拟退火法禁忌搜索禁忌搜索p190335.12332606315612889859.848682165145874p280055.52882313728332579409.718816757202148p393145.13923668861389296789.466161012649536p4116285.307790279388428109549.69756555557251p591315.344730377197266913710.06436276435852p677815.39656233787536679079.97243070602417p795775.525223731994629962510.066269397735596p8114395.5332002639770511143310.113304615020752p985934.83005332946777390318.910385608673096p1076484.93875694274902377249.172668695449829p1192265.14822697639465390628.851669073104858p12110334.917876720428467102778.693775177001953p1393125.169209718704224960410.038275718688965p1471645.242977142333984820410.189182996749878p1589645.441417932510376103359.89920711517334p16119255.371662139892578124969.891655445098877p1792785.45040369033813597959.87352442741394p1877815.39055061340332826510.18008804321289p1992675.236989974975586104089.878618240356445p20117585.060497522354126125939.863529920578003p2192954.85899639129638791039.683191061019897p2277385.12528634071350180889.918416261672974p2396545.08836030960083100709.597949266433716p24112634.70444393157959118009.354219436645508p251228112.8904881477355961358967.44565677642822p261162313.5537416934967041125470.20729756355286p271357413.0281190872192381337766.27394366264343p281572913.245565891265871757267.49193954467773p291351013.50685191154481368372.6292462348938p301189413.906796693801881179772.82930493354797p311482313.6664142608642581441272.44930481910706p321773312.950354576110841670872.29048991203308p331253612.9812731742858891234367.8763542175293p341158213.5367550849914551114269.67934989929199p351448713.4699332714080811309968.77509903907776p361568813.1477956771850591732067.9144344329834p371220012.650130748748781224867.00625324249268p381128313.4859235286712651117067.53133893013p391348013.1727778911590581286265.43386697769165p401568213.0899877548217771556964.73360013961792p4170088.108281373977661790624.470109462738037p4269327.624598264694214631221.45367980003357p4365296.635222673416138673317.159975051879883p4472068.201093912124634819727.290011882781982p4573847.414166450500488748921.240010738372803p4661666.826710224151611720817.667112588882446p4764348.080354928970337724925.64135766029358p4862997.8948752880096436643822.5309157371521p4963296.82571268081665676417.12479257583618p5093138.6319098472595211088029.890727758407593p5179138.980969190597534893931.077434062957764p5293478.536164283752441972633.53241181373596p5399469.4257862567901611114132.7076473236084p5491788.4992961883544921056033.5239634513855p5584659.157528400421143831832.04251146316528p562271321.77771425247192423903144.6936798095703p573358119.87682652473449730914142.13889384269714p585650118.7248744964599646049138.9957413673401p593273820.6597051620483438781140.89498019218445p602320123.38042521476745624435128.45606589317322p613144618.32096123695373529426122.40536570549011p626351816.8770191669464138882126.1437463760376p633202718.30001544952392634958120.8425030708313p642334721.73286342620849624081118.14805221557617p652848317.2887492179870632030115.24073910713196p664080915.93437194824218838021116.89702320098877p672990717.8990929126739529351125.08517384529114p6