Is a FSM in a state possible? - vhdl

I was wondering whether or not it's possible to implement the following.
What do I want to do?. Imagine the Big Ben's melody. I want to play a fourth of the melody every quarter of an hour. I want a FSM which has the following states:
Whole hour (so xx:00) = (input = 01)
Quarter past/ Quarter to (so xx:15 / xx:45) = (input = 10)
Half past (so xx:30) = (input = 11).
(xx = dont care)
For each state, i have another FSM. This FSM will make sure the music will play correctly.
If(state = whole hour)
{
switch music_state
case A
{ play 1 sec, next_music_state = F}
case and so on ......
Does this work? Can I make a FSM which has the different states of an hour, and for each state have another FSM?
If something is unclear, let me know, and I will try to clarify it. I know this part is not that hard, but I am still wondering whether or not this implementation will work. I hope, and also think, it will, though I want to be certain of it.
Thanks a lot in advance!

Meta-state machines with both an "inner state" and an "outer state" are certainly possible. I have done something similar for a while but it was only last week that the penny dropped and I finally wrote a state machine with two separate states. In a single process SM, it worked like a dream.
The "inner state machine" runs just like a normal state machine but in its last state, you test the meta-state or outer state, to determine which part of the outer sequence to despatch to next.

You could make single FSM to keep it simple...
The default state will be IDLE: This will check for the time (whole hour, quarters or half past), if one of them happens, you'll just run proper states like:
MUSIC_FOR_WHOLE_HOUR, MUSIC_FOR_HALF_PAST ...
Those states will run their different chain of states that will play their music and will return back to the IDLE state to wait for next proper time to play again.

Related

Looking for a more efficient way to pull data from multiple datasets in SAS

I'm trying to find a more efficient and speedier way (if possible) to pull subsets of observations that meet certain criteria from multiple hospital claims datasets in SAS. A simplified but common type of data pull would look like this:
data out.qualifying_patients;
set in.state1_2017
in.state1_2018
in.state1_2019
in.state1_2020
in.state2_2017
in.state2_2018
in.state2_2019
in.state2_2020;
array prcode{*} I10_PR1-I10_PR25;
do i=1 to 25;
if prcode{i} in ("0DTJ0ZZ","0DTJ4ZZ") then cohort=1;
end;
if cohort=1 then output;
run;
Now imagine that instead of 2 states and 4 years we have 18 states and 9 years -- each about 1GB in size. The code above works fine but it takes FOREVER to run on our non-optimized server setup. So I'm looking for alternate methods to perform the same task but hopefully at a faster clip.
I've tried including (KEEP=) or (DROP=) statements for each dataset included the SET statement to limit the variables being scanned, but this really didn't have much of an impact on speed -- and, for non-coding-related reasons, we pretty much need to pull all the variables.
I've also experimented a bit with hash tables but it's too much to store in memory so that didn't seem to solve the issue. This also isn't a MERGE issue which seems to be what hash tables excel at.
Any thoughts on other approaches that might help? Every data pull we do contains customized criteria for a given project, but we do these pulls a lot and it seems really inefficient to constantly be processing thru the same datasets over and over but not benefitting from that. Thanks for any help!
I happend to have a 1GB dataset on my compute, I tried several times, it takes SAS no more than 25 seconds to set the dataset 8 times. I think the set statement is too simple and basic to improve its efficient.
I think the issue may located at the do loop. Your program runs do loop 25 times for each record, may assigns to cohort more than once, which is not necessary. You can change it like:
do i=1 to 25 until(cohort=1);
if prcode{i} in ("0DTJ0ZZ","0DTJ4ZZ") then cohort=1;
end;
This can save a lot of do loops.
First, parallelization will help immensely here. Instead of running 1 job, 1 dataset after the next; run one job per state, or one job per year, or whatever makes sense for your dataset size and CPU count. (You don't want more than 1 job per CPU.). If your server has 32 cores, then you can easily run all the jobs you need here - 1 per state, say - and then after that's done, combine the results together.
Look up SAS MP Connect for one way to do multiprocessing, which basically uses rsubmits to submit code to your own machine. You can also do this by using xcmd to literally launch SAS sessions - add a parameter to the SAS program of state, then run 18 of them, have them output their results to a known location with state name or number, and then have your program collect them.
Second, you can optimize the DO loop more - in addition to the suggestions above, you may be able to optimize using pointers. SAS stores character array variables in memory in adjacent spots (assuming they all come from the same place) - see From Obscurity to Utility:
ADDR, PEEK, POKE as DATA Step Programming Tools from Paul Dorfman for more details here. On page 10, he shows the method I describe here; you PEEKC to get the concatenated values and then use INDEXW to find the thing you want.
data want;
set have;
array prcode{*} $8 I10_PR1-I10_PR25;
found = (^^ indexw (peekc (addr(prcode[1]), 200 ), '0DTJ0ZZ')) or
(^^ indexw (peekc (addr(prcode[1]), 200 ), '0DTJ4ZZ'))
;
run;
Something like that should work. It avoids the loop.
You also could, if you want to keep the loop, exit the loop once you run into an empty procedure code. Usually these things don't go all 25, at least in my experience - they're left-filled, so I10_PR1 is always filled, and then some of them - say, 5 or 10 of them - are filled, then I10_PR11 and on are empty; and if you hit an empty one, you're all done for that round. So not just leaving when you hit what you are looking for, but also leaving when you hit an empty, saves you a lot of processing time.
You probably should consider a hardware upgrade or find someone who can tune your server. This paper suggests tips to improve the processing of large datasets.
Your code is pretty straightforward. The only suggestion is to kill the loop as soon as the criteria is met to avoid wasting unnecessary resources.
do i=1 to 25;
if prcode{i} in ("0DTJ0ZZ","0DTJ4ZZ") then do;
output; * cohort criteria met so output the row;
leave; * exit the loop immediately;
end;
end;

How to run a Lua script at a certain hour mark?

I've altered one of the popular clock skins for Rainmeter to be exactly how I want it. However, the only issue is that the '1' in the clock for hours 10, 11, and 12 gets cut off by the edge of the skin with the current margins.
I know exactly which value needs to be altered at the given times, so all I need to figure out now is how to run a Lua script to change it when the clock hits them. Problem is, after much searching I don't have the slightest clue how. It definitely seems like something that should be easily possible.
You need to run your lua script as a cron job.
In order to achieve this I'd suggest you use this cron.lua module which has the functionality you want. One example of what you can do is the following:
local clock = cron.every(time, callback, ...).
--Creates a clock that will execute callback every time, periodically. Additional parameters are passed to the callback too.
The callback variable is the code you want to be executed at every interval.

What event fires when a player stands on a block in Roblox?

I want to change the colour of a part when a player stands on it but instead of putting the script inside the part can i put the script in the workspace and identify the part from an event, like Humanoid touched or something?
The reason is that i have 100's of parts which need to react to a touch event so i don't want to put the same script in each part.
Pseudo-code might be
Player touch part event fired
Identify part from event and change colour of part
Thanks
As M. Ziegenhorn wrote, you could put the script in the character or in the foot directly. That would be the "easiest" way of achieveing this.
However, you could also connect a function to each part easily.
In the code below, we check through a model in workspace named 'TouchParts' which (assumingly) contains the parts you want to tie the touch-function up to.
function Touched(self, Hit)
if Hit and Hit.Parent and Hit.Parent:FindFirstChildOfClass'Humanoid' then
-- We know it's a character (or NPC) since it contains a Humanoid
-- Do your stuff here
print(Hit.Parent.Name, 'hit the brick', self:GetFullName())
self.BrickColor = BrickColor.new('Bright red')
end
end
for _, object in pairs(workspace.TouchParts:GetChildren()) do
if object:IsA'BasePart' then
object.Touched:connect(function(Hit)
Touched(object, Hit)
end)
end
end
Doing it this way means anything in your character touching the part(s) will fire the Touched-event, so you would have to add in a check to see whether if tie part touching is a leg or not.
The pros of binding the function to each part instead of to the leg is that the function is only called when you actually touch one of the intended parts, instead of ANYTHING you touch. However, with an increased amount of parts you connect it to, there's also an increased amount of events which will be triggered and is stored in memory. Probably not noticeable on the scale you're working with, but worth keeping in mind.
Been a while since I coded something on roblox so please excuse any blunders I make.
local parent = workspace.Model --This is the model that contains all of that parts you are checking.
local deb = 5 --Simple debounce variable, it's the minimum wait time in between event fires, in seconds.
local col = BrickColor.new("White") --The color you want to change them to once touched.
for _,object in next,parent:GetChildren() do --Set up an event for each object
if object:IsA("BasePart") then --Make sure it's some sort of part
spawn(function() --Create a new thread for the event so that it can run separately and not yield our code
while true do --Create infinite loop so event can fire multiple times
local hit = object.Touched:wait() --Waits for the object to be touched and assigns what touched it to the variable hit
local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Either finds the player, or nil
if player then --If it was indeed a player that touched it
object.BrickColor = BrickColor.new(col) --Change color; note this is also where any other code that should run on touch should go.
end
wait(deb) --Wait for debounce
end
end)
end
end
This is probably one of the most, if not the most efficient way of doing this.
When a player stands on a block, the value "FloorMaterial" (which is in Humanoid) will be telling you what material the user is standing on, but if the user isn't standing on anything, this value will be nil.
Another efficient method is to use Rays. You would need to create a ray from your HumanoidRootPart.
Example:
IsOnGround=function()
local b=false;
local range=6;
local char=game:service("Players").LocalPlayer.Character;
local root=char:WaitForChild("HumanoidRootPart",1);
if root then
local ray=Ray.new(root.CFrame.p,((root.CFrame*CFrame.new(0,-range,0)).p).unit*range);
local ignore={char};
local hit,pos=workspace:FindPartOnRayWithIgnoreList(ray,ignore,false,false);
pcall(function()
if hit then
b=true;
end
end)
else
print("root not found");
end
return b;
end
This would cast a ray from the HumanoidRootPart, towards the direction where the ground should be, with a distance of 6 studs.
Sadly, this method isn't very effective with R15 characters as far as I know.

Windows batch card game not working properly, don't know how to fix

So for our computer science class, we had to write a program that randomly generates cards, i decided to do mine in batch, because im a massive noob XD
I was confident that i could do it as im quite experienced with it. Even though batch isn't by any means a good 'language' if your going to call it that. I was able to fix most of the problems by myself with some hard work. I am however, still having some issues i don't know how to resolve.
My biggest issues i don't know how to fix are...
Text not being displayed properly.
Numbers (i use for the base of the AI and card generation) sometimes not being defined properly in variables.
The point system just refuses to work not matter what i do.
It sometimes randomly just flat out decides to crash on me if i skip a 'TIMEOUT' or a 'PAUSE'.
Some 'IF' statements not being executed properly even though there exactly the same as the other ones.
I'm sorry if this question is too broad, but i really didn't know quite how to summarize it.
Here is a link to my card game: http://pastebin.com/t2S3yWk5
Here is our question:
1) Create a program that will generate two random numbers - one that maps to a suit (hearts,diamonds, clubs or spades) and one that maps to a card (Ace, 2, 3, ..... Jack, Queen, King)
*Mine is slightly different, it generates two different suits based on two random numbers.*
2) Create a program that will generate a random card and then ask the user to go Higher,Lower or Quit. If the player chooses Higher the next card must be of a higher value or theplayer is out. Likewise for Lower.
3)Extending the previous program, the user will select a trump suit at the start of the game. If the card is a trump suit card then the game continues regardless of the card’s value. The program will keep score and will save the score to a highscore file. The user will also be able to display the current highscore file.
I would like to try and do this stuff (listed above) myself. I just need help trying to fix my existing program.
I hope that if your reading this you could give me some advise or provide solutions to some of my problems. Thanks in advance! :3
Good news; nothing is super wrong with your code, it's just a bunch of little things that seem like a lot. Some off-by-one errors and a missing variable that I can only assume got replicated from copying and pasting.
Text not being displayed properly
I assume this is the "Access denied" errors that your code produces instead of the AI's comments. This is due to the fact that > and < are used for output redirection, so the emoticons you are adding are trying to create a file in a place you don't have access to. Either get rid of them (recommended) or use ^ to escape them (^>:)).
Numbers (I use for the base of the AI and card generation) sometimes not being defined properly in variables
%random% %% 5 results in one of the numbers in the set 0, 1, 2, 3, 4. You currently do not have an if statement for what should happen if 0 is randomly selected. Add an if statement or have the code go back to the top of the section if a 0 is selected.
The point system just refuses to work no matter what I do
You're going to kick yourself...
Your set statements are missing the assignment portion. SET /A %p1p% + 2 should be SET /A p1p=%p1p% + 2 and so forth (or set /a p1p+=2 if you think that looks better).
If sometimes randomly just flat out decides to crash on me if I skip a 'TIMEOUT' or 'PAUSE'
I couldn't replicate that, but the code seemed to work fine when I removed those statements.
Some 'IF' statements not being executed properly even though they're exactly the same as the other ones
Your comment indicated lines 119-132, which include the if statements that assign points. See above for why those aren't working.
Some other recommendations for your code
Your variable names should be more descriptive. For example, ctog doesn't tell me anything about what that variable should be; I can look at the code to see what it does, but without any context, that could be doing anything.
You should add the /i flag to the if statements that check which card you put down so that C1 and c1 get treated the same. On a related note, you should add a check for when the player enters something other than C1 or C2. You can even use a choice command like you did earlier.
:pvic is missing an exit command, so you automatically play again if you win. Combined with the fact that you only check if lt is equal to 2, not greater than or equal to 2, there's no way to stop playing if you win. Also on the subject of end game conditions, there's no if statement for if you tie the computer.
cp1 and num1 are effectively the same variable, there's no reason to have both (same with cp2/num2, ap1/num3, and ap2/num4).
You need some kind of goto at the end of :pc1 so that :pc2 doesn't automatically run after :pc1 finishes.

