Using same attribute in more than one trajectory

394 views
Skip to first unread message

Juan Ossa-Moreno

unread,
Oct 8, 2016, 3:55:08 AM10/8/16
to simmer-devel
Hello
I have several trajectories in my model and wanted to use in all of them an attribute that is defined in just one of them. I tried to create the same attribute with the same name at all trajectories, but simmer takes them as different attributes with the same name. To solve this, using the package data.table i defined the following line: 
tail(as.data.table(get_mon_attributes(M1))[key=="BlockBin"]$value,1)

The name of the attribute is BlockBin, and it is defined and modified in several trajectories. When I want to get its value, I go to the attribute table and take the last value. I guess this is not the most efficient solution, but have not been able to make it in another way. However, am trying to use that attribute in a Branch, and for some reason I cannot understand, I get an error when I use the following line in more than two trajectories. 

ifelse(tail(as.data.table(get_mon_attributes(M1))[key=="BlockBin"]$value,1)==1,1,2)

Error in eval(expr, envir, enclos) : expecting a single value

which makes me think that in one of the branches the ifelse(.....) shown previously is either giving multiple or none values. Anyone has an idea?

Regards

Juan
Message has been deleted

Iñaki Úcar

unread,
Oct 8, 2016, 5:37:04 AM10/8/16
to simmer-devel

Keep in mind that the attributes facility is about storing values per arrival: each arrival has its own set of attributes. So a common name in set_attributes() corresponds not only to different attributes across trajectories, but also across arrivals in the same trajectory. Let me illustrate this with a simple example:

library(simmer)

set.seed(1234)

t <- create_trajectory() %>%
  set_attribute("example", function() runif(1)) %>%
  timeout(1) %>%
  timeout(function(attr) { print(attr[["example"]]); 0 })

simmer() %>%
  add_generator("arrival", t, at(0, 0)) %>%
  run

#> [1] 0.1137034
#> [1] 0.6222994

I would like to remark this, because my feeling, from your description, is that you are trying to share an attribute for the whole simulation, with all the trajectories and all the arrivals. And this is not an attribute in the sense we define them in simmer. What you really need, if I understood well, is simply a global variable!

library(simmer)

set.seed(1234)

my_global <- NA

t0 <- create_trajectory() %>%
  timeout(function() { print(my_global); 0 })

t1 <- create_trajectory() %>%
  timeout(function() { 
    my_global <<- runif(1) # don't forget the double-assigment operator!
    1 }) %>%
  join(t0)

t2 <- create_trajectory() %>%
  timeout(1) %>%
  join(t0)

simmer() %>%
  add_generator("t1_", t1, at(0)) %>%
  add_generator("t2_", t2, at(0)) %>%
  run

#> [1] 0.1137034
#> [1] 0.1137034

For globals, plain and simple R does the trick. The attributes facility was conceived to manage the hard work of keeping different values on a per-arrival basis. But you don’t need this at all in this case. And with this approach, you don’t need to call get_mon_attributes() either.

Regards,
Iñaki


--
You received this message because you are subscribed to the Google Groups "simmer-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel+unsubscribe@googlegroups.com.
To post to this group, send email to simmer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/simmer-devel/1810eb70-e5eb-4a69-bd42-d44b4ddd5b54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Juan Ossa-Moreno

unread,
Oct 12, 2016, 5:55:28 PM10/12/16
to simmer-devel
Hi Inaki

Thanks a lot for your help. This is what I needed. I did not know you could use global variables inside simmer. However, the limitation of this is that,as far as am aware, if I use a global variables I will not be able to check its values throughout the period of simulations, at least not as easy as an attribute, which is something I need for some of those global variables. 

Regards

Juan
To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel...@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/simmer-devel/1810eb70-e5eb-4a69-bd42-d44b4ddd5b54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Iñaki Úcar

unread,
Oct 12, 2016, 7:37:29 PM10/12/16
to simmer-devel
2016-10-12 23:55 GMT+02:00 Juan Ossa-Moreno <js.os...@gmail.com>:
Hi Inaki

Thanks a lot for your help. This is what I needed. I did not know you could use global variables inside simmer. However, the limitation of this is that,as far as am aware, if I use a global variables I will not be able to check its values throughout the period of simulations, at least not as easy as an attribute, which is something I need for some of those global variables. 

Why not? I'm using one in my last example, writing and reading it, as easy as an attribute.

Juan Ossa-Moreno

unread,
Nov 2, 2016, 8:25:03 PM11/2/16
to simmer-devel
HI Iñaki

I meant that I will not be able to track the value of the variable throughout the simulation, as I would with an attribute. I meant that I would know the final value of the variable but I think I do not have the value of the variable at all times (e.g. t=1, t=2, t=3..........t=t) as I have with the attributes in the table get_mon_attributes. If am wrong please let me know so I can track those values.

REgards

Juan

Bart Smeets

unread,
Nov 3, 2016, 3:38:01 AM11/3/16
to simmer-devel
Hi Juan,

What about creating a custom monitor? Take for example the following code:

custom_monitor<-function(){
  results <- data.frame()
  
  function(time, value){
    if(missing(value)){
      return(results)
    }
    results<<-rbind(results, 
                    data.frame(time=time, value=value))
  }
}

You can then create your own (global) monitor:

my_mon<-custom_monitor()
my_mon
(5, "firstval")
my_mon
(18, "secondval")

Calling my_mon() will return a dataframe with the values:

