# Error in Agent pos attribute Prisoner’s Dilemma Agents

**URL:** https://discourse.julialang.org/t/error-in-agent-pos-attribute-prisoner-s-dilemma-agents/57655
**Category:** General Usage
**Tags:** error
**Created:** [March 21, 2021, 2:05pm UTC](https://discourse.julialang.org/t/error-in-agent-pos-attribute-prisoner-s-dilemma-agents/57655 "2021-03-21T14:05:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ola\_Megahed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ola_megahed/32/20860_2.png) [@Ola\_Megahed](https://discourse.julialang.org/u/Ola_Megahed)
#### Post date: [March 21, 2021, 2:05pm UTC](https://discourse.julialang.org/t/error-in-agent-pos-attribute-prisoner-s-dilemma-agents/57655/1 "2021-03-21T14:05:02Z")

</div>

I am trying to duplicate the demographic Prisoner’s Dilemma by Epstein. I am having a code problem related to moving the agent according to my below function. basically I canot get the attribute position.  
function movement!(agent, model)  
newsite = agent.pos  
# find all unoccupied position within vision  
neighbors = nearby\_positions(agent.pos, model, agent.Vision)  
empty = collect(empty\_positions(model))  
if length(empty) \> 0  
newsite = rand(empty)  
end  
newsite != agent.pos && move\_agent!(agent, newsite, model)  
agent.Age += 1  
#play nearby neighbors  
nearbyAgents = collect(nearby\_agents(agent, model, agent.Vision))  
if length(nearbyAgents)\>0  
for ne in 1:length(nearbyAgents)  
if agent.Strategy==true && nearbyAgents[ne].Strategy== true  
agent.Wealth+= payoff[:R]  
elseif agent.Strategy == false && nearbyAgents[ne].Strategy == true  
agent.Wealth+= payoff[:T]  
elseif agent.Strategy == false && nearbyAgents[ne].Strategy == false  
agent.Wealth+= payoff[:P]  
elseif agent.Strategy == false && nearbyAgents[ne].Strategy == true  
agent.Wealth+= payoff[:S]  
end  
end  
end  
end  
movement!(Test, Model)

and I get this error

 ![Screen Shot 2021-03-21 at 3.02.09 PM](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc855c29a68c7afe77f70726ef4521c850bfd302.png)

While this is how I define my agent  
mutable struct Test \<: AbstractAgent  
id::Int  
pos::Dims{2}  
Strategy::Bool  
Wealth::Int  
Age::Int  
Max\_Age::Int  
Vision::Int  
end

And this is how I define my model:  
#initializing agents  
Model= ABM(Test,space,properties = properties,scheduler = random\_activation)  
for ag in 1:N  
add\_agent\_single!(  
Model,rand(Bool),Intial\_Endowment,0,rand(max\_age\_dist[1]:max\_age\_dist[2]),  
rand(vision\_dist[1]:vision\_dist[2])  
)  
end

---

<div class="post-metadata">

### Author: ![fnin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnin/32/23300_2.png) [@fnin](https://discourse.julialang.org/u/fnin)
#### Post date: [March 21, 2021, 3:06pm UTC](https://discourse.julialang.org/t/error-in-agent-pos-attribute-prisoner-s-dilemma-agents/57655/2 "2021-03-21T15:06:44Z")

</div>

As far as I can tell the problem is in `movement!(Test, Model) ` in the last line of this block:

> [@Ola\_Megahed](#):
>
> ```julia
> function movement!(agent, model)
> newsite = agent.pos
> # find all unoccupied position within vision
> neighbors = nearby_positions(agent.pos, model, agent.Vision)
> empty = collect(empty_positions(model))
> if length(empty) > 0
> newsite = rand(empty)
> end
> newsite != agent.pos && move_agent!(agent, newsite, model)
> agent.Age += 1
> #play nearby neighbors
> nearbyAgents = collect(nearby_agents(agent, model, agent.Vision))
> if length(nearbyAgents)>0
> for ne in 1:length(nearbyAgents)
> if agent.Strategy==true && nearbyAgents[ne].Strategy== true
> agent.Wealth+= payoff[:R]
> elseif agent.Strategy == false && nearbyAgents[ne].Strategy == true
> agent.Wealth+= payoff[:T]
> elseif agent.Strategy == false && nearbyAgents[ne].Strategy == false
> agent.Wealth+= payoff[:P]
> elseif agent.Strategy == false && nearbyAgents[ne].Strategy == true
> agent.Wealth+= payoff[:S]
> end
> end
> end
> end
> movement!(Test, Model)
> 
> ```

`movement!`, your stepping function, needs an agent instance to work, but in the last liner it gets the Agent type `Test`.

---

<div class="post-metadata">

### Author: ![Ola\_Megahed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ola_megahed/32/20860_2.png) [@Ola\_Megahed](https://discourse.julialang.org/u/Ola_Megahed)
#### Post date: [March 22, 2021, 11:59pm UTC](https://discourse.julialang.org/t/error-in-agent-pos-attribute-prisoner-s-dilemma-agents/57655/3 "2021-03-22T23:59:18Z")

</div>

Thank you… I should have used Model[id] instead of Test.
