anylogic triggering an event - anylogic

In accordance, I have a question in the modeling process. I want my agent to have an event that is triggered by time and condition, example: goToSchool if it is more than 6 am and there is a school bus. I am confused about whether to use the timeout trigger (but cannot use the condition) or the condition (but cannot use the timeout) or is there any possible alternative?

In your example, "if it is more than 6 am" is a condition and not a timeout. A timeout trigger is used when you want an event to happen at an exact time. In your case, while "more than 6 am" is time related, it is still a condition. So I would use a condition triggered event with two conditions:
getHourOfDay() > 6 && <bus condition>
getHourOfDay() function returns the hour of the day in a 24-hr format.
You need to keep in mind something important related to condition triggered events, they are only evaluated "on change". I recommend you read this carefully:
https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fstatecharts%2Fcondition-event.html
My recommendation would be to use the onChange() function in the block controlling your bus arrival so that the condition is evaluated each time a bus arrives.

Related

Temporary adjustment of delay time

I have the following problem which I am unable to solve:
I have a situation where a security point (added as delay) holds every half an hour a 15 min break. After the break, the security guards increase their speed till the queue is shorter than 10pp.
I wanted to model this as follows: a state chart with delay.set_capacity(0) after 30 minutes and delay.set_capacity(1) again after the 15 min break. For the increased speed after the break, I added an additional state with condition: queue.size()>10 and now I want to set the action such that the delay function changes the delay time from exponential (1/10) to exponential (1/5) as long as queue.size()>10.
Anyone experience with which function in the action box to use? Or would you suggest a different function?
Since you are using, or at least want to use a statechart I would suggest the following design, where you have composite states inside the working state to indicate if the security agent is working fast or normal and a message transition to let it move from one state to the next.
It is advised to use a message transition and trigger it as needed instead of a conditional state which gets chected for every change inside the agent since this can be a computational expensive exercise.
I assume you already implemented the correct capacity settings for the different on enter actions for working and breaking
Now you simply need to send the message every time an agent enters the queue and every time it exits the delay block, and of course, see the delay time based on the state of the statechart.
Aee screenshot below.

Delay fixed window from triggering for several minutes

Using Fixed Windows in Apache Beam. The watermark is set by the event time.
Some data may arrive out of order and cause the window to close.
How can a trigger be defined in Java to occur say 2 minutes after the last data was seen?
It's not entire clear what behavior you expect. One question is what do you expect to happen if the data arrives within the two minutes? Do you want to restart the two minutes interval, don't restart it, re-emit the data or not?
Looks like the trigger you are trying to describe is something along these lines:
wait until the watermark passed the end of window, in event time;
wait for additional 2 minutes in processing time;
emit the data;
If in step 2 it was event time, i.e. you wanted to re-emit the window if a late element arrives that fits within window + 2min, then you could use withAllowedLateness(). Though it sounds different from what you want, because it can keep re-emitting the window contents every time a matching late element arrives.
With processing-time in step 2 this is not possible in general with basic triggers that are available in Beam. You can probably achieve a behavior you want if you manually manage state and timers in your own ParDo, e.g. you can watch for the incoming elements, keep track on them in the state, and then on timer emit what you want. This can become very complicated and might still be not flexible enough for your specific use case.
One of the major problems is that there is no good way to define processing time triggers in Beam in general. It would be complicated to define a general mechanism of working with timers in this manner. For example, when you want to express "wait for 2 minutes", the framework needs to understand in relation to what these two minutes are, when to start the timer, so you need a mechanism to express that as well. And with composition, continuation and other complications this doesn't seem easy to reason about. So it's not in the framework in this general form.
In order to implement only the "wait for 2 minutes after the last element was seen in the window", the framework has to watch for it and set the timer. Technically it is possible to do something like this but doesn't seem like anyone has done it yet.
There seems to be only one meaningful processing time trigger available in Beam but it's not generic enough and doesn't do what you want. You can look at composite triggers like AfterFirst or AfterAll but they likely won't help you without a better general processing time trigger.
I decided against using Beam and implemented the solution in Kafka Streams.
I basically grouped by, then used fixed windows and the aggregated the result.
The "grace" on the window allows data to arrive late.
KGroupedStream<Long, OxyStreamItem> grouped = input.groupByKey();
TimeWindowedKStream<Long, OxyStreamItem> windowed =
grouped.windowedBy(
TimeWindows.of(WIN_SIZE)
.advanceBy(WIN_SIZE)
.grace(Duration.ofSeconds(5L)));
return windowed
.aggregate(
makeInitializer(),
makeAggregator(),
Materialized
.<Long, Aggregate, WindowStore<Bytes, byte[]>>as("tmp")
.withValueSerde(new AggregateSerde()))
.suppress(
Suppressed.untilWindowCloses(Suppressed.BufferConfig.unbounded()))
.toStream()
.map(calculateAvg());

How to check time difference between two events

How to check time difference between two events. can someone suggest me the approach to do this?
I have different events coming from a device. initially it will be sending "started" event and after sometime it will send "completed" event. I need to calculate the difference in the start and completed event. How can I do this?
In CEP events the time variable is of java type Date so you can use something like:
d1.getTime() - d2.getTime()
and you have the difference in milliseconds

IBM IoT Real-Time insights and raising an alert on first occurrence

When using IBM IoT Real-Time insights, we can define alerts that trigger when an event arrives that passes some rule condition. This can trigger an action such as sending an email. Is there a technique that will allow us to generate an alert the first time that a condition is true but will not generate an alert for subsequent events until the condition first evaluates to false?
Imagine I am receiving temperature events from a pipeline sensor. When the temperature passes 120oF, I want to receive an alert ... however it appears to me that the temperature event value will continue to be above this threshold temperature for each subsequent event which will result in the condition evaluating true many times. If the action I want to have performed is to email me, then I believe I will receive a new email for each new event received which isn't what I am wanting. What I really want to happen is to receive an email the first time the temperature passes a threshold and no more until the problem is corrected and it may happen again.
The continuous triggering of alerts once a rule threshold is met is a limitation that is currently being addressed by the IoT Real-Time Insights team. Keep an eye on the What's new in Bluemix section for news about new features as they are released.
The latest update of IoT Real-Time Insights includes updates to rule conditions. To control the number of alerts that are triggered over a time period, you can set a condition frequency requirement to for example only trigger the rule the first time a condition is met, and then not trigger for the next hour. Rules can now be triggered each time a condition is met, or when a condition is met N times in M days/hours/minutes. For more information, see Conditional Triggering in the IoT Real-Time Insights documentation.

Good design suggestion for multiple tasks scheduling in C#

I have a service that requires an operation to be performed on an entity status 10 minutes after a specific event has been invoked. During the 10 minute wait, it has to listen for other events that would stop this event altogether so the operation would not be executed.
At any point in time, there could be multiple instances of entities to be handled.
Is there any suggestion as to what possible ways of implementation to create this service?
I have something similar in one of my projects that handles users presence management. The rough idea is like this:
1 You have a timer with tick precision that you need (i use 15
second)
2 Some input event triggers monitoring of an entity and puts entity into the execution queue with time of adding
3 If any canceling event occurs > you remove corresponding entities from the execution queue
4 On timer ticks you check time of adding for each iteam in executionQueue and execute operation for entities that are in there for 10 mins
PS.
you might want to consider using multi-threading / TPL for operations execution.