phploopscalculation

Is there a better way to reduce number of iterations without missing a given condition?


I'm working on a project which requires a lot of iterations and calculations. We're speaking about something around 2000 iterations, each of them doing between 15 and 40 different calculations. This.. takes a lot of time, and I'm trying to reduce that time.

I'll explain how it works as clearly as I can. It's about finding the best heat exchanger solution for given inputs (inlet temperature, pressure, power, stuff like that). To do so, I must compare all the different plates (each plates have different size, different properties), and they have four important things : a type (A, B or mixed), a fraction (it'll be 100% if the type is A, 0 if the type is B, and any multiple of 5 between 0 and 100, except 50, for the mixed type) a min number, and a max number.

So now let's say we have a plate which type is A, min number is 7 and max number is 93. Fraction will be 100% since it's defined by the plate type. In this case, my loop will look like this :

for ($nb = $plate['nb_min']; $nb <= $plate['nb_max']; $nb += 2) { ... }

Which will already do 44 iterations.

If a plate's type is B, the loop will be similar. But in the case of the mixed one (with the same min and max nb), I have to do this :

for ($nb = $plate['nb_min']; $nb <= $plate['nb_max']; $nb += 2) {
   for ($frac = 5; $frac <= 95; frac += 5) {
      ...
   }
}

Which results in 792 iterations. For a single plate, and there are many more.

The condition which break the iterations (we stop at the first result for each plate so that we have the margin the closest to 0, and the cheapest solution - the more plates there is, the more expensive the solution is) is if a margin is positive and if the differential pressures are in acceptable range (the user defines this range with an input) :

if ($margin > 0 && $dp_prim < $postData['dp_max_prim'] && $dp_sec < $postData['dp_max_sec']) {
   // current iteration is added to the array of solutions, loop is broken and we can iterate throught the next plate
}

So naturally I tried to read all the margins for each iterations, to see if I could just notice a pattern in it. But it failed, plates are very different, and the difference of margin for different numbers of plate is never the same.

What I mean is that the margin for a plate can be -70, then -67 on the next iteration, then -60, etc. But for another plate, it can be -70, then -30, etc.

One thing I noticed is that the number of plates is generally in the higher numbers of plates. So I tried to inverse the loop, starting with the max nb and going down. It gave me way faster results, but obviously the margin is super high and the price as well, since it's not the very first solution to reach a positive margin.

Another "solution" I found, is that I just skip some iterations if the margin is too low. Like this :

if ($margin < -50) {
   $nb += 6;
   continue;
}
if ($margin < -30) {
   $nb += 2;
   continue;
}

But still, there's a risk that I totally miss the margin closest to 0. I have no idea on how to correctly reduce the calculation time / iteration number and still getting the first positive margin.


Solution

  • What I did, as suggested by user Martin Brown, is a partial binary search. Basically, I take the value in the middle of the array, apply my function to it, and according to the result, I take the top or bottom array as my new array to iterate on. I did this step twice, and the calculation time literally halved.

    I couldn't do it forever because as suggested in my post I absolutely must find the first value which meets the conditions, and many values do meet them.