如何找到最接近目标输出(Y)的最佳函数输入整数(X)? [英] How do I find the optimal function input integer (X) to get closest to a target output (Y)?

查看:65
本文介绍了如何找到最接近目标输出(Y)的最佳函数输入整数(X)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案与VBA中的excel宏有关,我正在寻找最佳解决方案.可以使用伪代码,Python,Java或Excel兼容的VBA提供答案.该方法应该能够用Excel VBA编写.

This answer relates to an excel macro in VBA and I am looking for the optimal solution. The answer can be provided in Pseudo Code, Python, Java or Excel compatible VBA. The approach should be able to written in Excel VBA.

如果您有替代解决方案(例如Haskell中的函数式编程),我也希望看到这一点,但这不是主要问题.

If you have alternative solution (Ie functional programming in Haskell) I would like to see that too, but that is not the primary concern.

我有一个复杂的函数:y =函数(x),其中x是输入,y是输出.x是整数y是双精度型(可以包含小数)

I have a complicated function: y = function (x) where x is the input and y the output. x is an integer y is a double (can contain decimals)

我知道x必须位于的目标范围,比如说0到2000或800到1800.我也知道y应该是什么.

I know the target range that x must be in, lets say 0 to 2000, or 800 to 1800. I also know what y should be.

函数 是一个计算量大的函数,应尽可能少地计算一次.

function is a computationally expensive function and should be calculated as few times as possible.

我看不到 功能 的内容,或者它太复杂而无法分析.函数的输出为递增曲线.IE.x = 2> x = 1然后是y(2)> y(1)

I cannot see the contents of function or it is too complex to analyse. The output of the function is an increasing curve. ie. x=2 > x=1 then y(2) > y(1)

需要找到,使我得到与已知y值最接近的y值的x值.

I need to find the x value that gives me a y value closest to the known value of y.

最有效的计算方法是什么?

What is the most computationally efficient way of doing this?

我想将范围划分为20个区域并进行遍历.循环将在比目标大的第一个y处停止.IE.它将运行2到20次.然后,我将转到精确的单位(整数)并循环遍历该子区域或子范围,这将表示最大呼叫数量(startingrange/20-即,如果起始范围为0至600),则函数将被调用2到30次.

I thought of dividing the range in 20 regions and looping through. The loop will stop at the first y larger than the target. ie. it will run between 2 and 20 times. Then I would go to the precise unit (integer) and loop through this sub-region or sub-range, which would mean a maximum number of calls being (startingrange /20 - ie. if starting range was 0 to 600), then the function would be called between 2 and 30 times.

是否有更好的方法(也许是递归的)来做到这一点.我好像想念这里的船吗?

Is there a better way to do this, perhaps recursively. I seem to be missing the boat here?

推荐答案

对于y值始终增加的函数,可以使用二进制搜索以快速缩小潜在值的范围.示例Python实现:

For a function whose y value always increases, you can use binary search to quickly narrow down the range of potential values. Sample Python implementation:

def binary_search(func, min_x, max_x, target_y):
    while max_x - min_x > 1:
        midpoint = (min_x + max_x) / 2
        if func(midpoint) > target_y:
            max_x = midpoint
        else:
            min_x = midpoint
    return min_x

print binary_search(lambda n: n**2, 0, 600, 300**2)

此算法具有对数效率.在0到600的范围内,它只会调用 func 十次;并且只有11次,范围是原来的两倍.

This algorithm has logarithmic efficiency. For a range of 0 to 600, it calls func only ten times; and only eleven times for a range twice as large.

这篇关于如何找到最接近目标输出(Y)的最佳函数输入整数(X)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