How to determine when to start a counter to ensure it never catches the previous counter - matlab

I have a problem where I have several events that are occurring in a project, the events happen semi-concurrently, where they do not start at the same time but multiple can still be occurring at once.
Each event is a team of people working on a linear task, starting at the beginning and then working their way to the end. Their progress is based on a physical distance.
I essentially need to figure out each events start time in order for no teams to be at the same location, nor passing eachother, at any point.
I am trying to program this in MATLAB so that the output would be the start and end time for each event. The idea would be to optimize the total time taken for the project.
I am not sure where to begin with something like this so any advice would be greatly appreciated.

If I understand correct, you just want to optimize the "calendar" of events with limited resources (aka space/teams).
This kind of problems are those called NP and there is no "easy" way to search for the best solution.
You here have two options:
Greedy like algorithm: You will have your solution in a resonable time but it won't be the best one.
Brute force like algorithm: You will find the best solution but maybe not in the time you need it.
Usually if the amount of events is low you can go for 2nd option but if don't you may need to go for the first one.
No mather which one you choose first thing you will need to do is to compute if a solution is valid. What does this mean? It means to check for every event wheter if it collisions whith others in time, space and teams.
So lets imagine the problem of making the calendar on a University. There you have to think about:
Students
Teacher
Classroom
So for each event I have to check if another event have same students, teacher or classroom at the same time. First of all I will check the events that match in time with the actual event. Then I will compare the actual event with all the others.
Once you have this done you could just write a greedy algorithm that starts placing events on time just checking if it collides with some other.

Related

How to fix the 'Error in the model during iteration' in anylogic

