90
Competing Cultural Strategies: The Evolution of Religion by David Edward Ralph, B.S. A Thesis In Biology Submitted to the Graduate Faculty of Texas Tech University in Partial Fulfillment of the Requirements for the Degree of MASTER OF SCIENCES Approved Dr. Sean H. Rice Chair of Committee Dr. Arthur Durband Dr. Rich Strauss Peggy Gordon Miller Dean of the Graduate School May, 2012

Copyright 2012, David Ralph

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Competing Cultural Strategies: The Evolution of Religion

by

David Edward Ralph, B.S.

A Thesis

In

Biology

Submitted to the Graduate Faculty of Texas Tech University in

Partial Fulfillment of the Requirements for

the Degree of

MASTER OF SCIENCES

Approved

Dr. Sean H. Rice Chair of Committee

Dr. Arthur Durband

Dr. Rich Strauss

Peggy Gordon Miller Dean of the Graduate School

May, 2012

Copyright 2012, David Ralph

Texas Tech University, David Ralph, May 2012

ii

ACKNOWLEDGEMENTS

I would like to thank my advisor Dr. Rice and my other committee members

Dr. Strauss, and Dr. Durband for the guidance and support throughout my time as a

graduate student. I would also like to thank both Dr. Zak and Dr. Densmore for the

outstanding job they have both performed in running the department, and of course

all of the extremely helpful ladies in the front offices who are the real brains behind

the operation. I would also like to express my appreciation and love for my parents

for their support and their neverending patience, and my extraordinary wife and

her less-than neverending patience. I am extremely honored and grateful for having

Dr. Papadopoulos as both a friend and a colleague who has been an inspiration and a

grounding force throughout my time as a graduate student. I would also like to

thank Ben Qin, Morgan Baker, and John Harting; all of whom have made the lab a

great place to work. Finally I would like to express my gratitude to the wonderful

Biology faculty members and graduate students that I have spent time with over the

years.

Texas Tech University, David Ralph, May 2012

iii

TABLE OF CONTENTS

ACKNOWLEDGMENTS ........................................................................................................................... ii

ABSTRACT .................................................................................................................................................. v

LIST OF FIGURES .................................................................................................................................... vi

CHAPTER

I. INTRODUCTION ................................................................................................................................... 1

II. MODELS FOR COMPETING CULTURAL STRATEGIES ........................................................... 4

Simulation Model .............................................................................................................................. 4

Analytical Model ............................................................................................................................... 6

Differences Between Models........................................................................................................ 8

III. RESULTS ............................................................................................................................................. 11

Simple Case .......................................................................................................................................11

Limited Conversion .......................................................................................................................13

Rare Converts ..................................................................................................................................16

Rare Killers .......................................................................................................................................21

IV. DISCUSSION AND CONCLUSIONS .............................................................................................. 25

Pure Strategy Effectiveness ........................................................................................................25

Strategy Behavior ...........................................................................................................................26

Texas Tech University, David Ralph, May 2012

iv

Consequences for Human Evolution .......................................................................................27

Future Research ..............................................................................................................................27

REFERENCES .......................................................................................................................................... 29

APPENDICES ........................................................................................................................................... 32

A. SIMULATION PROGRAM ....................................................................................................... 32

B. ANALYTICAL PROGRAM ....................................................................................................... 70

Texas Tech University, David Ralph, May 2012

v

ABSTRACT

Understanding the impact of culture systems and cultural evolution is

integral to understanding human evolution. Cultural systems have the property of

both horizontal and vertical transmission of non-genetic highly heritable cultural

phenotypes. This is particularly important when group identity is related to a

specific cultural phenotype, as in religious systems. This study examines the effects

of the process of conversion, in which an individual’s phenotype is changed within

its generational timeframe. A game-theory approach using behavioral strategies

was used to model the cultural group interactions. A stochastic simulation and a

deterministic analytical framework were used to model this system. Groups that

used a conversion strategy outcompeted groups that used a kill strategy unless

heavily constrained. The results suggest that the ability to convert may have played

a substantial role in the evolution of cultural systems such as religion and

government, as well as directly impacted the direction of human evolution.

Texas Tech University, David Ralph, May 2012

vi

LIST OF FIGURES

2.1 Effects of drift on distribution shape ................................................................................ 9

3.1 First divergence with preliminary parameters ..........................................................11

3.2 Continued divergence with preliminary parameters ...............................................12

3.3 Final divergence with preliminary parameters ..........................................................13

3.4 Effects of reduced probability to convert within the analytical model .............14

3.5 Effects of reduced probability to convert within the simulation .........................15

3.6 A magnified look at the power of conversion within the analytical model ....16

3.7 The rise of 1 convert within the analytical model .....................................................17

3.8 Deme C increasing when rare within the analytical model ...................................18

3.9 Fixation of deme C after starting rare within the analytical model ....................18

3.10 Deme C increasing when rare within the simulation ...............................................19

3.11 Deme C starting rare with low probability to convert within the

analytical model ......................................................................................................................20

3.12 Deme K increasing when rare while convert rate is 0.1..........................................22

3.13 Deme K increasing when rare while convert rate is 0.05 .......................................23

Texas Tech University, David Ralph, May 2012

1

CHAPTER I

INTRODUCTION

A defining characteristic of Homo sapiens is the use of complex culture. The

ability to create and pass on novel behavior as well as build upon existing

knowledge has allowed our species to expand globally. In the last 50 years there

has been an extensive amount of multidisciplinary research directed towards

understanding the evolution of culture, with much of the work directed towards

understanding how culture is transmitted, and what the effects of culture are on the

evolution of behavior; such as the existence of altruism and cooperation (see Boyd &

Richerson, 1985, Boyd & Richerson, 2005 for a comprehensive review). While

research into the topics of cultural transmission (Smith et al., 2008, O'Brien et al.,

2010, Rendell et al., 2011) and pro-social group behavior (Lehmann & Feldman,

2008, Andre & Morin, 2011) are still relevant, much of the focus on human cultural

evolution has moved towards understanding cumulative culture; the act of cultural

ideas building upon themselves (Enquist & Ghirlanda, 2007, Caldwell & Millen,

2008, Boyd & Richerson, 2009, Aoki, 2010, Caldwell & Millen, 2010, Lehmann et al.,

2010, Enquist et al., 2011, Mesoudi, 2011).

What is seemingly lacking in the literature is the evaluation of the impact that

the basic inherent property of culture of having both horizontal and vertical

transmission of highly heritable non-genetic cultural phenotypes, may have on the

population dynamics of groups when group identity is defined by a specific cultural

Texas Tech University, David Ralph, May 2012

2

phenotype. In particular, the process of conversion, in which an individual’s

phenotype is changed within its generational timeframe, may have important

biological consequences in the evolution of populations and cultural systems. This

process may have a rather notable impact in human societies, both presently and

historically, as most groups are defined using social identifiers rather than kin

groups.

The evolution of religion may be one such case in which the underlying

evolutionary mechanics are impacted by conversion effects. In fact it was noted that

Catholics have a higher fitness than non-Cathlics due to religious beliefs regarding

birth control (Janssen & Hauser, 1981). Much of the current work concerning the

evolution of religion is focused on the diversification of religious systems which

seem to change to better mirror the concerns of the group with regard to societal

complexity (Sanderson & Roberts, 2008). Some models regarding the evolution of

religion indicate social mechanisms such as the priming of prosocial behavior or

reputational concerns may help maintain group cohesiveness (Shariff &

Norenzayan, 2007, Norenzayan & Shariff, 2008), while others suggest that religion

is a byproduct of these social mechanisms (Pyysiainen & Hauser, 2010). Whereas

these studies indicate processes that allow for cultural groups to persist, what is

missing is a method grounded in evolutionary mechanics that can shed light on the

origins of religion and other cultural groups.

Texas Tech University, David Ralph, May 2012

3

While religion is just one of several types of large scale cultural groups, it is

not only a logical choice to examine the possible effects of conversion, but it is also a

good initial model for examining cultural evolution in general due to the nature of

religious systems. Popular religions have the property of lasting many generations

with little variation; with offshoots forming when cultural ideology changes.

Religion is also highly heritable as offspring generally adopt the religion of their

parents. Another important property is that unlike many cultural systems such as

governments, western religion is mutually exclusive to the individuals that practice

those beliefs. For example, one cannot be both a Muslim and a Scientologist as the

creation myths within each system conflict (Janssen & Hauser, 1981, Haleem, 2005),

but one can belong to more than one government (e.g. a Navajo is both a member of

the Navajo Nation and the United States). Together, these properties of religion act

to simplify the cultural system without resorting to simplifying assumptions.

A game-theory approach first pioneered by Smith and Price (1973) using

behavioral strategies between competing organisms is used to examine the effects

of conversion between competing cultural groups. Two distinct models have been

created with Python 2.6 to examine the dynamics of competing cultural strategies.

Direct competition between two cultural strategies and the implications for human

evolution, both biologically and culturally, are presented.

Texas Tech University, David Ralph, May 2012

4

CHAPTER II

MODELS FOR COMPETING CULTURAL STRATEGIES

Simulation Model

The simulation model for competing cultural strategies is an individual-

based model that describes a stochastic system in which two or more cultural

groups compete for the same resources. Possible strategies include killing opposing

group members or converting them. While the model can easily accommodate

multiple groups each with a combination of strategies, both pure and mixed, the

following study describes a two-group pure strategy system.

The simulation relies on a few basic assumptions. Each individual has an

equal fitness to any other before interaction and competition is strictly determined

by the cultural strategy of their particular group. Generations are non-overlapping

and reproduction is asexual; with offspring generation occurring after interaction

between individuals. There is 100% vertical transmission of cultural values in the

model: offspring take on the phenotype of their parent.

Interaction between individuals using different strategies is governed by two

variables: encounter rate and influence. Encounter rate is the probability that a

group member i interacts with another individual that generation and can only take

real values between 0 and 1 at intervals of 0.1. This is a one-way interaction

designed to separate encounter rate into a group specific variable rather than a

Texas Tech University, David Ralph, May 2012

5

global variable for the region, and is impacted by the frequency distribution of

group members in the region. Influence is the probability that the encountered

group acted on group member i with results consistent with the encountered

group’s strategy and was also examined at 0 to 1 at intervals of 0.1 with an added

interval of 0.99. Conceptually, influence is a combination of cultural transmission

and persistence with converters, and the ease at which the kill strategy can kill.

Each simulation was run for 2500 iterations and followed 500 generations.

Within each generation reproduction took place after interaction; a function of

density dependency and group growth rate was used to define the mean of a

Poisson distribution which ultimately determined the number of offspring an

individual had based on an assigned random value. The number of individuals in

each group was recorded at set generation intervals of: 25, 50, 100, 250, and 500.

The expected population values were then taken at these intervals for analysis.

For the analysis the groups are labeled group K and group C to indicate

which pure strategy is employed. Group K’s strategy is to kill any outsiders they

encounter, whereas group C’s strategy is to convert any outsiders. Group K can be

thought of as the traditional way in which competitors directly compete if access

and ability to utilize resources is equal, whereas group C’s strategy is unique to

social groups with dynamic shared cultural values.

Texas Tech University, David Ralph, May 2012

6

Analytical Model

The analytical model for competing cultural strategies was developed by

breaking down what was observed during the simulation into biologically

meaningful parts and building a set of equations corresponding to each part; when

combined, the final equation predicts the change in group sizes. The values

generated by this deterministic model correlate to the expected values of the

simulation.

Table 2.1: Symbols and Notations

Symbol Definition

iN Number of individuals in group i

R Number of individuals in region

n Number of groups other than group i

iPK Probability of group i killing another group member when that group

encounters group i

iPC Probability of group i converting another group member when that

group encounters group i

iE Number of group i that encounter others in one generation

ir Growth rate for group i

Carrying capacity in region

iS Number of individuals in group i that do not interact

iSE Number of individuals in group i that interact safely

ic Number of converts gained by group i

’ Expected fitness for each individual in group i

iD’ Number of individuals in group i after one generation

Texas Tech University, David Ralph, May 2012

7

The equation describing the cultural competition system is easily broken

down into four important equations: first, equation 2.1, which represents the

number of group i members that do not interact in that generation,

iS = iN – iE ; (2.1)

second, equation 2.2, which represents the number of group i that interact safely,

iSE = iE * ( (jPK * jN/R) + (jPC * jN/R))); (2.2)

third, equation 2.3, which is perhaps the most important biologically, represents

population gain for group i from converts from other groups (represented as j),

ic =

jE * iPC * iN/R); (2.3)

and fourth, equation 2.4 accounts for the density dependency within the analytical

model,

’ = ir * (1 + (1- (R/ ))). (2.4)

The carrying capacity (represented as ) allows for a soft population cap

representing the resources for which the separate groups are competing. Note:

equation 2.3 represents a turning point in population growth dynamics.

Populations that are able to convert others are no longer constrained by

reproductive values or dependant on migrants for population gain.

Combining the above equations (2.1-2.4) yields iD’ = (iS + iSE + ic)* ’, which

expands into:

Texas Tech University, David Ralph, May 2012

8

iD’ = (iN – iE) + (iE * ( (jPK * jN/R) + (jPC * jN/R)))) + (2.5)

(

jE * iPC * iN/R)) * (ir * (1 + (1- (R/ )))).

D’ for group C and K were recorded at set generation intervals of: 25, 50, 100,

200, 300, 400, and 500. Encounter rate was examined from 0 to 1 at intervals of

0.05. Influence was also examined at the same rate with the additional value of 0.99

as an added test measure brought over from the simulation. As in the simulation,

the groups are labeled K and C to indicate the different pure strategies that are

employed: K for killing and C for converting.

Differences Between Models

As stated previously, the analytical model formulates an expected value of

the change in population size and corresponds directly to selection acting on the

cultural strategies of each group. The simulation generates data from following

individuals across thousands of possible outcomes. The expected value

distributions of these outcomes encompass more than just selection. Drift can play

a large role in shaping the distributions due to the probabilistic nature of many of

the simulation’s parameters, as well as the small group population size in several of

the cases examined.

Another important distinction between the differences noted is in what the

simulation figures actually represent due to the additional stochastic mechanisms

not found in the analytical model. For instance in figure 2.1A, it appears that at a

value of P = 0.6 (probability of either killing or converting) the expected size of

Texas Tech University, David Ralph, May 2012

9

group K at any given time within the total population is approximately 8 individuals.

The data however shows that at that value 2305 times out of the 2500 group K is

extinct, the other 195 times group C is extinct. Both groups, however, remain active

in the population with an influence of P = 0.1 in the simulation data, which

corresponds to the expected values seen in the analytical model (Fig 2.1B). This

difference is due to drift and an increase in the simulation’s starting population size

greatly reduces the variance in group K’s survivorship resulting in a much closer fit

(Fig 2.1 C). There are still slight variations after drift has been minimized due to

other moments of the distribution, but the differences can be safely ignored as there

is little impact on the overall shapes and trends observed.

(A) (B)

(C) (D)

Texas Tech University, David Ralph, May 2012

10

Figure 2.1 Effects of drift on distribution shape. 500 generations have passed with a 0.1 encounter

