Beispiel #1
0
 public void GetFinalStateTest()
 {
     string testingWord = "0101";
     string[] alphabetLetters = new string[] { "0", "1" };
     string[][] states = new string[][] { new string[]{ "1", "2"}, new string[]{ "2", "1"}, new string[]{ "2", "2" } };
     Automaton automaton = new Automaton(alphabetLetters, states);
     int finalState = automaton.GetFinalState(testingWord);
     Assert.AreEqual(2, finalState);
 }
Beispiel #2
0
 /// <summary>
 /// This function creates automaton instance from given position and counts number of errors. 
 /// </summary>
 public static int GetFunctionValue(Position position)
 {
     Automaton foundAutomaton = new Automaton(position);
     int count = 0;
     foreach (string word in _trainingSet)
     {
         if (_secretAutomaton.GetFinalState(word) != foundAutomaton.GetFinalState(word))
             count++;
     }
     return count;
 }
Beispiel #3
0
        public void AutomatonConstructionFromPositionObjectTest()
        {
            // Arrange
            Position position = new Position(2, 3, 0) { OnePositions = new[,] { { 1, 2, 1 }, { 2, 2, 1 } } };

            // Act
            Automaton automaton = new Automaton(position);

            // Assert
            Assert.AreEqual(automaton.GetFinalState("101"), 2);
        }
Beispiel #4
0
 public void GetGraphTest()
 {
     string testingWord = "01212";
     string autonatonName = "testowyAutomat";
     string[] alphabetLetters = new string[] { "0", "1", "2" };
     string[][] states = new string[][] { new string[] { "1", "0", "0" }, new string[] { "1", "1", "2" }, new string[] { "2", "2", "3" }, new string[] { "3", "3", "3"} };
     Automaton automaton = new Automaton(alphabetLetters, states);
     int finalState = automaton.GetFinalState(testingWord);
     Assert.AreEqual(3, finalState);
     File.Create(autonatonName).Close();
     AdjacencyGraph<int, TaggedEdge<int, string>> graph;
     automaton.GetGraph(autonatonName, out graph);
     File.Delete(autonatonName);
 }
Beispiel #5
0
 /// <summary>
 /// This function counts number of errors for given automaton instance.
 /// </summary>
 public static int GetFunctionValueForAutomaton(Automaton automaton)
 {
     return _testSet.Count(word => _secretAutomaton.GetFinalState(word) != automaton.GetFinalState(word));
 }
Beispiel #6
0
 /// <summary>
 /// Initialize new instance of TargetFunction class.
 /// </summary>
 public TargetFunction(Automaton automaton, List<string> trainingSet, List<string> testSet)
 {
     _secretAutomaton = automaton;
     _trainingSet = trainingSet;
     _testSet = testSet;
 }
Beispiel #7
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);
        }
Beispiel #8
0
        private void uploadFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog theDialog = new OpenFileDialog
            {
                Title = @"Open Text File",
                Filter = @"TXT files|*.txt",
                InitialDirectory = Directory.GetCurrentDirectory()
            };
            if (theDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = theDialog.FileName;
                string[][] functionTables = null;

                try
                {
                    alphabetLetters = new InputFileParser().Parse(filename,
                           out functionTables);
                }
                catch (Exception)
                {
                    MessageBox.Show("Wrong file format!","Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
                    automaton = null;
                    return;
                }

                automaton = new Automaton(alphabetLetters, functionTables);

                showInputPictureButton.Enabled = true;
                findResultButton.Enabled = true;
            }
        }
Beispiel #9
0
        private void GenerateTests(object sender, EventArgs e)
        {
            int[] basicStates = new int[] { 4, 6, 10, 15, 20, 30, 50, 80 };
            int automataAmount = 10;
            alphabetLetters = new string[] { "0", "1", "2", "3", "4" };

            for (int j = 0; j < basicStates.Length; j++)
            {
                for (int i = 0; i < automataAmount; i++)
                {
                    automaton = Automaton.GetRandomAutomaton(alphabetLetters, basicStates[j]);
                    showInputPictureButton.Enabled = true;
                    findResultButton_Click(sender, e);
                }
            }
        }