Functionality of a priority queue in a hospital? - pseudocode

So let's say I have a hospital that I'm running and I have 1 doctor and 5 patients. Patient 1 comes in, nobody is in queue so he goes first.
Next, I have 3 other patients. Bob has been waiting 3 hours. Sally has been waiting 2. Jim has been waiting 1. Based on arrival, Bob goes first, then Sally, then Jim. Like a regular queue (FIFO).
Bob goes next. During Bob's exam, Jack comes in and has an axe in his head. Jack needs immediate attention. How would I immediately stop Bob's inspection and swap over to Jack in this priority queue?
What would this look like structurally in pseudo code? Would this need some sort of timer?
I'm struggling to understand how Adaptable Priority Queues work and would like to understand the logic behind them. Not asking for code, but the thought process for this sort of thing.
Something like this:

I think what's being described is a combination of a signaling system like POSIX signals, with an actual queue/dequeue process, where you simply need to promote certain patients ahead of others at the time of dequeueing, I would guess by seriousness (triage). Maybe something like this:
SELECT patient_id
FROM wait_list
WHERE queue_status = 'awaiting service'
ORDER BY triage_code, queue_dt # queue_dt is a Date/Time
Where triage_code might have a mapping like:
1 # Very serious
3 # Serious
5 # Stable
7 # Stable Non-Serious
9 # Non-Serious
With the last group maybe vaccinations and the like. Make it sparse to give you a little growth, maybe, or just use 1-3. I've done this recently, it works well enough. Just keep a mapping of the numbers handy.

You can create a state field to inform you about the current patient treatment state that can be:
waiting
interrupted
finished
examining
...
And have a table where you register the last interrupted patient and the last patient with serious problem that you are going to examine first.
So that you can be able to track the lastly interrupted patient and call him back after you finish with the one having serious problems.
I think that can work

Related

Commitment Level

I've written a basic rpc client which polls the state of an Solana account to look for a specific condition (i.e. a unique int64 Id being written to it). When the condition arises, I call a smart contract which takes the same account as a mutable argument.
Before doing anything, the program checks for the same condition. However this check fails. I understand we're dealing with a distributed system and that state maybe inconsistent for a period of time, but I can repeatedly call for over 30 secs and it fails each time, before ultimately succeeding.
I've read about the concept of commitment levels but always assumed the account state passed into the smart contract would be the latest state of the world (i.e. processed)? What I appear to be observing is it's more like the finalised state.
Can anyone shed some light on what might be going on here?
I will try and come up with a minimal code example to demonstrate the problem but just wanted to ask the question first, to see if anyone can point me in the right direction.
Thanks
So if you look at the docs you linked, processed has a note:
the block may still be skipped by the cluster
This is a very important note if you're only looking for account state changes and don't want some that may be false. There's a number of reasons that a slot can be skipped, or a transaction could be rejected by the cluster.
If any of the above happens, then the account state that is accepted by the cluster as a whole may not be reflected in processed, but finalized.
In the end my specific problem came down to pre-flight checks using the 'finalized' commitment level when my logic for polling the account was using 'confirmed'. Modifying the preflightCommitment argument on sendTransaction fixed the problem for me.

How do I create a time-based delay that starts and ends by a clock time?

I'm trying to simulate a movie theater. Movies start and end at specified times, no matter when a customer arrives and sits down. I want to be able to start and end the delay by time based units, kicking everyone out at the same time (once the movie is over)
I've tried googling - because I am a student and this was way too ambitious to try but I really wanted to. I'm literally hoping for any kind of insight
Select output, 4 different services (not ped), ped go to, then all same sink
I want it to work and it doesn't
Try thinking of your movie as an agent. Each movie can have its own event, which you could set to go off at a specific calendar date/time. In the action of the event, you could send a message to all your customers that they can now leave the theater...or, if your customers are in a queue with a hold block for that particular movie, you can just unblock the hold.
With movies as agents, you could have a start/end time and then create your population of movies from a database linked to Excel. That is, you would be able to set up your movie schedule outside of AnyLogic, which would probably be easier for whoever your stakeholders are.
Your question is probably too broad to answer fully in stack overflow. There are a million different ways to model a movie theater, and approach would depend on what you are trying to answer, animation requirements, your comfort level with programming, user interface requirements, etc.

How to create a finite state machine that can process simultaneous events

