Exemple #1
0
 public StencilSpeciesArr(Random random, Arr <int> field, int[] cellsPerProcessor, Mutator[] mutators)
 {
     this.random            = random;
     this.cellsPerProcessor = (int[])cellsPerProcessor.Clone();
     this.Field             = field.Clone();
     this.Mutators          = (Mutator[])mutators.Clone();
 }
Exemple #2
0
        public Matrix Inverse()
        {
            Matrix Temp = new Matrix(Col, Row);
            Matrix X    = new Matrix(Col, Row);
            Matrix E    = new Matrix(Col, Row);

            E.Unity();
            double kf;
            int    RANG = Row;

            Temp.Arr = (double[, ])Arr.Clone();

            for (int p = 0; p < RANG; p++)
            {
                for (int i = p + 1; i < RANG; i++)
                {
                    if (Temp.Arr[p, p] == 0.0)
                    {
                        kf = 0;
                    }
                    else
                    {
                        kf = -Temp.Arr[i, p] / Temp.Arr[p, p];
                    }

                    for (int j = p; j < RANG; j++)
                    {
                        Temp.Arr[i, j] = Temp.Arr[i, j] + kf * Temp.Arr[p, j];
                    }

                    for (int j = 0; j < RANG; j++)
                    {
                        E.Arr[i, j] = E.Arr[i, j] + kf * E.Arr[p, j];
                    }
                }
            }

            for (int k = 0; k < RANG; k++)
            {
                X.Arr[RANG - 1, k] = E.Arr[RANG - 1, k] / Temp.Arr[RANG - 1, RANG - 1];

                for (int i = RANG - 2; i >= 0; i--)
                {
                    double sum = 0.0;
                    for (int j = i + 1; j < RANG; j++)
                    {
                        sum = sum + X.Arr[j, k] * Temp.Arr[i, j];
                    }

                    X.Arr[i, k] = (E.Arr[i, k] - sum) / Temp.Arr[i, i];
                }
            }
            return(X);
        }
Exemple #3
0
        public bool NextStepX(bool progress)
        {
            Arr <int> field    = Field.Clone();
            int       overhead = Overhead(Field);

            Arr <int> f          = field.Clone();
            int       validCount = field.Length;

            while (validCount > 0)
            {
                // choose start pos and color
                int startPos = f.ValidPosition(random.Next(0, validCount), -1);
                f[startPos] = -1;
                validCount--;
                int color = field[startPos];

                Arr <int> f2          = f.Clone();
                int       validCount2 = validCount;

                while (validCount2 > 0)
                {
                    // select target at random
                    int targetPos = f2.ValidPosition(random.Next(0, validCount2), -1);
                    f2[targetPos] = -1;
                    validCount2--;

                    // swap
                    field.Swap(startPos, targetPos);

                    if ((progress && Overhead(field) < overhead) || (!progress && Overhead(field) <= overhead))
                    {
                        this.Field = field;
                        return(true);
                    }
                    else
                    {
                        field.Swap(startPos, targetPos);
                    }
                }
            }

            if (!this.IsValid)
            {
                throw new Exception("INVALID");
            }

            return(false);
        }
Exemple #4
0
        public bool NextStep(bool progress)
        {
            Arr <int> field    = Field.Clone();
            int       overhead = Overhead(Field);

            // remove invalid cells (cells with only neighbors of same color)
            Arr <int> f          = field.Clone();
            int       validCount = 0;

            for (int y = 0; y < f.H; y++)
            {
                for (int x = 0; x < f.W; x++)
                {
                    if (field.NeighborsWithValueHV(field[x, y], x, y, 1, true) == 4)
                    {
                        f[x, y] = -1;
                    }
                    else
                    {
                        validCount++;
                    }
                }
            }

            while (validCount > 0)
            {
                // choose start pos and color
                int startPos = f.ValidPosition(random.Next(0, validCount), -1);
                f[startPos] = -1;
                validCount--;
                int color = field[startPos];

                // remove all cells that have no neighbor with matching color and cells with same color
                Arr <int> f2          = f.Clone();
                int       validCount2 = validCount;
                for (int y = 0; y < f.H; y++)
                {
                    for (int x = 0; x < f.W; x++)
                    {
                        if (f[x, y] != -1 && (f[x, y] == color || f.NeighborsWithValueHV(color, x, y, 1, false) == 0))
                        {
                            f2[x, y] = -1;
                            validCount2--;
                        }
                    }
                }

                while (validCount2 > 0)
                {
                    // select target at random
                    int targetPos = f2.ValidPosition(random.Next(0, validCount2), -1);
                    f2[targetPos] = -1;
                    validCount2--;

                    // swap
                    field.Swap(startPos, targetPos);

                    if ((progress && Overhead(field) < overhead) || (!progress && Overhead(field) <= overhead))
                    {
                        this.Field = field;
                        return(true);
                    }
                    else
                    {
                        field.Swap(startPos, targetPos);
                    }
                }
            }

            if (!this.IsValid)
            {
                throw new Exception("INVALID");
            }

            return(false);
        }