dya
dya

Reputation: 195

netlogo: nested ifelse cannot access the last ifelse command

I have three level of ifelse command. I attempt to make an agent to do something by comparing 2 variable of different agent. A brief description about the ifelse command.

Procedure 1 & 2 attempt to compare variable of two different agent on the same patch and do something; else

Procedure 3 attempt to compare variable of two agent on different patch in radius 2, then do something ; else.

Problem : When I run procedure 1 and 3 (disable procedure 2) OR procedure 2 and 3 (disable procedure 1), code is fine. But when I tried to run the whole procedure, it can't access [do something] command on procedure 3.

My code looks like this;

to cocreate-value1
  ask capabilities-here
    [ let this-resource one-of resource
     ask one-of prevalues-here  
      [ ifelse value = this-resource
             [ use-resource ]
             [ cocreate-value2]
      ] ]
end

to cocreate-value2
  ask capabilities-here
  [ let this-knowledge one-of knowledge 
    ask one-of prevalues-here   
    [ ifelse value = this-knowledge
      [use-knowledge ]
      [cocreate-value3]
    ] ]
end

to cocreate-value3
   ask one-of other capabilities in-radius 2 
       [let resource2 sentence (resource) (knowledge)
        let this-resource2 one-of resource2
        let new-cap capabilities with [one-of resource = this-resource2] 
        ask prevalues-here 
            [ ifelse value = this-resource2
                [ask capabilities-here
                  [if any? other new-cap in-radius 2 
                     [ create-link-to one-of other new-cap in-radius 2
                       set color white]] ;; this code is not executed
                       use-network ] ;; this one too
                      [set color yellow ]
 ]]
end

Can anyone find the problem? Thank you for your help

Upvotes: 1

Views: 237

Answers (1)

Alan
Alan

Reputation: 9620

Does this expose the problem?

to cocreate-value1
  ask capabilities-here [
    let _resource one-of resource
    let _pv one-of prevalues-here
    if (_pv != nobody) [
      ask _pv [
        ifelse (value = _resource) [
          use-resource
        ] [
          cocreate-value2
        ]
      ]
    ]
  ]
end

to cocreate-value2
  print "enter cv3"
  ask capabilities-here [
    let _knowledge one-of knowledge 
    let _pv one-of prevalues-here
    if (_pv != nobody) [
      ask _pv  [
        ifelse (value = _knowledge) [use-knowledge ] [cocreate-value3]
      ]
    ]
  ]
  print "exit cv3"
end

to cocreate-value3
  print "enter cv3"
  let _oc one-of other capabilities in-radius 2
  ifelse (_oc = nobody) [
    print "no other capabilities"
  ] [
    ask _oc [
      let _resource2 one-of (sentence resource knowledge)
      let _resource one-of resource
      let _new-cap capabilities with [_resource = _resource2] 
      if (not any? prevalues-here) [error "no prevalues here!"]
      ask prevalues-here [
        ifelse (value = _resource2) [
          ask capabilities-here [
            let _other one-of other _new-cap in-radius 2
            if (_other != nobody) [
              print "found a link partner!"
              create-link-to _other
              set color white
            ]
          ] ;; this code is not executed
          use-network
        ] [;; this one too
          set color yellow
        ]
      ]
    ]
  ]
  print "leave cv3"
end

Upvotes: 0

Related Questions