3
votes

I'm new at NetLogo. I'm not new at agent-based modeling. Now and then I see people saying NetLogo is so fun and easy that I'm giving it a try.

I'm stuck at square 1, can't figure out code for messaging between agents, scheduling actions for subsets of agents, other basic ABM things.

I'm working on a majority rule election model in which the turtles are voters with randomly placed x,y coordinates. Candidates (a new breed) make promises in x,y plane, the winning candidate is the one preferred by (closer to) more agents.

The patches play no role at all, except the spatial graph seems to require use of a grid

I'm having trouble in the updating of candidate positions. I conceived this as follows: Remove a 'winner' agent from the candidate agentset, tell the other candidates to adjust, then put the winner back in the agent list and see who wins in the next time.

I believe the following is close to minimum reproducible example. Please look at adjustOfferings. (Sorry, I left in a lot of show statements, otherwise can't tell what's going on.)

; Paul Johnson
; 20160801
breed [candidates candidate]
globals[
  N 
  D
]
; turtles are voters, Euclidean-based preferences. Use
; built-in 2d coordinates for ideal points for now,
; need to learn how to create weighted multi-dimensional
; preferences later
turtles-own
[
    name
]

to setup
  ca   ; clear all
  reset-ticks  ; time start 0

  set N 100
  ; N voters with random x,y ideal points
  create-turtles N
  [
    setxy random-xcor random-ycor
  ]

  set D 2
  create-candidates D
  [
    setxy random-xcor random-ycor
    set shape "square"
    set size 2
  ]
end

; collect up the votes. Don't worry about invading agent
; privacy, just measure for them
to-report election2 [choices]
  let fave [min-one-of choices [distance myself]] of turtles
  report fave
end

; Ask all non winners to adjust their offering
to adjustOfferings [winner]
  show (word "in adjustOfferings, winner was: " winner)
  let losers candidates with [self != winner]
  show (word "adjustOfferings losers are: " losers)
  ask losers
  [
    setxy random-xcor random-ycor
  ]
end

; ask each voter for its favorite, 
; use modes to calculate the winner
to electionCycle
  let theVotes election2 candidates
  show (word "electionCycle: " theVotes)
  let winner modes theVotes
  show (word "electionCycle Winner: " winner)
  adjustOfferings winner
end

to go
  tick
  if ticks > 1000
  [
    stop
  ]
  electionCycle
end

The setup works fine, I see agents and candidates. When I hit the "Step button", I can see the election works, a winner is selected, but all candidates adjust (not just losers). Here's the console:

observer: "electionCycle: [(candidate 100) (candidate 101) 
(candidate 101) (candidate 101) (candidate 100) (candidate 101)
[snip] 
(candidate 100) (candidate 101)]"
observer: "electionCycle Winner: [(candidate 100)]"
observer: "in adjustOfferings, winner was: [(candidate 100)]"
observer: "adjustOfferings losers are: (agentset, 2 turtles)"

Appears to me this is the source of error:

let losers candidates with [self != winner]

The winner is still in losers after that.

I realize now you can't actually run this without the rest of the NetLogo file. What is your custom here for providing the fully runable example? Can I point you at a file on a website? http://pj.freefaculty.org/scraps/majority.nlogo. I could put this in GitHub or someplace else if you prefer.

I appreciate your advice.

1
In the majority of cases, more seasoned NetLogo programmers can see what the problem is without running your code. We don't really have a procedure for sharing a fully runnable models, so honestly I'd hold off posting it unless someone specifically asks for it - they will probably suggest a place. I've seen GH, gists, or people putting them on their own web servers.Arthur Hjorth

1 Answers

3
votes

The problem is indeed with the line you suggested. Both agents report true for the condition [self != winner], but why is it the case. Look at how you create and pass the winner variable to the function. You create it by using the modes primitive, which is a good idea. But modes will report you a list with one element. The problem is that you pass this list directly to your adjustofferings function. This means, the agents than compare [self != [(candidatexy)]]instead of [self != (candidatexy)] (mind the additional brackets in the first example, indicating the list).

To solve the problem, you have to extract the winner agent from the list by using the first primitive, which reports you the first item of a list. You only have to add this to the line, where you define the winnervariable: let winner first modes theVotes