user11277648
user11277648

Reputation: 53

How to code an IF command with 2 conditions in NetLogo?

My turtles are firms and they have a turtles-own which is a profit varying from firm to firm, as well as offshored? and reshored? which is either true or false.

There is something not right about the code. I struggle with combining the IF and the AND command. IF the parameter of the label offshored? reports = true AND the profit of any of these firms is lower than the profit of the firms which report offshored? = false, than they are supposed to move. The moving section of the code works alright. Please find the (error-reporting) code I have so far below:

breed [ firms firm ]


firms-own [   
   profit
   offshored?   ;; reports either true or false
   reshored?   ;; reports either true or false
]

to setup
  ask firms [
    if offshored? true AND profit < [ profit ] of firms with [ offshored? = false ] [   ;; if the profit of an offshored firm is smaller than the lowest profit of firms at home, the decision to reshore is yes!
      ask one-of turtles [ move-to one-of patches with [ pcolor = 58 and not any? turtles-here ] ]  ;; the firm reshores
      AND set reshored? true ] ]   ;; the firm is now labelled as reshored
end

Upvotes: 1

Views: 3069

Answers (1)

Luke C
Luke C

Reputation: 10336

The setup above is not going to do anything,The main problem may be that you're comparing a firm's profit variable to a list ([profit] of firms with [ offshored? = false ]). You can't compare a single value to a list of values directly in this manner, so you'll have to go about it a different way. For example, you could use min to get the minimum profit value of those other firms of interest:

breed [ firms firm ]

firms-own [ profit offshored? reshored? ]

to setup
  ca
  ask patches with [ pxcor < -10 ] [
    set pcolor red
  ]

  create-firms 100 [
    set color white
    set profit random 101
    set offshored? one-of [ true false ]
    set reshored? false
    while [ any? other turtles-here ] [
      move-to one-of neighbors with [ pcolor = black ]
    ]
  ]

  ask firms [
    if offshored? and profit < min [ profit ] of firms with [ not offshored? ] [
      move-to one-of patches with [ pcolor = red and not any? turtles-here ]
      set reshored? true
      set color yellow
      set size 2
    ]
  ]
  reset-ticks
end 

Additionally, you have ask one-of turtles in your ask firms statement- I think you want to omit that as done in this example so that the firm doing the evaluating is the agent that moves- ask one-of turtles will just choose a random turtle of any breed.

Upvotes: 3

Related Questions