📜  使用分支定界法的作业分配问题

📅  最后修改于: 2021-05-06 17:29:22             🧑  作者: Mango

让有N个工人和N个工作。可以指派任何工人执行任何工作,所产生的费用可能会根据工作岗位的分配而有所不同。需要执行所有工作,方法是将每个工作恰好分配给一个工人,将每个代理恰好分配给一个工作,以使分配的总成本最小化。

工作分配

让我们探索解决此问题的所有方法。

解决方案1:蛮力
我们产生n!可能的工作分配,对于每个这样的分配,我们计算其总成本并返回较便宜的分配。由于解决方案是n个工作的排列,因此其复杂度为O(n!)。

解决方案2:匈牙利算法
最佳分配可以使用匈牙利算法找到。匈牙利算法的最坏情况运行时复杂度为O(n ^ 3)。

解决方案3:状态空间树上的DFS / BFS
状态空间树是具有属性的N元树,从根到叶节点的任何路径都具有针对给定问题的许多解决方案之一。我们可以在状态空间树上执行深度优先搜索,但是连续的移动可以使我们远离目标,而不是靠近目标。无论初始状态如何,对状态空间树的搜索都遵循从根到最左边的路径。在这种方法中可能永远找不到答案节点。我们还可以在状态空间树上执行广度优先搜索。但是,无论初始状态是什么,该算法都会尝试像DFS一样的顺序移动。

解决方案4:使用分支定界法找到最优解
BFS和DFS中下一个节点的选择规则是“盲”。也就是说,选择规则不会给具有极好的机会使搜索迅速到达答案节点的节点任何偏爱。通常,可以通过使用“智能”排名函数(也称为近似成本函数)来加快寻找最佳解决方案的速度,以避免在不包含最佳解决方案的子树中进行搜索。它类似于类似BFS的搜索,但有一项重大优化。代替遵循FIFO顺序,我们选择成本最低的活动节点。我们可能无法通过以最低有希望的成本跟踪节点来获得最佳解决方案,但是它将为迅速将搜索移至答案节点提供很好的机会。

有两种计算成本函数:

  1. 对于每个工人,我们从未分配的工作列表中选择成本最低的工作(从每一行中获取最少的条目)。
  2. 对于每项工作,我们从未分配的工作人员列表中选择成本最低的工作人员(从每列中获取最少的条目)。

在本文中,遵循第一种方法。

让我们以下面的示例为例,并尝试计算将作业2分配给工人A时的有前途的成本。
工作分配2
由于作业2被分配给工人A(标记为绿色),因此成本变为2,作业2和工人A变为不可用(标记为红色)。
工作分配3
现在,我们将作业3分配给工人B,因为它在未分配作业列表中的成本最低。成本变为2 + 3 = 5,并且作业3和工人B也变得不可用。
工作分配4
最后,作业1被分配给工人C,因为它在未分配的作业中具有最低的成本,而作业4被分配给工人C,因为它仅剩下作业。总费用变为2 + 3 + 5 + 4 = 14。
工作分配5

下图显示了完整的搜索空间图,以绿色显示了最佳解决方案路径。
工作分配6

完整算法:

/* findMinCost uses Least() and Add() to maintain the
   list of live nodes

   Least() finds a live node with least cost, deletes
   it from the list and returns it

   Add(x) calculates cost of x and adds it to the list
   of live nodes

   Implements list of live nodes as a min heap */


// Search Space Tree Node
node
{
   int job_number;
   int worker_number;
   node parent;
   int cost;
}

// Input: Cost Matrix of Job Assignment problem
// Output: Optimal cost and Assignment of Jobs
algorithm findMinCost (costMatrix mat[][])
{
   // Initialize list of live nodes(min-Heap)
   // with root of search tree i.e. a Dummy node
   while (true)
   {
      // Find a live node with least estimated cost
      E = Least();

      // The found node is deleted from the list
      // of live nodes
      if (E is a leaf node)
      {
         printSolution();
         return;
      }

     for each child x of E
     {
         Add(x); // Add x to list of live nodes;
         x->parent = E; // Pointer for path to root
     }
   }
} 

以下是其C++实现。

// Program to solve Job Assignment problem
// using Branch and Bound
#include 
using namespace std;
#define N 4
  
// state space tree node
struct Node
{
    // stores parent node of current node
    // helps in tracing path when answer is found
    Node* parent;
  
    // contains cost for ancestors nodes
    // including current node
    int pathCost;
  
    // contains least promising cost
    int cost;
  
    // contain worker number
    int workerID;
  
    // contains Job ID
    int jobID;
  
