1 问题定义
二次分配问题(Quadratic Assignment Problem,QAP)是一种组合优化问题,涉及确定将设施分配到位置的最优方案。它的目标是找到最佳分配,以最小化设施对之间的总成本或距离,考虑到它们之间的相互作用以及对应位置之间的距离。
定义:
目标函数:
约束条件:
2 读取文件
#ifndef INPUT_H
#define INPUT_H
#include
#include
class Input
{
public:
Input(const std::string& filename);
///
/// 读取文件内容
///
void read();
///
/// 返回文件名
///
///
std::string getFilename() const;
///
/// 数据的大小
///
///
int getDimension() const;
///
/// 返回距离矩阵
///
///
std::vector< std::vector > getDistances() const;
///
/// 返回位置矩阵
///
///
std::vector< std::vector > getFlow() const;
private:
//文件名
std::string filename_;
//维度
int dimension_;
//距离矩阵
std::vector< std::vector > distances_;
//位置矩阵
std::vector< std::vector > flow_;
};
#endif
3 解决方案的父类设计
#ifndef QAP_SOLVER_H
#define QAP_SOLVER_H
#include
#include
class QAPSolver {
public:
QAPSolver(const std::vector>& flowMatrix_, const std::vector>& distanceMatrix_);
virtual ~QAPSolver() {}
// 解决QAP的纯虚函数
virtual void execut() = 0;
// 获取解决后的最优排列
virtual std::vector getBestSolution() const;
// 获取解决后的目标函数值
virtual int getCost() const;
// 计算成本
int claculCost(std::vector& solution);
protected:
//解决方案
std::vector bestSolution;
//成本
int cost;
// 大小
int size;
//距离矩阵
std::vector< std::vector > distanceMatrix;
//位置矩阵
std::vector< std::vector > flowMatrix;
};
#endif // !QAP_SOLVER_H
3 贪心算法
贪心算法的思想是基于局部最优选择来构建整体最优解。具体来说,在解决QAP的贪心算法中,我们按照一定的规则选择变量的排列顺序,以使得每次选择都是当前局部最优的。贪心算法的每一步都基于当前最佳选择,而不考虑整体的最优解。
在解决QAP的贪心算法中,我们首先生成一个初始的排列,然后根据特定的规则,逐个选择未安排的变量并插入到已安排的变量中,以便最小化目标函数的值。在每次选择时,我们计算当前变量与已安排变量之间的成本,并选择使成本最低的变量进行插入。
#ifndef GREEDY_H
#define GREEDY_H
#include "QAPSolver.h"
class greedy : public QAPSolver
{
public:
greedy(const std::vector>& flowMatrix_, const std::vector>& distanceMatrix_) : QAPSolver(flowMatrix_, distanceMatrix_) {};
void execut() override;
};
#endif // !GREEDY_H
#include "greedy.h"
void greedy::execut()
{
//初始化排列
std::vector permutation(size);
for (int i = 0; i < size; ++i) {
permutation[i] = i;
}
std::sort(permutation.begin(), permutation.end(), [&](int a, int b) {
int aSum = 0, bSum = 0;
for (int j = 0; j < size; ++j) {
aSum += flowMatrix[a][j] * distanceMatrix[permutation[j]][permutation[a]];
bSum += flowMatrix[b][j] * distanceMatrix[permutation[j]][permutation[b]];
}
return aSum < bSum;
});
bestSolution = permutation;
}
4 运行结果
#include
#include
#include "Input.h"
#include "greedy.h"
using namespace std;
int main(int argc, char* argv[])
{
string filename = "";
Input input(filename);
unique_ptr pGreedy = make_unique(input.getDistances(),input.getDistances());
pGreedy->execut();
cout << "Greedy Search: " << endl;
cout << "\tCost: " << pGreedy->getCost() << endl;
cout << "\tSolution: ";
vector solution = pGreedy->getBestSolution();
for (int i = 0; i < solution.size(); ++i)
{
cout << solution[i] << " ";
} cout << endl;
cout << endl;
}
运行结果