0

我有一个 X x Y 表尺寸的动态范围,需要通过长时间运行的算法进行测试。

例如,我将尝试 40-50 英寸宽 x 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
4

2 回答 2

0

我要改写你的问题,这个描述是否正确?给定一个目标比率:

W / H

您希望X / Y尽可能接近优化W / H,而xmin < x < xmaxymin < 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
    }
}
于 2013-08-13T17:51:31.747 回答
0

由于迭代次数必须是整数,因此我们的选项是 1,30 2,15 3,10 和 5,6,尽管不满足 30 次迭代,您可能需要考虑 4,7 或 4,8。

如果我在没有优化的情况下将其伪造,那么它更容易遵循我的逻辑

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

不是很清楚,但希望足够清楚……回来为我工作!

于 2013-08-13T17:38:07.520 回答