예제 #1
0
        public override void modify(SolutionProgram p)
        {
            List <Node> allnodes = p.getAllNodes().ToList();

            if (allnodes.Count == 0)
            {
                return;
            }

            Node selectedNode = allnodes[r.Next(allnodes.Count)];

            if (selectedNode.successors.count == 0)
            {
                return;
            }

            int successorIndex = r.Next(selectedNode.successors.count);

            if (profile is NodeTypeRelativizedFrequencyProfile)
            {
                selectedNode.setSuccessor(SearchMethodsSupport.createRandomTree(selectedNode.successors.GetSlot(successorIndex).argumentClass, (NodeTypeRelativizedFrequencyProfile)profile, treeDepth, selectedNode.type), successorIndex);
            }
            else
            {
                selectedNode.setSuccessor(SearchMethodsSupport.createRandomTree(selectedNode.successors.GetSlot(successorIndex).argumentClass, profile, treeDepth), successorIndex);
            }
        }
예제 #2
0
        public override SolutionProgram search(TimeSpan timeLimit)
        {
            watch.Start();

            SolutionProgram current = new SolutionProgram((DirectiveNode)SearchMethodsSupport.createRandomTree(NodeClass.directive, profile, 3));
            int             steps   = 0;

            while (watch.Elapsed < timeLimit)
            {
                steps++;
                mutatedPrograms.Clear();
                for (int i = 0; i < mutationsInStep; i++)
                {
                    SolutionProgram mutated = (SolutionProgram)current.Clone();
                    mutator.modify(mutated);
                    mutatedPrograms.Add(new EvaluatedEntity <SolutionProgram>(mutated, 0));
                }
                mutatedPrograms.Add(new EvaluatedEntity <SolutionProgram>(new SolutionProgram((DirectiveNode)SearchMethodsSupport.createRandomTree(NodeClass.directive, profile, 3)), 0));
                addEvaluation(batchSize, mutatedPrograms);
                var selected = selector.select(mutatedPrograms);
                if (selected.value >= 99)                       //more than 99% accuracy reached -> ending the search.
                {
                    printMSG(selected.value.ToString());
                    return(selected.item);
                }

                printMSG("Steps: " + steps);
                printMSG("Best evaluation: " + mutatedPrograms.Max(p => p.value));
                printMSG("Best program: " + mutatedPrograms.Where(p => p.value >= mutatedPrograms.Max(q => q.value)).First().item.ToString());
            }
            watch.Stop();
            return(mutatedPrograms.Where(p => p.value >= mutatedPrograms.Max(q => q.value)).First().item);
        }
예제 #3
0
        /// <summary>
        /// Simple program for testing whether given number is a prime number
        /// </summary>
        /// <returns></returns>
        static SolutionProgram getProgram2()
        {
            SolutionProgram p = new SolutionProgram(new List <DirectiveNode>()
            {
                new directiveAssign(new NumericConstant(0), new InputNode(0)),
                new directiveAssign(new NumericConstant(1), new NumericConstant(2)),
                new directiveAssign(new NumericConstant(2), new NumericConstant(1)),
                new directiveWHILE(
                    new BoolANDOperator(
                        new BoolLessEqualOperator(
                            new ValueGetter(new NumericConstant(1)),
                            new DivOperator(new ValueGetter(new NumericConstant(0)), new ValueGetter(new NumericConstant(1)))

                            ),
                        new BoolEqualsOperator(
                            new ValueGetter(new NumericConstant(2)),
                            new NumericConstant(1))
                        ),
                    new directivesSequence(new List <DirectiveNode>()
                {
                    new directiveIF(
                        new BoolEqualsOperator(new ModOperator(new ValueGetter(new NumericConstant(0)), new ValueGetter(new NumericConstant(1))), new NumericConstant(0)),
                        new directiveAssign(new NumericConstant(2), new NumericConstant(0))),
                    new directiveIncrement(new NumericConstant(1))
                })),
                new directiveSetOutput(new NumericConstant(0), new ValueGetter(new NumericConstant(2)))
            });

            return(p);
        }
