Beispiel #1
0
        public static object[,] gxCross(
            [ExcelArgument(AllowReference = true)] object dataColArg,
            [ExcelArgument(AllowReference = true)] object idxColArg,
            [ExcelArgument(AllowReference = true)] object opt_mutprobArg
            )
        {
            int row  = 0; // place outside loops to enhance error reporting
            int rows = 1;

            object[,] result = null;

            try
            {
                var dataObjArr = (object[, ])(((ExcelReference)dataColArg).GetValue());
                var idxObjArr  = (object[, ])(((ExcelReference)idxColArg).GetValue());

                rows   = idxObjArr.GetLength(0);
                result = new object[rows, 1];

                // assume a mutation rate, but change it if one is specified
                int mutprob = 10; // 1/10
                if (!(opt_mutprobArg is ExcelMissing))
                {
                    switch (opt_mutprobArg)
                    {
                    case double d when opt_mutprobArg is double: // double was specified
                        mutprob = Math.Min(1, (int)(1 / d));     // turns 1/n into n
                        break;

                    case double d when((ExcelReference)opt_mutprobArg).GetValue() is double: // ref to double was specified
                        mutprob = Math.Min(1, (int)(1 / d));                                 // turns 1/n into n

                        break;
                    }
                }

                // return a cross (with mutation) of the specified population members
                // if indicies are the same, copy with no mutation
                for (row = 0; row < rows; row++)
                {
                    var i1 = Convert.ToUInt16((double)(idxObjArr[row, 0]));
                    var i2 = Convert.ToUInt16((double)(idxObjArr[row, 1]));
                    if (i1 == i2)
                    {
                        result[row, 0] = (string)(dataObjArr[i1, 0]);
                    }
                    else
                    {
                        byte[] barr1 = System.Convert.FromBase64String((string)(dataObjArr[i1, 0]));
                        byte[] barr2 = System.Convert.FromBase64String((string)(dataObjArr[i2, 0]));
                        string str   = System.Convert.ToBase64String(Support.cross(barr1, barr2, mutprob));
                        result[row, 0] = (string)str;
                    }
                }
            }
            catch
            {
                // fill remainder with error
                if (result == null)
                {
                    result = new object[rows, 1];
                }
                if (row == rows)
                {
                    row = 0;              // reset?
                }
                while (row < rows)
                {
                    result[row++, 0] = ExcelError.ExcelErrorValue;
                }
            }

            return(result);
        }
Beispiel #2
0
 public static ushort gxPerm(ushort n, ushort k)
 {
     return(Support.permutation_nbits(n, k));
 }