如何找到最小值而不必尝试所有可能性
2020-9-25 9:7:33
收藏:0
阅读:116
评论:1
我正在试图编写一个程序,(用 Lua 编写,但我想这更多是一个数学问题),计算一组数字之间的总距离,并且对于某些数字有两个或三个不同的可能性。
例如,一组数字是:2、5、0、1。在这种情况下,距离的总和为 9。(5-2 + 5-0 + 1-0) 第一行的备选项为:5、-、3、2。 第二行备选项为:3、-、-、3 两行之间距离最小的组合为:5、5、3、3,距离总和为 2。
我的第一次尝试是编写一个尝试所有可能迭代的程序,但是对于大约 40 个数字的集合,有太多的可能性,以至于我的电脑崩溃了…
这是那个版本的代码,它首先创建所有不同的可能性,然后计算差异并将它们放入第 0 列。之后,我可以轻松地找到最小值并看到产生那个值的数字组合。
local table1 = {2, 5, 0 ,1}
local table2 = {5, nil, 3, 2}
local imax = 1
local solution = {}
local answer = table1[1]
for x = 1,#table1 do
solution[x]={}
for i= 1, (2^imax)/2 do
solution[x][i] = table1[x]
end
if table2[x] ~= nil then -- there is an alternative number
for y = 1, x-1 do -- copy all the previous table entries except the last one
for j = ((2^imax)/2)+1, 2^imax do -- the number of new rows increases exponentially
solution[y][j] = solution[y][j-imax]
end
end
for j = ((2^imax)/2)+1, 2^imax do -- create the new table entry with the alternative number
solution[x][j] = table2[x]
end
imax = imax + 1 -- this number is to remind how many alternative numbers where found
end
end
solution[0]={}
for x = 1, #table1 do
for i = 1, (2^imax)/2 do
if x < #table1 then answer = math.sqrt((solution[x+1][i]-solution[x][i])^2) else answer = 0 end
if solution[0][i] == nil then solution[0][i] = answer else solution[0][i] = solution[0][i] + answer end
end
end
在阅读了有关动态编程的资料之后,我编写了这个程序的一个新版本。它计算差异的最小总和,但我还想知道那个总和的路径(数字的组合)…还有工作要做…
local table1 = {2, 5, 0 ,1}
local table2 = {5, nil, 3, 2}
local solution = {}
local smallestsolution = {}
solution[1]={}
solution[2]={}
solution[3]={}
solution[4]={}
for i = 1, (#table1-1) do
solution[1][i] = math.sqrt((table1[i+1]-table1[i])^2)
if table2[i] ~= nil then solution[2][i] = math.sqrt((table1[i+1]-table2[i])^2) end
if table2[i+1] ~= nil then solution[3][i] = math.sqrt((table2[i+1]-table1[i])^2) end
if table2[i] ~= nil and table2[i+1] ~= nil then solution[4][i] = math.sqrt((table2[i+1]-table2[i])^2) end
end
for i = 1, (#table1-1) do
smallestsolution[i]=100000
for j = 1, 4 do
if solution[j][i] ~= nil and solution[j][i] < smallestsolution[i] then smallestsolution[i]=solution[j][i] end
end
end
local smallestsum = 0
for i = 1, (#table1-1) do
smallestsum = smallestsum + smallestsolution[i]
end
谢谢,
Emile
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我已经自己解决了它!@EgorSkriptunoff 提供的动态规划提示解决了问题!
local table1 = {2, 5, 0 ,1} local table2 = {5, nil, 3, 2} local solution = {} local smallestsolution = {} solution[1]={} solution[2]={} solution[3]={} solution[4]={} local path = {} local temp = {} for i = 1, (#table1-1) do solution[1][i] = math.sqrt((table1[i+1]-table1[i])^2) if table2[i] ~= nil then solution[2][i] = math.sqrt((table1[i+1]-table2[i])^2) end if table2[i+1] ~= nil then solution[3][i] = math.sqrt((table2[i+1]-table1[i])^2) end if table2[i] ~= nil and table2[i+1] ~= nil then solution[4][i] = math.sqrt((table2[i+1]-table2[i])^2) end end for i = 1, (#table1-1) do smallestsolution[i]=100000 temp[i] = 0 for j = 1, 4 do if solution[j][i] ~= nil and solution[j][i] < smallestsolution[i] then smallestsolution[i]=solution[j][i] temp[i] = j end end end local smallestsum = 0 for i = 1, (#table1-1) do smallestsum = smallestsum + smallestsolution[i] end for i = 1, (#table1) do -- 找到属于差异最小和的路径 if temp[i] == 1 then path[i] = table1[i] end if temp[i] == 2 then path[i] = table2[i] end if temp[i] == 3 then path[i] = table1[i] end if temp[i] == 4 then path[i] = table2[i] end if i == (#table1) then if temp[i-1] == 1 then path[i] = table1[i] end if temp[i-1] == 2 then path[i] = table1[i] end if temp[i-1] == 3 then path[i] = table2[i] end if temp[i-1] == 4 then path[i] = table2[i] end end end