算法设计与分析项目-有容量设施选址问题 Capacitated Facility Location Problem(CFLP)
问题描述
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 facilitiesThe objective is to minimize the sum of the opening costand the assignment cost.
3.The total demand assigned to a facility must not exceed its capacity.
数据集结构
注意demand of customer和Assignment cost 都是十个数据一换行,与Facility和customer数量无关。Assignment cost第1第2个数据为:将第1第2个顾客分配到工厂1的花费,而不是将第1个顾客分配到第1第2个工厂的花费,注意顺序。
还要注意有些数据集demand of customer和Assignment cost的数据后面有一个点,这里所有数据都可以视为整型。
项目
项目使用贪心算法和SA模拟退火算法
源代码,数据集和结果上传到github
贪心算法
贪心算法思路很简单,对于每个顾客,寻找能容纳其需求的且Assignment cost最小的工厂。
主要代码:
facility_occpy.resize(facilities.size(), 0);
for (auto &i : facility_occpy) i = 0;
custumer_assign.resize(demands.size());
for (int i = 0; i < demands.size(); i++) {
size_t mincost = std::numeric_limits<size_t>::max();
int min_index;
//寻找能容纳其需求的且Assignment cost最小的工厂
for (int j = 0; j < facilities.size(); j++) {
if (get_cost(j, i) < mincost && demands[i] <= facilities[j].capacity - facility_occpy[j]) {
min_index = j;
mincost = get_cost(j, i);
}
}
facility_occpy[min_index] += demands[i];
custumer_assign[i] = min_index;
}
部分结果
数据集 | 花费 | 时间 | 结果 |
---|---|---|---|
p1 | 9440 | 0.004s | 8 8 1 6 3 8 2 4 4 1 9 0 3 2 0 3 4 0 9 7 3 4 6 4 2 5 1 5 0 5 2 6 0 3 9 4 4 3 0 4 1 8 1 5 7 0 4 0 4 3 |
p3 | 10126 | 0.003s | 8 8 1 6 3 8 2 4 4 1 9 0 3 2 0 3 4 0 9 7 3 4 6 4 2 5 1 5 0 5 2 6 0 3 9 4 4 3 0 4 1 8 1 5 7 0 4 0 4 3 |
p5 | 9375 | 0.003s | 8 8 1 6 3 8 2 4 4 1 9 8 3 |