Java 我的代码显示错误
我想实现以下算法:Java 我的代码显示错误,java,algorithm,Java,Algorithm,我想实现以下算法: 如果剩余的动作少于Config.MAX\u 然后,选择器必须选择最小的杆数(Config.MIN\u ACTION) 对于Config.MAX\u ACTION或更多剩余棍棒,然后根据 actionRanking参数 actionRanking数组对于每个可能的操作都有一个元素。0 索引对应于Config.MIN\u ACTION,最高索引对应于 至Config.MAX\u ACTION 例如,如果Config.MIN\u ACTION为1且Config.MAX\u ACTI
Config.MAX\u
然后,选择器必须选择最小的杆数(Config.MIN\u ACTION
)
Config.MAX\u ACTION
或更多剩余棍棒,然后根据
actionRanking
参数actionRanking
数组对于每个可能的操作都有一个元素。0
索引对应于Config.MIN\u ACTION
,最高索引对应于
至Config.MAX\u ACTION
Config.MIN\u ACTION
为1
且Config.MAX\u ACTION
为3
,则
可以拾取1、2或3根棍子actionRanking[0]
对应于1
,actionRanking[1]
对应于2
,等等。与其他元素相比,动作的元素越高,选择动作的可能性越大Config.MIN\u ACTION
为1
且Config.MAX\u ACTION
为3:如果ACTION排名为{9,90,1}
则总数为100
。由于actionRanking[0]
是9
,因此拾取1的操作应选择大约9/100次。2应选择约90/100次,1应选择约1/100次Config.RNG.nextInt(?)
方法生成适当的随机数sticksRemaining
表示要拾取的剩余棍棒数actionRanking
:要执行的每个操作的计数。0
索引对应于Config.MIN\u ACTION
,最高索引对应于Config.MAX\u ACTION
actionRanking
为null
,actionRanking
的长度为0
,或者sticksRemaining
为您看到的结果完全是可以预期的。随机数分布对于您提供的种子是正确的:
Random r = new Random();
r.setSeed(123);
int [] count = { 0, 0, 0 };
for(int i = 0; i < 10000; i++) {
count[r.nextInt(3)]++;
}
System.out.println(Arrays.toString(count));
问题是您的代码没有尝试调整随机响应的权重。您应该更改此选项:
return Config.RNG.nextInt(Config.MAX_ACTION)+Config.MIN_ACTION;
并执行以下操作:
在选择指数之前的某个阶段,计算所有操作的总权重:
int totalWeight = 0;
for(int i = 0; i < actionRanking.length; i++) {
totalWeight += actionRanking[i];
}
int totalWeight=0;
for(int i=0;i
然后,不只是随机选取一个指数,而是随机选取一个权重,并确定哪个指数对应于该权重:
int selection = Config.RNG.nextInt(totalWeight)+Config.MIN_ACTION;
int weight = Config.MIN_ACTION;
for(int i = 0; i < actionRanking.length - 1; i++) {
weight += actionRanking[i];
if (selection < weight) {
return i + Config.MIN_ACTION;
}
}
return Config.MAX_ACTION;
int selection=Config.RNG.nextInt(totalWeight)+Config.MIN\u ACTION;
int-weight=Config.MIN\u动作;
for(int i=0;i
测试表明,这与您的预期输出非常接近,但并不相同([295959981043]
)-可能是因为使用了nextInt(10)而不是nextInt(3)。您可能需要更改计算预期结果的方式。关键是第7项:
例如,如果Config.MIN\u ACTION
为1
且Config.MAX\u ACTION
为3:如果ACTION排名为{9,90,1}
则总数为100
。由于actionRanking[0]
是9
,因此拾取1的操作应选择大约9/100次。2应选择约90/100次,1应选择约1/100次
下面是该示例应如何实现:
- 首先生成一个介于0和99(含)之间的随机数(100个可能值)
- 如果随机数小于9,则返回1。否则,从随机数中减去9
- 如果调整后的随机数小于90,则返回2。否则,从调整后的随机数中减去90
- 剩下的唯一可能性是调整后的随机数为0,小于1,因此返回3
通常,AI函数的伪代码(在开始的特殊情况之后)应该如下所示:
compute the 'sum' of the entries in the 'actionRanking' array
generate a random number `R` between '0' and 'sum-1' inclusive
for each entry in 'actionRanking'
if the entry is greater than 'R'
return 'Config.MIN_ACTION' + the index for that entry
otherwise
subtract the entry from 'R'
我已更改为此代码。但这在测试中也失败了。@SagorAhmed说“这失败了”一点帮助都没有。描述行为选择操作4:预期3而不是1测试选择反应5:对于种子123,响应预期为[1037,5819,3144],但发现[6697,3303,0]@SagorAhmed,由于-1
,它跳过了最后一个操作。请将其删除,然后重试。但请注意:您的预期结果的计算方式可能与此代码产生的结果不同。如果不知道权重是如何产生的,您可能很容易得到不同的结果results@SagorAhmed再看看这个。选择的计算中有一个输入错误
。请从nextInt(Config.MIN\u ACTION)
更改为nextInt(totalWeight)
请在执行任何算法之前说明要解决的问题。不要写,也不要呈现未注释的代码。@SagorAhmed不客气,很高兴我能帮上忙:)
return Config.RNG.nextInt(Config.MAX_ACTION)+Config.MIN_ACTION;
int totalWeight = 0;
for(int i = 0; i < actionRanking.length; i++) {
totalWeight += actionRanking[i];
}
int selection = Config.RNG.nextInt(totalWeight)+Config.MIN_ACTION;
int weight = Config.MIN_ACTION;
for(int i = 0; i < actionRanking.length - 1; i++) {
weight += actionRanking[i];
if (selection < weight) {
return i + Config.MIN_ACTION;
}
}
return Config.MAX_ACTION;
compute the 'sum' of the entries in the 'actionRanking' array
generate a random number `R` between '0' and 'sum-1' inclusive
for each entry in 'actionRanking'
if the entry is greater than 'R'
return 'Config.MIN_ACTION' + the index for that entry
otherwise
subtract the entry from 'R'