Suppose you have an object 'A' that can potentially receive the following events from external objects:
Event 1
Event 2
...
Event n
Now suppose that the framework that hosts 'A' is such that all relevant events will be delivered to 'A' (one at a time), and then A::doEval() will be called.
It's important to note that 'A' could receive any combination of events in any order. 'A' might only get one event before doEval() is called, or it might get 5 events before doEval() is called. There's no way to know ahead of time.
It's also important to note that these events, because they are all delivered to 'A' before A::doEval() is called, should be considered simultaneous events. A regular state machine would react to each event as it was handed to 'A'. This would be incorrect in my usage case... I need 'A' to sit back and collect all events, and only in doEval() should 'A' perform any actions.
Now here's the trick bit: The doEval() logic needs to realize that only a subset of events occurred, but that it might need factor them all in. For example, the code (this is ugly and what I'm trying to avoid) might look like this:
doEval()
if(Event 1 occurred && Event 2 occurred) then <do something>
It's that 'if' statement... I only want to perform the action if both events occurred, but I don't want to have that 'if' statement. This is what FSMs are supposed to get rid of right? Do I need to have a hierarchy of state machines?
Any ideas on the "proper" way to address this? Any links or papers to read would be great, code is even better.
Thanks!
Make a queue to gather all events directed to "A" and pop them from queue and then process events.
I found what I was looking for in the form of Harel State Machines:
http://www.mathworks.com/videos/understanding-state-machines-harel-state-machines-4-of-4-90491.html
TL/DR: Take what we all know as state machines and add the ability to have hierarchical substates, parallel state machines, and communication between independent state machines (which he calls broadcasting).
The whole job of a state machine is to capture the relevant history of events in a succinct way. A state machine exactly allows you to avoid the checks of the kind:
doEval()
if(Event 1 occurred && Event 2 occurred) then <do something>
The Mathworks video is a good starting point, but I would also recommend the following sources:
UML state machine (Wikipedia)
A crash course in UML state machines (Embedded.com article)
Back to Basics (CUJ article)

how to approach a reward system [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a website where I've done a "mission generator" to encourage engagement. I've hard coded 3 sets of missions (Easy, Med, Hard).
Easy missions have 6-8 cases that could randomly happen
Update N times your hero (N is random number)
Update specific skill
Rest your hero for a day
etc..
Med and Hard require 1 task which is harder to achieve and adds two from the Easy tasks.
My generator works great but I would like to know how to design the reward system so that it is hard to game by the users.
The problems that I have are sort of:
The user might decide to quickly achieve the tasks and then delete them as soon as they get the "achieved badge"
For example
Update 3 times your hero
Add a new skill
Send a message to another hero
What I see as a problem is that the person might decide to update 3 times, choose a new skill and send a message, then erase the 3 updates (there is such a feature) remove the skill and delete the message that he has send.
So far I've come up with the following ideas but I'm looking for an opinion from someone who has done something like that.
- First approach:
Hard-code a table in the database with columns for each possible combination say
row1 - Updates | row2 - New skills etc...
Downside: I'm afraid that the row could end up very long and it would be hard to manage and add new types of challenges. The whole concept become very rigid.
- Second Approach:
For every task assign a unique id and once a skill or update or whatever is added append that to the task number.
Downside: every member gets 3 daily missions (which could be extended in time) if the generator requires on average 3 updates per mission that makes it to roughly 10 updates per user per day, adding to that the actual records for the updates with the data it adds up pretty quickly for a couple of thousand users. I would say 90% of this data will be useless and will just generate bills.
- Third approach:
Add a column to the table with the updates, skills (and all the other tables which are used in the missions). Then, when the user adds a new skill (for instance) the challenge ID will be added to the column in the skills table, then if the skill is removed the mission will fall back to "unachieved" (same goes for the updates and the other skills)
Downside: This might turn out very messy when the challenge is "put hero to rest for 1 day" because if the generator asks that twice between two days, the user will lose the first achievement of that sort because the challenge ID will be overwritten.
A big question is - what happens when the user decides to cut corners and delete some of their skills. I presume their missions will start piling up? (kind of like a punishment)
Try making some things more dynamic.
For example: You could put the skills in a static checkbox list on the side, and select or deselect them. Then add a button to delete the selected skill, and skills are added to the static checkbox list by selecting a skill on the page itself, outside of the checkbox list.
The skill is then added to the checkbox list as an element.
When you need to rest your character for a day, it's pretty much impossible to do that twice in one day. Make it so that the rest for a day event can only happen once in a day.
This way there is no need to worry about overwriting, and the player doesn't get confused as to why his hero needs to rest twice. Perhaps make the resting bound to a stamina bar that decreases with certain events. When you add a skill perhaps the training can cost stamina. When the stamina runs out his hero needs to rest.
About removing updates and skills:
It is not a very good idea to make things removable right after being added.
Make it so that the skills and updates can only be removed after a certain time.
Long enough to confirm the achievement and store the achievement.
Then they are able to remove their skill and updates and the achievement which was already saved won't disappear.
Sending a message to the other hero:
I have a question about that.
Why would you be able to remove the message.
Is the message sent to another player?
If so, it would make no sense to be able to remove it.
Also, sending a message obviously happens server side since it goes to another player so you can set it to done server side.

how to group entries in an activity stream

We currently have an activity stream for things like users posting a photo, liking a post, and commenting. Everyone essentially has their own stream which I fan out to on write. I want to group similar activity stream entries together like Facebook does. E.g. "Jim, Mary and John liked your post" shows up as one entry rather than 3 entries, one for Jim, Mary and John separately.
Does that mean I shouldn't use fan-out on write anymore and would have to change my architecture? Or, should I still keep my activity streams as is and group and process entries on read? Are there things I can do to make that more efficient? What are some best practices on how to group activities together, like maybe all entries of the same type in the past day, past hour, or since last login should be grouped together?
Should I do this client-side? Should I have an auto-incrmenting incrementing group by integer for each feed item that's tied to some time period?

Resources