예제 #4
0
        public override SolutionProgram search(TimeSpan timeLimit)
        {
            watch = Stopwatch.StartNew();
            var bestProgram     = new SolutionProgram((DirectiveNode)SearchMethodsSupport.createRandomTree(NodeClass.directive, profile, maxDepth));
            var bestPerformance = evaluate(batchSize, bestProgram);

            Console.WriteLine("best program: ");
            Console.WriteLine(bestProgram.ToString());
            Console.WriteLine($"performance: {bestPerformance}");

            while (watch.Elapsed < timeLimit)
            {
                var program     = new SolutionProgram((DirectiveNode)SearchMethodsSupport.createRandomTree(NodeClass.directive, profile, maxDepth));
                var performance = evaluate(batchSize, program);
                if (performance > bestPerformance)
                {
                    bestPerformance = performance;
                    bestProgram     = program;
                    Console.WriteLine("best program: ");
                    Console.WriteLine(bestProgram.ToString());
                    Console.WriteLine($"performance: {bestPerformance}");
                }
            }
            return(bestProgram);
        }
예제 #5
0
        /// <summary>
        /// Evaluates given solution program using given samplesGenerator. Prints a comparison of desiredOutputs to realOutputs and computes success rate.
        /// </summary>
        /// <param name="generator"></param>
        /// <param name="p"></param>
        /// <param name="samplesCount"></param>
        public static void runEvaluation(TrainingSamplesGenerator generator, SolutionProgram p, int samplesCount, bool quiet = false)
        {
            Interpret i            = new Interpret();
            int       currectCount = 0;

            foreach (var sample in generator.generateSamples(samplesCount))
            {
                p.evaluate(sample, i);
                Printing.PrintMsg(sample.ToString() + "\treal output: " + sample.realOutputs2String + " " + (sample.isCorrectlyAnswered ? "OK" : "WRONG"), quiet);
                if (sample.isCorrectlyAnswered)
                {
                    currectCount++;
                }
            }
            Console.WriteLine($"Success rate: {currectCount} out of {samplesCount} ({((double)(currectCount*100) / samplesCount).ToString("0.##")}%)");
        }
예제 #6
0
        /// <summary>
        /// Returns the percentage of correctly answered tests
        /// </summary>
        /// <param name="batchSize"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        protected virtual double evaluate(int batchSize, SolutionProgram p)
        {
            int currectCount = 0;

            //var samples = generator.generateSamples(batchSize).ToList();
            foreach (var sample in generator.generateSamples(batchSize))
            {
                p.evaluate(sample, i);
                bool isCorrect = sample.isCorrectlyAnswered;
                printMSG(sample.ToString() + "\treal output: " + sample.realOutputs2String + " " + (isCorrect ? "OK" : "WRONG"), true);
                if (isCorrect)
                {
                    currectCount++;
                }
            }
            return((double)(currectCount * 100) / batchSize);
        }
예제 #7
0
        static void Main2(string[] args)
        {
            //SolutionProgram p = getProgram0();
            //SolutionProgram p = getProgram1();
            SolutionProgram p = getProgram2();

            Interpret i = new Interpret();

            i.numericInputs = new List <int>()
            {
                97
            };
            Console.WriteLine(p.ToString());
            p.execute(i);
            new GraphVisualizer().visualizeDialog(ProgramTreeDrawer.createGraph(p));
            Console.WriteLine("inputs: " + string.Join(" ", i.numericInputs));
            Console.WriteLine("outputs: " + string.Join(" ", i.outputs));
        }
예제 #8
0
        public override SolutionProgram search(TimeSpan timeLimit)
        {
            watch = Stopwatch.StartNew();
            SolutionProgram bestProgram     = null;
            double          bestPerformance = -1d;

            int depth = 0;

            while (true)
            {
                depth++;
                Console.WriteLine($"Depth: {depth}");
                //Console.WriteLine(SearchMethodsSupport.enumerateAllTrees(NodeClass.directive, depth).Count());
                foreach (var item in SearchMethodsSupport.enumerateAllTrees(NodeClass.directive, depth))
                {
                    if (watch.Elapsed > timeLimit)
                    {
                        return(bestProgram);
                    }

                    var program     = new SolutionProgram((DirectiveNode)item);
                    var performance = evaluate(batchSize, program);
                    //Console.WriteLine(program.ToString());
                    //Console.WriteLine($"performance: {performance}");

                    if (performance > bestPerformance)
                    {
                        bestPerformance = performance;
                        bestProgram     = program;
                        Console.WriteLine("best program: ");
                        Console.Write(bestProgram.ToString());
                        Console.WriteLine($"performance: {bestPerformance} %");
                        Console.WriteLine();
                        if (performance >= 100)
                        {
                            Console.WriteLine("solution found");
                            return(bestProgram);
                        }
                    }
                }
            }
        }
예제 #9
0
        /// <summary>
        /// To test the Clone() method
        /// </summary>
        /// <param name="args"></param>
        static void Main3(string[] args)
        {
            NodeTypeFrequencyProfile p = NodeTypeFrequencyProfile.createProfile(new List <SolutionProgram> {
                getProgram0(), getProgram1(), getProgram2()
            }, true);

            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine("New random program:");
                SolutionProgram pr = new SolutionProgram((DirectiveNode)SearchMethodsSupport.createRandomTree(NodeClass.directive, p, 10));
                Console.WriteLine(pr.ToString());
                new GraphVisualizer().visualizeDialog(ProgramTreeDrawer.createGraph(pr));
                Console.WriteLine();
                Console.WriteLine("CLONE:");
                SolutionProgram clon = (SolutionProgram)pr.Clone();
                Console.WriteLine(clon.ToString());
                new GraphVisualizer().visualizeDialog(ProgramTreeDrawer.createGraph(clon));
                Console.WriteLine();
            }
        }
