Problem with 2-condition random number assign and using length and distance in a procedure

24 Views Asked by At

I have 2 questions regarding my code:

1- First, let Ai value be assigned between -1 and 1, then let Bi value be assigned between -1 and 1, where the difference between Ai and Bi values should be between 0 and 1.

set Ai random-float 2 - 1 ; -1 ile 1 arasında rastgele bir A değeri atar
set Bi random-float 2 - 1 ; -1 ile 1 arasında rastgele bir B değeri atar

2- The procedure "to create-temporary-links" is not working as I desired. First, I want to remove the links from my agent's links that have a length greater than 5 using "length". Then, I want to create links with n agents whose distance from my agent is less than 5 using "distance".


    to create-temporary-links
      ask turtles [
        ; link cutting
        ask my-links [
          if length [myself] of both-ends > 5 [
            die
          ]
        ]
    
        ; rewiring
        let close-turtles other turtles with [distance myself <= 5]
        if count close-turtles > 0 [
          let selected-turtle n-of close-turtles
          create-link-with selected-turtle [
            set color blue
            set weight random-float 0.5
            set permanency false
    
          ]
          set remaining-outer-link-number remaining-outer-link-number - 1
          ask selected-turtle [
            set remaining-outer-link-number remaining-outer-link-number - 1
          ]
        ]
      ]
    end

