示例#1
0
        public static string GetFunctionResultInfo(AbstractFunction function, AssertionInfo assertion)
        {
            var paras = function.Paras;

//            for (var i = 0; i < paras.Count; i++)
//                paras[i].Value = assertion.InputValues[i];
//
//            return $" execution path: {function.ExecutionPath} | result: {function.Result}";
            return("");
        }
示例#2
0
        //得到边界测试数据集
        public static List <AssertionInfo> GetAssertions(AbstractFunction function)
        {
            var boundaryValuesOfAllParas = function.Paras.Select(GetBoundaryValuesOfPara).ToList();
            var middleValues             = boundaryValuesOfAllParas.Select(v => v["middle"]).ToList();
            var assertions = new List <AssertionInfo>();

            for (var i = 0; i < boundaryValuesOfAllParas.Count; i++)
            {
                foreach (var value in boundaryValuesOfAllParas[i].Where(v => v.Key != "middle").Select(p => p).ToList())
                {
                    var assertion = new AssertionInfo();
                    assertion.InputValues.AddRange(middleValues);
                    assertion.InputValues[i] = value.Value;
                    assertions.Add(assertion);
                }
            }

            return(assertions);
        }
        public static List <AssertionInfo> GetAssertions(GaParameterInfo gaParameters, AbstractFunction function,
                                                         List <string> targetPaths)
        {
            var assertions = new List <AssertionInfo>();
            // 写文件的准备
            const string logPath   = @"c:\#GA_DEMO\galog.txt";
            var          writer    = new StreamWriter(logPath);
            var          stopwatch = new Stopwatch();
            var          builder   = new StringBuilder();

            stopwatch.Start();
            foreach (var targetPath in targetPaths)
            {
                var found = false;
                // 下面这句漏掉会出错!
                function.TargetPath = targetPath;
                //新建一个种群
                var population = new Population(gaParameters, function);
                //随机生成染色体
                population.RandomGenerateChromosome();

                for (var i = 0; i < gaParameters.GenerationQuantity; i++)
                {
                    var maxFitness  = population.Chromosomes.Max(c => c.Fitness);
                    var mostFittest = population.Chromosomes.First(c =>
                                                                   Equals(c.Fitness, maxFitness));
                    stopwatch.Stop();
                    // 得到当前代最优染色体信息
                    var line =
                        $"{$"{i + 1}",-6} | {$"value(s): {string.Join(" ", mostFittest.DecodedSubValues.ToArray())}",-30} | {$"target path: {function.TargetPath}",-30} | {$"execution path: {mostFittest.ExecutionPath}",-30} | {$"fitness: {mostFittest.Fitness}",-10} | {$"result: {mostFittest.Result}",-20}";
                    builder.AppendLine(line);
                    //以下为终止条件
                    if (mostFittest.ExecutionPath == targetPath)
                    {
                        found = true;
                        //将找到的数据添加到测试数据集中
                        var assertion = new AssertionInfo();
                        assertion.InputValues.AddRange(mostFittest.DecodedSubValues.Select(v => v).ToList());
                        assertions.Add(assertion);
                        builder.AppendLine("FOUND".PadLeft(130 / 2, '-').PadRight(130, '-'));
                        break;
                    }
                    //进化
                    population.Evolve(gaParameters.SelectionType);
                    stopwatch.Start();
                }
                stopwatch.Stop();
                if (!found)
                {
                    builder.AppendLine("NOT FOUND".PadLeft(130 / 2, '-').PadRight(130, '-'));
                }

                stopwatch.Start();
            }

            stopwatch.Stop();
            builder.AppendLine($"total time cost: {stopwatch.ElapsedMilliseconds} ms");
            writer.Write(builder.ToString());
            writer.Close();

            return(assertions);
        }
示例#4
0
        public static List <AssertionInfo> GetAssertions(GaParameterInfo gaParameters, AbstractFunction function,
                                                         List <string> targetPaths)
        {
            var          rnd        = new Random();
            var          builder    = new StringBuilder();
            var          values     = new double[function.Paras.Count];
            var          assertions = new List <AssertionInfo>();
            const string logPath    = @"c:\#GA_DEMO\rndlog.txt";
            var          writer     = new StreamWriter(logPath);
            var          stopwatch  = new Stopwatch();

            stopwatch.Start();
            foreach (var targetPath in targetPaths)
            {
                var found = false;
                // 下面这句漏掉会出错!
                function.TargetPath = targetPath;
                for (long i = 0; i < int.MaxValue; i++)
                {
                    // 随机生成所有实参的值
                    foreach (var para in function.Paras)
                    {
                        values[function.Paras.IndexOf(para)] =
                            (int)(rnd.NextDouble() * (para.UpperBound - para.LowerBound) + para.LowerBound);
                    }

                    var result        = function.OriginalFunction(values);
                    var executionPath = function.StubbedFunction(values);
                    stopwatch.Stop();
                    var line =
                        $"{$"{i + 1}",-6} | {$"value(s): {string.Join(" ", values.ToArray())}",-30} | {$"target path: {function.TargetPath}",-30} | {$"execution path: {executionPath}",-30} | {$"result: {result}",-20}";
                    builder.AppendLine(line);
                    //以下为终止条件
                    //如果当前执行路径包含任何目标路径
                    if (executionPath == targetPath)
                    {
                        found = true;
                        //将找到的数据添加到测试数据集中
                        var assertion = new AssertionInfo();
                        assertion.InputValues.AddRange(values.ToList());
                        assertions.Add(assertion);
                        builder.AppendLine("FOUND".PadLeft((line.Length - 5) / 2, '-').PadRight(line.Length, '-'));
                        break;
                    }
                    stopwatch.Start();
                }
                stopwatch.Stop();
                if (!found)
                {
                    builder.AppendLine("NOT FOUND".PadLeft(130 / 2, '-').PadRight(130, '-'));
                }

                stopwatch.Start();
            }

            stopwatch.Stop();
            builder.AppendLine($"total time cost: {stopwatch.ElapsedMilliseconds} ms");
            writer.Write(builder.ToString());
            writer.Close();

            return(assertions);
        }