anchor.diff:  %% ====================================================================
   %% Internal functions
   %% ====================================================================
  --spec loop([agent()],dict()) -> ok.
  +-spec loop([agent()],dict(),counters()) -> ok.
   %% @doc Glowna petla procesu. Kazda iteracja powoduje wytworzenie kolejnej generacji.
  -loop(Agents,FDs) ->
  +loop(Agents,FDs,Counters) ->
     receive
       {write,Last} ->
         Fitness = case misc_util:result(Agents) of
  
positive.code:       io_util:write(dict:fetch(population,FDs),length(Agents)),
        io:format("Island ~p Fitness ~p Population ~p~n",[self(),misc_util:result(Agents),length(Agents)]),
        timer:send_after(config:writeInterval(),{write,Fitness}),
        loop(Agents,FDs);
      {agent,_Pid,A} ->
        loop([A|Agents],FDs);
      {finish,_Pid} ->
        io_util:closeFiles(FDs),
        ok
  
negative.code:     Groups = misc_util:groupBy([{misc_util:behavior(A),A} || A <- Agents ]),
      NewGroups = [evolution:sendToWork(G) || G <- Groups],
      NewAgents = misc_util:shuffle(lists:flatten(NewGroups)),
      NewCounters = countGroups(Groups,#counters{}),
      loop(NewAgents,FDs,#counters{fight = Counters#counters.fight + NewCounters#counters.fight,
                                  reproduction = Counters#counters.reproduction + NewCounters#counters.reproduction,
                                  death = Counters#counters.death + NewCounters#counters.death,
                                  migration = Counters#counters.migration + NewCounters#counters.migration})
    end.
  
  -spec countGroups([tuple()],counters()) -> counters().
  countGroups([],Counter) ->
    Counter;
  countGroups([{death,AgentList}|Groups],Counter) ->
    countGroups(Groups,Counter#counters{death = length(AgentList)});
  countGroups([{migration,AgentList}|Groups],Counter) ->
    countGroups(Groups,Counter#counters{migration = length(AgentList)});
  countGroups([{fight,AgentList}|Groups],Counter) ->
    countGroups(Groups,Counter#counters{fight = length(AgentList)});
  countGroups([{reproduction,AgentList}|Groups],Counter) ->
    countGroups(Groups,Counter#counters{reproduction = length(AgentList)}).