I gave the whole code below just in case.


    globals [
      nd
      maximum-loop-count
    
      total-turtle-count
      y
      negative-Ai-count
      positive-Ai-count
      negative-Bi-count  
      positive-Bi-count
    ]
    
    lınks-own [
      weight
      permanency
    ]
    
    
    turtles-own [
      fixed-idea ; Zemin (ilk iç fikir)
      Ai ; (iç fikir)
      Bi ; (dış fikir)
    
    
    
      k1 ; sabit fikirlilik katsayısı
      k2 ; sahtelik katsayısı
      k3 ; cesaret katsayısı
    
      remaining-outer-link-number ; daha önce kurmadığı kalan yapabileceği bağ sayısı <= 4
    ]
    
    to setup
      clear-all
      set nd 1 ; Her ajanın en az 1 komşusu olsun
      set maximum-loop-count 20
    
      set total-turtle-count 3
      set negative-Ai-count 0  ; Başlangıçta 0'dan küçük Ai değerine sahip ajan sayısı sıfır olarak ayarlanır
      set positive-Ai-count 0 
        set negative-Bi-count 0 
        set positive-Bi-count 0 
      create-turtles total-turtle-count [
        set shape "person"
        set sıze 1.5
    
        set Ai random-float 2 - 1 ; -1 ile 1 arasında rastgele bir A değeri atar
        set Bi random-float 2 - 1 ; -1 ile 1 arasında rastgele bir B değeri atar
    
    
    
        set y 0.1
        
        set remaining-outer-link-number 2 ;bağlantı kurma kapasitesi
    ;   set remaining-outer-link-number random 15 + 1 ; 1 ile 15 arasında rastgele bir değer atanır
    
    
        set k1  0
        set k2  1
        set k3  0
        set fixed-idea Ai
      ]
    
    
    
      create-permanent-links
    
      update-turtle-colors
    
    
      create-temporary-links
    
      updatePosition
      ask patches [
      let half-width (world-width / 2)
      let half-height (world-height / 2)
    
      ifelse abs(pxcor) <= half-width and abs(pycor) <= half-height [
        ifelse pxcor >= 0 [
          ifelse pycor >= 0 [
            ; Birinci bölge
            set pcolor gray
          ] [
            ; Dördüncü bölge
            set pcolor gray + 4
          ]
        ] [
          ifelse pycor >= 0 [
            ; İkinci bölge
            set pcolor gray + 2
          ] [
            ; Üçüncü bölge
            set pcolor gray + 5
          ]
        ]
      ] [
        set pcolor black ; Eğer dört bölge dışındaysa, siyah renk ata (bu kısmı isteğinize göre değiştirebilirsiniz)
      ]
    ]
    
    
    
      reset-ticks
    end
    
    to create-permanent-links
      ask turtles [
        while [count my-links < nd] [
          let potential-partners other turtles with [not link-neighbor? myself]
          if any? potential-partners [
            let partner one-of potential-partners
            create-link-with partner [
              set color red
              set weight random-float 0.5 + 0.5 ; Sets the weight to a random value between 0.5 and 1
              set permanency true
            ]
          ]
    
        ]
      ]
    end
    
    
    
    
    to go
      if ticks >= maximum-loop-count [ stop ]
    
      update-Ai
      update-turtle-colors
    
      update-Bi
    
      
      create-temporary-links
    
    
      updatePosition
      
      count-negative-Ai-agents 
      count-positive-Ai-agents
      count-negative-Bi-agents
      count-positive-Bi-agents
    
    
      ; Linklerin ağırlıklarını ve bağlı olduğu ajanları yazdır
      ask links [
        let ajan1 end1
        let ajan2 end2
    ;    print (word "Link between turtles " [who] of ajan1 " and " [who] of ajan2 " - Weight: " weight)
      ]
    
    
      tick
    end
    
    
    
    ;TEST 
    to count-negative-Ai-agents
      set negative-Ai-count count turtles with [Ai < 0]  ; 0'dan küçük Ai değerine sahip ajanların sayısını hesapla
      print (word "0'dan küçük Ai değerine sahip ajan sayısı: " negative-Ai-count)
    end
    
    to count-positive-Ai-agents
      set positive-Ai-count count turtles with [Ai > 0]  ; 0'dan küçük Ai değerine sahip ajanların sayısını hesapla
      print (word "0'dan büyük Ai değerine sahip ajan sayısı: " positive-Ai-count)
    end
    
    to count-negative-Bi-agents
      set negative-Bi-count count turtles with [Bi < 0]  ; 0'dan küçük Ai değerine sahip ajanların sayısını hesapla
      print (word "0'dan küçük Bi değerine sahip ajan sayısı: " negative-Bi-count)
    end
    
    to count-positive-Bi-agents
      set positive-Bi-count count turtles with [Bi > 0]  ; 0'dan küçük Ai değerine sahip ajanların sayısını hesapla
      print (word "0'dan büyük Bi değerine sahip ajan sayısı: " positive-Bi-count)
    end
    
    
    
    
    
    to update-turtle-colors
      let max-fikir max (list max [Ai] of turtles max [Bi] of turtles)
      let min-fikir min (list min [Ai] of turtles min [Bi] of turtles)
    
      ask turtles [
        let fikir-color map-color Ai Bi min-fikir max-fikir
        set color fikir-color
    
        if (Ai > 0 and Bi > 0) OR
        (Ai < 0 and Bi < 0) [
          set sıze 2.5
        ]
        if (Ai > 0 and Bi < 0) OR
        (Ai < 0 and Bi > 0)  [
          set sıze 1.5
        ]
      ]
    end
    
    to-report map-color [value1 value2 min-value max-value]
      let red-level 255 - round((value2 - min-value) / (max-value - min-value) * 255)
      let green-level round((value2 - min-value) / (max-value - min-value) * 255)
    
      ifelse value2 > 0 [
        report (list 0 green-level 0)  ; Pozitif değerler için Yeşil
      ] [
        report (list 255 red-level 0)  ; Negatif değerler için Sarı
      ]
    end
    
    
    
    
    
    
    to updatePosition
      let axis-x (world-width - 5) ; substractşng 5 because getting a better visualation
      let axis-y (world-height - 5) ; substractşng 5 because getting a better visualation
      ask turtles [
    
    
        ;if the corresponding coordinate of Ai or Bi of the agent is bigger than world border we limite it to world border.
    
        let clamped-Ai Ai
        let clamped-Bi Bi
    
        if clamped-Ai > 5 [
        set clamped-Ai 5
        ]
        if clamped-Ai < -5 [
        set clamped-Ai -5
        ]
    
        if clamped-Bi > 5 [
        set clamped-Bi 5
        ]
        if clamped-Bi < -5 [
        set clamped-Bi -5
        ]
    
    
        ;ölçekleme
        let scaled-x (axis-x * clamped-Ai / 10)
        let scaled-y (axis-y * clamped-Bi / 10)
    
        ; Ajanın pozisyonunu güncelleme
        setxy scaled-x scaled-y
    
    
        ; Check if the agent is outside the world boundaries and reposition if necessary BUNU SİLEBİLİRSİN
        ifelse pxcor > max-pxcor [ setxy max-pxcor pycor ]
        [ ifelse pxcor < min-pxcor [ setxy min-pxcor pycor ] [ ] ]
    
        ifelse pycor > max-pycor [ setxy pxcor max-pycor ]
        [ ifelse pycor < min-pycor [ setxy pxcor min-pycor ] [ ] ]
      ]
    end
    
    ;yakınımda olan ajanlarla geçici bağ rewiring süreci
    to create-temporary-links
      ask turtles [
        ; Linklerden uzunluğu 5'ten büyük olanları kaldır
        ask my-links [
          if length [myself] of both-ends > 5 [
            die
          ]
        ]
    
        ; 5 birimlik uzaklığa sahip diğer ajanlarla link oluştur
        let close-turtles other turtles with [distance myself <= 5]
        if count close-turtles > 0 [
          let selected-turtle n-of close-turtles
          create-link-with selected-turtle [
            set color blue
            set weight random-float 0.5
            set permanency false
    
          ]
          set remaining-outer-link-number remaining-outer-link-number - 1
          ask selected-turtle [
            set remaining-outer-link-number remaining-outer-link-number - 1
          ]
        ]
      ]
    end

0

There are 0 best solutions below