Exemplo n.º 1
0
        private void findResultButton_Click(object sender, EventArgs e)
        {
            if(wordsInTrainingSet.Value > defaultTrainingWordMaxLength)
            {
                wordsInTrainingSet.Value = defaultTrainingWordMaxLength;
            }
            trainingWordMaxLength = (int) wordsInTrainingSet.Value;
            if(wordsInTestSet.Value > defaultTestingWordMaxLength)
            {
                wordsInTestSet.Value = defaultTestingWordMaxLength;
            }
            if (wordsInTestSet.Value < defaultTestingWordMinLength)
            {
                wordsInTestSet.Value = defaultTestingWordMinLength;
            }
            testingWordMaxLength = (int) wordsInTestSet.Value;

            // Prepare word sets
            string[] trainingLetters = prepareAlphabet(trainingWordMaxLength);
            string[] testingLetters = prepareAlphabet(testingWordMaxLength);

            WordSetGenerator w = new WordSetGenerator(testingLetters, trainingLetters, defaultTestingWordMinLength);
            Console.WriteLine("Generating training words!");
            // Generate training set
            w.GenerateTrainingWordsSet(new StringBuilder(), 0, trainingWordMaxLength);
            Console.WriteLine("Generating testing words!");
            // Generate testing set
            w.GenerateTestingWordsSet(new StringBuilder(), 0, testingWordMaxLength, new bool[testingLetters.Length]);
            for(int i = 0;  i< NoOfWords.Value; i++)
            {
                StringBuilder word = new StringBuilder();
                StringBuilder word2 = new StringBuilder();
                Random random = new Random();
                int length = random.Next((int)wordsInTrainingSet.Value, (int)WordLenght.Value + 1);
                for(int j = 0; j < length; j++)
                {
                    word.Append(alphabetLetters[random.Next() % alphabetLetters.Length]);
                    word2.Append(alphabetLetters[random.Next() % alphabetLetters.Length]);
                }
                w.TrainingWords.Add(word.ToString());
                w.TestingWords.Add(word2.ToString());
            }
            Console.WriteLine("Go go go!");
            TargetFunction targetFunction = new TargetFunction(automaton, w.TrainingWords, w.TestingWords);

            // Start algorithm and then remove unreached states
            PsoAlgorithm pso = new PsoAlgorithm((double)minErrLevel.Value, (int)maxIterationCount.Value, (int)minStateNumber.Value,
                (int)maxStateNumber.Value, alphabetLetters.Length, (int)ParticlesNumber.Value);
            System.Tuple<Automaton, double> result = pso.RunAlgorithm();
            foundAutomaton = result.Item1;

            showOutputPictureButton.Enabled = true;
            string errorRate = result.Item2.ToString("N2");
            //MessageBox.Show($"Best automaton found with error rate: {errorRate}", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            AdjacencyGraph<int, TaggedEdge<int, string>> g;
            foundAutomaton.GetGraph("OutputAutomaton" + automatoncounter, out g);
            GenerateFile(g, errorRate, automaton.States.Count);
        }
Exemplo n.º 2
0
 public void GenerateVariationsTest()
 {
     string[] letters = { "A", "B", "C" };
     WordSetGenerator wordSetGenerator = new WordSetGenerator(letters, letters, 0);
     int maxLength = 3;
     wordSetGenerator.GenerateTrainingWordsSet(new System.Text.StringBuilder(), 0, maxLength);
     var words = wordSetGenerator.TrainingWords;
     int expectedAmount = 0;
     int amountOfLetters = letters.Length;
     int temp = 1;
     for (int i = 0; i < maxLength; i++)
     {
         temp *= amountOfLetters;
         expectedAmount += temp;
     }
     Assert.AreEqual(expectedAmount, words.Count);
 }