gpt4 book ai didi

java - 带动态编程算法的资源分配

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:37:12 25 4
gpt4 key购买 nike

给定一组函数 f1...fn(离散时间)和一个时间限制 (int),应该找到最大输出,即在不同函数之间分配时间以最大化所用函数输出的总和。

对于任何函数,任何时间的值都代表函数在指定时间内使用的总输出。即 F(2) = 使用 2 秒时函数的总输出。不是 F(1) + F(2)。

所有值(时间、函数输出)都是整数。

我当前的算法通过检查 F(t) 来找出可能的损失中的最大值,因为所有时间都被放入一个函数中,将该最大值与前一个最大值 M(t- 1) + 为每个可能的功能增加 1 秒(记录已使用的功能和次数)。

public int computeDamage() {

    int totalTime = calculator.getTotalTime();
int numAttacks = calculator.getNumAttacks();

if(totalTime == 0) return 0;

int[] attackHist = new int[numAttacks];

return maxDamage(numAttacks, attackHist, 1, totalTime, 0);

}

public int maxDamage(int numAttacks, int[] attackHist, int start, int end, int max) {
//get the max of all the values at f(start), save the attack
int maxF = -1, attack = -1;
for(int i = 0; i < numAttacks; i++) {
int dam = calculator.calculateDamage(i, start);
if(dam > maxF) {
maxF = dam;
attack = i;
}
}

//if start isn't 1, get the max of all possible values added to the attackHist
int maxH = -1, attackH = -1;
if(start > 1) {
for(int j = 0; j < numAttacks; j++) {
int dChange = -1;
if(attackHist[j] > 0) dChange = calculator.calculateDamage(j, attackHist[j]+1) - calculator.calculateDamage(j, attackHist[j]);
else dChange = calculator.calculateDamage(j, attackHist[j]+1);

if((max + dChange) > maxH) {
maxH = max + dChange;
attackH = j;
}
}

//if max is greater, reset attackHist. Otherwise, add 1 to used attack
if(maxF > maxH) {
Arrays.fill(attackHist, 0);
attackHist[attack] = start;
max = maxF;
} else {
attackHist[attackH]++;
max = maxH;
}

} else {
//just set the max to maxF
max = maxF;
attackHist[attack] = 1;

}

if(end == start) return max;
else return maxDamage(numAttacks, attackHist, start+1, end, max);
}

输入12.in

20 12
0 3 4 7 9 12 12 14 15 15 17 19
2 5 6 9 11 12 15 15 16 19 21 22
1 4 6 8 9 11 13 14 16 18 21 22
1 4 4 4 5 5 6 8 9 11 12 14
0 3 4 5 7 10 12 13 15 17 20 20
1 3 5 5 8 10 10 12 14 15 16 18
1 1 3 5 7 8 10 11 11 13 14 16
1 1 2 2 2 3 6 7 10 11 11 12
1 3 5 5 7 7 8 11 11 12 14 16
0 1 4 5 6 9 10 11 12 12 15 18
3 5 5 7 8 10 12 12 14 15 15 16
3 5 6 9 12 12 13 14 15 18 21 21
1 2 3 4 7 9 10 12 12 15 18 18
3 4 5 7 8 10 12 13 13 16 17 20
3 5 7 7 10 11 14 16 17 18 21 23
0 1 4 7 7 8 10 12 13 13 14 16
2 3 3 6 8 9 12 15 17 18 20 21
0 2 3 3 6 8 9 10 13 15 17 17
1 2 4 7 9 9 9 11 14 14 17 19
3 5 6 7 10 11 12 12 13 16 17 19

第一行告诉我们有多少个函数(20)和多少时间(12 秒)来最大化输出。

每一行都是一个从 1 到 12 F(t) 定义的函数,详细说明该函数在该点之前总共造成了多少损害。

输出应该是 31,但我的代码输出的是 30。

最佳答案

我不会尝试调试您的代码,但我会让您确切知道它应该做什么。

您需要做的是创建一个表,按功能,按时间,(best_value, time_this_function)。根据您的输入,这是该表:

[[(0, 1), (3, 2), (4, 3), (7, 4), (9, 5), (12, 6), (12, 7), (14, 8), (15, 9), (15, 10), (17, 11), (19, 12)],
[(2, 1), (5, 2), (6, 3), (9, 4), (11, 5), (12, 6), (15, 7), (17, 2), (18, 3), (21, 4), (23, 5), (24, 6)],
[(2, 0), (5, 0), (6, 3), (9, 0), (11, 0), (13, 2), (15, 0), (17, 0), (19, 2), (21, 0), (23, 0), (25, 2)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(2, 0), (5, 0), (6, 0), (9, 0), (11, 0), (13, 0), (15, 0), (17, 0), (19, 0), (21, 0), (23, 0), (25, 0)],
[(3, 1), (5, 2), (8, 1), (10, 2), (12, 1), (14, 1), (16, 1), (18, 1), (20, 1), (22, 1), (24, 1), (26, 1)],
[(3, 1), (6, 1), (8, 0), (11, 1), (13, 1), (15, 1), (17, 1), (20, 5), (22, 5), (24, 5), (26, 5), (28, 5)],
[(3, 0), (6, 0), (8, 0), (11, 0), (13, 0), (15, 0), (17, 0), (20, 0), (22, 0), (24, 0), (26, 0), (28, 0)],
[(3, 1), (6, 0), (9, 1), (11, 0), (14, 1), (16, 1), (18, 1), (20, 0), (23, 1), (25, 1), (27, 1), (29, 1)],
[(3, 1), (6, 0), (9, 0), (12, 1), (14, 0), (17, 1), (19, 1), (21, 1), (23, 0), (26, 1), (28, 1), (30, 1)],
[(3, 0), (6, 0), (9, 0), (12, 0), (14, 0), (17, 0), (19, 0), (21, 0), (23, 0), (26, 0), (28, 0), (30, 0)],
[(3, 0), (6, 0), (9, 0), (12, 0), (14, 0), (17, 0), (19, 0), (21, 0), (23, 0), (26, 0), (28, 0), (30, 0)],
[(3, 0), (6, 0), (9, 0), (12, 0), (14, 0), (17, 0), (19, 0), (21, 0), (23, 0), (26, 0), (28, 0), (30, 0)],
[(3, 0), (6, 0), (9, 0), (12, 0), (14, 0), (17, 0), (19, 0), (21, 0), (23, 0), (26, 0), (28, 0), (30, 0)],
[(3, 1), (6, 0), (9, 0), (12, 0), (15, 1), (17, 0), (20, 1), (22, 1), (24, 1), (26, 0), (29, 1), (31, 1)]]

一旦有了该表,您只需从最后一个条目向后走即可找出路径。

每个功能花费的时间,从上到下分别是:

(31, 1) => 1
(28, 0) => 0
(28, 0) => 0
(28, 0) => 0
(28, 0) => 0
(28, 1) => 1
(25, 1) => 1
(22, 0) => 0
(22, 5) => 5
(10, 2) => 2
(5, 0) => 0
(5, 0) => 0
(5, 0) => 0
(5, 0) => 0
(5, 0) => 0
(5, 0) => 0
(5, 0) => 0
(5, 0) => 0
(5, 2) => 2
# At this point we back up out of our table, and we spent no time on the first.

反转我们在第二个函数上得到2,11号得到2,12号得到51 14 号,1 15 号,最后 1 号。

希望这能帮助您找到遗漏的步骤。

关于java - 带动态编程算法的资源分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55872378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com