How do i connect a 5 patches to 5 other patches without having double connections?

31 Views Asked by At

i'm working on a netlogo world with a certain number of supermarkets and distribution centers. Each supermarket has a single distribution centre that delivers goods once a week. But i don't know how to connect a single supermarket to a single distribution center.

with one of of min one of it always has double connections. This is my current code

  globals [
  households  ; random kleur huizen, achtergrond grijs
  supermarkets  ;; blauw
  distribution-centers  ; roze autos, achtergrond rood
]

patches-own[ ]

turtles-own [visited target block ]

to setup
  clear-all
  setup-households
  setup-supermarkets
  setup-distribution-centers
  reset-ticks
end

to setup-households
  ask n-of N patches with [pcolor = black] [
      set pcolor grey
    sprout 1 [
      set shape "house"
      set color orange
    ]
    ]
  set households patches with [pcolor = grey]
end

to setup-supermarkets

  ;; Blokje [groen] van 5x5 maken waar supermarkets inkunnen
  ask patches with[
    pxcor >= -2.5 and pxcor <= 2.5 and
    pycor >= -2.5 and pycor <= 2.5
  ] [
    set pcolor green
  ]
 
  
    
  ask n-of M patches with [pcolor = green] [
      set pcolor blue
  ]
  
 
  set supermarkets patches with [pcolor = blue]
end


to setup-distribution-centers
  ;; als blokje zwart of groen is creer dan m aantal distrubution-centers
   ask n-of M patches with [pcolor = black] [
      set pcolor red
    
      sprout 1 [
        set visited false
        set color pink
        set shape "car"
        set block patch-here
      set target min-one-of supermarkets [distance myself]
       
      ]
    ]
  set distribution-centers patches with [pcolor = red]
end

to go
  ask turtles with [color = pink] [
    if visited [
      move-to-target
      check-distribution-center

    ]

    if not visited [
      move-to-target
      check-supermarkets
    ]
  ]

  tick
  stop
end

to move-to-target
 if (color != green) [
    face target
    fd 1
  ]
end

to check-distribution-center
  if patch-here = block [
    set visited true
    set color green
  ]
end

to check-supermarkets
  if member? patch-here supermarkets [
    set visited true
    set target block
  ]
end  
1

There are 1 best solutions below

0
lmf0845 On

Since min-one-of supermarkets [distance myself] returns the closest supermarket, chances are high that this is the same for two turtles. One way would be to track which supermarket is already a target of a turtle. Here is how you could do this (see lines 7, 63 & 64). Please be careful, this requires that there are always less or equal supermarkets compared to distribution-centers and that you only connect them once during one run.

globals [
  households  ; random kleur huizen, achtergrond grijs
  supermarkets  ;; blauw
  distribution-centers  ; roze autos, achtergrond rood
]

patches-own[connected] ; add to track connected supermarkets

turtles-own [visited target block ]

to setup
  clear-all
  setup-households
  setup-supermarkets
  setup-distribution-centers
  reset-ticks
end

to setup-households
  ask n-of N patches with [pcolor = black] [
      set pcolor grey
    sprout 1 [
      set shape "house"
      set color orange
    ]
    ]
  set households patches with [pcolor = grey]
end

to setup-supermarkets

  ;; Blokje [groen] van 5x5 maken waar supermarkets inkunnen
  ask patches with[
    pxcor >= -2.5 and pxcor <= 2.5 and
    pycor >= -2.5 and pycor <= 2.5
  ] [
    set pcolor green
  ]
 
  
    
  ask n-of M patches with [pcolor = green] [
      set pcolor blue
    set connected false
  ]
  
  
 
  set supermarkets patches with [pcolor = blue]
end


to setup-distribution-centers
  ;; als blokje zwart of groen is creer dan m aantal distrubution-centers
   ask n-of M patches with [pcolor = black] [
      set pcolor red
    
      sprout 1 [
        set visited false
        set color pink
        set shape "car"
        set block patch-here
      set target min-one-of supermarkets with [not connected] [distance myself] ;; only target non-connected supermarkets
      ask target [set connected true] ;; set false to make sure it is not targeted again
       
      ]
    ]
  set distribution-centers patches with [pcolor = red]
end

to go
  ask turtles with [color = pink] [
    if visited [
      move-to-target
      check-distribution-center

    ]

    if not visited [
      move-to-target
      check-supermarkets
    ]
  ]

  tick
  stop
end

to move-to-target
 if (color != green) [
    face target
    fd 1
  ]
end

to check-distribution-center
  if patch-here = block [
    set visited true
    set color green
  ]
end

to check-supermarkets
  if member? patch-here supermarkets [
    set visited true
    set target block
  ]
end 

Btw. your code seems like it could benefit greatly from using breeds (for supermarkets, distribution-centers and housholds)!