Cutting algorithm of two dimensional board

2019-03-31 19:41发布

I have problem with my homework.

Given a board of dimensions m x n is given, cut this board into rectangular pieces with the best total price. A matrix gives the price for each possible board size up through the original, uncut board.

Consider a 2 x 2 board with the price matrix:

3 4

3 6

We have a constant cost for each cutting for example 1.
Piece of length 1 x 1 is worth 3.
Horizontal piece of length 1 x 2 is worth 4.
Vertical piece of length 1 x 2 is worth 3.
Whole board is worth 6.

For this example, the optimal profit is 9, because we cut board into 1 x 1 pieces. Each piece is worth 3 and we done a 3 cut, so 4 x 3 - 3 x 1 = 9.

Second example:

1 2

3 4

Now I have to consider all the solutions:

  • 4 1x1 pieces is worth 4x1 - (cost of cutting) 3x1 = 1
  • 2 horizontal 1x2 is worth 2x2 - (cost of cutting) 1x1 = 3
  • 2 vertical 1x2 is worth 3x2 - (cost of cutting) 1x1 = 5 -> best optimal profit
  • 1 horizontal 1x2 + 2 x (1x1) pieces is worth 2 + 2 - (cost of cutting) 2 = 2
  • 1 vertical 1x2 + 2 x (1x1) pieces is worth 3 + 2 - (cost of cutting) 2 = 3

I've read a lot about rod cutting algorithm but I don't have any idea how to bite this problem. Do you have any ideas?

4条回答
Deceive 欺骗
2楼-- · 2019-03-31 20:08

I did this in Python. The algorithm is

  • best_val = value of current board
  • check each horizontal and vertical cut for better value
    • for cut point <= half the current dimension (if none, return initial value)
    • recur on the two boards formed
    • if sum of values > best_val
    • ... best_val = that sum
    • ... record cut point and direction
  • return result: best_val, cut point, and direction

I'm not sure what you'll want for return values; I gave back the best value and tree of boards. For your second example, this is

(5, [[2, 1], [2, 1]])

Code, with debugging traces (indent and the labeled prints):

indent = ""
indent_len = 3

value = [[1, 2],
         [3, 4]
        ]