    // Boolean array assigned will contains
    // info about available jobs
    bool assigned[N];
};
  
// Function to allocate a new search tree node
// Here Person x is assigned to job y
Node* newNode(int x, int y, bool assigned[],
              Node* parent)
{
    Node* node = new Node;
  
    for (int j = 0; j < N; j++)
        node->assigned[j] = assigned[j];
    node->assigned[y] = true;
  
    node->parent = parent;
    node->workerID = x;
    node->jobID = y;
  
    return node;
}
  
// Function to calculate the least promising cost
// of node after worker x is assigned to job y.
int calculateCost(int costMatrix[N][N], int x,
                  int y, bool assigned[])
{
    int cost = 0;
  
    // to store unavailable jobs
    bool available[N] = {true};
  
    // start from next worker
    for (int i = x + 1; i < N; i++)
    {
        int min = INT_MAX, minIndex = -1;
  
        // do for each job
        for (int j = 0; j < N; j++)
        {
            // if job is unassigned
            if (!assigned[j] && available[j] &&
                costMatrix[i][j] < min)
            {
                // store job number
                minIndex = j;
  
                // store cost
                min = costMatrix[i][j];
            }
        }
  
        // add cost of next worker
        cost += min;
  
        // job becomes unavailable
        available[minIndex] = false;
    }
  
    return cost;
}
  
// Comparison object to be used to order the heap
struct comp
{
    bool operator()(const Node* lhs,
                   const Node* rhs) const
    {
        return lhs->cost > rhs->cost;
    }
};
  
// print Assignments
void printAssignments(Node *min)
{
    if(min->parent==NULL)
        return;
  
    printAssignments(min->parent);
    cout << "Assign Worker " << char(min->workerID + 'A')
         << " to Job " << min->jobID << endl;
  
}
  
// Finds minimum cost using Branch and Bound.
int findMinCost(int costMatrix[N][N])
{
    // Create a priority queue to store live nodes of
    // search tree;
    priority_queue, comp> pq;
  
    // initailize heap to dummy node with cost 0
    bool assigned[N] = {false};
    Node* root = newNode(-1, -1, assigned, NULL);
    root->pathCost = root->cost = 0;
    root->workerID = -1;
  
    // Add dummy node to list of live nodes;
    pq.push(root);
  
    // Finds a live node with least cost,
    // add its childrens to list of live nodes and
    // finally deletes it from the list.
    while (!pq.empty())
    {
      // Find a live node with least estimated cost
      Node* min = pq.top();
  
      // The found node is deleted from the list of
      // live nodes
      pq.pop();
  
      // i stores next worker
      int i = min->workerID + 1;
  
      // if all workers are assigned a job
      if (i == N)
      {
          printAssignments(min);
          return min->cost;
      }
  
      // do for each job
      for (int j = 0; j < N; j++)
      {
        // If unassigned
        if (!min->assigned[j])
        {
          // create a new tree node
          Node* child = newNode(i, j, min->assigned, min);
  
          // cost for ancestors nodes including current node
          child->pathCost = min->pathCost + costMatrix[i][j];
  
          // calculate its lower bound
          child->cost = child->pathCost +
            calculateCost(costMatrix, i, j, child->assigned);
  
          // Add child to list of live nodes;
          pq.push(child);
        }
      }
    }
}
  
// Driver code
int main()
{
    // x-cordinate represents a Worker
    // y-cordinate represents a Job
    int costMatrix[N][N] =
    {
        {9, 2, 7, 8},
        {6, 4, 3, 7},
        {5, 8, 1, 8},
        {7, 6, 9, 4}
    };
  
  
    /* int costMatrix[N][N] =
    {
        {82, 83, 69, 92},
        {77, 37, 49, 92},
        {11, 69,  5, 86},
        { 8,  9, 98, 23}
    };
    */
  
    /* int costMatrix[N][N] =
    {
        {2500, 4000, 3500},
        {4000, 6000, 3500},
        {2000, 4000, 2500}
    };*/
  
    /*int costMatrix[N][N] =
    {
        {90, 75, 75, 80},
        {30, 85, 55, 65},
        {125, 95, 90, 105},
        {45, 110, 95, 115}
    };*/
  
  
    cout << "\nOptimal Cost is "
        << findMinCost(costMatrix);
  
    return 0;
}

输出 :

Assign Worker A to Job 1
Assign Worker B to Job 0
Assign Worker C to Job 2
Assign Worker D to Job 3

Optimal Cost is 13

参考 :
www.cs.umsl.edu/~sanjiv/classes/cs5130/lectures/bb.pdf