<TeXmacs|1.0.4.2>

<style|generic>

<\body>
  <section|Classical models of group selection>

  <subsection|Haldane>

  (according to ``The Causes of Evolution'', appendix ``Socially valuable but
  individually disadvantageous characters'', 1932)

  populations grow, within population competition, and split if they get too
  big.

  We will use the same dynamics within the group as we did before: (Haldane's
  model is diploid, ours is haploid)

  populations grow, within population competition, and split if they get too
  big.

  We will use the same dynamics within the group as we did before:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      f=1:2; f[1]=1; f[2]=1.05
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      f
    </input>

    <\output>
      [1] 1.00 1.05
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      g=sample(1:2,10,rep=T)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      g
    </input>

    <\output>
      \ [1] 1 2 1 2 2 1 1 2 2 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      N=length(g)

      individuals=1:N

      fitnesses=f[g]

      parents=sample(individuals,N,replace=TRUE,prob=fitnesses/sum(fitnesses)
      )

      g.offsprings=g[parents]
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      g.offsprings
    </input>

    <\output>
      \ [1] 1 2 2 2 2 1 1 2 1 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  And, again we will let the population grow according to a benefit gained
  from altruists:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      b=0.05
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      N=length(g)

      Np=N*(1+sum(g==1)/N*b)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      N
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      Np
    </input>

    <\output>
      [1] 10
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  But, now we will have several populations. This is done by using a list of
  populations

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      replicate(3,1:4,simplify=F)
    </input>

    <\output>
      [1] 10.2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      l=replicate(3,1:4,simplify=F)
    </input>

    <\output>
      [[1]]

      [1] 1 2 3 4

      \;

      [[2]]

      [1] 1 2 3 4

      \;

      [[3]]

      [1] 1 2 3 4
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      l
    </input>

    <\output>
      [[1]]

      [1] 1 2 3 4

      \;

      [[2]]

      [1] 1 2 3 4

      \;

      [[3]]

      [1] 1 2 3 4
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      l[[1]]
    </input>

    <\output>
      [1] 1 2 3 4
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      l[[2]]
    </input>

    <\output>
      [1] 1 2 3 4
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  We created a list that has 3 elements, each of which is a list. This is
  very usefull for dealing with populations:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P=replicate(10,sample(1:2,10,rep=T),simp=F)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P
    </input>

    <\output>
      [[1]]

      \ [1] 1 1 1 2 2 2 1 1 2 2

      \;

      [[2]]

      \ [1] 2 2 2 2 2 2 2 1 1 2

      \;

      [[3]]

      \ [1] 1 1 1 2 2 1 1 1 1 1

      \;

      [[4]]

      \ [1] 2 2 2 1 1 1 2 2 2 1

      \;

      [[5]]

      \ [1] 2 2 2 2 2 2 1 1 1 2

      \;

      [[6]]

      \ [1] 2 1 2 1 2 2 1 1 2 1

      \;

      [[7]]

      \ [1] 1 1 1 1 1 2 2 2 2 2

      \;

      [[8]]

      \ [1] 1 2 2 1 1 2 2 2 2 2

      \;

      [[9]]

      \ [1] 1 2 2 2 2 1 2 2 1 1

      \;

      [[10]]

      \ [1] 2 1 1 1 2 1 2 2 2 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Just as with vectors, we can access parts of lists:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P[1:2]
    </input>

    <\output>
      [[1]]

      \ [1] 1 1 1 2 2 2 1 1 2 2

      \;

      [[2]]

      \ [1] 2 2 2 2 2 2 2 1 1 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P[2:4]
    </input>

    <\output>
      [[1]]

      \ [1] 2 2 2 2 2 2 2 1 1 2

      \;

      [[2]]

      \ [1] 1 1 1 2 2 1 1 1 1 1

      \;

      [[3]]

      \ [1] 2 2 2 1 1 1 2 2 2 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Notice the confusing difference:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P[1:2]
    </input>

    <\output>
      [[1]]

      \ [1] 1 1 1 2 2 2 1 1 2 2

      \;

      [[2]]

      \ [1] 2 2 2 2 2 2 2 1 1 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P[1]
    </input>

    <\output>
      [[1]]

      \ [1] 1 1 1 2 2 2 1 1 2 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  The result in both cases is a list, the second one of length 1.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P[[1]]
    </input>

    <\output>
      \ [1] 1 1 1 2 2 2 1 1 2 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  The the result now is a vector, it is just an element of the list!

  We can now just do operations on all elements of the list:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      for(g in P) {

      \ print(g)

      }
    </input>

    <\input|<with|color|red|+ + <with|color|black|>>>
      b=0.3; f=1:2; f[1]=1; f[2]=1.05
    </input>

    <\output>
      \ [1] 1 1 1 2 2 2 1 1 2 2

      \ [1] 2 2 2 2 2 2 2 1 1 2

      \ [1] 1 1 1 2 2 1 1 1 1 1

      \ [1] 2 2 2 1 1 1 2 2 2 1

      \ [1] 2 2 2 2 2 2 1 1 1 2

      \ [1] 2 1 2 1 2 2 1 1 2 1

      \ [1] 1 1 1 1 1 2 2 2 2 2

      \ [1] 1 2 2 1 1 2 2 2 2 2

      \ [1] 1 2 2 2 2 1 2 2 1 1

      \ [1] 2 1 1 1 2 1 2 2 2 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      for(g in P) {

      \ N=length(g)

      \ fitnesses=f[g]

      \ Np=N*(1+sum(g==1)/N*b)

      \ parents=sample(individuals,Np,replace=T,prob=fitnesses/sum(fitnesses)
      )

      \ g.offsprings=g[parents]

      \ print(g.offsprings)

      }
    </input>

    <\output>
      + + + + + + + \ [1] 1 2 1 1 2 1 1 1 2 1 2

      \;

      \ [1] 2 2 2 2 2 2 2 2 2 2

      \;

      \ [1] 2 1 1 2 2 1 1 1 1 1 1 1

      \;

      \ [1] 1 1 1 1 2 1 2 1 2 1 1

      \ [1] 2 2 2 2 2 1 2 1 2 2

      \;

      \ [1] 1 1 1 2 2 2 1 1 1 2 2

      \;

      \ [1] 2 1 1 1 2 1 2 2 2 1 2

      \;

      \ [1] 2 2 2 2 2 1 2 2 1 2

      \;

      \ [1] 2 2 2 1 1 2 1 2 2 1 1

      \;

      \ [1] 1 2 2 2 1 1 1 2 1 1 2
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Now we only need 2 more things:

  We need to remember the new populations - till now we only printed them
  out,

  and we need to split a population if it is big enough.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P=list()
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P[[length(new.P)+1]]=1:3
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P
    </input>

    <\output>
      [[1]]

      [1] 1 2 3
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P[[length(new.P)+1]]=1:3
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P
    </input>

    <\output>
      [[1]]

      [1] 1 2 3

      \;

      [[2]]

      [1] 1 2 3
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  So, now we can produce the new population:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P=list()

      for(g in P) {

      \ N=length(g)

      \ fitnesses=f[g]

      \ Np=N*(1+sum(g==1)/N*b)

      \ parents=sample(individuals,Np,replace=T,prob=fitnesses/sum(fitnesses)
      )

      \ g.offsprings=g[parents]

      \ new.P[[length(new.P)+1]]=g.offsprings

      }
    </input>

    <\input|<with|color|red|\<gtr\> + + + + + + + <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      new.P
    </input>

    <\output>
      [[1]]

      \ [1] 1 1 1 2 1 2 1 2 1 1 1

      \;

      [[2]]

      \ [1] 1 2 1 1 2 2 1 2 2 2

      \;

      [[3]]

      \ [1] 2 1 1 1 1 1 2 1 2 1 1 1

      \;

      [[4]]

      \ [1] 1 1 1 1 1 2 2 2 1 1 2

      \;

      [[5]]

      \ [1] 1 1 2 2 1 2 1 2 2 1

      \;

      [[6]]

      \ [1] 1 2 2 1 1 2 2 2 2 2 1

      \;

      [[7]]

      \ [1] 2 2 2 2 2 1 1 2 1 2 1

      \;

      [[8]]

      \ [1] 2 1 2 2 2 2 1 2 1 2

      \;

      [[9]]

      \ [1] 2 2 2 2 1 2 2 2 2 2 1

      \;

      [[10]]

      \ [1] 1 1 1 1 1 1 1 2 2 2 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Finally the split:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      g
    </input>

    <\output>
      \ [1] 2 1 1 1 2 1 2 2 2 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      if( length(g) \<gtr\> 5 ) {

      \ \ m=length(g)/2

      \ \ print(g[1:m])

      \ \ print( g[ (m+1):length(g) ] )

      } else {

      \ \ print(g)

      }
    </input>

    <\output>
      + + + + + + [1] 2 1 1 1 2

      [1] 1 2 2 2 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      g=g[1:4]
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      g
    </input>

    <\output>
      [1] 2 1 1 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      if( length(g) \<gtr\> 5 ) {

      \ \ m=length(g)/2

      \ \ print(g[1:m])

      \ \ print( g[ (m+1):length(g) ] )

      } else {

      \ \ print(g)

      }
    </input>

    <\output>
      + + + + + + [1] 2 1 1 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Now we can do it all together:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P=replicate(10,sample(1:2,10,rep=T),simp=F)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      b=0.3; f=1:2; f[1]=1; f[2]=1.2
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      for( generations in 1:100) {

      \ new.P=list()

      \ for(g in P) {

      \ \ N=length(g)

      \ \ fitnesses=f[g]

      \ \ Np=N*(1+sum(g==1)/N*b)

      \ \ individuals=1:N

      \ \ parents=sample(individuals,Np,replace=T,prob=fitnesses/sum(fitnesses)
      )

      \ \ g.offsprings=g[parents]

      \ \ Np=length(g.offsprings)

      \ \ if( Np \<gtr\> 20 ) {

      \ \ \ \ m=Np/2

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings[1:m]

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings[(m+1):Np]

      \ \ } else {

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings

      \ \ }

      \ }

      \ P = sample(new.P,10)

      }
    </input>

    <\input|<with|color|red|+ + + + + + + + + + + + + + + + + + +
    <with|color|black|>>>
      P
    </input>

    <\output>
      [[1]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[2]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[3]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[4]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[5]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[6]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[7]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[8]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[9]]

      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

      \;

      [[10]]
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>

    <\output>
      \ [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  The altruists have won!

  Let us see what happened:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P=replicate(10,sample(1:2,10,rep=T),simp=F)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      b=0.3; f=1:2; f[1]=1; f[2]=1.2
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      res=1:100
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      for( generations in 1:100) {

      \ new.P=list()

      \ for(g in P) {

      \ \ N=length(g)

      \ \ fitnesses=f[g]

      \ \ Np=N*(1+sum(g==1)/N*b)

      \ \ individuals=1:N

      \ \ parents=sample(individuals,Np,replace=T,prob=fitnesses/sum(fitnesses)
      )

      \ \ g.offsprings=g[parents]

      \ \ Np=length(g.offsprings)

      \ \ if( Np \<gtr\> 20 ) {

      \ \ \ \ m=Np/2

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings[1:m]

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings[(m+1):Np]

      \ \ } else {

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings

      \ \ }

      \ }

      \ P = sample(new.P,10)

      \ res[generations]=sum(unlist(P)==1)/length(unlist(P))

      }
    </input>

    <\input|<with|color|red|+ <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      plot(res,type="l");v()
    </input>

    <\output>
      <postscript|<tuple|<#252150532D41646F62652D332E3020455053462D332E300A2525446F63756D656E744E65656465645265736F75726365733A20666F6E742048656C7665746963610A25252B20666F6E742048656C7665746963612D426F6C640A25252B20666F6E742048656C7665746963612D4F626C697175650A25252B20666F6E742048656C7665746963612D426F6C644F626C697175650A25252B20666F6E742053796D626F6C0A25255469746C653A2052204772617068696373204F75747075740A252543726561746F723A205220536F6674776172650A252550616765733A20286174656E64290A2525426F756E64696E67426F783A203135342032373720343432203536350A2525456E64436F6D6D656E74730A2525426567696E50726F6C6F670A2F627020207B206773206773207D206465660A2520626567696E202E70732E70726F6C6F670A2F677320207B206773617665207D206465660A2F677220207B2067726573746F7265207D206465660A2F657020207B2073686F7770616765206772206772207D206465660A2F6D2020207B206D6F7665746F207D206465660A2F6C20207B20726C696E65746F207D206465660A2F6E7020207B206E657770617468207D206465660A2F637020207B20636C6F736570617468207D206465660A2F662020207B2066696C6C207D206465660A2F6F2020207B207374726F6B65207D206465660A2F632020207B206E65777061746820302033363020617263207D206465660A2F722020207B2034203220726F6C6C206D6F7665746F203120636F70792033202D3120726F6C6C20657863682030206578636820726C696E65746F203020726C696E65746F202D31206D756C2030206578636820726C696E65746F20636C6F736570617468207D206465660A2F703120207B207374726F6B65207D206465660A2F703220207B20677361766520626720736574726762636F6C6F722066696C6C2067726573746F7265206E657770617468207D206465660A2F703320207B20677361766520626720736574726762636F6C6F722066696C6C2067726573746F7265207374726F6B65207D206465660A2F742020207B2036202D3220726F6C6C206D6F7665746F20677361766520726F746174650A202020202020207073206D756C206E656720302032203120726F6C6C20726D6F7665746F0A202020202020203120696E64657820737472696E67776964746820706F700A202020202020206D756C206E6567203020726D6F7665746F2073686F772067726573746F7265207D206465660A2F636C20207B2067726573746F7265206773617665206E657770617468203320696E646578203320696E646578206D6F7665746F203120696E6465780A2020202020202034202D3120726F6C6C206C696E65746F202065786368203120696E646578206C696E65746F206C696E65746F0A20202020202020636C6F73657061746820636C6970206E657770617468207D206465660A2F726762207B20736574726762636F6C6F72207D206465660A2F732020207B207363616C65666F6E7420736574666F6E74207D206465660A2F522020207B202F466F6E74312066696E64666F6E74207D206465660A2F422020207B202F466F6E74322066696E64666F6E74207D206465660A2F492020207B202F466F6E74332066696E64666F6E74207D206465660A2F424920207B202F466F6E74342066696E64666F6E74207D206465660A2F532020207B202F466F6E74352066696E64666F6E74207D206465660A31207365746C696E656361702031207365746C696E656A6F696E0A2520656E642020202E70732E70726F6C6F670A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963610A2F48656C7665746963612066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7431206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963612D426F6C640A2F48656C7665746963612D426F6C642066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7432206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963612D4F626C697175650A2F48656C7665746963612D4F626C697175652066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7433206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963612D426F6C644F626C697175650A2F48656C7665746963612D426F6C644F626C697175652066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7434206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742053796D626F6C0A2F53796D626F6C2066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A202063757272656E74646963740A2020656E640A2F466F6E7435206578636820646566696E65666F6E7420706F700A2525456E6450726F6C6F670A2525506167653A203220320A62700A3230322E3834203333382E3134203431362E3434203531352E373420636C0A3230322E3834203333382E3134203431362E3434203531352E373420636C0A3020302030207267620A302E3735207365746C696E6577696474680A5B5D203020736574646173680A6E700A3231302E3735203338322E3936206D0A322E303020322E3438206C0A312E3939202D31322E3634206C0A322E3030202D372E3339206C0A322E3030202D32302E3639206C0A322E30302031312E3538206C0A322E3030202D312E3039206C0A312E39392031342E3735206C0A322E303020352E3732206C0A322E3030202D32312E3038206C0A322E30302032322E3136206C0A312E39392031332E3232206C0A322E303020332E3537206C0A322E303020352E3134206C0A322E30302034302E3039206C0A322E303020392E3235206C0A312E3939202D302E3038206C0A322E303020382E3633206C0A322E30302031392E3730206C0A322E303020322E3837206C0A312E3939202D31352E3837206C0A322E303020332E3933206C0A322E303020382E3938206C0A322E30302031312E3739206C0A322E303020322E3235206C0A312E393920332E3638206C0A322E30302031352E3236206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A6F0A3135332E3634203237362E3934203434312E3634203536342E393420636C0A3020302030207267620A302E3735207365746C696E6577696474680A5B5D203020736574646173680A6E700A3230382E3735203333382E3134206D0A3139392E37382030206C0A6F0A6E700A3230382E3735203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3234382E3731203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3238382E3636203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3332382E3632203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3336382E3537203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3430382E3533203333382E3134206D0A30202D362E3030206C0A6F0A2F707320313220646566205220313220730A3230382E3735203331362E353420283029202E352030203020740A3234382E3731203331362E35342028323029202E352030203020740A3238382E3636203331362E35342028343029202E352030203020740A3332382E3632203331362E35342028363029202E352030203020740A3336382E3537203331362E35342028383029202E352030203020740A3430382E3533203331362E3534202831303029202E352030203020740A6E700A3230322E3834203334382E3938206D0A30203136302E3139206C0A6F0A6E700A3230322E3834203334382E3938206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203337352E3638206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203430322E3337206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203432392E3037206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203435352E3737206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203438322E3437206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203530392E3137206D0A2D362E30302030206C0A6F0A3138382E3434203334382E39382028302E3429202E35203020393020740A3138382E3434203337352E36382028302E3529202E35203020393020740A3138382E3434203430322E33372028302E3629202E35203020393020740A3138382E3434203432392E30372028302E3729202E35203020393020740A3138382E3434203435352E37372028302E3829202E35203020393020740A3138382E3434203438322E34372028302E3929202E35203020393020740A3138382E3434203530392E31372028312E3029202E35203020393020740A6E700A3230322E3834203333382E3134206D0A3231332E36302030206C0A30203137372E3630206C0A2D3231332E36302030206C0A30202D3137372E3630206C0A6F0A3135332E3634203237362E3934203434312E3634203536342E393420636C0A2F707320313220646566205220313220730A3020302030207267620A3330392E3634203239322E35342028496E64657829202E352030203020740A3136342E3434203432362E3934202872657329202E35203020393020740A65700A2525547261696C65720A252550616765733A20320A2525454F460A>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P=replicate(10,sample(1:2,20,rep=T),simp=F)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      b=0.3; f=1:2; f[1]=1; f[2]=1.2
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      res=1:100
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      for( generations in 1:100) {

      \ new.P=list()

      \ for(g in P) {

      \ \ N=length(g)

      \ \ fitnesses=f[g]

      \ \ Np=N*(1+sum(g==1)/N*b)

      \ \ individuals=1:N

      \ \ parents=sample(individuals,Np,replace=T,prob=fitnesses/sum(fitnesses)
      )

      \ \ g.offsprings=g[parents]

      \ \ Np=length(g.offsprings)

      \ \ if( Np \<gtr\> 40 ) {

      \ \ \ \ m=Np/2

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings[1:m]

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings[(m+1):Np]

      \ \ } else {

      \ \ \ \ new.P[[length(new.P)+1]]=g.offsprings

      \ \ }

      \ }

      \ P = sample(new.P,10)

      \ res[generations]=sum(unlist(P)==1)/length(unlist(P))

      }
    </input>

    <\input|<with|color|red|+ <with|color|black|>>>
      \;
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      plot(res,type="l");v()
    </input>

    <\output>
      <postscript|<tuple|<#252150532D41646F62652D332E3020455053462D332E300A2525446F63756D656E744E65656465645265736F75726365733A20666F6E742048656C7665746963610A25252B20666F6E742048656C7665746963612D426F6C640A25252B20666F6E742048656C7665746963612D4F626C697175650A25252B20666F6E742048656C7665746963612D426F6C644F626C697175650A25252B20666F6E742053796D626F6C0A25255469746C653A2052204772617068696373204F75747075740A252543726561746F723A205220536F6674776172650A252550616765733A20286174656E64290A2525426F756E64696E67426F783A203135342032373720343432203536350A2525456E64436F6D6D656E74730A2525426567696E50726F6C6F670A2F627020207B206773206773207D206465660A2520626567696E202E70732E70726F6C6F670A2F677320207B206773617665207D206465660A2F677220207B2067726573746F7265207D206465660A2F657020207B2073686F7770616765206772206772207D206465660A2F6D2020207B206D6F7665746F207D206465660A2F6C20207B20726C696E65746F207D206465660A2F6E7020207B206E657770617468207D206465660A2F637020207B20636C6F736570617468207D206465660A2F662020207B2066696C6C207D206465660A2F6F2020207B207374726F6B65207D206465660A2F632020207B206E65777061746820302033363020617263207D206465660A2F722020207B2034203220726F6C6C206D6F7665746F203120636F70792033202D3120726F6C6C20657863682030206578636820726C696E65746F203020726C696E65746F202D31206D756C2030206578636820726C696E65746F20636C6F736570617468207D206465660A2F703120207B207374726F6B65207D206465660A2F703220207B20677361766520626720736574726762636F6C6F722066696C6C2067726573746F7265206E657770617468207D206465660A2F703320207B20677361766520626720736574726762636F6C6F722066696C6C2067726573746F7265207374726F6B65207D206465660A2F742020207B2036202D3220726F6C6C206D6F7665746F20677361766520726F746174650A202020202020207073206D756C206E656720302032203120726F6C6C20726D6F7665746F0A202020202020203120696E64657820737472696E67776964746820706F700A202020202020206D756C206E6567203020726D6F7665746F2073686F772067726573746F7265207D206465660A2F636C20207B2067726573746F7265206773617665206E657770617468203320696E646578203320696E646578206D6F7665746F203120696E6465780A2020202020202034202D3120726F6C6C206C696E65746F202065786368203120696E646578206C696E65746F206C696E65746F0A20202020202020636C6F73657061746820636C6970206E657770617468207D206465660A2F726762207B20736574726762636F6C6F72207D206465660A2F732020207B207363616C65666F6E7420736574666F6E74207D206465660A2F522020207B202F466F6E74312066696E64666F6E74207D206465660A2F422020207B202F466F6E74322066696E64666F6E74207D206465660A2F492020207B202F466F6E74332066696E64666F6E74207D206465660A2F424920207B202F466F6E74342066696E64666F6E74207D206465660A2F532020207B202F466F6E74352066696E64666F6E74207D206465660A31207365746C696E656361702031207365746C696E656A6F696E0A2520656E642020202E70732E70726F6C6F670A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963610A2F48656C7665746963612066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7431206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963612D426F6C640A2F48656C7665746963612D426F6C642066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7432206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963612D4F626C697175650A2F48656C7665746963612D4F626C697175652066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7433206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742048656C7665746963612D426F6C644F626C697175650A2F48656C7665746963612D426F6C644F626C697175652066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A20202F456E636F64696E672049534F4C6174696E31456E636F64696E67206465660A202063757272656E74646963740A2020656E640A2F466F6E7434206578636820646566696E65666F6E7420706F700A2525496E636C7564655265736F757263653A20666F6E742053796D626F6C0A2F53796D626F6C2066696E64666F6E740A647570206C656E677468206469637420626567696E0A20207B3120696E646578202F464944206E65207B6465667D207B706F7020706F707D206966656C73657D20666F72616C6C0A202063757272656E74646963740A2020656E640A2F466F6E7435206578636820646566696E65666F6E7420706F700A2525456E6450726F6C6F670A2525506167653A203220320A62700A3230322E3834203333382E3134203431362E3434203531352E373420636C0A3230322E3834203333382E3134203431362E3434203531352E373420636C0A3020302030207267620A302E3735207365746C696E6577696474680A5B5D203020736574646173680A6E700A3231302E3735203530392E3137206D0A322E3030202D31372E3938206C0A312E3939202D382E3534206C0A322E3030202D382E3238206C0A322E3030202D32392E3236206C0A322E3030202D31362E3232206C0A322E3030202D31342E3033206C0A312E3939202D382E3233206C0A322E3030202D31342E3036206C0A322E3030202D362E3439206C0A322E3030202D322E3139206C0A312E3939202D332E3431206C0A322E3030202D362E3139206C0A322E3030202D372E3338206C0A322E3030202D302E3235206C0A322E303020332E3832206C0A312E3939202D342E3338206C0A322E3030202D312E3633206C0A322E303020342E3836206C0A322E3030202D382E3934206C0A312E3939202D322E3835206C0A322E3030202D322E3838206C0A322E3030202D342E3238206C0A322E303020312E3431206C0A322E30302030206C0A312E3939202D312E3431206C0A322E3030202D312E3432206C0A322E303020312E3432206C0A322E3030202D302E3032206C0A312E3939202D322E3833206C0A322E3030202D312E3430206C0A322E3030202D312E3431206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A312E39392030206C0A322E30302030206C0A322E30302030206C0A322E30302030206C0A6F0A3135332E3634203237362E3934203434312E3634203536342E393420636C0A3020302030207267620A302E3735207365746C696E6577696474680A5B5D203020736574646173680A6E700A3230382E3735203333382E3134206D0A3139392E37382030206C0A6F0A6E700A3230382E3735203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3234382E3731203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3238382E3636203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3332382E3632203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3336382E3537203333382E3134206D0A30202D362E3030206C0A6F0A6E700A3430382E3533203333382E3134206D0A30202D362E3030206C0A6F0A2F707320313220646566205220313220730A3230382E3735203331362E353420283029202E352030203020740A3234382E3731203331362E35342028323029202E352030203020740A3238382E3636203331362E35342028343029202E352030203020740A3332382E3632203331362E35342028363029202E352030203020740A3336382E3537203331362E35342028383029202E352030203020740A3430382E3533203331362E3534202831303029202E352030203020740A6E700A3230322E3834203334342E3732206D0A30203134352E3839206C0A6F0A6E700A3230322E3834203334342E3732206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203338312E3139206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203431372E3636206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203435342E3134206D0A2D362E30302030206C0A6F0A6E700A3230322E3834203439302E3631206D0A2D362E30302030206C0A6F0A3138382E3434203334342E37322028302E3029202E35203020393020740A3138382E3434203338312E31392028302E3129202E35203020393020740A3138382E3434203431372E36362028302E3229202E35203020393020740A3138382E3434203435342E31342028302E3329202E35203020393020740A3138382E3434203439302E36312028302E3429202E35203020393020740A6E700A3230322E3834203333382E3134206D0A3231332E36302030206C0A30203137372E3630206C0A2D3231332E36302030206C0A30202D3137372E3630206C0A6F0A3135332E3634203237362E3934203434312E3634203536342E393420636C0A2F707320313220646566205220313220730A3020302030207267620A3330392E3634203239322E35342028496E64657829202E352030203020740A3136342E3434203432362E3934202872657329202E35203020393020740A65700A2525547261696C65720A252550616765733A20320A2525454F460A>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      P.N=100
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  We see that with a population size of 20, you need many populations for the
  altruists to win.

  Why is this?

  In every population, other than one of only alruists, selfish individuals
  win.

  If at some point, an all-altruist population is produced, it takes over
  with a high chance.

  <subsubsection|Homework>

  start with a single population with a fixed number of altruists and selfish
  individuals (for example 17 altruists and 3 selfish.) Evolve that one
  population for several generations, without sub-sampling populations, and
  look at the resulting distribution of populations.

  How often do you get 18-2, 19-1, 20-0?

  \;

  \;
</body>

<\references>
  <\collection>
    <associate|auto-1|<tuple|1|1>>
    <associate|auto-2|<tuple|1.1|1>>
    <associate|auto-3|<tuple|1.1.1|9>>
    <associate|auto-4|<tuple|1.2|?>>
  </collection>
</references>

<\auxiliary>
  <\collection>
    <\associate|toc>
      <vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Classical
      models of group selection> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
      <pageref|auto-1><vspace|0.5fn>

      <with|par-left|<quote|1.5fn>|Haldane
      <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
      <pageref|auto-2>>

      <with|par-left|<quote|3fn>|Homework
      <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
      <pageref|auto-3>>
    </associate>
  </collection>
</auxiliary>
