我有一个X x Y尺寸的动态范围,需要通过长时间运行的算法进行测试。
例如,我将尝试任何40-50英寸宽,40-80英寸长的纸张尺寸。尝试每个整数组合需要很长时间,所以我想将迭代限制为30.
由于X范围内只有10个单位,Y范围内只有40个单位,我需要测试大约3个X单位和10个Y单位并跳过其余部分。
如何编码以确定最接近的比率并最终只有30次迭代?它需要是动态的,因为这些范围会不断变化,而有时Y范围小于X范围。
回答:(受弗雷泽启发)
Dim ratioX As Integer = txtSizeFormSingleXmax - txtSizeFormSingleXmin
Dim ratioY As Integer = txtSizeFormSingleYmax - txtSizeFormSingleYmin
Dim FinalRatioNumerator As Integer
Dim FinalRatioDenominator As Integer
Dim XGreaterThanY As Boolean = False
If ratioX > ratioY Then
Dim tempRatio As Integer
tempRatio = ratioY
ratioY = ratioX
ratioX = tempRatio
XGreaterThanY = True
End If
For countRatio As Integer = 1 To 30
If ratioX / ratioY <= countRatio / CInt(30 / countRatio) Then
FinalRatioNumerator = countRatio
FinalRatioDenominator = CInt(30 / countRatio)
Exit For
End If
Next
答案 0 :(得分:0)
由于迭代次数必须是整数,我们的选项是1,30 2,15 3,10和5,6你可能要考虑4,7或4,8但不满足30次迭代。
如果我在没有优化的情况下将其伪造,那么它更容易遵循我的逻辑
get ranges x and y
if x>y y/x = ratio else swap x and y
现在您真正需要做的事情(假设您已经设置了30次迭代)
calc difference in ratio
for each pair possible
(((x/y)-(1/30 ))^2)^(1/2) =result
now you just take the closest match (smallest result) and use those
as your test numbers
for each xtestval in mylist of xtestvals
(Xrange / xTestNum) + xrange min = xtestval
mylist.add(xtestval)
xrange min = xtestval
next
然后你有每个轴的测试值,所以双循环得到组合
for each x
for each y
pair x and y
next
next
不是很清楚,但希望足够清楚......回去为我工作!
答案 1 :(得分:0)
我要重新解释你的问题,这个解密是否正确?鉴于taret比率:
W / H
您希望X / Y
尽可能接近W / H
,xmin < x < xmax
和ymin < y < ymax
。
建议的算法:
var x = xmin + xmax / 2;
var y = ymin + ymax / 2;
function int[] adjustRange(x, y, wHRatio)
{
var curRatio = x / y;
if(curRatio == wHRatio)
{
return [x, y];
}
else if(curRatio < wHRatio)
{
// you want to increase x or decrease y. If neither are possible, x/y
// is as close as you will get. You can fudge your intervals to be
// based on the xmax-xmin and ymax-ymin, or some number from a
// config file
}
else if(curRatio > wHRatio)
{
// you want to decrease x or increase y. If neither are possible, x/y
// is as close as you will get
}
}