> my_mon()
  time     value
1    5  firstval
2   18 secondval

This way you could pass the current time, now(env), as the first argument to my_mon() in order to follow the evolution of the variable through time.

Regards,
Bart

Op donderdag 3 november 2016 01:25:03 UTC+1 schreef Juan Ossa-Moreno:

Iñaki Úcar

unread,
Nov 3, 2016, 5:50:42 AM11/3/16
to simmer-devel
Hi Juan,

Ok, I understand now. Then it could interesting to add an option to `set_attributes()` to define and monitor global attributes as well.

In the meanwhile, you can implement a custom monitor as Bart suggested.

Regards,
Iñaki

--
You received this message because you are subscribed to the Google Groups "simmer-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel+unsubscribe@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Diego Silva

unread,
Nov 5, 2016, 4:14:47 AM11/5/16
to simmer-devel
Hi Inaki and Bart

Thanks a lot for your suggestion Bart. I did not understand how to call my_mon inside a simmer simulation. The variable I want to track is BlockBin, currently I have something like this
timeout(function() {
             BlockBin <<- 1
           }) %>%

I defined the following outside the trajectory, before creating the simulation.
custom_monitor<-function(){
  results <- data.frame()
  
  function(time, value){
    if(missing(value)){
      return(results)
    }
    results<<-rbind(results, 
                    data.frame(time=time, value=value))
  }
}
BlockBin_rec<-custom_monitor()

Inside the trajectory I tried something like

timeout(BlockBin_rec(now(Project),BlockBin))

But it is not working. Do you know how could i do this?

Regards

Juan

El jueves, 3 de noviembre de 2016, 19:50:42 (UTC+10), Iñaki Úcar escribió:
Hi Juan,

Ok, I understand now. Then it could interesting to add an option to `set_attributes()` to define and monitor global attributes as well.

In the meanwhile, you can implement a custom monitor as Bart suggested.

Regards,
Iñaki
2016-11-03 1:25 GMT+01:00 Juan Ossa-Moreno <js.os...@gmail.com>:
HI Iñaki

I meant that I will not be able to track the value of the variable throughout the simulation, as I would with an attribute. I meant that I would know the final value of the variable but I think I do not have the value of the variable at all times (e.g. t=1, t=2, t=3..........t=t) as I have with the attributes in the table get_mon_attributes. If am wrong please let me know so I can track those values.

REgards

Juan

El jueves, 13 de octubre de 2016, 9:37:29 (UTC+10), Iñaki Úcar escribió:
2016-10-12 23:55 GMT+02:00 Juan Ossa-Moreno <js.os...@gmail.com>:
Hi Inaki

Thanks a lot for your help. This is what I needed. I did not know you could use global variables inside simmer. However, the limitation of this is that,as far as am aware, if I use a global variables I will not be able to check its values throughout the period of simulations, at least not as easy as an attribute, which is something I need for some of those global variables. 

Why not? I'm using one in my last example, writing and reading it, as easy as an attribute.

--

--
You received this message because you are subscribed to the Google Groups "simmer-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel...@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/simmer-devel/8c0b5bbc-42c4-4a09-abb3-d5fb96106ed3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Iñaki Úcar

unread,
Nov 5, 2016, 6:58:09 AM11/5/16
to simmer-devel

Hi Diego,

You should call your custom monitor each time you change the value, and always inside a function (otherwise, it’s not dynamically evaluated). For instance:

timeout(function() {
  BlockBin <<- 1
  BlockBin_rec(now(Project), BlockBin)
  return(0)
}) %>%

Iñaki


To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel+unsubscribe@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Diego Silva

unread,
Nov 6, 2016, 1:49:43 AM11/6/16
to simmer-devel
Hi Inaki and Bart

Thanks a lot. The function is now working and I can retrieve the values throughout the simulation. However, I tried to turn the result into a matrix, data.frame or data.table to be able to manipulate the values and could not do it.

Do you have any suggestion in how to turn the result into a data frame or similar? I tried modifying the custom_monitor function but all my changes threw an error when running.

Regards

Diego

Bart Smeets

unread,
Nov 6, 2016, 4:42:09 AM11/6/16
to simmer-devel
Simply calling BlockBin_rec() should return a data.frame if you used the provided example monitor. 

If you provide a minimal example of your current set up it will be easier to troubleshoot.

Op zo 6 nov. 2016 om 06:49 schreef Diego Silva <cont...@sotrasil.cl>:

Iñaki Úcar

unread,
Nov 6, 2016, 5:45:29 AM11/6/16
to simmer-devel

If you call your monitor without any argument, BlockBin_rec(), it returns a data frame. :)


To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel+unsubscribe@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Iñaki Úcar

unread,
Nov 6, 2016, 5:46:19 AM11/6/16
to simmer-devel
Oh, sorry, Bart, I didn't see your response. :D

To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel+unsubscribe@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/simmer-devel/ebeee06f-717d-40c7-8e5f-27d3c633f79b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simmer-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel+unsubscribe@googlegroups.com.

To post to this group, send email to simmer...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

D.G.

unread,
Jun 26, 2017, 3:37:29 PM6/26/17
to simmer-devel
Update from 2017/06 -
Thanks to simmer developers! - New simmer version‘3.6.1’ allows now to have attributes as global variables:

 set_attribute("BWT_global", function() {   get_queue_count(sim)   } , global = TRUE)
Reply all
Reply to author
Forward
0 new messages