pablo
pablo

Reputation: 385

How to choose the agents that have different value in the set

I would like to change the value of the variable my-inventory-slot of a set of agents but only in the case that value is not repeated (i.e. his value is unique in the set).

breed [ tags tag ]
tags-own [ my-inventory-slot inventoried? ]
breed [ readers reader ]

and

and I have tested

ask my-tags with [ my-inventory-slot != [my-inventory-slot] of self ] [
      set inventoried? true
      set color red
  ] 

where my-tagsis a reader variable containing the tags around the reader.

The problem is in the selection with [ my-inventory-slot != [my-inventory-slot] of self ] because I have try my-inventory-slot = 5 and the code works fine.

Upvotes: 0

Views: 103

Answers (2)

pablo
pablo

Reputation: 385

I have solved the question asking any other my-tags, like this:

let kk my-tags
  ask my-tags [ 
      if not any? other kk with [ my-inventory-slot = [my-inventory-slot] of myself ] [
      set inventoried? true
      set color red 
  ] ]

which is short and clear.

Upvotes: 2

JenB
JenB

Reputation: 17678

UPDATED

let thistag  [ my-inventory-slot ] of one-of my-tags 
ask tags with [ my-inventory-slot != thistag ] [
      set inventoried? true
      set color red
  ] 

UPDATED AGAIN - after downloading code

This is the code that calls the problem procedure:

to go
  ask reader 0 [
    setup-inventory-in-frame
    one-frame-inventory
  ]
end

So a reader is being asked to run the code. However, readers do not have a variable my-inventory-slot, this is a variable belonging to the tags breed. This is your problem, you need to work out the connection between the reader running the code and the my-inventory-slot variable that you want the match to.

From the chat discussion, what you actually want is the tags belonging to the reader (that is, the variable my-tags) that have unique values of my-inventory-slot. I think this code will do that:

to one-frame-inventory
  let frame-time 0
  let unique-tags my-tags
  let forward-list [ my-inventory-slot ] of my-tags
  let reverse-list reverse forward-list
  let num-tags count my-tags
  ask my-tags
  [ if position my-inventory-slot forward-list + position my-inventory-slot reverse-list != num-tags - 1
    [ set unique-tags other unique-tags ]
  ]
  ask unique-tags
  [ set color red
    set inventoried? true
  ]
end

It is very ugly, so someone else's answer would be good. What this does is strips out the my-inventory-values for the relevant tags into a list. It creates two copies of that list, one in normal order and the other reversed. It identifies non-unique values by finding those with a different position for the first appearance in those two lists. If it's not unique, the relevant tag is removed from the unique-tags agentset.

Upvotes: 2

Related Questions