Creating robust real-time monitors for variables - dynamic

We can create a real-time monitor for a variable like this:
CreatePalette#Panel#Row[{"x = ", Dynamic[x]}]
(This is more interesting and useful if x happens to be something like $Assumptions. It's so easy to set a value and then forget about it.)
Unfortunately this stops working if the kernel is re-launched (Quit[], then evaluate something). The palette won't show changes in the value of x any more.
Is there a way to do this so it keeps working even across kernel sessions? I find myself restarting the kernel quite often. (If the resulting palette causes the kernel to be automatically started after Quit that's fine.)
Update: As mentioned in the comments, it turns out that the palette ceases working only if we quit by evaluating Quit[]. When using Evaluation -> Quit Kernel -> Local, it will keep working.
Link to same question on MathGroup.

I can only guess, because on my Ubuntu here the situations seems buggy. The trick with the Quit from the menu like Leonid suggested did not work here. Another one is: on a fresh Mathematica session with only one notebook open:
Dynamic[x]
x = 1
Dynamic[x]
x = 2
gives as expected
2
1
2
2
Typing in the next line Quit, evaluating and typing then x=3 updates only the first of the Dynamic[x].
Nevertheless, have you checked the command
Internal`GetTrackedSymbols[]
This gives not only the tracked symbols but additionally some kind of ID where the dynamic content belongs. If you can find out, what exactly these numbers are and investigate in the other functions you find in the Internal context, you may be able to add your palette Dynamic-content manually after restarting the kernel.
I thought I had something like that with
Internal`SetValueTrackExtra
but I'm currently not able to reproduce the behavior.

#halirutan's answer jarred my memory...
Have you ever come across: Experimental/ref/ValueFunction? (documentation address)
Although the documentation contains no examples, the 'more information' section provides the following tidbit:
The assignment ValueFunction[symb] = f specifies that whenever
symb gets a new value val, the expression f[symb,val] should be
evaluated.

Related

Is there a way to turn off a vehicle signal in SUMO?

I know that you can turn on a vehicle signal (for example, the left indicator) in traci using:
traci.vehicle.setSignals(vehID, int)
where the integer related to the specific signal can be found using the following link (https://sumo.dlr.de/docs/TraCI/Vehicle_Signalling.html#signaling), but is there a way of turning off a specific signal that would be otherwise turned on by the program (i.e., a setSignalOff)?
I think that there is a function in the underlying C++ code (switchOffSignal() in MSVehicle.h) but there doesn't appear to be a traci command that turns off a specific signal.
I appreciate that it is (generally) a pleasant visual aesthetic and has no impact on vehicle behaviour, but it would be very useful for what I am trying to do!
Switching off signals should work from traci. By using sometihng like traci.vehicle.setSignals("ego", 0), I can switch them off. Be aware that this will be reset after the step, so you may have to do that in every timestep.
So, Michael is right in that:
traci.vehicle.setSignals("ego", 0)
should turn off all signals (although the signals still appeared on for me visually, which confused me initially).
To turn off individual signals but keep the others on you need to:
For all the "on" signals find the value of 2^n, where n is the bit integer (which can be found using the following link: https://sumo.dlr.de/docs/TraCI/Vehicle_Signalling.html)
Sum all these 2^n values (let's call this variable x) and use this value in the setSignals function: traci.vehicle.setSignals("ego", x).
So for example, if we want the brake light, the right indicator and the high beam on (but all the other signals off) we would do:
RightIndicatorValue = pow(2,0)
BrakeLightValue = pow(2,3)
HighBeamValue = (2,6)
SignalValue = RightIndicatorValue + BrakeLightValue + HighBeamValue
traci.vehicle.setSignals(("ego", SignalValue)

Use program parameters for graph drawing - TI92+

I have a problem with a program on my TI92+.
It does work without a problem on a TI92, though.
I want to draw a graph, that I enter as a program argument, e.g. test(x^2)
Program goes like this:
test(var1)
Prgm
var1->k
k->y1(x)
ZoomStd
setMode("split 1 app","graph"):Pause
EndPrgm
On the TI92 it shows me a simple x^2 function, on the TI92+ I get "Undefined Variable" as an error message. Interestingly enough, when I enter "k" on the home screen, I get x^2 as an output. So the variable is there.
I am in the main folder and this happens with a reset calculator as well.
I understand that TI changed something about the variables between TI92 and TI92+, you can have local variables, but this is no local variable.
And finally: this is just a test program I am using to demonstrate my problem, of course I know I could just type x^2 into the y= editor and hey presto. But a program I have written years ago doesn't work and I would like to know, why and where I have to change something.
If you look at the Y= Editor, does it display y1=k? On my TI-89 Titanium (which I understand uses similar software), k→y1(x) literally stores k to y1. This is a problem since it tries to substitute all instances of x in y1 (of which there are none) with the value passed to y1. Then, since k is an expression type, not a number, the calculator refuses to graph it. I'm still trying to find a work around for this.

Weird behavior of the input command in Scilab

I want my students to program a little game with Scilab like this one :
a=ceil(100*rand())
disp("I think of a number between 1 and 100. Which one ?")
guess=0
while(guess<>a) do
guess= input("Guess :")
if (a>guess) then
disp("+")
elseif (a<guess) then
disp("-")
end
end
disp("You got it")
But there are some weird behavior with the first inputs and the lasts. Just after the first input, sometimes I don't have an answer (but sometimes I do), I am asked "Guess" again, but this time with a usual prompt --> on the console. I can put anything, it doesn't seem to matter for the program. But the program is not over though, just after it returns to the expected behavior.
At the end, after the "You got it", there seems to be some inputs around. I got one or two prompts before I got the --> again.
Here is what the console may look like :
Guess :
50
Guess :
--> 50
Guess :
50
+
Guess :
75
-
Guess :
I can't figure out why Scilab react like this.
I've experienced that a lot, and this is a known bug in input() of Scilab 6.0.0:
http://bugzilla.scilab.org/show_bug.cgi?id=15069
http://bugzilla.scilab.org/show_bug.cgi?id=14376
http://bugzilla.scilab.org/show_bug.cgi?id=14424
The first link also presents a workaround, but I wouldn't say it is suitable for students. As #Desire couldn't reproduce that error, I recommend that you downgrade you Scilab to version 5.5.1, and also tell your students to do it too.

misunderstanding how the threads work

I have a problem, big problem with threads in vb.net
First of all I want to tell that I didn't work with threads before (just on the school), I read lot of pages about it, but none of them could help me for my problem.
My main question here is understand the logic, and after if is possible, solve the problem that I have, both are related, then I going to explain the problem.
The code hasn't comments and/or documentation related, and is a program developed lot of years ago and the guy that did this is not working on the office, nobody knows how it works :S
I have a list called listOfProccess, and when is only 1 works fine.
in the callback function in QueueUserWorkItem fill the information about p, then execute the thread, I suppose
That list contain an array with information type
listOfProccess[].type = 'a/b/c/d/e/f/g/'
also the list include an ID.
Code:
If listOfProccess.Count > 0 Then
Threading.ThreadPool.SetMinThreads(1, 1)
Threading.ThreadPool.SetMaxThreads(4, 4)
For Each p In listOfProccess
Try
Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(Object p.function))
Catch e As Exception
sendMail("mail#mail", "alerts#mail.ie", "", e.StackTrace)
End Try
Next
Problems:
I have two problems here:
Sometimes execute an item in the list i.e. 'a' in a infinite loop, and expends all resources of the machine, but if i close and restart again, works, I didn't know if is a problem with the threads or not, sincerely I think that is other thing, because this problem starts two or tree weeks ago, and the program still running during a year.
This one i think is related about threads, if I have two (or more) p on the list like this:
p[1].type = 'a/b/c/d/e/f/g/'
p[1].ID = 1
p[2].type = 'ww/xx/ff/yy/aa/rr/'
p[2].ID =2
when the system execute something like this, the way that it follows is 'random' ie. takes for the first one, a, b,c and after this it does ww, and come back to the first. the problem is bigger if I have more items in the list, like 4 or 5; this is not a very big problem, because the program works, it not works 100% fine, but works, is more to try the understand why it works on this way.
Any help is welcome.
The second problem is a race condition problem, as you can't guarantee the order of the execution for the threads, there is a non-zero probability that your threads will replace each other's values. There are a lot of wayss to solve this issue: algorythms (either lock-oriented and lock-free), synchronization techniques and so on, and there is no silver-bullet solution for this.
First problem is unclear for me, as I can't understand what exactly do you mean by a infinite loop - may be this can happen if you link items to deleted (from other thread) ones and there is no way to go out of your task, as the links in a list are broken. This still should be solved with sychronization.
I think your should start with MSDN articles or some book about multithreading in general, and after that you should split your program step by step for a small parts you understand.
Update:
p.function - about the infinite loop you should consider the code of this delegate. If there is a condition to restart work, you should check a recursion limit. For example, if there is an optimistic locking algorythm, your code can find out that the update it tried isn't valid, and restart it. And if the links are broken, it will never end it's task, and will stay forever in an infinite loop.

How to use MPI_Barrier with another communicator?

I'm a bit of newbie on MPI programming ( mpich2 fedora ).
I'm writing be cause, i got Dead lock when use MPI_Barrier with another comunicator different to MPI_COMM_WORLD.
I make 2 communicators like this:
MPI_Comm_split (MPI_COMM_WORLD, color, rank, &split_comm);
If i put a MPI_Barrier where all colors can pass, it'll be all right.
But if i put a MPI_Barrier where only color == 1 can pass, i got Dead lock.
How to use MPI_Barrier with another communicator ?
I was also using MPI_Bcast () (with another different communicator MPI_COMM_WORLD) but it wasn't blocked when nobody call MPI_Bcast too. Can one different communicator to MPI_COMM_WORLD synchronise your own processes?
It would be helpful if you could post a code snippet. It's hard to debug a deadlock from your words alone.
At any rate, you pass the communicator you want to block as an argument to MPI_Barrier:
http://mpi.deino.net/mpi_functions/mpi_barrier.html
http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Barrier.html
MPI_Bcast is a blocking function. So, if one or more ranks do not reach the MPI_Bcast call, then you could have a deadlock.
Remember that MPI_COMM_WORLD includes all ranks, even after the MPI_Comm_Split call.