rate. Groups have equal starting populations and influence. A- B) Simulation and analytical

distributions with 55 members of each group. C- D) Simulation and analytical distributions with 275

members of each group.

Texas Tech University, David Ralph, May 2012

11

CHAPTER III

RESULTS

Simple Case

From equation 2.3 it is evident that the convert strategy has an added benefit

over killing. The question remains of the magnitude of difference. The first case

that will be examined is a simple case in which both groups start out at the same

population size and have equal probabilities in both encounter rate and influence.

The effectiveness of the conversion strategy is evident even when the percentage to

encounter is the lowest measured at 0.1 as there is an immediate divergence and

decline of group K within 25 generations (Fig 3.1A). This effect is mirrored in the

analytical model with an encounter rate of 0.05 suggesting that at any encounter

rate above 0 will produce the same effects (Fig 3.1B).

(A) (B)

Figure 3.1 First divergence with preliminary parameters. Population distribution when starting size

(55 individuals) and probability of influence are equal. A) Simulation: the first split after 25

Texas Tech University, David Ralph, May 2012

12

generations with encounter rate of 0.1. B) Analytical model showing the same trend with an

encounter rate of 0.05.

Following the groups over 500 generations shows group K declining to

extinction in the analytical model at influence values of 0.25 or greater when the

encounter rate is maintained at 0.05 (Fig 3.2A). While the divergence is similar in

the simulation model at a 0.01 encounter rate, group K may persist at all values due

to drift (Fig 3.2B).

(A) (B)

Figure 3.2 Continued divergence with simplified parameters. Population distribution when starting

size (55 individuals) and probability of influence are equal. A) Distribution of analytical model after

500 generations with encounter rate at 0.05. B) Distribution of simulation after 500 generations with

encounter rate at 0.1.

An increase in encounter rate to 0.80 results in extinction at every value of

influence for group K within 200 generations (Fig 3.3A) although an encounter rate

of 0.35 or above will delay results to within 500 generations. This trend is mirrored

in the simulation when population size is increased to 550 total individuals. It

Texas Tech University, David Ralph, May 2012

13

should be noted that even with drift group K will go extinct at values above P = 0.3

influence by 500 generations if encounter rate is 0.7 or higher (Fig 3.3B).

(A) (B)

Figure 3.3 Final divergence with simplified parameters. Population distribution when starting size

and probability of influence are the same. Encounter rate is 0.80. A) Analytical model: initial

population of 55 individuals in each group. B) Simulation: initial population of 275 individuals in each

group.

It is obvious that the convert strategy of group C will always win against the

killing strategy of group K when population size and influence are equal. This fact

may have a profound impact on population dynamics in cases in which a large

number of migrants move into a region at one time; however, it does not

demonstrate how the converts became populous in the first place.

Limited Conversion

The simplified parameter results are based on the assumption that there is

an equal influence probability for each group; realistically that may not be the case.

Depending on the environment it may be easier or harder to convert an opposing

Texas Tech University, David Ralph, May 2012

14

group member rather than kill them. While it is obvious that converts will continue

to do better if the probability to convert increases relative to killing, the opposite in

not necessarily true.

In fact setting the probability to convert to a constant 0.1 in the analytical

model reveals data on just how effective converting is compared to killing. Setting a

constant limit of PC = 0.1 to conversion influence, while maintaining equal starting

populations shows a very different trend emerge than that of the simplified case

(Fig 3.4A). A structurally unstable equilibrium point is created at the crossover

event at PK = 0.2 and is observed throughout the data set. To the left of this point

group C will continue to rise in number, and to the right group K will increase. The

first stable population distribution occurs by 500 generations with only a 0.3

encounter rate (Fig 3.4B).

(A) (B)

Figure 3.4 Effects of reduced probability to convert within the analytical model. Starting size is 55

individuals in each group and group C’s influence is set to 0.05 across all values. A) The first crossover

after 25 generations, encounter rate is 0.05. B) The first stable distribution after 500 generations,

encounter rate is 0.3. A structurally unstable equilibrium is seen at 0.2 group K influence.

Texas Tech University, David Ralph, May 2012

15

The simulation follows what is observed in the analytical model when the

influence of group C is set to PC = 0.1 (Fig 3.5). A stable equilibrium is centered at

PK = 0.2 influence, with converts increasing in frequency to the left of the point.

(A) (B)

Figure 3.5 Effects of reduced probability to convert within the simulation. Starting size is 55

individuals in each group and group C’s influence is set to 0.1 across all values. A) The first crossover

after 25 generations, encounter rate is 0.1. B) The distribution after 500 generations, encounter rate is

0.3. A stable equilibrium is seen at 0.2 group K influence.

A closer examination of group K’s influence values of 0 to 0.1 indicates that

the convert strategy is twice as effective as the killing strategy when population

sizes start out equally. The analytical model shows that within 500 generations

group C becomes fixed at all values less than 0.1 when encounter rate is 0.5 (Fig

3.6).

Texas Tech University, David Ralph, May 2012

16

Figure 3.6 A magnified look at the power of conversion within the analytical model. Starting size is 55

individuals in each group and group C’s influence is set to 0.05 across all values. 500 generations have

passed with an encounter rate of 0.5. Group C has become fixed at all values leading up to 0.1 including

0.099.

Rare Converts

The next question examined is perhaps the most important biologically: can

converts increase when rare? In fact when the probability to influence is equal

between groups the convert strategy is able to persist and increase in the analytical

model, even when group C starts with 1 individual. At high interaction values group

C is able to overtake group K with moderate encounter rates of 0.6 or higher within

200 generations (Fig 3.7A). By 500 generations an encounter rate of 0.25 or higher

will produce a crossover (Fig 3.7B), and when the encounter rate is 1.0 group K will

go extinct when influence is P = 0.25 or higher (Fig 3.7C). Not only can converts

increase when rare, but they have the capacity to drive group K extinct.

Texas Tech University, David Ralph, May 2012

17

(A) (B)

(C)

Figure 3.7 The rise of 1 convert within the analytical model. Starting size of 1 individual in group C

and 109 in group K. Equal probability of influence between groups. A) First crossover event:

Encounter rate is 0.6, 200 generations have passed. B) First crossover when encounter rate is 0.25,

500 generations have passed C) Distribution with maximum parameter values: Encounter rate is 1.0,

500 generations have passed.

An increase in starting population size of Group C to 10 individuals instead of

1 drastically increases the impact group C has on group K. The analytical model

shows that within 25 generations a crossover event occurs if influence is high (Fig

3.8A), or even extinction of group K if the encounter rate is high (Fig 3.8B).

Texas Tech University, David Ralph, May 2012

18

(A) (B)

Figure 3.8 Group C increasing when rare within the analytical model. Starting size of 10 individuals in

group C and 100 in group K. Probability of influence is equal between groups. A) First crossover

event: Encounter rate is 0.45, 25 generations have passed. B) Encounter rate is 1.0, 25 generations

have passed.

This culminates in extinction of group K across all values of influence by 500

generations if the rate of encounter is 0.8 or higher (Fig 3.9).

Figure 3.9 Fixation of group C after starting rare within the analytical model. Starting size of 10

individuals in group C and 100 in group K. Probability of influence is equal between groups. The end

result after 500 generations. Encounter rate is 0.80.

Texas Tech University, David Ralph, May 2012

19

The simulation produces similar results (Fig 3.10); however drift does play a

large role on group C due to the very low starting population size. The overall trend

remains with group C increasing in frequency across all values of influence and

encounter rate as early as 25 generations.

(A) (B)

(C) (D)

Figure 3.10 Group C increasing when rare within the simlation. 25 generations have passed. A-B)

Starting size of 10 individuals in group C and 100 in group K. First crossover at 0.7 encounter rate, and

shown again at 1.0 encounter rate. C-D) Starting size of 50 individuals in group C and 500 in group K.

First crossover at 0.5 encounter rate, and shown again at 1.0 encounter rate.

Texas Tech University, David Ralph, May 2012

20

The final step in this progression is answering the issue, can the convert

strategy increase when rare with low probability to convert? The answer to this

question depends on the following. When the ability to convert is low, starting

population of converts matters greatly. With 1 individual in group C a PC = 0.05

conversion influence maintains them in the region through 500 generations at low

expected frequency in the analytical model if the probability to kill remains low (Fig

3.11A), while starting with 10 converts may move them to fixation in the same

timeframe (Fig 3.11B).

(A) (B)

Figure 3.11 Group C starting rare with low probability to convert within the analytical model. Group

C’s influence is set to 0.05 across all values. 500 generations have passed with a 0.75 encounter rate.

A) Starting size of 1 individual in group C and 109 in group K. B) Starting size of 10 individuals in

group C and 100 in group K.

This suggests that group C is hard to remove from the population unless group K

kills at twice the effectiveness or more, even when small.

Texas Tech University, David Ralph, May 2012

21

Rare Killers

In the previous figures it should be noted that group K does well at any

influence values greater than twice that of group C. This leads into the final case

examined of whether or not group K can increase in frequency when rare. In the

analytical model, in which the probability of influence is set to 0.1 for group C, the

killers may persist in the region for a number of generations (Fig 3.12A); but

ultimately with encounter rates of 0.2 or greater they will go extinct within 500

generations at all values of influence (Fig 3.12B). The simulation shows a different

pattern with group K increasing in frequency at influence rates of PK = 0.2 and

higher (Fig 3.12C). Surprisingly reducing drift does not correct the curvature of the

distribution as much as expected (Fig 3.12D). An examination of the data indicates

that either group C or group K is present and have moved to fixation. The expected

time to extinction for group K is less than 25 generations and less than 50

generations for group C regardless of the starting population size. This suggests

that if group C does not eliminate group K quickly, group K will go to fixation due to

drift when selection becomes weak at high values of killer influence.

Texas Tech University, David Ralph, May 2012

22

(A) (B)

(C) (D)

Figure 3.12 Group K increasing when rare while convert rate is 0.1. Starting size of 10 individuals in

group K and 100 in group C. A) Analytical model: 25 generations have passed with a 1.0 encounter

rate. B) Analytical model: earliest extinction across all values after 100 generations and 0.7 encounter

rate. C) Simulation: 500 generations have passed with encounter rate set to 1.0. D) Simulation: 500

generations have passed with encounter rate set to 1.0. Starting size of 50 group K and 500 of group C.

Lowering Group C’s influence probability to 0.05 from the previous values of

0.1 drastically changes the population dynamics in the analytical model, and further

increases the rise of group K in the simulation. In both models the first crossover is

noted by 25 generations with group K gradually increasing in frequency (Fig 3.13A-

B). As the parameters increase the crossover point moves to the left to create a

Texas Tech University, David Ralph, May 2012

23

stable equilibrium between the values of 0.55 and 0.6 influence probability. Group C

remains fixed below PK = 0.55 influence in the analytical model, while at PK =0.6

and above group K successfully overcomes group C and becomes fixed in the region

(Fig 3.13C). The population change happens more gradually in the simulation;

however the stable equilibrium remains between 0.55 and 0.6 influence

probabilities for group K (Fig 3.13D).

(A) (B)

(C) (D)

Figure 3.13 Group K increasing when rare while convert rate is 0.05. A) Analytical model: Starting size

of 10 individuals in group K and 100 in group C. First crossover event after 25 generations with a 0.7

encounter rate. B). Simulation: starting size of 50 individuals in group K and 500 in group C. First

Texas Tech University, David Ralph, May 2012

24

crossover event after 25 generations with a 0.7 encounter rate. C) Analytical model: starting size of 10

individuals in group K and 100 in group C. The first stable equilibrium is achieved by 200 generations

with a 0.5 encounter rate. D) Simulation: starting size of 50 individuals in group K and 500 in group C.

Stable equilibrium at 1.0 encounter rate after 500 generations.

Texas Tech University, David Ralph, May 2012

25

CHAPTER IV

DISCUSSION AND CONCLUSIONS

Pure Strategy Effectiveness

The pure convert strategy is demonstrated to be highly effective against the

pure kill strategy. Examining the reduced convert probability data indicates that

converts are twice as effective as killers when the groups start out at the same size.

Converts are also able to increase when very rare (one individual) if the probability

of influence is equal to or greater than that of killers. If group C has a starting

population size that is 10% of group K, then group C quickly increase and may reach

fixation when there is an equal probability of influence between both groups. Even

with drift there is a greater probability that converts will reach fixation rather than

go extinct. In fact, group C is so effective that they can persist in a region at low

frequencies starting with only 1 individual in a region, with a highly reduced

conversion probability. If given a chance to increase a small amount group C will

quickly sweep through the population if killers become less than twice as effective.

This data indicates that not only can a pure convert strategy outcompete a pure kill

strategy, but it can do so even when rare given the right circumstances.

While the pure kill strategy is generally outcompeted by the pure converts

when parameters are equal, group K can still do well if PK is more than twice that of

Texas Tech University, David Ralph, May 2012

26

PC. Group K can even increase when rare if killing is at least eleven times more

effective than conversion.

Strategy Behavior

Understanding the behaviors of the separate strategies gives insight into why

a pure convert strategy is so effective when competing against a pure kill strategy. A

pure kill strategy simply removes individuals from competing groups. This act

naturally reduces the population size of other groups. This effectively opens up

resources within the region, so every group inhabiting the same space receives the

benefit. Population gain for killers can only occur through reproduction or

migration.

A pure convert strategy not only removes an individual from a competing

group, but also adds that individual to their own which alters the population size of

both groups. Overall the region size stays the same so new resources are not

opened up; only the converts gain from the interaction. In the model, the new

individual is ready to reproduce resulting in a higher probability for more offspring

the next generation for that group. Outside of the model this may not be the case;

however the new convert is at least closer to reproducing than a nonexistent

individual in another group. The acquisition of new group members through

conversion is extremely important biologically as population gain is no longer

constrained by reproductive values or migration.

Texas Tech University, David Ralph, May 2012

27

Consequences for Human Evolution

As a species Homo sapiens dependency on culture and cultural transmission

is significant. Shared cultural beliefs allow for the formation of non-kin groups

which leads to societies and religions. Both constructs may encompass huge

populations that compete between their respective groups. The various strategies

seen in today’s religions are a direct consequence of the effectiveness conversion

has on group fitness.

While altruism and cooperation may play important roles in cultural

evolution; the models presented here indicate that the basic inherent property of

culture, that is the ability to spread between individuals outside of traditional

genetic generations coupled with group identification as a property of cultural

phenotype, is powerful enough in itself to facilitate the propagation of culture

without the need to influence individual group member’s fitness. However,

behavior practiced by the individuals within the groups, and competition between

groups may further alter the evolutionary trajectory of these cultural systems. In

fact behaviors that negatively impact long term survivorship and fitness may be

passed on culturally if a cultural group is good at conversion.

Future Research

The current model for competing cultural strategies showcases the power of

conversion in population dynamics and engenders the need for future research.

There are many directions to take such as relaxing the assumptions within the

Texas Tech University, David Ralph, May 2012

28

current model by including sexual reproduction, and migration. Multiple strategies

should also be examined along with multiple groups.

One implied assumption that should be relaxed is static strategies. Human

