【问题标题】:Creating a custom interated discrete evolutionary distribution创建自定义的交互离散进化分布
【发布时间】:2015-07-01 19:09:34
【问题描述】:

我这几天一直在处理这个问题。我正在运行一个进化模型,该模型使用分布来进行物种之间的相互作用。我在这里粘贴了函数。我需要使用模板:

template <class InputIt>

如果我将模板直接粘贴在函数声明之前,函数将无法识别该模板。如果我在 main() 之前粘贴它,模板会被识别,但我得到一个错误:

错误 2 错误 LNK1120: 1 未解决的外部

代码:

void evolution(TeamArray& teamData)
{

default_random_engine randomGenerator((unsigned int)time(NULL));
uniform_int_distribution<int> rand120(0, 120);

int* RandA;
int* RandB;

RandA = new int[rounds];
RandB = new int[rounds];
// Time Period 1

for (int i = 0; i < rounds / 10; ++i)
{
    RandA[i] = rand120(randomGenerator); // generates random numbers from 0 to 120 (121 elements)
}

for (int i = 0; i < rounds / 10; ++i)
{
    RandB[i] = rand120(randomGenerator); // generates random numbers from 0 to 120 (121 elements)
}

for (int i = 0; i < rounds / 10; i++)
{
    interact(teamData[RandA[i]], teamData[RandB[i]]);
}

delete[] RandA;
delete[] RandB;

    // Later time periods (ERA 2 through 10)

// The inside of this loop does the rest of the interactions 
for (int t = 1; t < 10; ++t) // looping through the ERA's
{
    // Intializing distribution 
    std::vector< int> weights(121);
    for (int i = 0; i < 121; i++)
    {
        weights[i] = (teamData[i]).S();
    }

    std::discrete_distribution<int&> dist(weights.begin(), weights.end());

    for (int i = t* (rounds / 10); i < (t+1) * (rounds / 10); ++i)
    {
        RandA[i] = dist(randomGenerator); 
    }

    for (int i = t* (rounds / 10); i < (t + 1) * (rounds / 10); ++i)
    {
        RandB[i] = dist(randomGenerator); 
    }

    for (int i = t* (rounds / 10); i < (t + 1) * (rounds / 10); ++i)
    {
        interact(teamData[RandA[i]], teamData[RandB[i]]);
    }

    delete[] RandA;
    delete[] RandB;
}

【问题讨论】:

  • constructor of std::discrete_distribution 只接受一个参数。你给了它两个参数。
  • 好的。那么我将如何输入权重向量?我也试过这个:std::discrete_distribution dist(weights);
  • 不幸的是,我不太了解随机数生成器的复杂性,无法提出解决方案。

标签: c++ distribution


【解决方案1】:

链接器报告错误是因为调用了一个方法(在这种情况下为构造函数),其签名与类中的任何特化都不匹配。

std::discrete_distribution 文档表明有 4 个专用构造函数可用,它们可能涵盖您需要的情况。

您看起来最接近您尝试调用的那个需要[InputInterator][2] 引用,而不是integer 边界。

Visual C++ 的example 表示此构造函数在 Microsoft 平台上不可用,但提供了具有下限和上限的等效解决方案。

【讨论】:

  • 我是编程新手。我对参考书有点了解。如何在离散分布中使用 [InputInterator][2] 引用?
  • 还有其他人知道吗?我很想弄清楚这一点。
猜你喜欢
  • 2020-10-24
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多