creating different farms

49 views
Skip to first unread message

gizem eren

unread,
Jun 14, 2022, 8:48:46 AM6/14/22
to netlogo-users

Hello everyone,

I'm new at NetLogo; I have a few questions. I want to make farms with different land sizes, and I want to differentiate each of them with their colors, but I can't draw the borders of the farm; my yellow farms that need to be next to each other are at other points, I tried to use radius but I couldn't, can you help with the code?

Thank you very much.

Gizem



Wannes...@hotmail.be

unread,
Jun 15, 2022, 9:22:16 AM6/15/22
to netlogo-users
Hello Gizem,

Could you provide us with some more information about your exact goal? Should every patch be part of a farm? Does the shape of the farms matter?
Could you also show us the code you have already tried? It is much easier to advise when we have something to start from.

Best,
Wannes

Op dinsdag 14 juni 2022 om 14:48:46 UTC+2 schreef gizemk...@gmail.com:

Wannes...@hotmail.be

unread,
Jun 20, 2022, 5:52:32 AM6/20/22
to netlogo-users
Thank you for sending me the code. I played around with it a bit and found something that I think should be good enough. I'll add the major steps I went through to show you my thought process.

_____________________________
Original

setup-owner-0

    ask n-of 2 patches [set owner 1]

    ask n-of 6 patches [set owner 2]

    ask n-of 20 patches [set owner 3]

    ask n-of 50 patches [set owner 5]

    ask n-of 100 patches [set owner 6]

end

The original version had 2 major problems. One is that you always ask n random patches to change their owner without checking if they have already changed their owner before. This means that a patch can change owner multiple times which in turn means that the number of patches each farm has will generally be lower than the number you inputted (except for owner 6 since that is the last one).
The second problem is that there is nothing that ensures spatial clustering of the patches.

_______________________________

First version

to setup-owner-1

  ask patches [set owner 0]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 6]
  repeat 99 [
    ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 6]] [set owner 6]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 5]
  repeat 49 [
    ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 5]] [set owner 5]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 3]
  repeat 19 [
    ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 3]] [set owner 3]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 2]
  repeat 5 [
    ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 2]] [set owner 2]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 1]
  repeat 1 [
    ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 1]] [set owner 1]
  ]  
 
end

In this version I tackled the two original problems:
1. I only let them change the owner of patches that haven't been changed before, by using `with [owner = 0]`.
2. I ensure clustering by first choosing a single patch with as many unclaimed patches around it as possible. I change the owner of that patch and then go through a loop that looks for unclaimed patches that are neighboring a claimed patch and changing them. This ensures that patches with the same owner always are connected to one-another.

One thing to notice is the difference between `neighbors` and `neighbors4`.  Neighbors checks all 8 surrounding patches and is used here to ensure that I start my farm in a spot that is as empty as possible. Neighbors4 on the other hand checks only 4 patches, the ones right above, below, left, and right. I use that one to determine which patches can be attached to the farm since diagonally attached patches allows the farms to cross through one-another.

With this version, you regularly had unclaimed patches in the middle of claimed territory, which felt a bit off to me.


_______________________________
Second version

to setup-owner-2

 
  ask patches [set owner 0]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 6]
  repeat 99 [
    ifelse any? patches with [owner = 0 and count neighbors with [owner = 6] > 4] [
      ask one-of patches with [owner = 0 and count neighbors with [owner = 6] > 4] [set owner 6]
    ][
      ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 6]] [set owner 6]
    ]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 5]
  repeat 49 [
    ifelse any? patches with [owner = 0 and count neighbors with [owner = 5] > 4] [
      ask one-of patches with [owner = 0 and count neighbors with [owner = 5] > 4] [set owner 5]
    ][
      ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 5]] [set owner 5]
    ]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 3]
  repeat 19 [
    ifelse any? patches with [owner = 0 and count neighbors with [owner = 3] > 4] [
      ask one-of patches with [owner = 0 and count neighbors with [owner = 3] > 4] [set owner 3]
    ][
      ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 3]] [set owner 3]
    ]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 2]
  repeat 5 [
    ifelse any? patches with [owner = 0 and count neighbors with [owner = 2] > 4] [
      ask one-of patches with [owner = 0 and count neighbors with [owner = 2] > 4] [set owner 2]
    ][
      ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 2]] [set owner 2]
    ]
  ]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner 1]
  repeat 1 [
    ifelse any? patches with [owner = 0 and count neighbors with [owner = 1] > 4] [
      ask one-of patches with [owner = 0 and count neighbors with [owner = 1] > 4] [set owner 1]
    ][
      ask one-of patches with [owner = 0 and any? neighbors4 with [owner = 1]] [set owner 1]
    ]
  ]
   
end


This version tackles the problem of the unclaimed islands by first checking if there are any unclaimed patches with 5, 6, 7 or 8 neighbors that are claimed (  patches with [owner = 0 and count neighbors with [owner = 6] > 4] )  . If that is the case, they are first added to the farm instead of attaching a random neighboring patch. This ensures that there are fewer empty patches in the middle of a farm. There can still be some but not as bad a previously. You can play around with the number of neighbors that leads to this preferential attachment of the patch to the farm. I found that between >5 and >2 all gives good effects, with the main difference being how rough the edges of your farms are.