예제 #10
0
        /// <summary>
        /// Program enumerates numbers from 0 to given number and sums every second number in such list. Returns the sum.
        /// </summary>
        /// <returns></returns>
        static SolutionProgram getProgram0()
        {
            SolutionProgram p = new SolutionProgram(new List <DirectiveNode>()
            {
                new directiveFOR(new NumericConstant(0), new InputNode(0), new directiveAddLast(
                                     new NumericConstant(0), new ValueGetter(new NumericConstant(0)))),

                new directiveAssign(new NumericConstant(1), new NumericConstant(0)),
                new directiveAssign(new NumericConstant(2), new NumericConstant(0)),
                new directiveWHILE(new BoolLessOperator(new ValueGetter(new NumericConstant(1)), new ListSizeGetter(new NumericConstant(0))),
                                   new directivesSequence(new List <DirectiveNode>()
                {
                    new directiveAssign(new NumericConstant(2), new PlusOperator(new ValueGetter(new NumericConstant(2)), new ListValueGetter(new NumericConstant(0), new ValueGetter(new NumericConstant(1))))),
                    new directiveIncrement(new NumericConstant(1)),
                    new directiveIncrement(new NumericConstant(1)),
                }
                                                          )),
                new directiveSetOutput(new NumericConstant(0), new ValueGetter(new NumericConstant(2)))
            });

            return(p);
        }
예제 #11
0
        public static Graph createGraph(SolutionProgram p)
        {
            Graph     g        = new Graph();
            graphNode previous = createStartNode();

            g.AddNode(previous);

            DirectiveNode d    = p.getEntryPoint();
            graphNode     next = generateSubtree(d, g);

            g.AddEdge(previous.Id, next.Id);

            /*
             * while (d != null)
             * {
             *      graphNode next = generateSubtree(d, g);
             *      g.AddNode(next);
             *      g.AddEdge(previous.Id, next.Id);
             *      previous = next;
             *      d = d.getNextDirective();
             * }
             */
            return(g);
        }
예제 #12
0
        /// <summary>
        /// Program that repeatedly transforms given number by n % 2 == 0 ? n = n/2 : n = n*3 + 1, and count the number of steps required before n reaches value 1.
        /// </summary>
        /// <returns></returns>
        static SolutionProgram getProgram1()
        {
            SolutionProgram p = new SolutionProgram(new List <DirectiveNode>()
            {
                new directiveAssign(new NumericConstant(0), new InputNode(0)),
                new directiveAssign(new NumericConstant(1), new NumericConstant(0)),
                new directiveWHILE(
                    new BoolLogicNotOperator(
                        new BoolEqualsOperator(
                            new ValueGetter(new NumericConstant(0)),
                            new NumericConstant(1))),
                    new directivesSequence(new List <DirectiveNode>()
                {
                    new directiveIFELSE(
                        new BoolEqualsOperator(new ModOperator(new ValueGetter(new NumericConstant(0)), new NumericConstant(2)), new NumericConstant(0)),
                        new directiveAssign(new NumericConstant(0), new DivOperator(new ValueGetter(new NumericConstant(0)), new NumericConstant(2))),
                        new directiveAssign(new NumericConstant(0), new PlusOperator(new MultiplyOperator(new ValueGetter(new NumericConstant(0)), new NumericConstant(3)), new NumericConstant(1)))),
                    new directiveIncrement(new NumericConstant(1))
                })),
                new directiveSetOutput(new NumericConstant(0), new ValueGetter(new NumericConstant(1)))
            });

            return(p);
        }
예제 #13
0
 public abstract void modify(SolutionProgram p);