behavior is not static and we have the capacity to learn and adjust. While the

current model examines the effects of various strategies over 500 generations, in

the natural world the strategies may have changed within that time frame. In fact,

an important aspect of culture is that it can change within the generational

timeframe of an organism. This in effect produces a much more complex system in

which to model. Again religion would be a good starting point to examine as the

general characteristics of religions and the behavioral strategies employed by each

are well documented and long lived.

Texas Tech University, David Ralph, May 2012

29

REFERENCES

Andre, J. B. & Morin, O. 2011. Questioning the cultural evolution of altruism. Journal

of Evolutionary Biology 24: 2531-2542.

Aoki, K. 2010. Evolution of the social-learner-explorer strategy in an

environmentally heterogeneous two-island model. Evolution 64: 2575-86.

Boyd, R. & Richerson, P. J. 1985. Culture and the Evolutionary Process. Univ Chicago

Press, Chicago.

Boyd, R. & Richerson, P. J. 2005. The Origin and Evolution of Cultures. Oxford Univ

Press, New York.

Boyd, R. & Richerson, P. J. 2009. Culture and the evolution of human cooperation.

Philosophical Transactions of the Royal Society B-Biological Sciences 364:

3281-3288.

Caldwell, C. A. & Millen, A. E. 2008. Studying cumulative cultural evolution in the

laboratory. Philosophical Transactions of the Royal Society B-Biological

Sciences 363: 3529-3539.

Caldwell, C. A. & Millen, A. E. 2010. Human cumulative culture in the laboratory:

Effects of (micro) population size. Learning & Behavior 38: 310-318.

Texas Tech University, David Ralph, May 2012

30

Enquist, M. & Ghirlanda, S. 2007. Evolution of social learning does not explain the

origin of human cumulative culture. Journal of Theoretical Biology 246: 129-

135.

Enquist, M., Ghirlanda, S. & Eriksson, K. 2011. Modelling the evolution and diversity

of cumulative culture. Philos Trans R Soc Lond B Biol Sci 366: 412-23.

Haleem, A. M. A. 2005. The Qur'an. Oxford University Press, New York.

Janssen, S. G. & Hauser, R. M. 1981. Religion, socialization, and fertility. Demography

18: 511-28.

Lehmann, L. & Feldman, M. W. 2008. The co-evolution of culturally inherited

altruistic helping and cultural transmission under random group formation.

Theoretical Population Biology 73: 506-516.

Lehmann, L., Feldman, M. W. & Kaeuffer, R. 2010. Cumulative cultural dynamics and

the coevolution of cultural innovation and transmission: an ESS model for

panmictic and structured populations. Journal of Evolutionary Biology 23:

2356-2369.

Mesoudi, A. 2011. Variable cultural acquisition costs constrain cumulative cultural

evolution. PLoS One 6.

Norenzayan, A. & Shariff, A. F. 2008. The origin and evolution of religious

prosociality. Science 322: 58-62.

Texas Tech University, David Ralph, May 2012

31

O'Brien, M. J., Lyman, R. L., Mesoudi, A. & VanPool, T. L. 2010. Cultural traits as units

of analysis. Philosophical Transactions of the Royal Society B-Biological

Sciences 365: 3797-3806.

Pyysiainen, I. & Hauser, M. 2010. The origins of religion: evolved adaptation or by-

product? Trends Cogn Sci 14: 104-9.

Rendell, L., Fogarty, L., Hoppitt, W. J. E., Morgan, T. J. H., Webster, M. M. & Laland, K.

N. 2011. Cognitive culture: theoretical and empirical insights into social

learning strategies. Trends in Cognitive Sciences 15: 68-76.

Sanderson, S. K. & Roberts, W. W. 2008. The evolutionary forms of the religious life:

a cross-cultural, quantitative analysis. American Anthropologist 110: 454-

466.

Shariff, A. F. & Norenzayan, A. 2007. God is watching you: priming God concepts

increases prosocial behavior in an anonymous economic game. Psychol Sci

18: 803-9.

Smith, J. M. & Price, G. R. 1973. The logic of animal conflict. Nature 246: 15-18.

Smith, K., Kalish, M. L., Griffiths, T. L. & Lewandowsky, S. 2008. Introduction. Cultural

transmission and the evolution of human behaviour. Philosophical

Transactions of the Royal Society B-Biological Sciences 363: 3469-3476.

Texas Tech University, David Ralph, May 2012

32

APPENDIX A

SIMULATION PROGRAM

import random

import operator

import copy

import sys

#FI = raw_input('Input parameters from file?: ')

FI = str('y')

if FI == str('y'):

#FN = input('How many files do you want to use?: ')

FN = 132

sys.stdin = file('paramsample.txt','rb')

else:

FN = 1

FC = 0

while FC < FN:

Ignore = raw_input()

del Ignore

# Initial Parameters

def User_Input():

'''Define parameters of simulation:'''

global Reg_Num, Reg_Size, Pop_Num, Pop_Size,

Phen_Num, Phen, Total, MO, Migration, MiD,

Mig,Detail,Phas,DenD,Truns,Gen,Out,Fout,OutP,Mtrade,M

R,MC,Pfout,Cres

global

Lint,LocalInt,LiD,LC,LK,LCR,LI,K,R,IV,EXT,EXTcount,GenO

ut,pfcount,Pfout0,Pfout1,Pfout2,Pfout3,Pfout4,Pfout5,Pfo

ut6