I've built a model in which a fleet of trucks delivers multiple orders to different customers. This model works fine when I perform one simulation experiment. However, when I try to run a parameter variation, the following error occurs: 'Error in the model during iteration x'. A snapshot of that particular error can be found in 2.
A question about this topic is earlier asked here:
NullPointerException during Parameter Variation Experiment with agent statistics
I have tried the tips given in that post but none of them seems to solve the problem.
I have replaced all the conditional transitions with messages in my state chart (see figure).
My data sets are stored in the database, so that cannot be the problem.
I can't get my head around why the model works with some seed values and with some not. I understand that finding the modelling flaw from just the snapshot is hard, but any tips on how I could find the mistake could be helpful.
PS: I have the learning edition so there is no debugger
Edit:
The error happens at a specific line of code written in the transitions pointing towards the state from the state "movingToClient1". The line that seems to cause the error is:
Order order = orderStore.myOrdercollection.get(0);
the iterations seem to work. However, I need it to be equal to one (to specifically measure certain KPIs of the last route). Hopefully, this helps in finding a solution.
The most likely thing to cause the problem is that your arraylist called collectionOfOrders is missused.
so at some point on the "on enter" of one of your states, you do :
collectionOfOrders.get(something)
when collectionOfOrders is actually empty.
sometimeswhat happens is that multiple things happen at the same time in your model, and when you ask if collectionOfOrders==1, another of your truck agents does the same and they both return true, which means that one of them will get the issue.
This happens only with certain seeds, because it occurs with a very low probability.
This is my guess, with the current information provided
Due to the insight given by Felipe and Benjamin, I found the problem in my model. My model starts with an import order with a specific arrival rate of one in a source block. The rate of 1 is equivalent to exponentially distributed interarrival time with mean = 1/ratedefined (https://anylogic.help/library-reference-guides/process-modeling-library/source.html). This means that it is possible for some seed values that the orders are generated at the same time. Therefore, changing the setting from 'rate' to 'interarrival time' solved the problem.

Time mesauring of unique objects with Nesper

I would like to measure the time of cars with an unique Id with NEsper. My problem is, that there are several cars on a road. This road has predefined steps. I want to start to stop the time for every unique car that is in first step until this car receives the last step.
select ice.* from pattern[ice = IncomingCarEvent -> every timer:interval(1 sec) and not LastStepEvent]
The problem is, that I don't know how to separate the cars with a given Id. In this example, if one of the cars receives the last step the event does not fire anymore.
I'm not very clear on why you measure until the last step and the pattern says "not lastestep".
Partitioning can be done by correlating the events in the patterns such as "a=A => B(id=a.id)".
You can also partition using a context declaration which is in this link: http://esper.codehaus.org/esper-5.1.0/doc/reference/en-US/html_single/index.html#context_def_keyed
Or you may find the match-recognize to be easier to use as it has more of a step-wise approach to expressing a match much like regular expressions.

Puzzle Solver - TreeNode Help

I'm trying to code a puzzle solver app.
I need to find out how many moves it takes, and how many solutions there are.
I would rather not give too many details on the puzzle.
but the player moves around a grid ( say 5 x 7 )
as they move, obstacles could be captured so the state of the board needs to be tracked.
( this could be done as a string or an array )
I understand I need to create a TreeNode, starting with a root ( the players start position )
and give each node children of the possible moves until all the possible moves are calculated.
The puzzle stats could then be collected.
Number of Possible solutions, minimum number of moves to solve, average number of moves to solve, etc.
I have the puzzle logic created that will return if moves are possible and such.
I'm having problems creating the TreeNode structure and making sure moves are not duplicated.
The puzzle app itself is on the iPhone, but I'm writing this solver/editor on the Mac.
Any help would be VERY much appreciated.
Perhaps you could do a variant of a tree recursion? Traverse the tree recursively, having each end node return a value of how hard it was to get there (if there are costs associated with different moves) and a description of how it got there. This of course requires the player to only move in one direction, otherwise the tree-structure doesn't describe the problem. A bit more info on what your exact problem looks like would be helpful.
It might be a heavy algorithm, but it gets the job done.
For detecting repeated states, you would put the states in a set as you went along, and then check every time you found new states to see if they already existed. Though if space is an issue, you will have to resort to only checking if the children are not the same as the parent, or some kind of limited version of this approach.
A node class is very simple. It just contains a pointer back to a parent (if it has one) and the variable it holds (such as a state). You will also probably want other variables depending on your application.
When you get to a node, you use a successor function to get all the child nodes from there (the states that can be reached in one move) and add them to a list. You pluck from the list to traverse the tree.

Recreate a graph that change in time

I have an entity in my domain that represent a city electrical network. Actually my model is an entity with a List that contains breakers, transformers, lines.
The network change every time a breaker is opened/closed, user can change connections etc...
In all examples of CQRS the EventStore is queried with Version and aggregateId.
Do you think I have to implement events only for the "network" aggregate or also for every "Connectable" item?
In this case when I have to replay all events to get the "actual" status (based on a date) I can have near 10000-20000 events to process.
An Event modify one property or I need an Event that modify an object (containing all properties of the object)?
Theres always an exception to the rule but I think you need to have an event for every command handled in your domain. You can get around the problem of processing so many events by making use of Snapshots.
http://thinkbeforecoding.com/post/2010/02/25/Event-Sourcing-and-CQRS-Snapshots
I assume you mean currently your "connectable items" are part of the "network" aggregate and you are asking if they should be their own aggregate? That really depends on the nature of your system and problem and is more of a DDD issue than simple a CQRS one. However if the nature of your changes is typically to operate on the items independently of one another then then should probably be aggregate roots themselves. Regardless in order to answer that question we would need to know much more about the system you are modeling.
As for the challenge of replaying thousands of events, you certainly do not have to replay all your events for each command. Sure snapshotting is an option, but even better is caching the aggregate root objects in memory after they are first loaded to ensure that you do not have to source from events with each command (unless the system crashes, in which case you can rely on snapshots for quicker recovery though you may not need them with caching since you only pay the penalty of loading once).
Now if you are distributing this system across multiple hosts or threads there are some other issues to consider but I think that discussion is best left for another question or the forums.
Finally you asked (I think) can an event modify more than one property of the state of an object? Yes if that is what makes sense based on what that event represents. The idea of an event is simply that it represents a state change in the aggregate, however these events should also represent concepts that make sense to the business.
I hope that helps.

How to approximate processing time?

It's common to see messages like "Installation will take 10 min aprox." , etc in desktop applications. So, I wonder how can I calculate an approximate of how much time a certain process will take. Off course I won't install anything but I want to update some internal data and depending on the user usage this might take some time.
Is this possible in a iPhone app? How Cocoa guys do this, would it be the same way in iPhone apps?
Thanks in advance.
UPDATE: I want to rewrite/edit some files on disk, most of the time these files are not the same size so I cannot use timers for the first iteration and calculate the rest from that.
Is there any API that helps on calculating this?
If you have some list of things to process, each "thing" - usually better to measure a group of 10 or so "things" - is a unit of work. Your goal is to see how long it takes to process a single group and report the estimated time to completion.
One way is to create an NSDate at the start of each group and a new one at the end (the top and bottom of your for loop) for each group. Multiply the difference in seconds by however many groups you have left (minus the one you just processed) and that should be a reasonable estimate of the time remaining.
Of course this gets more complicated if one "thing" takes a lot longer to process than another "thing" - the above approach assumes all things take the same amount of time. In this case, however, you may need to keep track of an average window (across the last n "things" or groups thereof).
A more detailed response would require more details about your model and what work you're performing.