算法概论 project | Capacitated Facility Location Problem —— 贪心算法、模拟退火、禁忌搜索

代码及结果

python代码:anneal.py

贪心算法:

模拟退火:

禁忌搜索:


目录

题目描述

题意分析

样例格式

贪心算法

  • python代码(部分)
  • Detailed solution (部分)

模拟退火

  • python代码(部分)
  • Detailed solution (部分)

禁忌搜索

  • python代码(部分)
  • Detailed solution (部分)

题目描述

Suppose there are n facilities and m customers. We wish to choose:

  1. which of the n facilities to open
  2. 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.


题意分析

可以这样理解:

在这里插入图片描述

有n个facility, 每个facility具有一个capacity,如果开启这个facility,那么就会产生一个open cost。

有m个customer, 每个customer具有一个demond,customer可以自由选择去哪个facility, 但是选择这个facility的所有customer的demond之和不能大于它的capacity。每当customer选定一个facility,就会产生一个assign cost。

题目还有个隐含条件:所有customer的demond都要满足。

因此,总的cost可以表示为:所有开启了的facility的open cost之和 + 所有customer选择facility所产生的assign cost之和

我们的目标是令总的cost最小。


样例格式

在这里插入图片描述

在这里插入图片描述

例如:
在这里插入图片描述


1 贪心算法

我们可以让customer按顺序选择facility,当一个customer在选择时,它可以从所有facility中选择最优惠的那个。那么什么样的facility才是最优惠的呢?我目前想到三种方案:

  • assign cost 最小的
  • assign cost + open cost 最小的
  • assign cost (如果该facility未开启,+ open cost) 最小的

实践证明,第一种方案得到的cost最小。第二种方案由于未考虑facility已开启的情况,导致错过了更优惠的facility,而第三种方案,由于过于在意facility是否开启,最终更倾向于已开启的facility,导致错过了很多开启后可能更优惠的facility。


1.1 python代码(部分)

def greedy():
    global total_cost
    // 对于每个customer
    for i in range(len(assign[0]))
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值