当可以事先知道哪些是满的时,随机选择一个空向量元素

发布于 2024-12-18 07:00:30 字数 1328 浏览 2 评论 0原文

我最终确定这个函数是造成我大部分瓶颈问题的原因。我认为这是因为当大多数突触已经处于活动状态时会发生大量过度的随机访问。基本上,正如标题所说,我需要以某种方式优化算法,这样我就不会在找到剩下的少数元素之一之前随机检查大量活动元素。

另外,我还包含了整个功能,以防发现其他缺陷。

void NetClass::Explore(vector <synapse> & synapses, int & n_syns)   //add new synapses
{
    int size = synapses.size();
    assert(n_syns <= size );

    //Increase the age of each active synapse by 1
    Age_Increment(synapses);

    //make sure there is at least one inactive vector left
    if(n_syns == size)
        return;

        //stochastically decide whether a new connection is added
        if((rand_r(seedp) %1000) < ( x / (1 +(n_syns * ( y / 100)))))  
        {
            n_syns++; //a new synapse has been created

            //main inefficiency here
            while(1)
            {
                int syn = rand_r(seedp) % (size);
                if (!synapses[syn].active)
                {
                    synapses[syn].active = true;
                    synapses[syn].weight = .04 + (float (rand_r(seedp) % 17) / 100);     
                    break;
                }
            }
        }  
}

void NetClass::Age_Increment(vector <synapse> & synapses)  
{
    for(int q=0, int size = synapses.size(); q < size; q++)
        if(synapses[q].active)
            synapses[q].age++;
}

I finally determined that this function is responsible for the majority of my bottleneck issues. I think its because of the massively excessive random access that happens when most of the synapses are already active. Basically, as the title says, I need to somehow optimize the algorithm so that I'm not randomly checking a ton of active elements before landing on one of the few that are left.

Also, I included the whole function in case of other flaws that can be spotted.

void NetClass::Explore(vector <synapse> & synapses, int & n_syns)   //add new synapses
{
    int size = synapses.size();
    assert(n_syns <= size );

    //Increase the age of each active synapse by 1
    Age_Increment(synapses);

    //make sure there is at least one inactive vector left
    if(n_syns == size)
        return;

        //stochastically decide whether a new connection is added
        if((rand_r(seedp) %1000) < ( x / (1 +(n_syns * ( y / 100)))))  
        {
            n_syns++; //a new synapse has been created

            //main inefficiency here
            while(1)
            {
                int syn = rand_r(seedp) % (size);
                if (!synapses[syn].active)
                {
                    synapses[syn].active = true;
                    synapses[syn].weight = .04 + (float (rand_r(seedp) % 17) / 100);     
                    break;
                }
            }
        }  
}

void NetClass::Age_Increment(vector <synapse> & synapses)  
{
    for(int q=0, int size = synapses.size(); q < size; q++)
        if(synapses[q].active)
            synapses[q].age++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俏︾媚 2024-12-25 07:00:30

传递一个范围为 [0, size-n_syns)Age_Increment 的随机数 k。让 Age_Increment 返回第 k 个空槽。

Pass a random number, k, in the range [0, size-n_syns) to Age_Increment. Have Age_Increment return the kth empty slot.

娇女薄笑 2024-12-25 07:00:30

由于您已经遍历了 Age_Increment 中的整个列表,因此请更新该函数以返回非活动突触的索引列表。

然后,您可以直接从该列表中随机选择一个项目。

Since you're already traversing the whole list in Age_Increment, update that function to return the list of the indexes of inactive synapses.

You can then pick a random item from that list directly.

森林迷了鹿 2024-12-25 07:00:30

这类似于在内存管理中查找空闲块的问题,因此我会看看该域中使用的算法,特别是 空闲列表,这是空闲位置列表。 (这些通常作为链表实现,以便能够有效地从一端弹出元素。链表中的随机访问仍然是 O(n) - n 较小,但仍然不是您的用例的最佳选择。)

This is similar to the problem of finding free blocks in memory management, so I would take a look at algorithms used in that domain, specifically free lists, which is a list of free positions. (These are usually implemented as linked lists to be able to pop elements off an end efficiently. Random access in a linked list would still be O(n) - with a smaller n, but still not the best choice for your use case.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文