My last two concerns are that the code looks quite bulky and repetitive, and that sometimes the initial patch for a farm is chosen in such a way that it cannot expand far enough, which causes an error.

_______________________________

Final version

to setup-owner
 
  let succeeded false
  let iteration 0
 
  while [succeeded = false] [
    set iteration iteration + 1
    if iteration > 10 [ error "No valid farm setup was found after 10 iterations."]
    
  carefully [
      ask patches [set owner 0]
      setup-individual-farm 6 100
      setup-individual-farm 5 50
      setup-individual-farm 3 20
      setup-individual-farm 2 6
      setup-individual-farm 1 2
     
      set succeeded true
    ] []
  ]
 
  ;show iteration
 
end

to setup-individual-farm [ farm-owner farm-size]
 
  ask max-one-of patches with [owner = 0] [count neighbors with [owner = 0]] [ set owner farm-owner]
  repeat farm-size - 1 [
    ifelse any? patches with [owner = 0 and count neighbors with [owner = farm-owner] > 4] [
      ask one-of patches with [owner = 0 and count neighbors with [owner = farm-owner] > 4] [set owner farm-owner]
    ][
      ask one-of patches with [owner = 0 and any? neighbors4 with [owner = farm-owner]] [set owner farm-owner]
    ]
  ]
 
end


To streamline everything, I made a new procedure called setup-individual-farm which takes the owner and the size of the farm as input. That makes it easier to play around with the values and the number of farms.

To prevent the errors, I added two different pieces of code.
1. `Carefully`  tries running the code within its first command block. If that causes an error, it ignores the error and tries to run its second command block instead. Since I left the second command block empty, it doesn't do anything in case the first block causes an error. 
2. I added a `while` loop that runs either 10 times or until the setup has succeeded (controlled by adding `set succeeded true` into the first carefully block after all the setups).
3. If the setup doesn't succeed after 10 times, the program gives you an error in order to ensure that you don't get stuck in an infinite loop trying to setup the world with parameters that are too restrictive.

_______________________________


I hope this helps you.

Best regards,
Wannes


Op woensdag 15 juni 2022 om 15:22:16 UTC+2 schreef lei...@natur.cuni.cz:

gizem eren

unread,
Jun 20, 2022, 1:41:35 PM6/20/22
to Wannes...@hotmail.be, netlogo-users

Hello Wannes,

firstly I Thank you for your answer, so I will try to explain my problem. I want to make five farms with different patch sizes, I create a 15*15 world, and my farm’s sizes are 2, 6, 20, 50, and 100. Total land patches are equal to 178. Finally, five farm owners are in the model. I want to see farm's own land patches adjacent. But my lands with different colors are scattered to World randomly; how do I get reunited patches to make farms? my codes are below,

best regards

Gizem

 

turtles-own [

  land       ]

 

patches-own [                        

  owner      ]

 

to setup          

  clear-all

  setup-owner

  reset-ticks

  setup-people

  update-variables

end

 

to setup-owner

    ask n-of 2 patches [set owner 1]

    ask n-of 6 patches [set owner 2]

    ask n-of 20 patches [set owner 3]

    ask n-of 50 patches [set owner 5]

    ask n-of 100 patches [set owner 6]

end

 

to update-variables

  check-display

end

 

to check-display

;; to update-Display and turtles colors

ask patches

  [if _Display = "none"  [set pcolor gray ]

 

  if _Display = "owner"

  [if owner = 0 [set pcolor white]

   if owner = 1 [set pcolor orange + 4]

   if owner = 2 [set pcolor yellow + 3]

   if owner = 3 [set pcolor gray + 2]

   if owner = 5 [set pcolor 109]

   if owner = 6 [set pcolor green + 3] ]

  ]

ask turtles

 [ ifelse _Turtle_color = "hide" [set hıdden? true] [set hıdden? false]

 ]

End

 


lei...@natur.cuni.cz <Wannes...@hotmail.be>, 15 Haz 2022 Çar, 16:22 tarihinde şunu yazdı:
--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/f3ac899c-5058-4614-80ea-40a546146d10n%40googlegroups.com.

gizem eren

unread,
Jun 21, 2022, 12:50:20 PM6/21/22
to Wannes...@hotmail.be, netlogo-users
Thanks a lot for your help, Wannes; all of your three-part codes saved my life; I understand my code's problem :) Indeed I planned to use gis data, but my supervisor wants to see different alternatives. I want to define one farm as an agro-economy, so the neighbor effect is different between smallholders and large holders.  I will share my results.

Thanks in advance for your help!

Cheers

Gizem

lei...@natur.cuni.cz <Wannes...@hotmail.be>, 20 Haz 2022 Pzt, 12:52 tarihinde şunu yazdı:
--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages