【问题标题】:Dynamic programming algorithm for facility locations设施位置的动态规划算法
【发布时间】:2015-10-16 18:49:51
【问题描述】:

在一条线上的位置 a_1、a_2、...、a_n 有 n 座房屋。我们想沿着同一条线设置门便盆,以便每所房子都在至少一个门便盆的距离 R 内。这些入口便盆仅限于指定位置 b_1、b_2、...、b_m。设 c_i 为在位置 b_i 设置一个便盆的成本。

找到一种动态规划算法,以最大限度地降低设置门便盆的总成本。该算法应该能够检测是否存在解决方案。假设所有 a 和 b 值都是不同的。

输入:

  • A[1, 2,...n] 保存房屋位置

  • B[1, 2,...m] 包含潜在的便盆位置

  • C[1, 2,...m] 包含在每个地方设置一个便盆的成本 位置

输出:在每个房子必须在某个门厕的距离 R 范围内的约束下,放置便盆的最低成本

我无法找出一个递归表达式来解决。任何帮助,将不胜感激!

【问题讨论】:

    标签: algorithm dynamic-programming


    【解决方案1】:

    您的问题让我有机会为类似的问题编写一些代码,该问题通常表现为手机发射塔放置问题手机基站覆盖问题

    伪代码如下:

    1) Sort houses in ascending order
    2) Sort facilities positions and their costs in ascending order by facilities positions
    3) Let dp(i) be the minimum cost to cover i houses and lastBase(j) the last base used to cover j houses
    4) Set the base case dp(0) = 0 and lastBase(0) = -1
    5) For each house i:
    6)   Check if previous solution is either valid or in range of this new house
    7)   if it is -> grab it as solution
    8)   else
    9)     find a new base starting from lastBase(i) + 1 which can cover this house
    10)    let it be the minimum-cost one
    11)  if a base could be found -> add it to the previous solution
    12)  else -> Problem cannot be solved
    

    我建议自己先尝试一下。

    为了完整起见:解释、图像和 C++ 代码are available here

    欢迎反馈或错误。

    【讨论】:

      【解决方案2】:

      我将告诉您如何进行,如何编写代码由您决定。
      给定A,B,C(还假设AB中的所有元素都在数轴上) -

      -> Sort A in ascending order.
      
      -> Sort B and C together(as they are dependent) based on B's values in ascending order. 
      
      -> Maintain a temp array(size n) which keeps track of which "porta potty" 
        an A element belongs to,by mapping to the "porta potty" index.
      
      -> Now take each element from B and move both forward and backward R steps from that 
         point on the number line.
      
      -> If any A element is found in those R steps(on the number line) 
         AND if(and only if) it does not presently belong to any "porta potty" OR 
         the cost of setting up the current "porta potty" element is more than the "porta potty" 
         it(A element) already belongs to, then only shall you set the value in temp array 
         for that A element to the current "porta potty" index number.
      
      -> Now once we are done with all B points, do the following
      
      -> Traverse the temp array and push the "porta potty" index numbers we have into a set
      
      -> You now have a list of all the "porta potty" indices which are the cheapest
         yet crucial to be placed.
      

      想一想,如果您有不清楚的地方,请告诉我。排序部分也只是为了提高性能。

      【讨论】:

        【解决方案3】:

        这个是针对手机信号塔放置问题的。我猜你的应该是相似的。

        【讨论】:

          【解决方案4】:

          应该有一个递归。这是 Python 中的一个注释示例,它假设输入是有序的:

          a = [1, 7,11,13,15]
          b = [1,8,9,12,13]
          c = [1,3,2, 2, 5]
          r = 3
          
          na = len(a)
          nb = len(b)
          
          def f (ia,ib,prev_ib,s):
            # base case no suitable potties
            if ib == nb:
              return 1000  # a number larger than sum of all costs
          
            # base case end of row of houses
            if ia == na:
              return s
          
            # house is in range of last potty
            if prev_ib >= 0 and abs(a[ia] - b[prev_ib]) < r:
              return f(ia + 1,ib,prev_ib,s)
          
            # house is too far
            if abs(a[ia] - b[ib]) >= r:
              # house is west of potty
              if a[ia] < b[ib]:
                return 1000
              # house is east of potty
              else:
                return f(ia,ib + 1,prev_ib,s)
          
            # house is in range of current potty
            else: 
              # choose or skip
              return min(f(ia + 1,ib + 1 if ib < nb - 1 else ib,ib,s + c[ib]),f(ia,ib + 1,prev_ib,s))
          

          输出:

          print f(0,0,-1,0) # 8
          

          【讨论】:

            猜你喜欢
            • 2012-03-26
            • 2013-11-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-28
            相关资源
            最近更新 更多