public SimulatedAnnealing(Learner l)
        {
            SimulatedAnnealingRunningParameters pam;
            using (var file = File.OpenText(@"SimulatedAnnealingParameters.json"))
            {
                var serializer = new JsonSerializer();
                pam =
                    (SimulatedAnnealingRunningParameters)
                        serializer.Deserialize(file, typeof(SimulatedAnnealingRunningParameters));
            }


            learner = l;

            threshold = pam.ThresholdTemperature;
            coolingFactor = pam.CoolingFactor;
            currentIteration = bestIteration = 1;
            reportEveryNIterations = pam.ReportEveryNIteration;

            currentHypothesis = bestHypothesis = learner.GetInitialGrammar();

            currentHypothesis.GenerateDerivedRulesFromSchema();
            currentEnergy = bestEnergy = learner.Energy(currentHypothesis);
            currentHypothesis.GenerateInitialRulesFromDerivedRules();

            currentTemp = currentEnergy.TotalEnergy*pam.InitialTemperatureTimesInitialEnegrgy;
            using (var sw = File.AppendText("SessionReport.txt"))
            {
                sw.WriteLine(string.Format("cooling factor: {0}, initial energy: {1}, initial temperature: {2}",
                    coolingFactor, currentEnergy, currentTemp));
            }
        }
Example #2
0
        private static void Main(string[] args)
        {
            var voc = Vocabulary.GetVocabularyFromFile(@"Vocabulary.json");
            var grammar = new Grammar(voc);

            ProgramParams programParams;
            using (var file = File.OpenText(@"ProgramParameters.json"))
            {
                var serializer = new JsonSerializer();
                programParams = (ProgramParams) serializer.Deserialize(file, typeof(ProgramParams));
            }


            if (programParams.DataWithMovement)
                CreateMovementGrammar(grammar);
            else
                CreateSimpleGrammar(grammar);
            grammar.GenerateDerivedRulesFromSchema();

            var p = new Parser(grammar);
            var data = p.GenerateSentences(programParams.NumberOfDataSentences);

            using (var sw = File.AppendText("SessionReport.txt"))
            {
                sw.WriteLine("-------------------");
                sw.WriteLine("Session {0} ", DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
                sw.WriteLine("sentences: {0}, runs: {1}, movement: {2}", programParams.NumberOfDataSentences,
                    programParams.NumberOfRuns, programParams.DataWithMovement);
            }

            var stopWatch = StartWatch();

            var learner = new Learner(voc, grammar.NonTerminalsTypeDictionary, grammar.POSTypes, data, grammar);

            learner.originalGrammar.GenerateDerivedRulesFromSchema();
            var targetGrammarEnergy = learner.Energy(learner.originalGrammar);
            learner.originalGrammar.GenerateInitialRulesFromDerivedRules();
            var s = string.Format("Target Hypothesis:\n{0} with energy: {1}\n", learner.originalGrammar,
                targetGrammarEnergy);

            Console.WriteLine(s);
            using (var sw = File.AppendText("SessionReport.txt"))
            {
                sw.WriteLine(s);
            }

            for (var i = 0; i < programParams.NumberOfRuns; i++)
            {
                var sa = new SimulatedAnnealing(learner);
                sa.Run();
            }
            StopWatch(stopWatch);
        }