从 golang 中受限键范围内的映射生成的切片中随机选择元素。有 O(1) 的捷径吗?
来源:stackoverflow
时间:2024-04-26 11:39:37 263浏览 收藏
珍惜时间,勤奋学习!今天给大家带来《从 golang 中受限键范围内的映射生成的切片中随机选择元素。有 O(1) 的捷径吗?》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!
在我的模拟多粒子进化的程序中,我有一个地图,它采用键值 pop (种群大小)并返回包含具有此种群的站点的切片:mymap[pop][]int。这些切片通常都很大。
在每个进化步骤中,我选择随机群体大小 randompop。然后,我想随机选择一个人口至少为 randompop 的站点。 sitechosen 用于更新我的人口结构,我利用第二个地图来有效更新 mymap 键。我当前(缓慢)的实现看起来像
func Evolve( ..., myMap map[int][]int ,...){
RandomPop = rand.Intn(rangeofpopulation)+1
for i:=RandPop,; i<rangeofpopulation;i++{
preallocatedslice=append(preallocatedslice,myMap[i]...)
}
randomindex:= rand.Intn(len(preallocatedslice))
sitechosen= preallocatedslice[randomindex]
UpdateFunction(site)
//reset preallocated slice
preallocatedslice=preallocatedslice[0:0]
}
这段代码(显然)在将值从映射复制到预分配切片时遇到了巨大的瓶颈,runtime.memmove 占用了我 87% 的 cpu 使用率。我想知道是否有一种 o(1) 方法可以随机选择 mymap 指示的切片并集中包含的条目,其键值介于 0 和 randompop 之间?如果有人知道的话,我对允许您操作自定义哈希表的软件包持开放态度。建议不需要保证并发安全
尝试过的其他事情:我之前让我的地图记录了所有值至少为 pop 的站点,但这占用了 >10gb 的内存,而且很愚蠢。我尝试将指针隐藏到相关切片以创建查找切片,但 go 禁止这样做。我可以总结每个切片的长度并基于此生成一个随机数,然后按长度迭代 mymap 中的切片,但这比仅保留人口的更新 cdf 并进行二分搜索要慢得多在上面。二分查找速度很快,但更新 cdf(即使手动完成)也是 o(n)。我真的希望滥用哈希表来加速随机选择和更新(如果可能的话)
我的一个模糊的想法是编造某种地图的嵌套结构,指向其内容,并且还指向具有比他们的键少的键或其他东西的地图。
解决方案
我正在查看您的代码,我有一个问题。 为什么必须将值从地图复制到切片?我的意思是,我认为我正在遵循背后的逻辑......但我想知道是否有办法跳过这一步。
所以我们有:
func evolve( ..., mymap map[int][]int ,...){
randompop = rand.intn(rangeofpopulation)+1
for i:=randpop,; i<rangeofpopulation;i++{
// slice of preselected `sites`. one of this will be 'sitechosen'
// we expect to have `n sites` on `preallocatedslice`
// where `n` is the amount of iterations,
// ie; n = rangeofpopulation - randpop
preallocatedslice=append(preallocatedslice,mymap[i]...)
}
// once we have a list of sites, we select `one`
// under a normal distribution every site ha a chance of 1/n to be selected.
randomindex:= rand.intn(len(preallocatedslice))
sitechosen= preallocatedslice[randomindex]
updatefunction(site)
...
}
但是如果我们将其更改为:
func Evolve( ..., myMap map[int][]int ,...){
if len(myMap) == 0 {
// Nothing to do, print a log!
return
}
// This variable will hold our site chosen!
var siteChosen []int
// Our random population size is a value from 1 to rangeOfPopulation
randPopSize := rand.Intn(rangeOfPopulation) + 1
for i := randPopSize; i < rangeOfPopulation; i++ {
// We are going to pretend that the current candidate is the siteChosen
siteChosen = myMap[i]
// Now, instead of copying `myMap[i]` to preAllocatedSlice
// We will test if the current candidate is actually the 'siteChosen` here:
// We know that the chances for an specific site to be the chosen is 1/n,
// where n = rangeOfPopulation - randPopSize
n := float64(rangeOfPopulation - randPopSize)
// we roll the dice...
isTheChosenOne := rand.Float64() > 1/n
if isTheChosenOne {
// If the candidate is the Chosen site,
// then we don't need to iterate over all the other elements.
break
}
}
// here we know that `siteChosen` is a.- a selected candidate, or
// b.- the last element assigned in the loop
// (in the case that `isTheChosenOne` was always false [which is a probable scenario])
UpdateFunction(siteChosen)
...
}
此外,如果您想可以在循环外计算 n 或 1/n 。
因此,我们的想法是在循环内测试候选者是否是 sitechosen,并避免将候选者复制到此预选池中。
今天关于《从 golang 中受限键范围内的映射生成的切片中随机选择元素。有 O(1) 的捷径吗?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习