Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 committed Oct 26, 2023
1 parent 4794a0d commit cb66dd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/interactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,20 @@ function interact!(ag1::Ind, ag2::Ind, model::ABM)
ag1.interaction_history[sp2] = model.step[1]
ag2.interaction_history[sp1] = model.step[1]

if sp1 != sp2 && model.food_sources[sp1, sp2] > 0 # predation
if (sp1 != sp2) && (model.food_sources[sp1, sp2] > 0) # predation
d = phenotypic_distance(ag1, ag2, model)
prob = interaction_power(ag1, ag2, d, model)
if rand(model.rng) < prob
eat!(ag1, ag2, model)
end
elseif sp1 != sp2 && model.food_sources[sp2, sp1] > 0 # predation
elseif (sp1 != sp2) && (model.food_sources[sp2, sp1] > 0) # predation
d = phenotypic_distance(ag2, ag1, model)
prob = interaction_power(ag2, ag1, d, model)
if rand(model.rng) < prob
eat!(ag2, ag1, model)
end
else # interaction
if sp1 == sp2 && ag1.sex != ag2.sex && model.ploidy[sp1] == 2 && in_reproduction_age(ag1, model) && in_reproduction_age(ag2, model) # reproduce
if (sp1 == sp2) && (ag1.sex != ag2.sex) && (model.ploidy[sp1] == 2) && in_reproduction_age(ag1, model) && in_reproduction_age(ag2, model) # reproduce
# reproduce!(ag1, ag2, model)
ag1.mate = ag2.id
ag1.time_met_other_sex = model.step[1]
Expand Down Expand Up @@ -264,14 +264,14 @@ function interact!(agent::Ind, model::ABM)
for id in target_ids
target = model[id]
target_sp = target.species
if agent.interaction_history[target_sp] != model.step[1] && target.interaction_history[sp] != model.step[1] # if agent and target have not interacted with such species before
if (agent.interaction_history[target_sp] != model.step[1]) && (target.interaction_history[sp] != model.step[1]) # if agent and target have not interacted with such species before
interact!(agent, target, model)
# if agent was a prey, check whether it is still alive
if model.food_sources[target_sp, sp] > 0
# if model.food_sources[target_sp, sp] > 0
if !agent.isalive
return
end
end
# end
end
end
end
8 changes: 5 additions & 3 deletions src/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ end

function model_step!(model::ABM)
model.step[1] += 1
model.resources .= model.resources_org[model.step[1]+1]
model.resources .= model.resources_org[model.step[1]+1] # TODO: add one more param for the growth rate of resources. resources_org would be the maximum value of the resources.
end

function agent_step!(agent::Ind, model::ABM)
Expand All @@ -216,7 +216,7 @@ function agent_step!(agent::Ind, model::ABM)
# interact with other species: predation, cooperation, competition.
interact!(agent, model)
# Kill the agent if it doesn't have energy
if agent.isalive && agent.energy < 0
if agent.isalive && (agent.energy < 0)
kill_agent!(agent, model)
return
end
Expand All @@ -230,12 +230,14 @@ function agent_step!(agent::Ind, model::ABM)

if agent.isalive && agent.age model.max_ages[agent.species]
kill_agent!(agent, model)
return
end
# bottleneck
bn_prob = model.bottlenecks[agent.species][model.step[1]+1][agent.pos...]
if agent.isalive && bn_prob > 0.0
if rand(model.rng) < bn_prob
kill_agent!(agent, model)
return
end
end
# update age
Expand Down Expand Up @@ -289,7 +291,7 @@ end

function consume_food!(agent::Ind, model::ABM)
environmental_consumption = model.food_sources[agent.species, agent.species]
if environmental_consumption > 0 && model.resources[agent.pos...] >= environmental_consumption
if (environmental_consumption > 0) && (model.resources[agent.pos...] >= environmental_consumption)
model.resources[agent.pos...] -= environmental_consumption
agent.energy += environmental_consumption
end
Expand Down

0 comments on commit cb66dd8

Please sign in to comment.