public Agent(Algorithm algorithm, InferenceManager inferences, SnapShot snapShot)
 {
     this.algorithm  = algorithm;
     this.inferences = inferences;
     this.snapShot   = snapShot;
 }
Beispiel #2
0
        static void train(TextWriter writer, char csv_seperator, char dec_seperator)
        {
            // Ask file location questions
            string input_location      = writer.askFromConfig("Enter the file path to import data from. ", "GENERAL", "input-location");
            string snapshot_location   = writer.askFromConfig("Enter the directory to output snapshots to. ", "EXPORT", "snapshot-location");
            string thoughts_location   = writer.askFromConfig("Enter the directory to output inferences to. ", "EXPORT", "inference-location");
            string vocabulary_location = writer.askFromConfig("Enter the file path to import the vocabulary form ", "VOCABULARY", "location");

            // General settings
            string catchinput = writer.askFromConfig("Catch error and output?", "GENERAL", "output-on-error");
            bool   catcherror = (catchinput == "TRUE");


            string model_extension   = "txt";
            string rules_extension   = "rules.txt";
            string drawing_extension = "GRAPH";

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            DataController import       = new DataController(csv_seperator, dec_seperator);
            ObservationSet observations = import.importExamples(input_location);

            VocabularyImporter vocab = new VocabularyImporter();

            vocab.import(vocabulary_location);

            InferenceManager inferences = new InferenceManager(vocab.vocabulary, csv_seperator);

            SnapShot snapShot = new SnapShot(writer, stopwatch, snapshot_location, model_extension, rules_extension, drawing_extension);


            string    algorithmChoice = writer.askFromConfig("What algorithm should be used? [ID3, C4.5]", "GENERAL", "algorithm");
            Algorithm algorithm       = null;
            Dictionary <string, object> parameters = new Dictionary <string, object>();

            switch (algorithmChoice)
            {
            case "ID3":
                algorithm = new ID3Algorithm();
                break;

            case "C4.5":
                string confidence_input  = writer.askFromConfig("What confidence level should be used", "C4.5", "confidence");
                string keep_values_input = writer.askFromConfig("Should all values keep getting considered even when not in subset?", "C4.5", "keep_considering_values");
                string minimum_number_of_objects_input = writer.askFromConfig("How many objects should a leaf at least contain?", "C4.5", "minimum_number_of_objects");

                parameters["confidence"]                = float.Parse(confidence_input);
                parameters["keep_values_input"]         = bool.Parse(keep_values_input);
                parameters["minimum_number_of_objects"] = int.Parse(minimum_number_of_objects_input);

                algorithm = new C45Algorithm();
                break;

            default:
                throw new Exception($"Unknown algorithm given: {algorithmChoice}");
            }
            Agent agent = new Agent(algorithm, inferences, snapShot);

            Console.WriteLine($"READY ({stopwatch.ElapsedMilliseconds} ms setup time). Press a key to start training process \n");
            Console.ReadKey(true);

            // Train the algorithm based on the Training set
            Console.WriteLine("Starting Training process (TRAIN).");
            Console.WriteLine("");
            stopwatch.Reset();
            stopwatch.Start();
            if (catcherror)
            {
                try
                {
                    agent.TRAIN(observations, parameters);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Encountered an error! Writing output and model anyways.");
                    writer.set_location(thoughts_location);
                    inferences.write(writer);
                    throw (e);
                }
            }
            else
            {
                agent.TRAIN(observations, parameters);
            }

            Console.WriteLine("");
            long training_time = stopwatch.ElapsedMilliseconds;
            long snapshot_time = training_time - snapShot.secondsBySnapShot;

            Console.WriteLine("Training completed. Processing thoughts.");

            writer.set_location(thoughts_location);
            inferences.write(writer);

            long thought_time = stopwatch.ElapsedMilliseconds;

            Console.WriteLine($"Training time: {training_time}ms including snapshotting, {snapshot_time}ms excluding. Thoughts processing time: {thought_time - training_time}ms.");
        }