苹果采摘优化算法

huangapple go评论79阅读模式
英文:

Apple Picking Optimization Algorithm

问题

我发现了这些问题:假设我有N棵苹果树,每棵树上有Ai颗苹果。我还有M个篮子,每个篮子都有容量Ci和灵活性Fi属性。当我要摘苹果树上的苹果时,我只能按顺序从1到N选择树。在每棵树上,我有两个选项,要么摘下树上的所有苹果,要么扩展我的篮子以增加其容量F。当我扩展篮子时,不能摘取那棵树上的水果。找出每个篮子可以摘取的最大苹果数。

示例:

N = 5; A = [3, 2, 4, 7, 5]

M = 2; (C,F) = [(5,3), (3,6)]

答案:
对于第一个篮子,可以获得的最大苹果数量是12:

  1. 扩展篮子3个单位(最大容量=5+3 = 8)
  2. 扩展篮子3个单位(最大容量=8+3 = 11)
  3. 扩展篮子3个单位(最大容量=11+3 = 14)
  4. 摘取第4棵树上的苹果(获得7颗苹果,当前总数=7)
  5. 摘取第5棵树上的苹果(获得5颗苹果,当前总数=12)

对于第二个篮子,最多可以获得15颗:

  1. 摘取第1棵树上的苹果(获得3颗苹果,当前总数=3)
  2. 扩展篮子6个单位(最大容量=3+6 = 9)
  3. 扩展篮子6个单位(最大容量=9+6 = 15)
  4. 摘取第4棵树上的苹果(获得7颗苹果,当前总数=10)
  5. 摘取第5棵树上的苹果(获得5颗苹果,当前总数=15)
英文:

I found these problem: Suppose that I have fields with N apple tree, each with A<sub>i</sub> apple on it. I also have M basket, each basket have the property C<sub>i</sub> for capacity and F<sub>i</sub> for flexibility. When I'm going to pick the apple trees I can only pick from tree in order from 1 to N. At each tree I have two options, to pick all the apple on the tree or to expand my basket to increase it's capacity by F. When I expand the basket, I cannot pick the fruit in the that tree. Find the maximum number of apple can be picked by each basket.

Example:

N = 5; A = [3, 2, 4, 7, 5]

M = 2; (C,F) = [(5,3), (3,6)]

Answer:
For the first basket the maximum amount of apple obtained is 12:

  1. Expand the bag by 3 (Max capacity = 5+3 = 8)
  2. Expand the bag by 3 (Max capacity = 8+3 = 11)
  3. Expand the bag by 3 (Max capacity = 11+3 = 14)
  4. Pick the 4-th tree (Get 7 apple, current total = 7)
  5. Pick the 5-th tree (Get 5 apple, current total = 12)

And for the second bag the maximum is 15:

  1. Pick the 1-th tree (Get 3 apple, current total = 3)
  2. Expand the bag by 6 (Max capacity = 3+6 = 9)
  3. Expand the bag by 6 (Max capacity = 9+6 = 15)
  4. Pick the 4-th tree (Get 7 apple, current total = 10)
  5. Pick the 5-th tree (Get 5 apple, current total = 15)

How should I approach to solve this problem?

答案1

得分: 0

我认为这个问题本质上是一个回溯问题。首先,我建议您查阅一下关于回溯法的一般描述。https://en.wikipedia.org/wiki/Backtracking

如果您需要将算法实现为实际代码,可能会有更具体的回溯法描述。

英文:

I think this was meant to be a backtrack problem. First of all, I recommend you check out a general description of that. https://en.wikipedia.org/wiki/Backtracking

There are probably more specific descriptions of backtracking if you need to have the algorithm implemented in actual code.

答案2

得分: 0

这个问题可以通过递归关系来思考:

假设有一个函数 MaxApples(treeNum, capacityLeft),它给出在给定的篮子中能获得的最大苹果数量。

这个函数的初始参数将为 treeNum = 0,因为你从第一棵树开始,以及 capacityLeft = C,因为篮子是空的,还没有扩展。

然后对于每棵树,你有两个选择 - 要么扩展篮子,要么摘取树上的苹果。这可以通过对 MaxApple 进行两次递归调用来表示,在第一个选择中,你增加树的编号,并将容量增加 F(这是篮子的扩展量)。另一个选择是将当前树上的所有苹果添加到篮子中,然后在递归调用中增加树编号,并减少刚刚摘取的数量的容量。

注意:在每个阶段,你都需要检查是否有选择来摘取苹果。也就是说,检查剩余容量是否大于等于你想要摘取的树上的苹果数。

你可以计算出这两个选择所获得的总数,并选择其中的最大值。

下面是 Java 代码示例(仅针对一个篮子):

class Main {

  static int[] trees = new int[]{3, 2, 4, 7, 5};
  static int basketCapacity = 5;
  static int basketFlex = 3;

  public static void main(String[] args) {
    System.out.println(maxApples(0, basketCapacity));
  }

  public static int maxApples(int treeNum, int capacityLeft) {
    if (treeNum >= trees.length) {
      return 0;
    }

    int pick = 0;
    if (trees[treeNum] <= capacityLeft) {
      pick = trees[treeNum] + maxApples(treeNum+1, capacityLeft - trees[treeNum]);
    }
    int expand = maxApples(treeNum+1, capacityLeft + basketFlex);

    return Math.max(pick, expand);
  }
}
英文:

It will help to think of this problem as a recurrence relation:

苹果采摘优化算法

Say there is a function MaxApples(treeNum, capacityLeft) that gives you the max number of apples you can get for a given basket.

The initial parameters to this function will be treeNum = 0 because you start at the first tree and capacityLeft = C for the given basket because the basket is empty and not yet expanded.

Then for each tree you have 2 choices - either expand the basket or pick the tree. This can be represented by 2 recursive calls to MaxApple where in the first choice you increment the tree number and expand the capacity by F (which is how much the basket expands). The other choice is to add all the apples from the current tree to your basket, and then on the recursive call increment the treeNum and reduce capacity by how much you just picked.

Note: you'll have to check at each stage whether you have the choice to pick or not. That is, check whether the capacityLeft is greater or equal to the tree you want to pick.

You can calculate the total you'll get from both of these and just take the max of it.

Here's the code below in Java (for just one basket):

class Main {

  static int[] trees = new int[]{3, 2, 4, 7, 5};
  static int basketCapacity = 5;
  static int basketFlex = 3;

  public static void main(String[] args) {
    System.out.println(maxApples(0, basketCapacity));
  }

  public static int maxApples(int treeNum, int capacityLeft) {
    if (treeNum &gt;= trees.length) {
      return 0;
    }

    int pick = 0;
    if (trees[treeNum] &lt;= capacityLeft) {
      pick = trees[treeNum] + maxApples(treeNum+1, capacityLeft - trees[treeNum]);
    }
    int expand = maxApples(treeNum+1, capacityLeft + basketFlex);

    return Math.max(pick, expand);
  }
}

huangapple
  • 本文由 发表于 2020年10月8日 01:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64248982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定