duanbei3747 2015-08-20 08:26
浏览 42

尝试在PHP中创建类似于近似子集和表的算法

I've been trying to construct an algorithm in PHP that in some ways resembles a subset-sum problem, but in this case, i'm not looking for an exact match, only the closest matches, as in most cases there is unlikely to be an exact match.

Let me elaborate in a little more detail. Say I own various hypothetical investments, and need to raise the sum X from existing investments. Say X = 40000.

The array of inputs are (4500, 8750, 12900) and the number of each held are (8, 10, 10).

Now, I can intuitively work out that 4500 * 8 = 36000, 12900 * 3 = 38700, but a closer match would be (4500 * 6) + 12900 = 39900.

I soon found that try to loop through each possible combination rapidly creates 10's of million of possible arrays, even more if additional inputs are added. That would not be a problem if it was just for my own purposes, but not viable for a web-application.

I'm not asking for anybody to write code for me. I'm not a mathematician, so I'm just wondering if there is a different way I could tackle this problem, or are least short-cut parts of it ?

Thanks.

  • 写回答

1条回答 默认 最新

  • dongwen7187 2015-08-20 09:40
    关注

    It's a well known problem called the "Knapsack Problem" that can be solved with a dynamic programming approach.

    https://en.wikipedia.org/wiki/Knapsack_problem

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集