3. Using Turing machine

3.1 Using F# Artificial Intelligence library in fsx script

  • Create folder e.g. C:\fsai and place following files there
    • FSharp.AI.dll - AI library
  • Create new fsx script file
// Path where libraries are placed
#I @"C:\fsai"

// Reference to main library
#r "FSharp.AI.dll"

// Opening of namespace and module
open FSharp.AI

//
//  Initialization of the Turing machine
//
// Initial inner state
let q0 = 1
// Initial position of the head
let headPosition = 7
// Data tape symbols
let tapeSymbols = ['#'; '1']
// Data tape (example of unary addition 7 + 2)
let tape = [| '#'; '#'; '#'; '#'; '#'; '#'; '#'; '1'; '1'; '#'; '1'; '1'; '1'; '1'; '1'; '1'; '1'; '#'; '#'; '#'; '#'; '#'; '#'; '#'; '#'; '#'; '#' |]
// Final inner state
let F = 6
// Rules of the Turing machine
let rules : int[][][] =
   [|
      [|
//        (q1, '#')    (q1, '1')
         [|1; 1; 1|]; [|2; 2; 1|]
      |];
      [|
//        (q2, '#')    (q2, '1')
         [|3; 2; 1|]; [|2; 2; 1|]
      |];
      [|
//        (q3, '#')    (q3, '1')
         [|4; 1; -1|]; [|3; 2; 1|]
      |];
      [|
//        (q4, '#')    (q4, '1')
         [|4; 1; -1|]; [|5; 1; -1|]
      |];
      [|
//        (q5, '#')    (q5, '1')
         [|6; 1; 1|]; [|5; 2; -1|]
      |];
   |]
// Number of iterations
let iterations = 100

// Creating Object
let turingMachine = new TM()
// Running Turing machine
let outputTape = turingMachine.Run (q0, headPosition, tape, tapeSymbols, F, rules, iterations)

// Output data tape
printfn "%A" outputTape

System.Console.ReadKey()

3.2 Using F# Artificial Intelligence library in C# project

  • In Visual Studio, create new C# ConsoleApplication which is based on .NET Framework 4.
  • Add references to FSharp.AI.dll and FSharp.Core.dll version 4.

using System;
using System.Collections.Generic;
using System.Text;
using FSharp.AI;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //
            // Initialization of the Turing machine
            //
            // Initial inner state
            int q0 = 1;
            // Initial position of the head
            int headPosition = 7;
            // Data tape symbols
            List<char> tapeSymbols = new List<char>() {'#', '1'};
            // Data tape (example of unary addition 7 + 2)
            char[] tape = new char[] {'#', '#', '#', '#', '#', '#', '#', '1', '1', '#', '1', '1', '1', '1', '1', '1', '1', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'};
            // Final inner state
            int F = 6;
            // Rules of the Turing machine
            int[][][] rules = new int[][][]
            {
                new int[][]
                {
                    new int[]{1, 1, 1}, new int[]{2, 2, 1}
                },
                new int[][]
                {
                    new int[]{3, 2, 1}, new int[]{2, 2, 1}
                },
                new int[][]
                {
                    new int[]{4, 1, -1}, new int[]{3, 2, 1}
                },
                new int[][]
                {
                    new int[]{4, 1, -1}, new int[]{5, 1, -1}
                },
                new int[][]
                {
                    new int[]{6, 1, 1}, new int[]{5, 2, -1}
                }               
            };
            // Number of iterations
            int iterations = 100;

            // Creating TM object
            TM turingMachine = new TM();

            // Running Turing machine
            char[] outputTape = turingMachine.Run(q0, headPosition, tape, tapeSymbols, F, rules, iterations);

            // Printing output data tape
            foreach (var item in outputTape)
            {
               Console.Write(item.ToString());
            }

            Console.ReadKey();            
        }
    }
}

Last edited Aug 26, 2011 at 10:59 AM by lko, version 6

Comments

No comments yet.