def best_cut(high, wide):
    global indent
    print indent, "ENTER", high, wide
    indent += " " * indent_len

    best_val = value[high-1][wide-1]
    print indent, "Default", best_val
    cut_vert = None
    cut_val = best_val
    cut_list = []

    # Check horizontal cuts
    for h_cut in range(1, 1 + high // 2):
        print indent, "H_CUT", h_cut
        cut_val1, cut_list1 = best_cut(h_cut, wide)
        cut_val2, cut_list2 = best_cut(high - h_cut, wide)
        cut_val = cut_val1 + cut_val2

        if cut_val > best_val:
            cut_list = [cut_list1, cut_list2]
            print indent, "NEW H", h_cut, cut_val, cut_list
            best_val = cut_val
            cut_vert = False
            best_h = h_cut

    # Check vertical cuts
    for v_cut in range(1, 1 + wide // 2):
        print indent, "V_CUT", v_cut
        cut_val1, cut_list1 = best_cut(high, v_cut)
        cut_val2, cut_list2 = best_cut(high, wide - v_cut)
        cut_val = cut_val1 + cut_val2

        if cut_val > best_val:
            cut_list = [cut_list1, cut_list2]
            print indent, "NEW V", v_cut, cut_val, cut_list
            best_val = cut_val
            cut_vert = True
            best_v = v_cut

    # Return result of best cut
    # Remember to subtract the cut cost
    if cut_vert is None:
        result = best_val  , [high, wide]
    elif cut_vert:
        result = best_val-1, cut_list
    else:
        result = best_val-1, cut_list

    indent = indent[indent_len:]
    print indent, "LEAVE", cut_vert, result
    return result

print best_cut(2, 2)

Output (profit and cut sizes) for each of the two tests:

(9, [[[1, 1], [1, 1]], [[1, 1], [1, 1]]])

(5, [[2, 1], [2, 1]])
查看更多
smile是对你的礼貌
3楼-- · 2019-03-31 20:13

Let f(h,w) represent the best total price achievable for a board with height h and width w with cutting price c. Then

f(h,w) = max(
  price_matrix(h, w),
  f(i, w) + f(h - i, w) - c,
  f(h, j) + f(h, w - j) - c
)
for i = 1 to floor(h / 2)
for j = 1 to floor(w / 2)

Here's a bottom-up example in JavaScript that returns the filled table given the price matrix. The answer would be in the bottom right corner.

function f(prices, cost){
  var m = new Array(prices.length);

  for (let i=0; i<prices.length; i++)
    m[i] = [];

  for (let h=0; h<prices.length; h++){
    for (let w=0; w<prices[0].length; w++){

      m[h][w] = prices[h][w];

      if (h == 0 && w == 0)
        continue;

      for (let i=1; i<(h+1>>1)+1; i++)
        m[h][w] = Math.max(
          m[h][w],
          m[i-1][w] + m[h-i][w] - cost
        );

      for (let i=1; i<(w+1>>1)+1; i++)
        m[h][w] = Math.max(
          m[h][w],
          m[h][i-1] + m[h][w-i] - cost
        );
    }
  }

  return m;
}

$('#submit').click(function(){
  let prices = JSON.parse($('#input').val());
  let result = f(prices, 1);
  let str = result.map(line => JSON.stringify(line)).join('<br>');
  $('#output').html(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="input">[[3, 4],
[3, 6]]</textarea>
<p><button type="button" id="submit">Submit</button></p>
<div id="output"><div>

查看更多
走好不送
4楼-- · 2019-03-31 20:17

According to your answers I've prepared bottom-up and top-down implementation.

Bottom-up:

function bottomUp($high, $wide, $matrix){
    $m = [];

    for($h = 0; $h < $high; $h++){
        for($w = 0; $w < $wide; $w++){
           $m[$h][$w] = $matrix[$h][$w];

            if($h == 0 && $w == 0){
                continue;
            }

            for($i = 1; $i < ($h + 1 >> 1) + 1; $i++){
                $m[$h][$w] = max(
                    $m[$h][$w],
                    $m[$i - 1][$w] + $m[$h - $i][$w] - CUT_COST
                );
            }

            for($i = 1; $i < ($w + 1 >> 1) + 1; $i++){
                $m[$h][$w] = max(
                    $m[$h][$w],
                    $m[$h][$i - 1] + $m[$h][$w - $i] - CUT_COST
                );
            }            
        }        
    }

    return $m[$high-1][$wide-1];
}

Top-down:

function getBestCut($high, $wide, $matrix){
    global $checked;

    if(isset($checked[$high][$wide])){
        return $checked[$high][$wide];
    }

    $bestVal = $matrix[$high-1][$wide-1];
    $cutVert = CUT_VERT_NONE;
    $cutVal = $bestVal;
    $cutList = [];

    for($hCut = 1; $hCut < 1 + floor($high/2); $hCut++){
        $result1 = getBestCut($hCut, $wide, $matrix);
        $cutVal1 = $result1[0];
        $cutList1 = $result1[1];
        $result2 = getBestCut($high - $hCut, $wide, $matrix);
        $cutVal2 = $result2[0];
        $cutList2 = $result2[1];        
        $cutVal = $cutVal1 + $cutVal2;

        if($cutVal > $bestVal){
            $cutList = [$cutList1, $cutList2];
            $bestVal = $cutVal;
            $cutVert = CUT_VERT_FALSE;
            $bestH = $hCut;
        }

        $checked[$hCut][$wide] = $result1;
        $checked[$high - $hCut][$wide] = $result2;
    }

    for($vCut = 1; $vCut < 1 + floor($wide/2); $vCut++){
        $result1 = getBestCut($hCut, $vCut, $matrix);
        $cutVal1 = $result1[0];
        $cutList1 = $result1[1];
        $result2 = getBestCut($high, $wide - $vCut, $matrix);
        $cutVal2 = $result2[0];
        $cutList2 = $result2[1];        
        $cutVal = $cutVal1 + $cutVal2;   

        if($cutVal > $bestVal){
            $cutList = [$cutList1, $cutList2];
            $bestVal = $cutVal;
            $cutVert = CUT_VERT_TRUE;
            $bestH = $vCut;
        }      

        $checked[$hCut][$vCut] = $result1;
        $checked[$high][$wide - $vCut] = $result2;        
    }

    if($cutVert == CUT_VERT_NONE){
        $result = [$bestVal, [$high, $wide]];
    }else if($cutVert == CUT_VERT_TRUE){
        $result = [$bestVal - CUT_COST, $cutList];
    }else{
        $result = [$bestVal - CUT_COST, $cutList];
    }

    return $result;
}

Please tell me are they correct implementation of this method?

I wonder if time complexity is O(m^2*n^2) in top-down method?

查看更多
放我归山
5楼-- · 2019-03-31 20:18

Some thoughts on the problem rather than an answer:

It was a long time ago i studied dynamic programming, but i wrote up the following pseudo code which is think is O(n^2):

// 'Board'-class not included

val valueOfBoards: HashMap<Board, int>

fun cutBoard(b: Board, value: int) : int {

    if (b.isEmpty()) return 0
     if (valueOfBoards[b] > value) {
        return 0;
    } else {
        valueOfBoards[b] = value
    }

    int maxValue = Integer.MIN_VALUE

    for (Board piece : b.getPossiblePieces()) {
        val (cuttingCost, smallerBoard) = b.cutOffPiece(piece)
        val valueGained: int = piece.getPrice() - cuttingCost

        maxValue = Max(maxValue, valueGained + cutBoard(smallerBoard, value + valueGained))
    }

    return maxValue;
}

The board class is not trivially implemented, here is some elaboration:

// returns all boards which fits in the current board
// for the initial board this will be width*height subboards
board.getPossiblePieces() 


// returns a smaller board and the cutting cost of the cut
// I can see this becoming complex, depends on how one chooses to represent the board.
board.cutOffPiece(piece: Board)

It is not clear to me at the moment if cutOffPiece() breaks the algorithm in that you do not know how to optimally cut. I think since the algorithm will proceed from larger pieces to smaller pieces at some point it will be fine.

I tried to solve the re computation of sub problems (identical boards) by storing results in something like HashMap<Board, price> and comparing the new board with the stored best price before proceeding.

查看更多
登录 后发表回答