NTP, Unix and Messaging - unix-timestamp

I have two machines and both of them are set to UTC by the same NTP server. I have them set up so that Machine A sends messages to Machine B at time T and Machine B receives these messages at T + N. The messages seem to be receivable at Machine B for any positive value of N and Im wondering if anyone can tell me of a way to get Machine B to receive these messages at values of N which are less than zero.
The application of this is for frivolous message passing around magnetised water coolers where varying flux densities are related through Brown Capacitors on the fly.
I dont think this problem is one which can be solved in an instant but might merit some responses from those who know what Im talking about.

You can use the type or code that. Say Machine B = 0 and Machine A = 1 or if you have them both in a way that Time is greater than N so the overall outcome is of T + N is a positive outcome. The second code that I added was to unscramble the message but I couldn't mix it with my old code so if you could tweak it to your code it could fix your problem. So I removed all the unscrambling code and put a list code so when it is transmitted it should be fixed
Message = input("Please put your message")
Machine_B = 0
Machine_A = 1
while Machine_B <= Machine_A:
Machine_B -= 1
if Machine_B = -10
Machine_B += 1
print(Message)
else:
Machine_B -= 1
message1 =[Message]
print(message1)

Related

Simulating first come first serve agent distribution

I currently have agents of type patient seizing an exam room when it becomes available, then, to mimic a first come first serve system where different healthcare practitioners see the patient, I have utilized a SelectOutputIn and 4 SelectOutputOut blocks corresponding to the 4 different practitioners that can see the patient. Each SelectOutputOut block has the same probability corresponding to their resource type. The problem arises when all practitioners are busy, it seems to send patients only to the Physio path and overload it. Is this because it is physically the last block in the order? How can I make the distribution of patients random even if all practitioners are busy?
[][1
It is correct. If all probabilities are zero, AnyLogic seems to pick the last option. To address your issue, we need to add a condition for when all practitioners are busy. So you will have 3 possible outcomes as follows:
Surgeons.idle() + Fellows.idle() + Residents.idle() + Physios.idle() == 0 ? 0.25 : Surgeons.idle() > 0 ? 0.25 : 0
The difference between the first and second 0.25 is that in the first case, all ports will have a 0.25 probability. Whereas in the second case, 3 of 4, or 2 of 4 or even 1 of 4 will have a 0.25 probability depending on how many resources are available. AnyLogic normalizes the probabilities. So if two ports have 0.25 probability, then it's like saying it's a 50/50 chance.
Finally, if the code seems too long, you can replace the first part by a function to have a cleaner/shorter code.
The function body could be:
return Surgeons.idle() + Fellows.idle() + Residents.idle() + Physios.idle();
Assume you named the function function, you can simplify your code in the probability field as follows:
function() == 0 ? 0.25 : Surgeons.idle() > 0 ? 0.25 : 0

Questiins about DTCM, how to model this process?

In a certain manufacturing system, there are 2 machines M 1 and M 2.
M 1 is a fast and high precision machine whereas M 2 is a slow and low
precision machine. M 2 is employed only when M 1 is down, and it is
assumed that M 2 does not fail. Assume that the processing time of
parts on M 1, the processing time of parts on M 2, the time to failure
of M 1, and the repair time of M 1 are independent geometric random
variables with parameters p 1, p 2, f, and r, respectively. Identify a
suitable state space for the DTMC model of the above system and
compute the TPM. Investigate the steady-state behavior of the DTMC.
How to model this into. DTMC? What is the state space? I have tried to use state-space like this:
0: M1 is working, M1 does not fail
1: M1 failed, M2 is working, M1 is repairing, M1 does not finish repairing
But there are still some problems, like what will happen after M1 finishes 1 part? Immediately process the next one or will change decide whether to fail or not? What will happen if M1 fails during process one part? What is the probability transfer matrix?
Thank you very much for your help!!!!

System Dynamics simulation - Translating Stella into AnyLogic syntax

I modelled the following logic in stella:
(IF "cause" > 0 THEN MONTECARLO("probabilityofconsequence") ELSE 0
But Im not getting the correct syntax on AnyLogic:
(cause > 0) ? (uniform() < probabilityofconsequence) ? 1 : 0 : 0
Any ideas?
Disclaimer:
What stella does is with the Montecarlo function a series of zeros and ones from a Bernoulli distribution based on the probability provided. The probability is the percentage probability of an event happening per DT divided by DT (it is similar too, but not the same as, the percent probability of an event per unit time). The probability value can be either a variable or a constant, but should evaluate to a number between 0 and 100/DT (numbers outside the range will be set to 0 or 100/DT). The expected value of the stream of numbers generated summed over a unit time is equation to probability/100.
MONTECARLO is equivalent to the following logic:
IF (UNIFORM(0,100,<seed>) < probability*DT THEN 1 ELSE 0
the equivalent in anylogic should be:
cause>0 && uniform(0,100) < probability*DT ? 1 : 0
you need to create a variable called DT that is the equal to either the fixed time step that you have chosen in your model configuration, or the value you consider that should be adequate.
Since anylogic depending on how you are running the model, doesn't consider the fixed time step as fixed, you need to define the DT yourself.
No matter what, you are going to get results not exactly equal to stella probably since the time steps are not necessarily the same... but maybe similar enough should satisfy you

How does a sawtooth pattern of Kafka consumer lag emerge?

Some of my Kafka consumers (but not all) show an interesting pattern regarding their lag.
The following image shows two good examples:
dark-blue:
about 200 messages per second in topic
32 partitions
1 consumer in group (Python client, running on Kubernetes)
light-blue (same topic as dark-blue):
so also about 200 messages per second in topic
so also 32 partitions
1 consumer in group (also a Python client, running on Kubernetes)
brown:
about 1500 messages per second in topic
40 partitions
2 consumers in group (Java/Spring client, running on Kubernetes)
Both sawtoothy clients can handle much larger throughput than that (tested by pausing, resuming and letting them catch up), so they are not working on their limits.
Rebalancing does happen sometimes (according to the logs) but much less often than the jumps in the diagram, and the few events also don't correlate in time with the jumps.
The messages also do not come in batches. Here is the additional information for one of the affected topics:
Where can this pattern originate from?
Just found out that the low-frequency sawtooth pattern is not real. And the explanation is quite interesting. ;)
When I check the consumer lag using the command line (kafka-consumer-groups --bootstrap-server=[...] --group [...] --describe), I see that the total consumer lag (sum of lags per partition) fluctuates very quickly. At one point it's around 6000, 2 seconds later its around 1000, again 2 seconds later it might be 9000.
The graph shown however seems to be based on samples taken with a lower frequency, which violates the Nyquist–Shannon sampling theorem. So the averaging does not work, and we see a Moiré pattern.
Conclusion: The sawtooth pattern is just an illusion.
For completeness, here is a simulation depicting the effect:
#!/usr/bin/env python3
"""Simulate moire effect of Kafka-consumer-lag graph.
"""
import random
import matplotlib.pyplot as plt
def x_noise_sampling() -> int:
return 31 + random.randint(-6, 6)
def main() -> None:
max_x = 7000
sample_rate = 97
xs = list(range(max_x))
ys = [x % 100 for x in xs]
xs2 = [x + x_noise_sampling() for x in range(0, max_x - 100, sample_rate)]
ys2 = [ys[x2] for x2 in xs2]
plt.figure(figsize=(16, 9))
plt.xlabel('Time')
plt.xticks([])
plt.yticks([])
plt.ylabel('Consumer lag')
signal, = plt.plot(xs, ys, '-')
samples, = plt.plot(xs2, ys2, 'bo')
interpolated, = plt.plot(xs2, ys2, '-')
plt.legend([signal, samples, interpolated], ['Signal', 'Samples', 'Interpolated samples'])
plt.savefig('sawtooth_moire.png', dpi=100)
plt.show()
if __name__ == '__main__':
main()

What is the correct way to implement this loop that will average values with a changing counter?

I have looked thoroughly on the internet for an answer to this question, but it seems to be too specific for an answer anywhere else. This is my last stop.
To preface, this is not a homework problem, but it is adapted from an online Coursera course, whose quiz has already passed. I got the correct answer, but it was mostly luck. Also, it is a more of a general programming question than anything related to the course, so I know for a fact that it is within my right to ask it on a public forum.
The last thing is that I'm trying to do this in MatLab; however if you have an answer that is in C++ or Python or any other high level language, that would be wonderful, as I could easily adapt those solutions to MatLab syntax.
Here it is:
I have two vectors, T and M, each with 600,000 elements/entries/integers.
T is entered as milliseconds from 1 to 600,000 in ascending order, and each element in M represents 'on' or 'off' (entered as 1 or 0 respectively) for each corresponding millisecond entry in T. So there are random 1's and 0's in M that correspond to a particular millisecond from 1 to 600,000 in T.
I need to take, starting with the 150th millisecond of T, and in 150 element/millisecond increments from there on (inclusive), the average millisecond value of those groups of 150 but ONLY of those milliseconds whose entries are 1 in M ('on'). For example, I need to look at the first 150 milliseconds in T, see which ones have a value of 1 in M, and then average them. Then I need to do it again with entries 151 to 300 in T, then 301 to 450, etc. etc. These new averages should also be stored in a new vector. The problem is, the number of corresponding 1's in M isn't going to be the same for every group of 150 milliseconds in T. (And yes, we are trying to average the actual value of the milliseconds, so the values we are using to average and the order of the entries in T will be the same).
My attempt:
It turns out there are only 53,583 random 1's in M (out of the 600,000 entries, the rest are 0). I used a 'find' operator to extract those entries from M that are a 1 into a new vector K that has the millisecond value corresponding from T. So K looks like a bunch of random numbers in ascending order, which is just a list of all the milliseconds in T who are 'on' (assigned a 1 in M).
So K looks something like [2 5 11 27 39 40 79 ...... 599,698 599,727 etc.] (all of the millisecond values who are a 1 in M).
So I have the vector K which is all of the values that I need to average in groups of 150, but the problem is that I need to go in groups of 150 based on the vector T (1 to 600,000), which means there won't always be the same number of 1's (or values in K) in every group of 150 milliseconds in T, which in turn means the number I need to divide by to get the average of each group is going to change for each group of 150. I know I need to use a loop to do the average millisecond value for every 150 entries, but how do I get the dividing number (the number of entries for each group of 150 who is assigned a 1 or 'on') to change on each iteration of the loop? Is there a way to bind T and M together so that they only use the requisite values from K whenever there is a 1 in M, and then just use a simple counter to average?
It's not a complicated problem, but it is very hard to explain. Sorry about that! I hope I explained as clearly as I could. Any help would be appreciated, although I'm sure you'll have questions first.
Thank you very much!
I think this should work OK.
sz = length(T);
n = sz / 150;
K = T.*M';
t = 1;
aver = zeros(n-1,1); % Your result vector
for i = 1:150:sz-150
aver(t) = mean(K(i:(i+150)-1));
t = t + 1;
end
-Rob