Model.Observe C# (CSharp) Method

Observe() private method

private Observe ( ) : bool?
return bool?
    bool? Observe()
    {
        double min = 1E+3, sum, mainSum, logSum, noise, entropy;
        int argminx = -1, argminy = -1, amount;
        bool[] w;

        for (int x = 0; x < FMX; x++) for (int y = 0; y < FMY; y++)
            {
                if (OnBoundary(x, y)) continue;

                w = wave[x][y];
                amount = 0;
                sum = 0;

                for (int t = 0; t < T; t++) if (w[t])
                    {
                        amount += 1;
                        sum += stationary[t];
                    }

                if (sum == 0) return false;

                noise = 1E-6 * random.NextDouble();

                if (amount == 1) entropy = 0;
                else if (amount == T) entropy = logT;
                else
                {
                    mainSum = 0;
                    logSum = Math.Log(sum);
                    for (int t = 0; t < T; t++) if (w[t]) mainSum += stationary[t] * logProb[t];
                    entropy = logSum - mainSum / sum;
                }

                if (entropy > 0 && entropy + noise < min)
                {
                    min = entropy + noise;
                    argminx = x;
                    argminy = y;
                }
            }

        if (argminx == -1 && argminy == -1)
        {
            observed = new int[FMX][];
            for (int x = 0; x < FMX; x++)
            {
                observed[x] = new int[FMY];
                for (int y = 0; y < FMY; y++) for (int t = 0; t < T; t++) if (wave[x][y][t])
                        {
                            observed[x][y] = t;
                            break;
                        }
            }

            return true;
        }

        double[] distribution = new double[T];
        for (int t = 0; t < T; t++) distribution[t] = wave[argminx][argminy][t] ? stationary[t] : 0;
        int r = distribution.Random(random.NextDouble());
        for (int t = 0; t < T; t++) wave[argminx][argminy][t] = t == r;
        changes[argminx][argminy] = true;

        return null;
    }