Out = raw_input('Output results into a file? Y/N

:')

Out = Out.rstrip('\r\n')

Out = Out.lower()

if Out == str('y'):

Fout = raw_input('Name the output file: ')

Fout = Fout.rstrip('\r\n')

Pfout = Fout + 'p'

Pfout = Pfout.rstrip('\r\n')

Fout += str(FC) + '.txt'

EXT = Pfout + 'ext' + str(FC) + '.txt'

GenOut = [25,50,100,250,500]

pfcount = 0

Pfout0 = Pfout + '25G' + str(FC) + '.txt'

Pfout1 = Pfout + '50G' + str(FC) + '.txt'

Pfout2 = Pfout + '100G' + str(FC) + '.txt'

Pfout3 = Pfout + '250G' + str(FC) + '.txt'

Pfout4 = Pfout + '500G' + str(FC) + '.txt'

Detail = int(raw_input('Basic Details = 0 Debug

= 1 Modified = 2 None = 3\nDetail Level: '))

DenD = raw_input('Use Density Dependency?

Y/N : ')

DenD = DenD.rstrip('\r\n')

DenD = DenD.lower()

Migration = raw_input('Use migration in

simulation? Y/N : ')

Migration = Migration.rstrip('\r\n')

Migration = Migration.lower()

Texas Tech University, David Ralph, May 2012

33

if Migration == str('y'):

MiD = raw_input('Is migration probability

density dependent? Y/N : ')

MiD = MiD.rstrip('\r\n')

MiD = MiD.lower()

Mtrade = raw_input('Is there resistance to

migration? Y/N : ')

Mtrade= Mtrade.rstrip('\r\n')

Mtrade = Mtrade.lower()

else:

MiD = 'n'

Mtrade = 'n'

LocalInt = raw_input('Use local interaction in

simulation? Y/N : ')

LocalInt = LocalInt.rstrip('\r\n')

LocalInt = LocalInt.lower()

if LocalInt == str('y'):

LiD = raw_input('Is local interaction

probability density dependent? Y/N : ')

LiD = LiD.rstrip('\r\n')

LiD = LiD.lower()

else:

LiD = 'n'

if Migration == str('y') or LocalInt == str('y'):

IV = int(raw_input('Migration Test = 0

Dymanic = 1, Non-Dynamic = 2, Dynamic among

Interactors = 3, Non-Dynamic among Interactors = 4

\nInteraction Type: '))

Truns = int(raw_input('How many simulation

runs? : '))

Gen = int(raw_input('How many generations? :

'))

print

Reg_Num = int(raw_input('How many Regions in

the simulation? '))

Pop_Num = int(raw_input('How many Groups in

simulation? '))

Phen_Num = int(raw_input('How many

Phenotypes in simulation? '))

print

Phen = []

for i in range(Phen_Num):

Phen.append(i)

Reg_Size = []

Pop_Size = []

for i in range(Reg_Num):

rt = 0

for j in range(Pop_Num):

Pop_S = int(raw_input('In Region %d

how many individuals are in Group %d ?: ' % (i, j)))

Pop_Size.append(Pop_S)

rt += Pop_S

Reg_Size.append(rt)

print

Phas = []

l = 0

for k in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

pas = int(raw_input('In Region %d

Out of %d Individuals in Group %d How many are

Phenotype %d? : ' % (k, Pop_Size[l],i, Phen[j])))

Phas.append(pas)

l += 1

print

if Migration == str('y'):

Mig = []

Texas Tech University, David Ralph, May 2012

34

for k in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

mmig = float(raw_input('In

Region %d For Phenotype %d in Group %d what is the

initial probability that individual will migrate?: '% (k, j,

i)))

Mig.append(mmig)

if Mtrade == str('y'):

print

MR = []

MC = []

Cres = []

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Phen_Num):

mr = float(raw_input('In

Region %d Group %d Phenotype %d, what is the

probability of killing a migrant 0 to 1: ' % (i, j, k)))

mc = float(raw_input('In

Region %d Group %d Phenotype %d, what is the

probability of converting a migrant 0 to %g: ' % (i, j, k,(1-

mr))))

cr = float(raw_input('In

Region %d Group %d Phenotype %d, what is the

probability of resisting a conversion attempt: ' % (i, j, k)))

MR.append(mr)

MC.append(mc)

Cres.append(cr)

print

if LocalInt == str('y'):

LI = []

for k in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

li = float(raw_input('In Region

%d For Phenotype %d in Group %d what is the initial

probability that individual will interact localy?: '% (k, j,

i)))

LI.append(li)

print

LK = []

LC = []

LCR = []

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Phen_Num):

lk = float(raw_input('In Region

%d Group %d Phenotype %d, what is the probability of

killing a local 0 to 1: ' % (i, j, k)))

lc = float(raw_input('In Region

%d Group %d Phenotype %d, what is the probability of

converting a local 0 to %g: ' % (i, j, k,(1-lk))))

lcr = float(raw_input('In Region

%d Group %d Phenotype %d, what is the probability of

resisting a local conversion attempt: ' % (i, j, k)))

LK.append(lk)

LC.append(lc)

LCR.append(lcr)

print

Total = sum(Pop_Size)

if DenD == str('y'):

K = []

for i in range(Reg_Num):

kc = float(raw_input('What is the

carrying capacity for Region %d?: '% (i)))

K.append(kc)

R = []

Texas Tech University, David Ralph, May 2012

35

for k in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

gr = float(raw_input('In Region

%d what is the expected growth rate for Phenotype %d in

Group %d ?: '% (k, j, i)))

R.append(gr)

else:

MO = []

for k in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

mo = float(raw_input('In Region

%d what is the expected number of offspring for

Phenotype %d in Group %d ?: '% (k, j, i)))

MO.append(mo)

EXTcount = []

for i in range(Pop_Num):

obj = 0

EXTcount.append(obj)

print

print User_Input.__doc__

User_Input()

def PrintP():

print >>OutP, ' Simulation

Parameters'

print >>OutP, 'File Number: ', FC

print >>OutP, 'Detail Level: ', Detail

print >>OutP, 'Local Interaction: ', LocalInt

print >>OutP, 'Density Dependency: ', DenD

#print >>OutP, 'Migration: ', Migration

if Migration == str('y'):

print >>OutP, 'Migration Resistance: ',

Mtrade

print >>OutP, 'Number of Simulations: ', Truns

print >>OutP, 'Number of Generations: ', Gen

print >>OutP, 'Number of Regions: ', Reg_Num

print >>OutP, 'Number of Groups: ', Pop_Num

#print >>OutP, 'Number of Phenotypes: ',

Phen_Num

print >>OutP, 'Population Sizes: ', Pop_Size

if DenD == str('y'):

print >>OutP, 'Carrying Capicity by Region: ',

K

print >>OutP, 'Growth rate for each group

within each region: ', R

else:

print >>OutP, 'Expected Number of

Offspring in each Population by Phentotype: ', MO

if LocalInt == str('y'):

if LiD == str('y'):

print >>OutP, 'Local Interaction Density

Dependency:'

for i in

range(Reg_Num*Pop_Num*Phen_Num):

LX[i].lptell(3)

else:

print >>OutP, 'Local Interaction Array:'

for i in

range(Reg_Num*Pop_Num*Phen_Num):

LX[i].lptell(1)

print >>OutP, 'Local Interaction Kill %:', LK

print >>OutP, 'Local Interaction Conversion

%:', LC

Texas Tech University, David Ralph, May 2012

36

#print >>OutP, 'Local Interaction

Conversion Resistance %:', LCR

if Migration == str('y'):

if MiD == str('y'):

print >>OutP, 'Migration Density

Dependency:'

for i in

range(Reg_Num*Pop_Num*Phen_Num):

MX[i].mptell(3)

else:

#print >>OutP, 'Probability of Migrants

for each Group by Phenotype: ', Mig

print >>OutP, 'Migration Array:'

for i in

range(Reg_Num*Pop_Num*Phen_Num):

MX[i].mptell(1)

print >>OutP

# Migration Distribution

class MDist:

def __init__(self, MReg, MPopId, MPhen, MMean,

Lstart, Linc, Sstart, Sinc, Mx):

self.MReg = MReg

self.MPopId = MPopId

self.MPhen = MPhen

self.MMean = MMean

self.Lstart = Lstart

self.Linc = Linc

self.Sstart = Sstart

self.Sinc = Sinc

self.Mx = Mx

def mptell(self,n):

if n == 0:

print '[%d][%d][%d][%f][%g]' %

(self.MReg, self.MPopId, self.MPhen, self.MMean, self.Mx)

if n == 1:

print >>OutP, '[%d][%d][%d][%f][%g]'

% (self.MReg, self.MPopId, self.MPhen, self.MMean,

self.Mx)

if n == 2:

print

'[%d][%d][%d][%f][%d][%g][%d][%g][%g]' %

(self.MReg, self.MPopId, self.MPhen, self.MMean,

self.Lstart, self.Linc, self.Sstart, self.Sinc, self.Mx)

if n == 3:

print >>OutP,

'[%d][%d][%d][%f][%d][%g][%d][%g][%g]' %

(self.MReg, self.MPopId, self.MPhen, self.MMean,

self.Lstart, self.Linc, self.Sstart, self.Sinc, self.Mx)

# Local Interaction Distribution

class LDist:

def __init__(self, LReg, LPopId, LPhen, LMean,

Lstart, Linc, Sstart, Sinc, Lx):

self.LReg = LReg

self.LPopId = LPopId

self.LPhen = LPhen

self.LMean = LMean

self.Lstart = Lstart

self.Linc = Linc

self.Sstart = Sstart

self.Sinc = Sinc

self.Lx = Lx

def lptell(self,n):

if n == 0:

Texas Tech University, David Ralph, May 2012

37

print '[%d][%d][%d][%f][%g]' %

(self.LReg, self.LPopId, self.LPhen, self.LMean, self.Lx)

if n == 1:

print >>OutP, '[%d][%d][%d][%f][%g]'

% (self.LReg, self.LPopId, self.LPhen, self.LMean, self.Lx)

if n == 2:

print

'[%d][%d][%d][%f][%d][%g][%d][%g][%g]' %

(self.LReg, self.LPopId, self.LPhen, self.LMean, self.Lstart,

self.Linc, self.Sstart, self.Sinc, self.Lx)

if n == 3:

print >>OutP,

'[%d][%d][%d][%f][%d][%g][%d][%g][%g]' %

(self.LReg, self.LPopId, self.LPhen, self.LMean, self.Lstart,

self.Linc, self.Sstart, self.Sinc, self.Lx)

# (interaction) Migration Hold

class MigH:

def __init__(self, Ind, Reg, Id, Death, Cpop,

Location):

self.Ind = Ind

self.Reg = Reg

self.Id = Id

self.Death = Death

self.Cpop = Cpop

self.Location = Location

def mhtell(self, n):

if n == 0:

print '[%d][%d][%s][%d][%d][%s]' %

(self.Ind, self.Reg,self.Id, self.Death, self.Cpop,

self.Location)

if n == 1:

print >>OutP,

'[%d][%d][%s][%d][%d][%s]' % (self.Ind, self.Reg,self.Id,

self.Death, self.Cpop, self.Location)

# Migration Density Dep

def MigD():

global MX

MX = []

k = 0

if MiD == str('y'):

for l in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

Ls = int(raw_input('In Region

%d for Phenotype %d in Group %d probability of

migration increases at population size: '% (l, j, i)))

Lm = float(raw_input('In Region

%d what is the magnitude of increase for Phenotype %d

in Group %d ?: '% (l, j, i)))

print

Ss = int(raw_input('In Region

%d for Phenotype %d in Group %d probability of

migration decreases at population size: '% (l, j, i)))

Sm = float(raw_input('In Region

%d what is the magnitude of reduction for Phenotype %d

in Group %d ?: '% (l, j, i)))

print

obj =

MDist(l,i,j,Mig[k],Ls,Lm,Ss,Sm,Mig[k])

MX.append(obj)

k += 1

if Detail == 1:

for i in range(len(Mig)):

MX[i].mptell(2)

#if Out == str('y'):

# for i in range(len(Mig)):

# MX[i].mptell(3)

else:

Texas Tech University, David Ralph, May 2012

38

for l in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

obj =

MDist(l,i,j,Mig[k],0,0,0,0,Mig[k])

MX.append(obj)

k += 1

if Detail == 1:

for i in range(len(Mig)):

MX[i].mptell(0)

#if Out == str('y'):

# for i in range(len(Mig)):

# MX[i].mptell(1)

print

if Migration == str('y'):

MigD()

def LD():

global LX

LX = []

k = 0

if LiD == str('y'):

for l in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

Ls = int(raw_input('In Region

%d for Phenotype %d in Group %d probability of local

interaction increases at population size: '% (l, j, i)))

Lm = float(raw_input('In Region

%d what is the magnitude of increase for Phenotype %d

in Group %d ?: '% (l, j, i)))

print

Ss = int(raw_input('In Region

%d for Phenotype %d in Group %d probability of local

interaction decreases at population size: '% (l, j, i)))

Sm = float(raw_input('In Region

%d what is the magnitude of reduction for Phenotype %d

in Group %d ?: '% (l, j, i)))

print

obj =

LDist(l,i,j,LI[k],Ls,Lm,Ss,Sm,LI[k])

LX.append(obj)

k += 1

if Detail == 1:

for i in range(len(LI)):

LX[i].lptell(2)

#if Out == str('y'):

# for i in range(len(LI)):

# LX[i].lptell(3)

else:

for l in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

obj =

LDist(l,i,j,LI[k],0,0,0,0,LI[k])

LX.append(obj)

k += 1

if Detail == 1:

for i in range(len(LI)):

LX[i].lptell(0)

#if Out == str('y'):

Texas Tech University, David Ralph, May 2012

39

# for i in range(len(LI)):

# LX[i].lptell(1)

print

if LocalInt == str('y'):

LD()

# Print Label

def label(n):

if n == 0:

print ' Reg Grp ID IPh'

if n == 1:

print ' Reg Grp ID IPh IW'

if n == 2:

print ' Reg Grp ID IPh IW IR'

if n == 3:

print ' Reg Grp ID IPh IW IR MR

LR'

if n == 4:

print >>OutP, ' Reg Grp ID IPh'

if n == 5:

print >>OutP, ' Reg Grp ID IPh IW'

if n == 6:

print >>OutP, ' Reg Grp ID IPh IW IR'

if n == 7:

print >>OutP, ' Reg Grp ID IPh IW IR

MR LR'

# Individual Array

class Indiv(object):

'''Characteristics of an individual'''

def __init__(self, Reg, Pop, Id, Iphen, Iw, IRand,

MRand, LRand):

self.Reg = Reg

self.Pop = Pop

self.Id = Id

self.Iphen = Iphen

self.Iw = Iw

self.IRand = IRand

self.MRand = MRand

self.LRand = LRand

def tell(self, n):

if n == 0:

print '[ %d ][ %d ][ %s ][ %d ]'% (self.Reg,

self.Pop, self.Id, self.Iphen)

if n == 1:

print '[ %d ][ %d ][ %s ][ %d ][ %d ]'%

(self.Reg, self.Pop, self.Id, self.Iphen, self.Iw)

if n == 2:

print '[ %d ][ %d ][ %s ][ %d ][ %d ][ %g

]'% (self.Reg, self.Pop, self.Id, self.Iphen, self.Iw,

self.IRand)

if n == 3:

print '[ %d ][ %d ][ %s ][ %d ][ %d ][ %g ][

%g ][ %g ]'% (self.Reg, self.Pop, self.Id, self.Iphen, self.Iw,

self.IRand, self.MRand, self.LRand)

if n == 4:

print >>OutP, '[ %d ][ %d ][ %s ][ %d ]'%

(self.Reg, self.Pop, self.Id, self.Iphen)

if n == 5:

print >>OutP, '[ %d ][ %d ][ %s ][ %d ][

%d ]'% (self.Reg, self.Pop, self.Id, self.Iphen, self.Iw)

if n == 6:

Texas Tech University, David Ralph, May 2012

40

print >>OutP, '[ %d ][ %d ][ %s ][ %d ][

%d ][ %g ]'% (self.Reg, self.Pop, self.Id, self.Iphen, self.Iw,

self.IRand)

if n == 7:

print >>OutP, '[ %d ][ %d ][ %s ][ %d ][

%d ][ %g ][ %g ][ %g ]'% (self.Reg, self.Pop, self.Id,

self.Iphen, self.Iw, self.IRand, self.MRand, self.LRand)

#Creating Array - each element stores the particular

class vars

Ind = []

l = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Pop_Size[l]):

obj = Indiv(i,j,str(i)+ str(j)+

str(k),0,0,0.0,0.0,0.0)

Ind.append(obj)

l +=1

k = 0

m = 0

for h in range(Reg_Num):

for i in range(Pop_Num):

for j in range(Phen_Num):

l = 0

while l < Phas[m]:

Ind[k].Iphen = Phen[j]

k +=1

l += 1

m += 1

k = 0

l = 0

m = 0

print

# Backup parameters for simulation runs

OReg_Size = list(Reg_Size)

OPop_Size = list(Pop_Size)

OTotal = Total

OPhas = list(Phas)

OInd = copy.deepcopy(Ind)

if Detail == 1:

label(0)

for i in range(Total):

Ind[i].tell(0)

#Output file header

if Out == str('y'):

OutP = open(Fout, 'w')

OutP0 = open(Pfout0, 'w')

OutP1 = open(Pfout1, 'w')

OutP2 = open(Pfout2, 'w')

OutP3 = open(Pfout3, 'w')

OutP4 = open(Pfout4, 'w')

Extout = open(EXT, 'w')

PrintP()

if Detail == 1:

label(4)

for i in range(Total):

Ind[i].tell(4)

print >>OutP

Texas Tech University, David Ralph, May 2012

41

# creating Factorial table

e = 2.71828182845904523536

def fact(n):

f=1

while (n>0):

f=f*n

n=n-1

return f

# Print test for fact

#for i in xrange(21):

# print fact(i)

# Poisson Dist

class PDist:

def __init__(self, RegId, PopId, Phen, IMean, Off,

Px, Pxt):

self.RegId = RegId

self.PopId = PopId

self.Phen = Phen

self.IMean = IMean

self.Off = Off

self.Px = Px

self.Pxt = Pxt

def ptell(self,n):

if n == 0:

print '[%d][%d][%d][%g][%d][%g][%g]'

% (self.RegId, self.PopId, self.Phen, self.IMean, self.Off,

self.Px, self.Pxt)

if n == 1:

print >>OutP,

'[%d][%d][%d][%g][%d][%g][%g]' % (self.RegId,

self.PopId, self.Phen, self.IMean, self.Off, self.Px, self.Pxt)

# Mean Offspring Generation

def MeanOff():

global PX, MO

PX = []

if DenD == str('y'):

MO = []

l = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Phen_Num):

mo = (R[l]*(1 + (1 -

(Reg_Size[i]/K[i]))))

MO.append(mo)

for m in range(21):

px =

(pow(MO[l],m)*(pow(e,-MO[l])))/(fact(m))

if m == 0:

pxt = px

else:

pxt += px

obj = PDist(i,j, k, MO[l], m,

px, pxt)

PX.append(obj)

l += 1

else:

l = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Phen_Num):

Texas Tech University, David Ralph, May 2012

42

for m in range(21):

px =

(pow(MO[l],m)*(pow(e,-MO[l])))/(fact(m))

if m == 0:

pxt = px

else:

pxt += px

obj = PDist(i,j, k, MO[l], m,

px, pxt)

PX.append(obj)

l += 1

if Detail == 1:

for i in range(len(MO)*21):

PX[i].ptell(0)

if Out == str('y'):

for i in range(len(MO)*21):

PX[i].ptell(1)

# Migration Assignment

def IntAss(n):

global Total, NM

k = 0

i = 0

if MiD == str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Reg_Size[Ind[k].Reg] > MX[i].Lstart:

Ind[k].MRand = (Ind[k].MRand -

((Reg_Size[Ind[k].Reg] - MX[i].Lstart)*MX[i].Linc))

if Ind[k].MRand < 0:

Ind[k].MRand = 0

k += 1

elif Reg_Size[Ind[k].Reg] < MX[i].Sstart:

Ind[k].MRand = (Ind[k].MRand +

((MX[i].Sstart - Reg_Size[Ind[k].Reg])*MX[i].Sinc))

if Ind[k].MRand >= 1:

Ind[k].MRand = 1

k += 1

else:

k += 1

k = 0

i = 0

if LiD == str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Reg_Size[Ind[k].Reg] > LX[i].Lstart:

Ind[k].LRand = (Ind[k].LRand -

((Reg_Size[Ind[k].Reg] - LX[i].Lstart)*LX[i].Linc))

if Ind[k].LRand < 0:

Ind[k].LRand = 0

k += 1

elif Reg_Size[Ind[k].Reg] < LX[i].Sstart:

Ind[k].LRand = (Ind[k].LRand +

((LX[i].Sstart - Reg_Size[Ind[k].Reg])*LX[i].Sinc))

if Ind[k].LRand >= 1:

Ind[k].LRand = 1

Texas Tech University, David Ralph, May 2012

43

k += 1

else:

k += 1

if n == 0:

k = 0

i = 0

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l = random.randint(0,Reg_Num-

1)

if l != Ind[k].Reg:

Reg_Size[l] = Reg_Size[l] + 1

Reg_Size[Ind[k].Reg] =

Reg_Size[Ind[k].Reg] - 1

m =

((Ind[k].Reg*Pop_Num)+ Ind[k].Pop)

Pop_Size[m] = Pop_Size[m]-

1

n =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

Phas[n] -= 1

Ind[k].Reg = l

m =

((Ind[k].Reg*Pop_Num)+ Ind[k].Pop)

Pop_Size[m] =

Pop_Size[m]+1

n =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

Phas[n] += 1

j = 1

k += 1

else:

k += 1

if n == 1:

NM = 0

k = 0

i = 0

MH = []

MHR = []

if Mtrade == str('y') and LocalInt ==

str('n'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l =

random.randint(0,Reg_Num-1)

if l != Ind[k].Reg:

NM += 1

obj =

MigH(k,l,Ind[k].Id,0,0,'m')

MH.append(obj)

j = 1

k += 1

else:

k += 1

elif Mtrade == str('n') and LocalInt ==

str('y'):

Texas Tech University, David Ralph, May 2012

44

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].LRand < LX[i].Lx:

NM += 1

obj =

MigH(k,Ind[k].Reg,Ind[k].Id,0,0,'l')

MH.append(obj)

k += 1

else:

k += 1

# possibly randomize the ordering

elif Mtrade == str('y') and LocalInt ==

str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l =

random.randint(0,Reg_Num-1)

if l != Ind[k].Reg:

NM += 1

obj =

MigH(k,l,Ind[k].Id,0,0,'m')

MH.append(obj)

j = 1

k += 1

else:

if Ind[k].LRand < LX[i].Lx:

NM += 1

obj =

MigH(k,Ind[k].Reg,Ind[k].Id,0,0,'l')

MH.append(obj)

k += 1

else:

k += 1

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MH[i].mhtell(1)

print >>OutP

MHR = random.sample(MH,NM)

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

for i in range(NM):

if Reg_Size[MHR[i].Reg] == 0:

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test:

Reg Size 0', Phas

Reg_Size[MHR[i].Reg] =

Reg_Size[MHR[i].Reg] + 1

Texas Tech University, David Ralph, May 2012

45

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg = MHR[i].Reg

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test: ',

Phas

else:

r = 0.0

for p in range(Pop_Num):

if

Pop_Size[((MHR[i].Reg*Pop_Num)+ p)] != 0:

if p != Ind[MHR[i].Ind].Pop:

for q in

range(Phen_Num):

n =

((MHR[i].Reg*(Pop_Num*Phen_Num))+(p*Phen_Num)+

q)

if MHR[i].Location

== str('m'):

r +=

Phas[n]*MR[n]

else:

r +=

Phas[n]*LK[n]

r = r/Reg_Size[MHR[i].Reg]

s = random.random()

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual Migrating

to Region %d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print 'Migration Resistance

check:r = [%f], s = [%f] ' % (r,s)

if Out == str('y'):

print >>OutP,

'Individual Migrating to Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print >>OutP,

'Migration Resistance check:r = [%f], s = [%f] ' % (r,s)

else:

print 'Individual is

Interacting within Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print 'Local Interaction

Resistance check:r = [%f], s = [%f] ' % (r,s)

if Out == str('y'):

print >>OutP,

'Individual is Interacting within Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

Texas Tech University, David Ralph, May 2012

46

print >>OutP, 'Local

Interaction Resistance check:r = [%f], s = [%f] ' % (r,s)

if r > s:

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test

Death: ', Phas

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas

test: ', Phas

Total = Total - 1

MHR[i].Death = 1

else:

rcm = 0.0

l = 0.0

RChk = 0

p = 0

CRK = 0.0

while p < Pop_Num:

rc = 0.0

if

Pop_Size[((MHR[i].Reg*Pop_Num)+ p)] != 0:

if p ==

Ind[MHR[i].Ind].Pop:

rc +=0

else:

for q in

range(Phen_Num):

n =

((MHR[i].Reg*(Pop_Num*Phen_Num))+(p*Phen_Num)+

q)

if

MHR[i].Location == str('m'):

rc +=

Phas[n]*MC[n]

else:

rc +=

Phas[n]*LC[n]

l =

Pop_Size[((MHR[i].Reg*Pop_Num)+ p)]

rcm +=

rc/Reg_Size[MHR[i].Reg]

if MHR[i].Location ==

str('m'):

CRK =

Cres[((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(In

d[MHR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)]

else:

CRK =

LCR[((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(In

d[MHR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)]

if Detail == 1:

if Out == str('y'):

#if

MHR[i].Location == str('m'):

Texas Tech University, David Ralph, May 2012

47

#print

>>OutP, 'Cres = :' % Cres

#else:

#print

>>OutP, 'LCR = :' % LCR

print >>OutP,

'Pop = %d' % p

print >>OutP,

'rc = %f' % rc

print >>OutP, 'l

= %d' % l

print >>OutP,

'rcm = %f' % rcm

print >>OutP,

'CRK = %f' % CRK

print >>OutP, 'r

= %f' % r

print >>OutP, 's

= %f' % s

print >>OutP,

'Check = %f' % ((r + rcm) - (rcm*CRK))

if ((r + rcm) -

(rcm*CRK)) > s:

if Detail == 1:

if Out ==

str('y'):

print

>>OutP, 'Converted = [%f] > [%f]' % (((r + rcm) -

(rcm*CRK)), s)

print

>>OutP, 'Phas test Conversion: ', Phas

Reg_Size[MHR[i].Reg] = Reg_Size[MHR[i].Reg] + 1

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] =

Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg

= MHR[i].Reg

Ind[MHR[i].Ind].Pop

= p

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] =

Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

if Detail == 1:

if Out ==

str('y'):

print

>>OutP, 'Phas test:Conversion: ', Phas

p = Pop_Num

RChk = 1

else:

p += 1

else:

p += 1

if RChk == 0:

if Detail == 1:

if Out == str('y'):

print >>OutP,

'Passed total chance = [%f] < [%f]' % (((r + rcm) -

(rcm*CRK)), s)

print >>OutP, 'Phas

test: ', Phas

Texas Tech University, David Ralph, May 2012

48

if MHR[i].Location ==

str('m'):

Reg_Size[MHR[i].Reg] =

Reg_Size[MHR[i].Reg] + 1

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] =

Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg =

MHR[i].Reg

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] =

Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas

test: ', Phas

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

MHR.sort(key = operator.attrgetter('Ind'))

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

KC = 0

for i in range(NM):

if MHR[i].Death == 1:

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual %s failed to

migrate' % Ind[MHR[i].Ind - KC].Id

if Out == str('y'):

print >>OutP,

'Individual %s failed to migrate' % Ind[MHR[i].Ind -

KC].Id

else:

print 'Individual %s died

during local interaction' % Ind[MHR[i].Ind - KC].Id

if Out == str('y'):

print >>OutP,

'Individual %s died during local interaction' %

Ind[MHR[i].Ind - KC].Id

del Ind[(MHR[i].Ind)-KC]

KC += 1

# Non Dynamic Interaction

if n == 2:

NM = 0

k = 0

i = 0

MH = []

Texas Tech University, David Ralph, May 2012

49

MHR = []

if Mtrade == str('y') and LocalInt ==

str('n'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l =

random.randint(0,Reg_Num-1)

if l != Ind[k].Reg:

NM += 1

obj =

MigH(k,l,Ind[k].Id,0,0,'m')

MH.append(obj)

j = 1

k += 1

else:

k += 1

elif Mtrade == str('n') and LocalInt ==

str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].LRand < LX[i].Lx:

NM += 1

obj =

MigH(k,Ind[k].Reg,Ind[k].Id,0,0,'l')

MH.append(obj)

k += 1

else:

k += 1

# possibly randomize the ordering

elif Mtrade == str('y') and LocalInt ==

str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l =

random.randint(0,Reg_Num-1)

if l != Ind[k].Reg:

NM += 1

obj =

MigH(k,l,Ind[k].Id,0,0,'m')

MH.append(obj)

j = 1

k += 1

else:

if Ind[k].LRand < LX[i].Lx:

NM += 1

obj =

MigH(k,Ind[k].Reg,Ind[k].Id,0,0,'l')

MH.append(obj)

k += 1

else:

k += 1

if Detail == 1:

Texas Tech University, David Ralph, May 2012

50

if Out == str('y'):

for i in range(NM):

MH[i].mhtell(1)

print >>OutP

MHR = random.sample(MH,NM)

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

for i in range(NM):

if Reg_Size[MHR[i].Reg] == 0:

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test:

Reg Size 0', Phas

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test: ',

Phas

else:

r = 0.0

for p in range(Pop_Num):

if

Pop_Size[((MHR[i].Reg*Pop_Num)+ p)] != 0:

if p != Ind[MHR[i].Ind].Pop:

for q in

range(Phen_Num):

n =

((MHR[i].Reg*(Pop_Num*Phen_Num))+(p*Phen_Num)+

q)

if MHR[i].Location

== str('m'):

r +=

Phas[n]*MR[n]

else:

r +=

Phas[n]*LK[n]

r = r/Reg_Size[MHR[i].Reg]

s = random.random()

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual Migrating

to Region %d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print 'Migration Resistance

check:r = [%f], s = [%f] ' % (r,s)

if Out == str('y'):

print >>OutP,

'Individual Migrating to Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print >>OutP,

'Migration Resistance check:r = [%f], s = [%f] ' % (r,s)

else:

print 'Individual is

Interacting within Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

Texas Tech University, David Ralph, May 2012

51

print 'Local Interaction

Resistance check:r = [%f], s = [%f] ' % (r,s)

if Out == str('y'):

print >>OutP,

'Individual is Interacting within Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print >>OutP, 'Local

Interaction Resistance check:r = [%f], s = [%f] ' % (r,s)

if r > s:

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test

Death: ', Phas

MHR[i].Death = 1

else:

rcm = 0.0

l = 0.0

RChk = 0

p = 0

CRK = 0.0

while p < Pop_Num:

rc = 0.0

if

Pop_Size[((MHR[i].Reg*Pop_Num)+ p)] != 0:

if p ==

Ind[MHR[i].Ind].Pop:

rc +=0

else:

for q in

range(Phen_Num):

n =

((MHR[i].Reg*(Pop_Num*Phen_Num))+(p*Phen_Num)+

q)

if

MHR[i].Location == str('m'):

rc +=

Phas[n]*MC[n]

else:

rc +=

Phas[n]*LC[n]

l =

Pop_Size[((MHR[i].Reg*Pop_Num)+ p)]

rcm +=

rc/Reg_Size[MHR[i].Reg]

if MHR[i].Location ==

str('m'):

CRK =

Cres[((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(In

d[MHR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)]

else:

CRK =

LCR[((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(In

d[MHR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)]

if Detail == 1:

if Out == str('y'):

#if

MHR[i].Location == str('m'):

#print

>>OutP, 'Cres = :' % Cres

#else:

#print

>>OutP, 'LCR = :' % LCR

print >>OutP,

'Pop = %d' % p

print >>OutP,

'rc = %f' % rc

Texas Tech University, David Ralph, May 2012

52

print >>OutP, 'l

= %d' % l

print >>OutP,

'rcm = %f' % rcm

print >>OutP,

'CRK = %f' % CRK

print >>OutP, 'r

= %f' % r

print >>OutP, 's

= %f' % s

print >>OutP,

'Check = %f' % ((r + rcm) - (rcm*CRK))

if ((r + rcm) -

(rcm*CRK)) > s:

if Detail == 1:

if Out ==

str('y'):

print

>>OutP, 'Converted = [%f] > [%f]' % (((r + rcm) -

(rcm*CRK)), s)

print

>>OutP, 'Phas test Conversion: ', Phas

MHR[i].Cpop = p

MHR[i].Death = 2

if Detail == 1:

if Out ==

str('y'):

print

>>OutP, 'Phas test:Conversion: ', Phas

p = Pop_Num

RChk = 1

else:

p += 1

else:

p += 1

if RChk == 0:

if Detail == 1:

if Out == str('y'):

print >>OutP,

'Passed total chance = [%f] < [%f]' % (((r + rcm) -

(rcm*CRK)), s)

print >>OutP, 'Phas

test: ', Phas

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas

test: ', Phas

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

MHR.sort(key = operator.attrgetter('Ind'))

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

for i in range(NM):

if MHR[i].Death == 0:

if Detail == 1:

if MHR[i].Location == str('m'):

Texas Tech University, David Ralph, May 2012

53

print 'Individual %s

Migrated to Region %d' % (Ind[MHR[i].Ind].Id,

MHR[i].Reg)

if Out == str('y'):

print >>OutP,

'Individual %s Migrated to Region %d' %

(Ind[MHR[i].Ind].Id, MHR[i].Reg)

else:

print 'Individual %s

Interacted Safely' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s Interacted Safely' % Ind[MHR[i].Ind].Id

if MHR[i].Location == str('m'):

Reg_Size[MHR[i].Reg] =

Reg_Size[MHR[i].Reg] + 1

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg =

MHR[i].Reg

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

if MHR[i].Death == 1:

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual %s failed to

migrate' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s failed to migrate' % Ind[MHR[i].Ind].Id

else:

print 'Individual %s died

during local interaction' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s died during local interaction' %

Ind[MHR[i].Ind].Id

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Total = Total - 1

if MHR[i].Death == 2:

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual %s was

converted by group %d in Region %d' %

(Ind[MHR[i].Ind].Id, MHR[i].Cpop, MHR[i].Reg)

if Out == str('y'):

print >>OutP,

'Individual %s was converted by group %d in Region %d'

% (Ind[MHR[i].Ind].Id, MHR[i].Cpop, MHR[i].Reg)

else:

print 'Individual %s was

converted during local interaction' % Ind[MHR[i].Ind].Id

Texas Tech University, David Ralph, May 2012

54

if Out == str('y'):

print >>OutP,

'Individual %s was converted during local interaction' %

Ind[MHR[i].Ind].Id

Reg_Size[MHR[i].Reg] =

Reg_Size[MHR[i].Reg] + 1

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg = MHR[i].Reg

Ind[MHR[i].Ind].Pop = MHR[i].Cpop

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

KC = 0

for i in range(NM):

if MHR[i].Death == 1:

del Ind[(MHR[i].Ind)-KC]

KC += 1

# Non Dynamic Interaction with interaction

among selected

if n == 3:

NM = 0

k = 0

i = 0

MH = []

MHR = []

if Mtrade == str('y') and LocalInt ==

str('n'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l =

random.randint(0,Reg_Num-1)

if l != Ind[k].Reg:

NM += 1

obj =

MigH(k,l,Ind[k].Id,0,0,'m')

MH.append(obj)

j = 1

k += 1

else:

k += 1

elif Mtrade == str('n') and LocalInt ==

str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].LRand < LX[i].Lx:

NM += 1

Texas Tech University, David Ralph, May 2012

55

obj =

MigH(k,Ind[k].Reg,Ind[k].Id,0,0,'l')

MH.append(obj)

k += 1

else:

k += 1

# possibly randomize the ordering

elif Mtrade == str('y') and LocalInt ==

str('y'):

while k < Total:

i =

((Ind[k].Reg*(Pop_Num*Phen_Num))+(Ind[k].Pop*Phen_

Num)+ Ind[k].Iphen)

if Ind[k].MRand < MX[i].Mx:

j = 0

while j < 1:

l =

random.randint(0,Reg_Num-1)

if l != Ind[k].Reg:

NM += 1

obj =

MigH(k,l,Ind[k].Id,0,0,'m')

MH.append(obj)

j = 1

k += 1

else:

if Ind[k].LRand < LX[i].Lx:

NM += 1

obj =

MigH(k,Ind[k].Reg,Ind[k].Id,0,0,'l')

MH.append(obj)

k += 1

else:

k += 1

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MH[i].mhtell(1)

print >>OutP

MHR = random.sample(MH,NM)

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

#Setup

RS = []

PS = []

PHS = []

rs = 0

ps = 0

phs = 0

for i in range(Reg_Num):

for j in range(NM):

if i == MHR[j].Reg:

rs += 1

RS.append(rs)

Texas Tech University, David Ralph, May 2012

56

rs = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(NM):

if i == MHR[k].Reg and j ==

Ind[MHR[k].Ind].Pop:

ps += 1

PS.append(ps)

ps = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Phen_Num):

for l in range(NM):

if i == MHR[l].Reg and j ==

Ind[MHR[l].Ind].Pop and k == Ind[MHR[l].Ind].Iphen:

phs += 1

PHS.append(phs)

phs = 0

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Region Size = ' +

str(RS)

print >>OutP, 'Pop Size = ' + str(PS)

print >>OutP, 'Phenotypes = ' +

str(PHS)

for i in range(NM):

if Reg_Size[MHR[i].Reg] == 0:

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test:

Reg Size 0', Phas

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test: ',

Phas

else:

r = 0.0

for p in range(Pop_Num):

if PS[((MHR[i].Reg*Pop_Num)+

p)] != 0:

if p != Ind[MHR[i].Ind].Pop:

for q in

range(Phen_Num):

n =

((MHR[i].Reg*(Pop_Num*Phen_Num))+(p*Phen_Num)+

q)

if MHR[i].Location

== str('m'):

r +=

PHS[n]*MR[n]

else:

r +=

PHS[n]*LK[n]

r = r/RS[MHR[i].Reg]

s = random.random()

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual Migrating

to Region %d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print 'Migration Resistance

check:r = [%f], s = [%f] ' % (r,s)

Texas Tech University, David Ralph, May 2012

57

if Out == str('y'):

print >>OutP,

'Individual Migrating to Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print >>OutP,

'Migration Resistance check:r = [%f], s = [%f] ' % (r,s)

else:

print 'Individual is

Interacting within Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print 'Local Interaction

Resistance check:r = [%f], s = [%f] ' % (r,s)

if Out == str('y'):

print >>OutP,

'Individual is Interacting within Region

%d:Ind[%s]Reg[%d]Group[%d]Phen[%d]' %

(MHR[i].Reg,

Ind[MHR[i].Ind].Id,Ind[MHR[i].Ind].Reg,Ind[MHR[i].Ind].P

op,Ind[MHR[i].Ind].Iphen)

print >>OutP, 'Local

Interaction Resistance check:r = [%f], s = [%f] ' % (r,s)

if r > s:

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas test

Death: ', Phas

MHR[i].Death = 1

else:

rcm = 0.0

l = 0.0

RChk = 0

p = 0

CRK = 0.0

while p < Pop_Num:

rc = 0.0

if

PS[((MHR[i].Reg*Pop_Num)+ p)] != 0:

if p ==

Ind[MHR[i].Ind].Pop:

rc +=0

else:

for q in

range(Phen_Num):

n =

((MHR[i].Reg*(Pop_Num*Phen_Num))+(p*Phen_Num)+

q)

if

MHR[i].Location == str('m'):

rc +=

PHS[n]*MC[n]

else:

rc +=

PHS[n]*LC[n]

l =

PS[((MHR[i].Reg*Pop_Num)+ p)]

rcm +=

rc/RS[MHR[i].Reg]

if MHR[i].Location ==

str('m'):

CRK =

Cres[((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(In

d[MHR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)]

else:

Texas Tech University, David Ralph, May 2012

58

CRK =

LCR[((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(In

d[MHR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)]

if Detail == 1:

if Out == str('y'):

#if

MHR[i].Location == str('m'):

#print

>>OutP, 'Cres = :' % Cres

#else:

#print

>>OutP, 'LCR = :' % LCR

print >>OutP,

'Pop = %d' % p

print >>OutP,

'rc = %f' % rc

print >>OutP, 'l

= %d' % l

print >>OutP,

'rcm = %f' % rcm

print >>OutP,

'CRK = %f' % CRK

print >>OutP, 'r

= %f' % r

print >>OutP, 's

= %f' % s

print >>OutP,

'Check = %f' % ((r + rcm) - (rcm*CRK))

if ((r + rcm) -

(rcm*CRK)) > s:

if Detail == 1:

if Out ==

str('y'):

print

>>OutP, 'Converted = [%f] > [%f]' % (((r + rcm) -

(rcm*CRK)), s)

print

>>OutP, 'Phas test Conversion: ', Phas

MHR[i].Cpop = p

MHR[i].Death = 2

if Detail == 1:

if Out ==

str('y'):

print

>>OutP, 'Phas test:Conversion: ', Phas

p = Pop_Num

RChk = 1

else:

p += 1

else:

p += 1

if RChk == 0:

if Detail == 1:

if Out == str('y'):

print >>OutP,

'Passed total chance = [%f] < [%f]' % (((r + rcm) -

(rcm*CRK)), s)

print >>OutP, 'Phas

test: ', Phas

if Detail == 1:

if Out == str('y'):

print >>OutP, 'Phas

test: ', Phas

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

MHR.sort(key = operator.attrgetter('Ind'))

Texas Tech University, David Ralph, May 2012

59

if Detail == 1:

if Out == str('y'):

for i in range(NM):

MHR[i].mhtell(1)

print >>OutP

for i in range(NM):

if MHR[i].Death == 0:

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual %s

Migrated to Region %d' % (Ind[MHR[i].Ind].Id,

MHR[i].Reg)

if Out == str('y'):

print >>OutP,

'Individual %s Migrated to Region %d' %

(Ind[MHR[i].Ind].Id, MHR[i].Reg)

else:

print 'Individual %s

Interacted Safely' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s Interacted Safely' % Ind[MHR[i].Ind].Id

if MHR[i].Location == str('m'):

Reg_Size[MHR[i].Reg] =

Reg_Size[MHR[i].Reg] + 1

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg =

MHR[i].Reg

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

if MHR[i].Death == 1:

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual %s failed to

migrate' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s failed to migrate' % Ind[MHR[i].Ind].Id

else:

print 'Individual %s died

during local interaction' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s died during local interaction' %

Ind[MHR[i].Ind].Id

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Total = Total - 1

Texas Tech University, David Ralph, May 2012

60

if MHR[i].Death == 2:

if Detail == 1:

if MHR[i].Location == str('m'):

print 'Individual %s was

converted by group %d in Region %d' %

(Ind[MHR[i].Ind].Id, MHR[i].Cpop, MHR[i].Reg)

if Out == str('y'):

print >>OutP,

'Individual %s was converted by group %d in Region %d'

% (Ind[MHR[i].Ind].Id, MHR[i].Cpop, MHR[i].Reg)

else:

print 'Individual %s was

converted during local interaction' % Ind[MHR[i].Ind].Id

if Out == str('y'):

print >>OutP,

'Individual %s was converted during local interaction' %

Ind[MHR[i].Ind].Id

Reg_Size[MHR[i].Reg] =

Reg_Size[MHR[i].Reg] + 1

Reg_Size[Ind[MHR[i].Ind].Reg] =

Reg_Size[Ind[MHR[i].Ind].Reg] - 1

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]-1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] -= 1

Ind[MHR[i].Ind].Reg = MHR[i].Reg

Ind[MHR[i].Ind].Pop = MHR[i].Cpop

m =

((Ind[MHR[i].Ind].Reg*Pop_Num)+ Ind[MHR[i].Ind].Pop)

Pop_Size[m] = Pop_Size[m]+1

n =

((Ind[MHR[i].Ind].Reg*(Pop_Num*Phen_Num))+(Ind[M

HR[i].Ind].Pop*Phen_Num)+ Ind[MHR[i].Ind].Iphen)

Phas[n] += 1

KC = 0

for i in range(NM):

if MHR[i].Death == 1:

del Ind[(MHR[i].Ind)-KC]

KC += 1

Ind.sort(key = operator.attrgetter('Pop'))

Ind.sort(key = operator.attrgetter('Reg'))

# Ind Fitness Assignment

def FitAss():

k = 0

i = 0

while k < Total:

if Ind[k].Reg == PX[i].RegId:

if Ind[k].Pop == PX[i].PopId:

if Ind[k].Iphen == PX[i].Phen:

if Ind[k].IRand <= PX[i].Pxt:

Ind[k].Iw = PX[i].Off

k += 1

i = 0

else:

i += 1

else:

Texas Tech University, David Ralph, May 2012

61

i += 1

else:

i += 1

else:

i +=1

# Covariance

def Cov(n):

global CI, CW, CG, CR

if n == 0:

toff = 0.0

phsum = 0.0

phcov = 0.0

for m in range(Total):

phsum += Ind[m].Iphen

toff += Ind[m].Iw

phmean = (phsum/Total)

pofmean = (toff/Total)

if Detail == 1:

print 'Individual Covariance'

print 'Meta-population size: \n', Total

print 'Sum of Individual Phenotypes: \n',

phsum

print 'Mean Phenotype: \n', phmean

print 'Sum of Offspring: \n', toff

print 'Mean Offspring Number: \n',

pofmean

if Out == str('y'):

print >>OutP, 'Individual

Covariance'

print >>OutP, 'Meta-population size:

\n', Total

print >>OutP, 'Sum of Individual

Phenotypes: \n', phsum

print >>OutP, 'Mean Phenotype: \n',

phmean

print >>OutP, 'Sum of Offspring: \n',

toff

print >>OutP, 'Mean Offspring

Number: \n', pofmean

for i in range(Total):

phcov += ((Ind[i].Iphen -

phmean)*(Ind[i].Iw - pofmean))

CI = (phcov/Total)

if Detail == 1:

print 'Individual Covariance: \n', CI

if Out == str('y') and Detail != 3:

print >>OutP, 'Individual Covariance:

\n', CI

if n == 1:

cosum = []

cophen = []

comean = []

cof = []

cfmean = []

CW = []

k = 0

l = 0

for i in range(Reg_Num*Pop_Num):

cosum.append(Pop_Size[i])

cphen = 0.0

cw = 0.0

cmean = 0.0

for j in range(Pop_Size[i]):

Texas Tech University, David Ralph, May 2012

62

cphen += Ind[k].Iphen

cw += Ind[k].Iw

k +=1

cophen.append(cphen)

cof.append(cw)

if Pop_Size[i] == 0:

comean.append('*')

cfmean.append('*')

else:

comean.append(cophen[i]/cosum[i])

cfmean.append(cof[i]/cosum[i])

if Detail == 1:

print 'Within Group Covariance Details'

print 'Population size: \n', cosum

print 'Sum of Phenotypes: \n', cophen

print 'Mean Phenotype: \n', comean

print 'Sum of Offspring: \n', cof

print 'Mean Offspring Number: \n',

cfmean

if Out == str('y'):

print >>OutP, 'Within Group

Covariance Details'

print >>OutP, 'Population size: \n',

cosum

print >>OutP, 'Sum of Phenotypes:

\n', cophen

print >>OutP, 'Mean Phenotype: \n',

comean

print >>OutP, 'Sum of Offspring: \n',

cof

print >>OutP, 'Mean Offspring

Number: \n', cfmean

for i in range(Reg_Num*Pop_Num):

if Pop_Size[i] == 0:

CW.append('*')

else:

csum = 0

for j in range(Pop_Size[i]):

csum += ((Ind[l].Iphen -

comean[i])*(Ind[l].Iw - cfmean[i]))

l += 1

CW.append(csum/cosum[i])

if Detail == 1:

print 'Within Group Covariance per

Region: \n', CW

if Out == str('y') and Detail != 3:

print >>OutP, 'Within Group Covariance

per Region: \n', CW

if n == 3:

toff = 0.0

psum = 0.0

pcov = 0.0

for m in range(Total):

psum += Ind[m].Pop

toff += Ind[m].Iw

popmean = (psum/Total)

pofmean = (toff/Total)

if Detail == 1:

print 'Group Covariance Details'

print 'Meta-population size: \n', Total

print 'Sum of Population Phenotype: \n',

psum

print 'Mean Population Phenotype: \n',

popmean

print 'Sum of Offspring: \n', toff

Texas Tech University, David Ralph, May 2012

63

print 'Mean Offspring Number: \n',

pofmean

if Out == str('y'):

print >>OutP, 'Group Covariance

Details'

print >>OutP, 'Meta-population size:

\n', Total

print >>OutP, 'Sum of Population

Phenotype: \n', psum

print >>OutP, 'Mean Population

Phenotype: \n', popmean

print >>OutP, 'Sum of Offspring: \n',

toff

print >>OutP, 'Mean Offspring

Number: \n', pofmean

for i in range(Total):

pcov += ((Ind[i].Pop -

popmean)*(Ind[i].Iw - pofmean))

CG = (pcov/Total)

if Detail == 1:

print 'Group Covariance: \n', CG

if Out == str('y') and Detail != 3:

print >>OutP, 'Group Covariance: \n', CG

if n == 4:

toff = 0.0

rsum = 0.0

rcov = 0.0

for m in range(Total):

rsum += Ind[m].Reg

toff += Ind[m].Iw

regmean = (rsum/Total)

rofmean = (toff/Total)

if Detail == 1:

print 'Regional Covariance Details'

print 'Meta-population size: \n', Total

print 'Sum of Regional Phenotype: \n',

rsum

print 'Mean Regional Phenotype: \n',

regmean

print 'Sum of Offspring: \n', toff

print 'Mean Offspring Number: \n',

rofmean

if Out == str('y'):

print >>OutP, 'Regional Covariance

Details'

print >>OutP, 'Meta-population size:

\n', Total

print >>OutP, 'Sum of Regional

Phenotype: \n', rsum

print >>OutP, 'Mean Regional

Phenotype: \n', regmean

print >>OutP, 'Sum of Offspring: \n',

toff

print >>OutP, 'Mean Offspring

Number: \n', rofmean

for i in range(Total):

rcov += ((Ind[i].Reg -

regmean)*(Ind[i].Iw - rofmean))

CR = (rcov/Total)

if Detail == 1:

print 'Regional Covariance: \n', CR

if Out == str('y') and Detail != 3:

print >>OutP, 'Regional Covariance: \n',

CR

# Random Number Generation

def Rand():

random.seed()

for i in range(Total):

Texas Tech University, David Ralph, May 2012

64

Ind[i].IRand = random.random()

if Migration == str('y'):

Ind[i].MRand = random.random()

if LocalInt == str('y'):

Ind[i].LRand = random.random()

# Phenotype Count Function

def PhCount(n):

global pfcount

pcount = []

pc = []

m = 0

l = 0

if n == 0:

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Pop_Size[l]):

pc.append(Ind[m].Iphen)

m += 1

pcount.append(pc)

l += 1

pc =[]

if Detail == 1:

l = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

for k in range(Phen_Num):

print '[ %d ][ %d ][ %d ][ %d

]' % (i, j, Pop_Size[l], pcount[j].count(k))

l += 1

print

if Out == str('y') and Detail != 3:

l = 0

for i in range(Reg_Num):

for j in range(Pop_Num):

print >>OutP, '[ %d ][ %d ][ %d

]' % (i, j, Pop_Size[l]),

for k in range(Phen_Num):

print >>OutP, '[ %d ]' %

(pcount[l].count(k)),

l += 1

print >>OutP

print >>OutP

#Group Size Count Print Output - Final

if n == 1:

global pfcount

gcount = []

for i in range(Total):

obj = Ind[i].Pop

gcount.append(obj)

if Out == str('y'):

if pfcount == 0:

print >>OutP0, '%d,%d' %

(gcount.count(0),gcount.count(1))

pfcount += 1

elif pfcount == 1:

print >>OutP1, '%d,%d' %

(gcount.count(0),gcount.count(1))

pfcount += 1

elif pfcount == 2:

Texas Tech University, David Ralph, May 2012

65

print >>OutP2, '%d,%d' %

(gcount.count(0),gcount.count(1))

pfcount += 1

elif pfcount == 3:

print >>OutP3, '%d,%d' %

(gcount.count(0),gcount.count(1))

pfcount += 1

elif pfcount == 4:

print >>OutP4, '%d,%d' %

(gcount.count(0),gcount.count(1))

#for j in range(1):

# for i in range(Pop_Num-1):

# x = str(gcount.count(i)) + ','

# print >>OutP2, x,

# print >>OutP2,

gcount.count(Pop_Num-1)

if n == 2:

gcount = []

for i in range(Total):

obj = Ind[i].Pop

gcount.append(obj)

if Out == str('y'):

for i in range(Pop_Num):

if gcount.count(i)== 0 and

EXTcount[i] == 0:

#print >>Extout, 'group [%d]

went extinct during generation %d: Sim = %d, %d, %d'

%(i, Gruns, Sruns, i, Gruns)

print >>Extout, '%d,%d' %(i,

Gruns)

EXTcount[i] = 1

#Starting Actual Simulation

Sruns = 0

#SPSG = []

while Sruns < Truns:

Popcount = []

Cout = []

CWout = []

CGout = []

CRout = []

Gruns = 0

pfcount = 0

#PSG = []

if Detail == 1:

print 'Simulation run %d' % (Sruns)

if Out == str('y') and Detail != 3:

print >>OutP, 'Simulation run %d' %

(Sruns)

while Gruns < Gen:

if Detail == 1:

print 'Generation %d' % (Gruns)

if Out == str('y') and Detail != 3:

print >>OutP, 'Generation %d' %

(Gruns)

print >>OutP

if Detail != 2 and Detail != 3:

label(0)

for i in range(Total):

Ind[i].tell(0)

if Out == str('y'):

label(4)

for i in range(Total):

Ind[i].tell(4)

Texas Tech University, David Ralph, May 2012

66

print >>OutP

Rand()

if Detail == 1:

if Migration == str('y') or LocalInt ==

str('y'):

label(3)

for i in range(Total):

Ind[i].tell(3)

else:

label(2)

for i in range(Total):

Ind[i].tell(2)

if Out == str('y'):

if Migration == str('y') or LocalInt

== str('y'):

label(7)

for i in range(Total):

Ind[i].tell(7)

else:

label(6)

for i in range(Total):

Ind[i].tell(6)

print >>OutP

# Migration/Local Interaction Assignment

if Migration == str('y') or LocalInt ==

str('y'):

IntAss(IV)

# Density Impact

if Detail == 1:

if Migration == str('y') or LocalInt ==

str('y'):

label(3)

for i in range(Total):

Ind[i].tell(3)

if Out == str('y'):

label(7)

for i in range(Total):

Ind[i].tell(7)

print >>OutP

else:

label(2)

for i in range(Total):

Ind[i].tell(2)

if Out == str('y'):

label(6)

for i in range(Total):

Ind[i].tell(6)

print >>OutP

# Offspring Distribution

#PhCount(0)

MeanOff()

# Ind Fitness Assignment

FitAss()

if Detail == 1:

if Migration == str('y') or LocalInt ==

str('y'):

Texas Tech University, David Ralph, May 2012

67

label(3)

for i in range(Total):

Ind[i].tell(3)

if Out == str('y'):

label(7)

for i in range(Total):

Ind[i].tell(7)

else:

label(2)

for i in range(Total):

Ind[i].tell(2)

if Out == str('y'):

label(6)

for i in range(Total):

Ind[i].tell(6)

elif Detail !=2 and Detail != 3:

label(1)

for i in range(Total):

Ind[i].tell(1)

if Out == str('y'):

label(5)

for i in range(Total):

Ind[i].tell(5)

print >>OutP

if Total == 0:

if Detail == 1:

print 'Metapopulation is extinct at

Generation %d' % Gruns

print >>OutP, 'Metapopulation is

extinct at Generation %d' % Gruns

if Out == str('y'):

if Detail != 3:

print >>OutP, ' Reg Grp Size

#Phen'

PhCount(0)

PhCount(2)

break

else:

Cov(0)

Cov(1)

Cov(3)

Cov(4)

Cout.append(CI)

CWout.append(CW)

CGout.append(CG)

CRout.append(CR)

if Out == str('y') and Detail != 3:

print >>OutP

if Out == str('y'):

if Detail != 3:

print >>OutP, ' Reg Grp Size

#Phen'

PhCount(0)

PhCount(2)

# Plot Array Formation

#GPop()

# Generation Changeover

if Out == str('y'):

if Detail !=2 and Detail != 3:

Texas Tech University, David Ralph, May 2012

68

print >>OutP, Reg_Size

print >>OutP, Pop_Size

GTotal = Total

GReg = list(Reg_Size)

GPopS = list(Pop_Size)

for i in range(GTotal):

for j in range(Ind[i].Iw):

obj = Indiv(Ind[i].Reg,

Ind[i].Pop, Ind[i].Id, Ind[i].Iphen, 0, 0.0, 0.0,0.0)

Ind.append(obj)

Total += 1

Reg_Size[Ind[i].Reg] += 1

n =

((Ind[i].Reg*(Pop_Num*Phen_Num))+(Ind[i].Pop*Phen_

Num)+ Ind[i].Iphen)

Phas[n] += 1

m = 0

for k in range (Reg_Num):

for l in range(Pop_Num):

if Ind[i].Reg == k:

if Ind[i].Pop == l:

Pop_Size[m] +=

1

m +=1

for i in range(GTotal):

n =

((Ind[0].Reg*(Pop_Num*Phen_Num))+(Ind[0].Pop*Phen

_Num)+ Ind[0].Iphen)

Phas[n] -= 1

del Ind[0]

Total = (Total - GTotal)

k = 0

for i in range(Reg_Num):

Reg_Size[i] = Reg_Size[i] - GReg[i]

for j in range(Pop_Num):

Pop_Size[k] = Pop_Size[k] -

GPopS[k]

k += 1

#print >>OutP, Reg_Size

#print >>OutP, Pop_Size

if Detail == 1:

label(2)

for i in range(Total):

Ind[i].tell(2)

print Total

for j in range(Reg_Num):

print '[%d]' % (Reg_Size[j])

for i in range(Pop_Num):

print '[%d]' % (Pop_Size[i])

if Out == str('y'):

label(6)

for i in range(Total):

Ind[i].tell(6)

print >>OutP, 'Total = :', Total

print >>OutP, 'Region Size = :',

Reg_Size

print >>OutP, 'Pop Size = :',

Pop_Size

print >>OutP

Gruns += 1

Texas Tech University, David Ralph, May 2012

69

if Gruns == (int(GenOut[pfcount]) - 1):

PhCount(1)

Ind = copy.deepcopy(OInd)

Reg_Size = list(OReg_Size)

Pop_Size = list(OPop_Size)

Total = OTotal

Phas = list(OPhas)

#if Detail == 1:

#Generation changover check

#print >>OutP, 'Total = :', Total

#print >>OutP, 'Reg_Size = :', Reg_Size

#print >>OutP, 'Pop_Size = :', Pop_Size

if Detail == 1:

for i in range(Gruns):

print Cout[i]

print CGout[i]

print CRout[i]

if Out == str('y') and Detail != 3:

print >>OutP, 'Cout = ', Cout

print >>OutP, 'CGout = ', CGout

print >>OutP, 'CRout = ', CRout

print >>OutP, 'Grun = ', Gruns

EICov = (sum(Cout)/Gruns)

EGCov = (sum(CGout)/Gruns)

ERCov = (sum(CRout)/Gruns)

if Detail == 1:

print 'Individual Selection :', EICov

print 'Group Selection :', EGCov

print 'Regional Selection : ', ERCov

if Out == str('y') and Detail != 3:

print >>OutP

print >>OutP, 'Individual Selection :', EICov

print >>OutP, 'Group Selection :', EGCov

print >>OutP, 'Regional Selection : ', ERCov

print >>OutP

Sruns += 1

EXTcount = []

for i in range(Pop_Num):

obj = 0

EXTcount.append(obj)

pfcount = 0

#SEICov = (sum(EICov)/Sruns)

#SEGCov = (sum(EGCov)/Sruns)

#SERCov = (sum(ERCov)/Sruns)

if Out == str('y'):

OutP.close()

OutP0.close()

OutP1.close()

OutP2.close()

OutP3.close()

OutP4.close()

Extout.close()

FC += 1

Texas Tech University, David Ralph, May 2012

68

APPENDIX B

ANALYTICAL PROGRAM

import random

import operator

import copy

import sys

FI = raw_input('Input parameters from file?: ')

#FI = str('y')

if FI == str('y'):

#FN = input('How many files do you want to use?: ')

FN = 462

sys.stdin = file('paramAsample.txt','rb')

else:

FN = 1

FC = 0

while FC < FN:

Ignore = raw_input()

del Ignore

# Initial Parameters

def User_Input():

'''Define parameters of simulation:'''

global Out, Fout, OutP2, Detail, LocalInt, Truns,

Gen, Reg_Num, Grp_Num, Reg_Size, Grp_Size, DenD, K, R

global LI, LK, LC, LCR, PGout, EXT, EXTcount,

tcount

global Sout,x, GenA, PFA, PFB, PFC, PFD, PFPB,

PFAB, OPA, OPB, OPC, OPD, OPPB, OPAB

Detail = 9

Out = raw_input('Output results into files? Y/N

:')

Out = Out.rstrip('\r\n')

Out = Out.lower()

if Out == str('y'):

tcount = raw_input('What is the file count

number?: ')

tcount = tcount.rstrip('\r\n')

Fout = raw_input('Name the output file: ')

Fout = Fout.rstrip('\r\n')

Fout += (tcount + '.txt')

Detail = int(raw_input('Number of Print

Files:1(Final Gen) = 0, Growth = 1, Final + Final Parts =

2, Everything = 3 \nDetail Level: '))

GenA = [25,50,100,200,300,400,500]

if Detail == 0:

temp = raw_input('Name the print

output files: ')

temp = temp.rstrip('\r\n')

PFAB = [(temp + 'AB%dG' + tcount +

'.txt') % i for i in GenA]

EXT = (temp + 'ext' + tcount + '.txt')

if Detail == 1:

PGout = raw_input('Name the print

output file: ')

PGout = PGout.rstrip('\r\n')

PGout += (tcount + '.txt')

if Detail == 2 or Detail ==3:

Texas Tech University, David Ralph, May 2012

69

temp = raw_input('Name the print

output files: ')

temp = temp.rstrip('\r\n')

GenA = [25,50,100,200,300,400,500]

PFA = [(temp + 'A%dG' + tcount + '.txt')

% i for i in GenA]

PFB = [(temp + 'B%dG' + tcount + '.txt')

% i for i in GenA]

PFC = [(temp + 'C%dG' + tcount + '.txt')

% i for i in GenA]

PFD = [(temp + 'D%dG' + tcount + '.txt')

% i for i in GenA]

PFPB = [(temp + 'PB%dG' + tcount +

'.txt') % i for i in GenA]

PFAB = [(temp + 'AB%dG' + tcount +

'.txt') % i for i in GenA]

EXT = temp + 'ext' + tcount + '.txt'

Sout = raw_input('Output to Screen? Y/N: ')

Sout = Sout.rstrip('\r\n')

Sout = Sout.lower()

LocalInt = raw_input('Use local interaction in

simulation? Y/N : ')

LocalInt = LocalInt.rstrip('\r\n')

LocalInt = LocalInt.lower()

DenD = raw_input('Use Density Dependancy in

simulation? Y/N : ')

DenD = DenD.rstrip('\r\n')

DenD = DenD.lower()

Gen = int(raw_input('How many generations? :

'))

print

Reg_Num = int(raw_input('How many Regions in

the simulation? '))

Grp_Num = int(raw_input('How many Groups in

simulation? '))

Reg_Size = []

Grp_Size = []

EXTcount = []

for i in range(Grp_Num):

obj = 0

EXTcount.append(obj)

for i in range(Reg_Num):

rt = 0

for j in range(Grp_Num):

Pop_S = float(raw_input('In Region %d

how many individuals are in Group %d ?: ' % (i, j)))

Grp_Size.append(Pop_S)

rt += Pop_S

Reg_Size.append(rt)

print

if DenD == str('y'):

K = []

R = []

for i in range(Reg_Num):

obj = float(raw_input('What is the

carrying capacity in Region %d?: ' %(i)))

K.append(obj)

for j in range(Grp_Num):

obj = float(raw_input('What is the

growth rate for group %d in Region %d?: ' %(j,i)))

R.append(obj)

print

if LocalInt == str('y'):

LI = []

for i in range(Reg_Num):

for j in range(Grp_Num):

Texas Tech University, David Ralph, May 2012

70

li = float(raw_input('In Region %d For

Group %d what is the initial probability that individual

will interact localy?: '% (i, j)))

LI.append(li)

print

LK = []

LC = []

#LCR = []

for i in range(Reg_Num):

for j in range(Grp_Num):

lk = float(raw_input('In Region %d

Group %d, what is the probability of killing a local 0 to 1: '

% (i, j)))

lc = float(raw_input('In Region %d

Group %d, what is the probability of converting a local 0

to %g: ' % (i, j,(1-lk))))

#lcr = float(raw_input('In Region

%d Group %d, what is the probability of resisting a local

conversion attempt: ' % (i, j)))

LK.append(lk)

LC.append(lc)

# LCR.append(lcr)

print

def PrintP():

print >>OutP, ' Analytical Model Parameters'

print >>OutP, 'File Number: ', tcount

print >>OutP, 'Detail Level: ', Detail

print >>OutP, 'Local Interaction: ', LocalInt

print >>OutP, 'Density Dependency: ', DenD

print >>OutP, 'Number of Generations: ', Gen

print >>OutP, 'Number of Regions: ', Reg_Num

print >>OutP, 'Number of Groups: ', Grp_Num

print >>OutP, 'Population Sizes: ', Grp_Size

if DenD == str('y'):

print >>OutP, 'Carrying Capicity by Region: ',

K

print >>OutP, 'Growth rate for each group

within each region: ', R

if LocalInt == str('y'):

print >>OutP, 'Local Interaction Array:', LI

print >>OutP, 'Local Interaction Kill %:', LK

print >>OutP, 'Local Interaction Conversion

%:', LC

#Group Array

class Group(object):

def __init__(self, Reg, GID, Pops, GI, PK, PC, PCR, A,

B, C, D, PB, AB):

self.Reg = Reg

self.GID = GID

self.Pops = Pops

self.GI = GI

self.PK = PK

self.PC = PC

self.PCR = PCR

self.A = A

self.B = B

self.C = C

self.D = D

self.PB = PB

self.AB = AB

def tell(self, n):

if n == 0:

print

'[%d][%d][%g][%g][%g][%g][%g][%g][%g][%g][%g][%

g]'% (self.Reg, self.GID, self.Pops, self.GI, self.PK, self.PC,

self.A, self.B, self.C, self.D, self.PB, self.AB)

Texas Tech University, David Ralph, May 2012

71

if n == 1:

print >>OutP,

'[%d][%d][%g][%g][%g][%g][%g][%g][%g][%g][%g][%

g]'% (self.Reg, self.GID, self.Pops, self.GI, self.PK, self.PC,

self.A, self.B, self.C, self.D, self.PB, self.AB)

# Print Label

def label(n):

if n == 0:

print ' R G # GI PK PC A B

C D PB AB'

if n == 1:

print >>OutP, ' R G # GI PK PC

A B C D PB AB'

#Intial Paramaters

User_Input()

#Creating Array

Grp = []

l = 0

for i in range(Reg_Num):

for j in range(Grp_Num):

obj = Group(i, j, Grp_Size[l],0.0,

0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)

Grp.append(obj)

l += 1

#Print File Intilization: Output file header

if Out == str('y'):

OutP = open(Fout, 'w')

if Detail == 0:

OPAB = [('OutPAB%d') % i for i in

range(len(PFAB))]

for i in range(7):

[globals().__setitem__(OPAB[i],open(PFAB[i],'w'))]

Extout = open(EXT, 'w')

if Detail == 1:

OutP2 = open(PGout, 'w')

if Detail == 2 or Detail ==3:

OPA = [('OutPA%d') % i for i in

range(len(PFA))]

OPB = [('OutPB%d') % i for i in

range(len(PFB))]

OPC = [('OutPC%d') % i for i in

range(len(PFC))]

OPD = [('OutPD%d') % i for i in

range(len(PFD))]

OPPB = [('OutPPB%d') % i for i in

range(len(PFPB))]

OPAB = [('OutPAB%d') % i for i in

range(len(PFAB))]

for i in range(7):

[globals().__setitem__(OPA[i],open(PFA[i],'w'))]

[globals().__setitem__(OPB[i],open(PFB[i],'w'))]

[globals().__setitem__(OPC[i],open(PFC[i],'w'))]

[globals().__setitem__(OPD[i],open(PFD[i],'w'))]

[globals().__setitem__(OPPB[i],open(PFPB[i],'w'))]

[globals().__setitem__(OPAB[i],open(PFAB[i],'w'))]

Extout = open(EXT, 'w')

if Out == str('y'):

PrintP()

#Interaction Function

Texas Tech University, David Ralph, May 2012

72

def Inter(n):

#Initialization of Interaction Probabilities

if n == 0:

for i in range(Reg_Num*Grp_Num):

Grp[i].GI = LI[i]*Grp[i].Pops

#for i in range(Reg_Num*Grp_Num):

#Grp[i].tell(0)

#for i in range(Reg_Num):

#print Reg_Size[i]

#Inter(0)

#A Non Interaction Group Size-GI

def NonInt():

for i in range(Reg_Num*Grp_Num):

Grp[i].A = (Grp[i].Pops - Grp[i].GI)

if Detail == 3:

if x < 24:

print >>OutPA0, '%f,%f' % (Grp[0].A,

Grp[1].A)

if x < 49:

print >>OutPA1, '%f,%f' % (Grp[0].A,

Grp[1].A)

if x < 99:

print >>OutPA2, '%f,%f' % (Grp[0].A,

Grp[1].A)

if x < 199:

print >>OutPA3, '%f,%f' % (Grp[0].A,

Grp[1].A)

if x < 299:

print >>OutPA4, '%f,%f' % (Grp[0].A,

Grp[1].A)

if x < 399:

print >>OutPA5, '%f,%f' % (Grp[0].A,

Grp[1].A)

if x < 499:

print >>OutPA6, '%f,%f' % (Grp[0].A,

Grp[1].A)

#NonInt()

#B Population Loss due to Interaction GI(PK +PC)

def PopLoss():

for i in range(Reg_Num):

for j in range(Reg_Num*Grp_Num):

if i == Grp[j].Reg:

plk = 0.0

plc = 0.0

for k in range(Grp_Num*Reg_Num):

if i == Grp[k].Reg:

if k != j:

if (Reg_Size[i]-

Grp[j].Pops) < 1:

plk += 0

plc += 0

else:

plk +=

LK[k]*(Grp[k].Pops/(float(Reg_Size[i])))

plc +=

LC[k]*(Grp[k].Pops/(float(Reg_Size[i])))

#plk +=

LK[k]*(Grp[k].Pops/(float(Reg_Size[i]- Grp[j].Pops)))

Texas Tech University, David Ralph, May 2012

73

#plc +=

LC[k]*(Grp[k].Pops/(float(Reg_Size[i]- Grp[j].Pops)))

Grp[j].PK = plk

Grp[j].PC = plc

Grp[j].B = Grp[j].GI * (Grp[j].PK +

Grp[j].PC)

if Detail == 3:

if x < 24:

print >>OutPB0, '%f,%f' % (Grp[0].B,

Grp[1].B)

if x < 49:

print >>OutPB1, '%f,%f' % (Grp[0].B,

Grp[1].B)

if x < 99:

print >>OutPB2, '%f,%f' % (Grp[0].B,

Grp[1].B)

if x < 199:

print >>OutPB3, '%f,%f' % (Grp[0].B,

Grp[1].B)

if x < 299:

print >>OutPB4, '%f,%f' % (Grp[0].B,

Grp[1].B)

if x < 399:

print >>OutPB5, '%f,%f' % (Grp[0].B,

Grp[1].B)

if x < 499:

print >>OutPB6, '%f,%f' % (Grp[0].B,

Grp[1].B)

#PopLoss()

#C Population that Interacts Saftly G1I1(1-(PK

+PC))

def PopSafe():

for i in range(Reg_Num*Grp_Num):

Grp[i].C = (Grp[i].GI * (1 - (Grp[i].PK +

Grp[i].PC)))

if Detail == 3:

if x < 24:

print >>OutPC0, '%f,%f' % (Grp[0].C,

Grp[1].C)

if x < 49:

print >>OutPC1, '%f,%f' % (Grp[0].C,

Grp[1].C)

if x < 99:

print >>OutPC2, '%f,%f' % (Grp[0].C,

Grp[1].C)

if x < 199:

print >>OutPC3, '%f,%f' % (Grp[0].C,

Grp[1].C)

if x < 299:

print >>OutPC4, '%f,%f' % (Grp[0].C,

Grp[1].C)

if x < 399:

print >>OutPC5, '%f,%f' % (Grp[0].C,

Grp[1].C)

if x < 499:

print >>OutPC6, '%f,%f' % (Grp[0].C,

Grp[1].C)

#PopSafe()

#D Population Gain from Converts #G2I2(P(conv))

def PopGain():

Conv = 0.0

for i in range(Reg_Num):

for j in range(Reg_Num*Grp_Num):

if i == Grp[j].Reg:

for k in range(Grp_Num*Reg_Num):

Texas Tech University, David Ralph, May 2012

74

if i == Grp[k].Reg:

if k != j:

if (Reg_Size[i]-

Grp[k].Pops) < 1:

Conv += 0.0

else:

Conv += (Grp[k].GI

* (Grp[j].Pops/(float(Reg_Size[i])) * LC[j]))

#Conv +=

(Grp[k].GI * ((Grp[j].Pops/(float(Reg_Size[i] -

Grp[k].Pops))) * LC[j]))

Grp[j].D = Conv

if Detail == 3:

if x < 24:

print >>OutPD0, '%f,%f' % (Grp[0].D,

Grp[1].D)

if x < 49:

print >>OutPD1, '%f,%f' % (Grp[0].D,

Grp[1].D)

if x < 99:

print >>OutPD2, '%f,%f' % (Grp[0].D,

Grp[1].D)

if x < 199:

print >>OutPD3, '%f,%f' % (Grp[0].D,

Grp[1].D)

if x < 299:

print >>OutPD4, '%f,%f' % (Grp[0].D,

Grp[1].D)

if x < 399:

print >>OutPD5, '%f,%f' % (Grp[0].D,

Grp[1].D)

if x < 499:

print >>OutPD6, '%f,%f' % (Grp[0].D,

Grp[1].D)

#PopGain()

#A+C+D = Potential Breeders n=1 equals total

group pop across regions

def Gprime(n):

if n == 0:

for i in range(Reg_Num*Grp_Num):

Grp[i].PB = (Grp[i].A + Grp[i].C +

Grp[i].D)

if Grp[i].PB < 1:

Grp[i].PB = 0.0

if Detail == 3:

if x < 24:

print >>OutPPB0, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

if x < 49:

print >>OutPPB1, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

if x < 99:

print >>OutPPB2, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

if x < 199:

print >>OutPPB3, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

if x < 299:

print >>OutPPB4, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

if x < 399:

print >>OutPPB5, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

if x < 499:

print >>OutPPB6, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

Texas Tech University, David Ralph, May 2012

75

if n == 1 :

global Gpri

Gpri = []

total = 0.0

for i in range(Reg_Num):

for j in range(Grp_Num):

for k in range(Reg_Num*Grp_Num):

if j == Grp[k].GID:

total += Grp[k].PB

Gpri.append(total)

total = 0.0

def Gnaught(n):

if n == 0:

for i in range(Reg_Num):

Nreg = 0.0

for j in range(Grp_Num):

for k in range(Reg_Num*Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

Grp_Size[k] = Grp[k].PB

Grp[k].Pops = Grp[k].PB

Nreg += Grp[k].PB

Reg_Size[i] = Nreg

for i in range(Reg_Num):

for j in range(Grp_Num):

for k in range(Reg_Num*Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

Grp[k].AB = Grp[k].PB*

(R[k]*(1 + (1 - (float(Reg_Size[i]/K[i])))))

if Detail == 3:

if x < 24:

print >>OutPAB0, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if x < 49:

print >>OutPAB1, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if x < 99:

print >>OutPAB2, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if x < 199:

print >>OutPAB3, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if x < 299:

print >>OutPAB4, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if x < 399:

print >>OutPAB5, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if x < 499:

print >>OutPAB6, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if n == 1:

global Gnot

Gnot = []

total = 0.0

for i in range(Reg_Num):

for j in range(Grp_Num):

for k in range(Reg_Num*Grp_Num):

if j == Grp[k].GID:

total += Grp[k].AB

Texas Tech University, David Ralph, May 2012

76

Gnot.append(total)

total = 0

#Program Start

if DenD == str('y'):

x = 0

for x in range(Gen):

if Sout == str('y'):

print

print 'Group Population Before

Interaction: '

for i in range(Reg_Num):

print 'Reg_Size %d = %g' % (i,

Reg_Size[i])

for k in range(Reg_Num*Grp_Num):

for j in range(Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

print 'In Reg %d, Group

%d is size: %g' % (i,j,Grp_Size[k])

Inter(0)

NonInt()

PopLoss()

PopSafe()

PopGain()

Gprime(0)

Gprime(1)

if Sout == str('y'):

print 'Generation %d'% (x)

label(0)

for i in range(Reg_Num*Grp_Num):

Grp[i].tell(0)

Gnaught(0)

Gnaught(1)

if Detail != 1:

for i in range(Grp_Num):

if Gnot[i] == 0 and EXTcount[i] ==

0:

print >>Extout, '%d, %d' %(i, x)

EXTcount[i] = 1

if Out == str('y'):

if Detail == 0:

if x == 24:

print >>OutPAB0, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 49:

print >>OutPAB1, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 99:

print >>OutPAB2, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 199:

print >>OutPAB3, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 299:

print >>OutPAB4, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 399:

print >>OutPAB5, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 499:

print >>OutPAB6, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

Texas Tech University, David Ralph, May 2012

77

if Out == str('y'):

if Detail == 2 or Detail == 3:

if x == 24:

print >>OutPA0, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB0, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC0, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD0, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB0, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB0, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 49:

print >>OutPA1, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB1, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC1, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD1, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB1, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB1, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 99:

print >>OutPA2, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB2, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC2, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD2, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB2, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB2, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 199:

print >>OutPA3, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB3, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC3, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD3, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB3, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB3, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 299:

print >>OutPA4, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB4, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC4, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD4, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB4, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB4, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 399:

print >>OutPA5, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB5, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC5, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD5, '%f,%f' %

(Grp[0].D, Grp[1].D)

Texas Tech University, David Ralph, May 2012

78

print >>OutPPB5, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB5, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 499:

print >>OutPA6, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB6, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC6, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD6, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB6, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB6, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if Sout == str('y'):

print 'Generation %d'% (x)

label(0)

for i in range(Reg_Num*Grp_Num):

Grp[i].tell(0)

print 'Group Population After

Interaction: '

for i in range(Grp_Num):

print 'Group # %d : [%f]' % (i,

Gpri[i])

print 'Group Population After Breeding: '

for i in range(Grp_Num):

print 'Group # %d : [%f]' % (i,

Gnot[i])

if Out == str('y'):

if Detail == 1:

print >>OutP2, Gnot[i]

#test DD

if Sout == str('y'):

print 'test DD'

for i in range(Reg_Num):

print 'Reg_Size %d = %g' % (i,

Reg_Size[i])

for k in range(Reg_Num*Grp_Num):

for j in range(Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

print 'In Reg %d, Group

%d is size: %g' % (i,j,Grp_Size[k])

for i in range(Reg_Num):

Nreg = 0.0

for j in range(Grp_Num):

for k in range(Reg_Num*Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

Grp_Size[k] = Grp[k].AB

Grp[k].Pops = Grp[k].AB

Nreg += Grp[k].AB

Reg_Size[i] = Nreg

#test2 DD

if Sout == str('y'):

print 'test2 DD'

for i in range(Reg_Num):

Texas Tech University, David Ralph, May 2012

79

print 'Reg_Size %d = %g' % (i,

Reg_Size[i])

for k in range(Reg_Num*Grp_Num):

for j in range(Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

print 'In Reg %d, Group

%d is size: %g' % (i,j,Grp_Size[k])

else:

x = 0

for x in range(Gen):

Inter(0)

NonInt()

PopLoss()

PopSafe()

PopGain()

Gprime(0)

Gprime(1)

if Sout == str('y'):

print 'Generation %d'% (x)

label(0)

for i in range(Reg_Num*Grp_Num):

Grp[i].tell(0)

print 'Group Population After Breeding: '

for i in range(Grp_Num):

print 'Group # %d : [%f]' % (i,

Gpri[i])

#test

print 'test'

for i in range(Reg_Num):

print 'Reg_Size %d = %g' % (i,

Reg_Size[i])

for k in range(Reg_Num*Grp_Num):

for j in range(Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

print 'In Reg %d, Group

%d is size: %g' % (i,j,Grp_Size[k])

for i in range(Reg_Num):

Nreg = 0.0

for j in range(Grp_Num):

for k in range(Reg_Num*Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

Grp_Size[k] = Grp[k].PB

#Gnaught[l]

Grp[k].Pops = Grp[k].PB

Nreg += Grp[k].PB

#Gnaught[l]

Reg_Size[i] = Nreg

if Sout == str('y'):

#test2

print 'test2'

for i in range(Reg_Num):

print 'Reg_Size %d = %g' % (i,

Reg_Size[i])

for k in range(Reg_Num*Grp_Num):

for j in range(Grp_Num):

if i == Grp[k].Reg and j ==

Grp[k].GID:

Texas Tech University, David Ralph, May 2012

80

print 'In Reg %d, Group

%d is size: %g' % (i,j,Grp_Size[k])

if Out == str('y'):

if Detail == 0:

if x == 24:

print >>OutPAB0, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 49:

print >>OutPAB1, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 99:

print >>OutPAB2, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 199:

print >>OutPAB3, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 299:

print >>OutPAB4, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 399:

print >>OutPAB5, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 499:

print >>OutPAB6, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if Out == str('y'):

if Detail == 2 or Detail == 3:

if x == 24:

print >>OutPA0, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB0, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC0, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD0, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB0, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB0, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 49:

print >>OutPA1, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB1, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC1, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD1, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB1, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB1, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 99:

print >>OutPA2, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB2, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC2, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD2, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB2, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB2, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 199:

print >>OutPA3, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB3, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC3, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD3, '%f,%f' %

(Grp[0].D, Grp[1].D)

Texas Tech University, David Ralph, May 2012

81

print >>OutPPB3, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB3, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 299:

print >>OutPA4, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB4, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC4, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD4, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB4, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB4, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 399:

print >>OutPA5, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB5, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC5, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD5, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB5, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB5, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

elif x == 499:

print >>OutPA6, '%f,%f' %

(Grp[0].A, Grp[1].A)

print >>OutPB6, '%f,%f' %

(Grp[0].B, Grp[1].B)

print >>OutPC6, '%f,%f' %

(Grp[0].C, Grp[1].C)

print >>OutPD6, '%f,%f' %

(Grp[0].D, Grp[1].D)

print >>OutPPB6, '%f,%f' %

(Grp[0].PB, Grp[1].PB)

print >>OutPAB6, '%f,%f' %

(Grp[0].AB, Grp[1].AB)

if Out == str('y'):

if Detail == 0:

[globals()[i].close() for i in OPAB]

Extout.close()

if Detail == 2 or Detail == 3:

[globals()[i].close() for i in OPA]

[globals()[i].close() for i in OPB]

[globals()[i].close() for i in OPC]

[globals()[i].close() for i in OPD]

[globals()[i].close() for i in OPPB]

[globals()[i].close() for i in OPAB]

Extout.close()

FC += 1