你如何用 C++ 实现分配策略

how do you implement allocating strategy with c++

我有两个 workers.If 我配置了一个策略,60% 的任务分配给 A worker,其余的分配给 B worker。

你是怎么用c++实现的

你有什么建议?

map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));

map<string,int> m_count;

m_count.insert(make_pair("total",0));

map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{      

    m_count.insert(make_pair("countA",1));

 }     
else
{
    int &c = it->second; 
    if(itc!=m_count.end()&&itp!=m_percent.end())
    {
        float f=(c+1)*100/(itc->second+1)*100.0
        if (f<=itp->second)
        {
            it->second=it->second+1;


        }   
    }
}   


if(itc!=m_count.end())
{
   itc->second=itc->second+1;    
}

如果您谈论的是任务数量而不考虑复杂性,只需计算分配给每个任务的工作数量即可。对于分配给 A 的作业,我们将这些计数称为 countA,将作业总数称为 count(为计算简单起见),并将它们初始化为零。

然后,当有工作来的时候,按照下面的方式分配:

  • 如果 count 等于零,将其分配给 A 并递增 countAcount
  • 否则,如果countA / count小于0.6,分配给A并增加countAcount
  • 否则将其分配给 B 并仅递增 count

从长远来看,这将倾向于平均分配,以便 A 获得 60%:

countA  count  countA/count  allocateTo
------  -----  ------------  ----------
     0      0             ?      A
     1      1         1.000      B
     1      2         0.500      A
     2      3         0.667      B
     2      4         0.500      A
     3      5         0.600      B
     3      6         0.500      A
     4      7         0.571      A
     5      8         0.625      B
     5      9         0.556      A
     6     10         0.600      B
     6     11         0.545      A
     7     12         0.583      A
     8     13         0.615      B
     8     14         0.571      A
     9     15         0.600      B
     9     16         0.563      A
    10     17         0.588      A
    11     18         0.611      B
    11     19         0.579      A
    12     20         0.600

...等等。