Monte Carlo on Arduino? Am I over-thinking it?

This is first off, a pure learning experience, and a thought experiment that I haven't thought too long on. So go easy on me. I just thought it would be a good one to toss out here and see what comes back.

I need Arduino to predict the future. :slight_smile: I couldn't find a Future library, so I'm going to have to do this myself.

The problem I have to solve is keeping the levels of two chemicals stable within limits in some water based on scheduled testing results. The problem is a simple one if you know the rate of consumption. You just match it. But in this case, there are a number of semi-random events (water removed, water added with unknown level etc. etc.) that are going to impact that consumption rate either positively or negatively. The overall natural rate can increase and decrease by small amounts randomly.

But the project has plenty of power, almost 2 gigs of leftover room on an SD card, and 2 hours to kill between actual doses. Why not give it something to do?

So what about a Monte Carlo approach? The events are random, but usually small in number, so the math to run each one of the infinite number of simulations is pretty simple. I know the basic statistical distribution of the events. In the two hours I have, I could run thousands of simulations with randomly placed but statistically level events happening, keep the results on the SD, and then average the results into a new dosing scheme. Perhaps even weight the average based on performance and keeping steady simulated levels.

The pieces seem doable. Running a simlation based on a known addition rate over a set amount of time with the last known consumption rate and some randomly timed and randomly sized change events. A little hard-coded math to get the stats right. Some hard coded math that either uses calculus or just runs through the simulation since it's simple and returns the levels at the end. If the level is too high or too low at the end, then you apply a random algorithm to take a step with the number and run the simulation again. In the data of dosing rates and final outcomes, there is an average that would be the best guess. That math doesn't seem too hard with so few variables. Average them and weight by how far off they were would be my first guess. I know enough about that math to tweak it by hand until I can think of a way for the computer to handle it.

The question isn't one that has to be solved. I'll be watching the numbers with a human eye the whole time. If I am up to the math, do you think Arduino could be up to such a computational challenge? Am I totally over-thinking this situation?

Delta_G:
I need Arduino to predict the future.

I had to force myself to keep reading at this point. It wasn't easy. :stuck_out_tongue:

If I am up to the math, do you think Arduino could be up to such a computational challenge?

The original IBM PC ran at 4.77 MHz (from memory). The Arduino runs at 16 MHz (the Uno does anyway). So pure computational power shouldn't be a real problem.

I should warn you however that SD card accesses are somewhat slow, and there is a finite lifetime of writes you can do. You might be better off getting more SRAM (there is an add-on card for the Mega that does that).

As for the maths part, I am not enough of an expert on Monte Carlo to say whether or not your scheme is feasible. But with plenty of computational power, a couple of hours to spare, and memory requirements that can be met, why not?

You asked if you were over thinking this. Possibly you are re-inventing the wheel. I think, maybe, what you want is a PID controller.

A PID controller calculates an "error" value as the difference between a measured process variable and a desired setpoint. The controller attempts to minimize the error by adjusting the process control inputs.

In the absence of knowledge of the underlying process, a PID controller is the best controller.

Although you know the chemistry the random factors mean you don't actually know the process so a PID controller may be the way to go.