I'm new to programming. I want to create a flowchart for my algorithm. In my algorithm there is a coordinator which waits for values from several devices. Each time it receives a value, it does some operation on it and after finishing the operation, waits for the next value to come.
So my question is that should I put an oval at the end of the the flowchart to show the end of each operation on a value or I should just connect the last rectangle (showing the last operation done on the received value) to the start part of the flowchart?
The attached photo is the flowchart I mean. Is it wrong to omit the end oval? If I put an end symbol (oval), won't it be wrong since the coordinator should wait for the next values?
I would put a wait for values node before the start, which would go with a directed vertice into start and operation2 would go into the wait.
Your chart, as it is suggests that the next chunk of work is started right away when the current is finished, yet, you need to illustrate the fact that there might be a wait time. Take a look at this chart:
You see the "Wait for data processing result" node. This is the wait symbol, which you need before start, as your very first node.
Related
I have a channel that emits events. I'd like to set up an "oscillometer" on top of this channel, i.e. some sort of a "frequency counter" that gets constantly updated (with some interval, of course, e.g. one second).
My first thought is to create a counter that will be incremented every time an event arrives on the channel and then have a ticker that every second prints out this counter and resets it to 0. It looks like the most straightforward approach, but I'm wondering if, maybe, there are some built-in tools for this in Go? Or maybe some libraries that can do it for me (I've tried finding one, but haven't succeeded)?
I'm not sure if the title is right but...
I want to animate (with html + canvas + javascript) a section of a road with a given density/flow/speed configuration. For that, I need to have a "source" of vehicles in one end, and a "sink" in the other end. Then, a certain parameter would determine how many vehicles per time unit are created, and their (constant) speed. Then, I guess I should have a "clock" loop, to increment the position of the vehicles at a given frame-rate. Preferrably, a user could change some values in a form, and the running animation would update accordingly.
The end result should be a (much more sophisticated, hopefully) variation of this (sorry for the blinking):
Actually this is a very common problem, there are thousands of screen-savers that use this effect, most notably the "star field", which has parameters for star generation and star movement. So, I believe there must be some "design pattern", or more widespread form (maybe even a name) for this algoritm. What would solve my problem would be some example or tutorial on how to achieve this with common control flows (loops, counters, ifs).
Any idea is much appreciated!
I'm not sure of your question, this doesn't seem an algorithm question, more like programming advice. I have a game which needs exactly this (for monsters not cars), this is what I did. It is in a sort of .Net psuedocode but similar stuff exists in other environments.
If you are running an animation by hand, you essentially need a "game-loop".
while (noinput):
timenow = getsystemtime();
timedelta = timenow - timeprevious;
update_object_positions(timedelta);
draw_stuff_to_screen();
timeprevious = timenow;
noinput = check_for_input()
The update_object_positions(timedelta) moves everything along timedelta, which is how long since this loop last executed. It will run flat-out redrawing every timedelta. If you want it to run at a constant speed, say once every 20 mS, you can stick in a thread.sleep(20-timedelta) to pad out the time to 20mS.
Returning to your question. I had a car class that included its speed, lane, type etc as well as the time it appears. I had a finite number of "cars" so these were pre-generated. I held these in a list which I sorted by the time they appeared. Then in the update_object_position(time) routine, I saw if the next car had a start time before the current time, and if so I popped cars off the list until the first (next) car had a start time in the future.
You want (I guess) an infinite number of cars. This requires only a slight variation. Generate the first car for each lane, record its start time. When you call update_object_position(), if you start a car, find the next car for that lane and its time and make that the next car. If you have patterns that you want to repeat, generate the whole pattern in one go into a list, and then generate a new pattern when that list is emptied. This would also work well in terms of letting users specify variable pattern flows.
Finally, have you looked at what happens in real traffic flows as the volume mounts? Random small braking activities cause cars behind to slightly over-react, and as the slight over-reactions accumulate it turns into cars completely stopping a kilometre back up the road. Its quite strange, and so might be a great effect in your wallpaper/screensaver whatever as well as being a proper simulation.
I'm having trouble computing the critical path of a network of activities. The data I have to work with is a little different than what I have seen in simple examples on the web. That is I have the start and end times of each activities, from whom I deduce the length. I have been using that algorithm to compute earliest and latest start and end times for each activity :
"To find the earliest start times, start at activities with no predecessors, and say they start at time zero. Then repeatedly find an activity whose predecessors' start times have all been filled, and set the start time to the maximum predecessor finish time.
To find the latest start times, run the preceding algorithm backwards. Start at activities with no successors. Set their finish time to the maximum finish time from the previous phase. Repeatedly find a predecessor whose successors have all been evaluated. Set its finish time to the earliest successor's start time.
Now it is trivial to evaluate slack = latest start – earliest start. Some chain of events will have slack time equal to zero; this is the critical path."
source : https://stackoverflow.com/questions/6368404/find-the-critical-path-and-slack-time
My code is identifying sometimes correctly the critical activities which compose the critical path, but due to the data I have, it sometimes fail. I have found when it does happen : it's when the given times for an activity (from whom cost is deduced) does not respect the early and latest times computed. Right now I only take into account the cost of each activity, but obviously it is not enough because in case like the one in picture below, critical path computed is not accurate :
one fail case for algorithm above http://imageshack.us/a/img688/2420/casemp.png
Obviously activity B is critical (if its end time is shifted, the end of project is also shifted) but the algorithm compute a slack of 1...
I don't know how to change the algorithm to make it work for case above.
I found some easy way to identify the critical activities from the data I have. For each activity, I simulate one second delay (add one second to the end time) and propagate that delay to all successors and test if it affects the time of last activity. If so, that task is critical.
This way works great, I have now a list of all the critical activities, but for some case it can take several seconds (23 seconds for 450 activities with a lot of dependances !). So still trying to find a better way.
A DVR needs to store a list of programs to record. Each program has a starting time and duration. This data needs to be stored in a way that allows the system to quickly determine if a new recording request conflicts with existing scheduled recordings.
The issue is that merely looking to see if there is a show with a conflicting start time is inadequate because the end of a longer program can overlap with a shorter one. I suppose one could create a data structure that tracked the availability of each time slice, perhaps at half-hour granularity, but this would fail if we cannot assume all shows start and end at the half-hour boundary, and tracking at the minute level seems inefficient, both in storage and look up.
Is there a data structure that allows one to query by range, where you supply the lower and upper bound and it returns a collection of all elements that fall within or overlap that range?
An interval tree (maybe using the augmented tree data structure?) does exactly what you're looking for. You'd enter all scheduled recordings into the tree and when a new request comes in, check whether it overlaps any of the existing intervals. Both this lookup and adding a new request take O(log(n)) time, where n is the number of intervals currently stored.
Situation: There are several entities in a simulated environment, which has an artificial notion of time called "ticks", which has no link to real time. Each entity takes it in turns to move, but some are faster than others. This is expressed by a delay, in ticks. So entity A might have a delay of 10, and B 25. In this case the turn order would go:
A A B A A
I'm wondering what data structure to use. At first I automatically thought "priority queue" but the delays are relative to "current time" which complicates matters. Also, there will be entities with larger delays and it's not unforseeable that the program will run through millions of ticks. It seems silly for an internal counter to be building higher and higher when the delays themselves stay relatively small and don't increase.
So how would you solve this?
You store the entities in a Heap and group them by their time left to wait. The group of entities that are next to move would be at the top of the Heap. You only have to update these entities. When their time remaining to wait drops to 0, you remove them from the heap. Put the next group of entities in line at the top of the Heap while decrementing their time to wait by the amount of time that just passed before the previous move.
For example:
Your Heap has 3 nodes (A,B, and C), the top is node A with two entities both having 5 ticks remaining. The childern have 10 and 12 ticks remaining respectively.
At time t=5 you move all the entities that are bucketed in node A
Remove A from the Heap
B moves to the top of the Heap with 10-5 = 5 ticks remaining then
repeat.
It seems to me by your description that the concept of "what's next?" is more important than "how long is it until the next action?". This being the case, sort your queue by "next-to-go" or lowest number of ticks remaining to highest. Inserts, of course, get entered in their appropriate order, and altered entries ("Speed up" spells) get removed trom the queue, altered, and then re-entered appropriately.
Then, you just pop the next job off the queue. Whatever number of ticks it had remaining must be the "time-elapsed". Makes a pass over the queue, decrementing the ticks remaining field of each entry by the amount of ticks you just discovered.
This has the advantage of keeping track of the concept of time remaining, but also of not having to fire events or execute any other code for ticks that go by when there is no action to take. You can afford this, since there is no relation to real time at all. There is only "what's next?", and "How long did it take to get there?".
If we assume your entities are observing or watching the simulation time, they could each implement an interface which makes them track the ticks left and provides a method to get how many ticks are left for a particular entity. At each tick, the entity reduces its ticks left by 1.
You could then keep a sorted set queue (set because each entity will be in the queue only once) of these entities, sorted based on get ticks left, so that the 0th entity is the one to move next, and the Nth entity is the "slowest".
When the entity's get ticks left method is 0, it is removed from the sorted set, the ticks left timer is reset, and it is re-inserted into the sorted set.
Option #1: Polling
I would probably build a controller that can discover the delay for all the different entities and maintain a ticks-remaining for each entity. The controller would cycle through ticks and on each tick it would reduce the ticks-remaining for all entities in the game.
Once an entities ticks-remaining value reaches zero you know it's their turn, either controlled by the heartbeat method that handles the ticks or a method that you call.
Option #2 Events
Think like the UI paradigm, the interface doesn't constantly poll the button to see when it is clicked. Rather it let's the button notify the UI when it has been clicked via events. Have your Entities (or an EntityBattleContext) fire an event when it is ready. You will have to handle you game time in some manner, since it isn't based off real world time at all you may need to have all the Entities listen for a GameTick event and when they receiver that event update their interal TicksRemaining variable.
Before following the event driven route make sure the polling route won't work. Remember the cardinal rule always optimize later because more often then not you don't need the optimization.
Look at how Java's DelayQueue is implemented.