How to check whether the FrontEnd considers evaluation still running?

Is there a way to check programmatically whether the FrontEnd considers evaluation still running?
Or even better: is there a way to check whether the FrontEnd has some pending inputs to be sent to the kernel?
P.S. This question has arisen from previous question.
EDIT
When evaluating a Cell in the FrontEnd we usually create a queue of inputs for the kernel.
I need a function that will return True if the FrontEnd has sent to the kernel the last input of the queue of inputs from the EvaluationNotebook[]. Or in other words I need a function that returns True if this current input is the last input of the queue of inputs generated by the FrontEnd.
This should work. Of course, you have to run it in a different kernel than the one that is performing the evaluation you want to check for.
NotebookEvaluatingQ[nb_] := (
SelectionMove[nb, All, Notebook];
Or ## Map["Evaluating" /. # &, Developer`CellInformation[nb]]
)
Obviously, it's best to set things up before hand using a tool like Monitor. For example,
Monitor[
Do[Pause[6], {i, 10}],
i]
will allow you to observe the progress of the index variable i. If you haven't set things up before hand, you might be able to do something using the "Interrupt Evaluation" button under the Evaluation menu. For example, try the following:
Do[Pause[6], {i, 10}]
Now, wait six or more seconds and then select "Interrupt Evaluation". You can then examine the state of i to see how far along it is. You resume evaluation using Continue under "Debugger Controls".

Resources