static void TestCLGII(string sBenchmarkPath, int cAttempts)
        {
            RandomGenerator.Init();
            string sExePath = BASE_PATH + @"\PDDL\CLG\";

            Parser parser = new Parser();
            Domain domain = parser.ParseDomain(sBenchmarkPath + "d.pddl");
            Problem problem = parser.ParseProblem(sBenchmarkPath + "p.pddl", domain);
            BeliefState bsInitial = problem.GetInitialBelief();

            StreamWriter sw = new StreamWriter(sBenchmarkPath + @"..\CLGResults.txt", true);
            sw.Write(problem.Name + "\t" + DateTime.Now);
            sw.Close();
            DateTime dtBeforeTranslate = DateTime.Now;

            Process pCCF2CS = new Process();
            pCCF2CS.StartInfo.WorkingDirectory = sBenchmarkPath;
            pCCF2CS.StartInfo.FileName = sExePath + "ccf2cs";
            pCCF2CS.StartInfo.Arguments = "-t0 -cond -cod -cmr -csl -ckit -ckinl -cminl -cmit -cdisjk0 -cdisjm0 -mac  -cfc -fp -sn d.pddl p.pddl";
            pCCF2CS.StartInfo.UseShellExecute = false;
            pCCF2CS.Start();
            if (!pCCF2CS.WaitForExit(1000 * 60 * 20))//20 minutes max
            {
                pCCF2CS.Kill();
                sw = new StreamWriter(sBenchmarkPath + @"..\CLGResults.txt", true);
                sw.Write("\tcould not translate problem\n");
                sw.Close();
                throw new Exception("Could not translate problem");
            }

            sw = new StreamWriter(sBenchmarkPath + @"..\CLGResults.txt", true);
            sw.Write("\t" + (DateTime.Now - dtBeforeTranslate).TotalSeconds);
            sw.Close();

            int cFailures = 0;
            List<double> lActions = new List<double>();
            List<double> lTimes = new List<double>();

            for (int i = 1; i <= cAttempts; i++)
                File.Delete(sBenchmarkPath + i + ".hs");

            bool bLocalizeDomain = false;
            if (domain.Name.Contains("localize") || domain.Name.Contains("sliding-doors") || domain.Name.Contains("medical"))
                bLocalizeDomain = true;
            Console.WriteLine("Done " + domain.Name + " translation");

            for (int i = 1; i <= cAttempts; i++)
            {
                DateTime dtStart = DateTime.Now;
                Debug.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++");
                State sChosen = null;
                Process pCLG = new Process();
                pCLG.StartInfo.WorkingDirectory = sBenchmarkPath;
                pCLG.StartInfo.FileName = sExePath + "CLG";
                pCLG.StartInfo.UseShellExecute = false;
                if (bLocalizeDomain)
                {
                    pCLG.StartInfo.Arguments = "-a 1 -f new-p.pddl -o new-d.pddl";
                }
                else
                {
                    pCLG.StartInfo.Arguments = "-a 1 -f new-p.pddl -o new-d.pddl" + " -w " + i + ".hs";
                    sChosen = bsInitial.WriteHiddenState(sBenchmarkPath + i + ".hs", false);
                }

                //pCLG.StartInfo.RedirectStandardOutput = true;
                Debug.WriteLine("Starting CLG");
                File.Delete(sBenchmarkPath + "CLGplan.txt");
                pCLG.Start();
                /*
                    StreamWriter swOutput = new StreamWriter(sBenchmarkPath + "CLGOutput.txt");
                    swOutput.Write(pCLG.StandardOutput.ReadToEnd());
                    swOutput.Close();
                 * */
                if (!pCLG.WaitForExit(1000 * 60 * 20))//20 minutes max
                {
                    pCLG.Kill();
                    cFailures++;
                }
                else if (!File.Exists(sBenchmarkPath + "CLGplan.txt"))
                {
                    cFailures++;
                }
                else
                {
                    StreamReader sr = new StreamReader(sBenchmarkPath + "CLGplan.txt");
                    List<string> lPlan = new List<string>();
                    sr.ReadLine();//root
                    while (!sr.EndOfStream)
                    {
                        string sLine = sr.ReadLine();
                        string sParsedLine = sLine.Trim().ToLower().Replace("_", " ").
                            Replace("smell wumpus", "smell_wumpus").Replace("cd ", "cd_").Replace("my file", "my_file")
                            .Replace(" package ", "_package_").Replace(" truck ", "_truck_")
                            //.Replace(" airplane", "_airplane")
                            ;
                        lPlan.Add(sParsedLine);
                    }
                    sr.Close();
                    int cActions = 0;
                    TimeSpan tsTime;
                    bool bSuccess = true;
                    if (!bLocalizeDomain)
                        bSuccess = TestCLGPlan(sBenchmarkPath, domain, problem, lPlan, sChosen, out cActions, out tsTime);
                    else
                        cActions = lPlan.Count;
                    if (!bSuccess)
                    {
                        cFailures++;
                        Debug.WriteLine("CLG Failed");
                    }
                    else
                    {
                        lActions.Add(cActions);
                        tsTime = DateTime.Now - dtStart;
                        lTimes.Add(tsTime.TotalSeconds);
                    }
                }
                Console.WriteLine("Done " + domain.Name + " execution " + i);
            }

            TestBenchmarkThread.WriteResultsToFile(sBenchmarkPath + @"..\CLGResults.txt", lActions);
            TestBenchmarkThread.WriteResultsToFile(sBenchmarkPath + @"..\CLGResults.txt", lTimes);
            sw = new StreamWriter(sBenchmarkPath + @"..\CLGResults.txt", true);
            sw.Write("\t" + cFailures + "\n");
            sw.Close();
            Console.WriteLine("Done " + domain.Name);
        }
        static void TestKReplanner(string sBenchmarkPath, int cAttempts)
        {
            RandomGenerator.Init();
            string sExePath = BASE_PATH + @"\PDDL\KReplanner\";

            Parser parser = new Parser();
            Domain domain = parser.ParseDomain(sBenchmarkPath + "d.pddl");
            Problem problem = parser.ParseProblem(sBenchmarkPath + "p.pddl", domain);

            BeliefState bsInitial = problem.GetInitialBelief();

            string sOutput = "";

            DirectoryInfo di = new DirectoryInfo(sBenchmarkPath);
            foreach (FileInfo fi in di.GetFiles())
            {
                if (fi.Name.Contains("k_replanner"))
                    fi.Delete();
            }

            sOutput = problem.Name + "\t" + DateTime.Now;
            DateTime dtBeforeTranslate = DateTime.Now;

            domain.WriteKReplannerDomain(sBenchmarkPath + "d.k_replanner.pddl");

            sOutput += "\t" + (DateTime.Now - dtBeforeTranslate).TotalSeconds;

            int cFailures = 0;
            List<double> lActions = new List<double>();
            List<double> lTimes = new List<double>();

            Console.WriteLine("Done " + problem.Name + " translation");

            for (int i = 1; i <= cAttempts; i++)
            {
                DateTime dtStart = DateTime.Now;
                Debug.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++");
                State sChosen = null;
                Process pKReplanner = new Process();
                pKReplanner.StartInfo.WorkingDirectory = sExePath;
                pKReplanner.StartInfo.FileName = sExePath + "k_replanner.exe";
                pKReplanner.StartInfo.UseShellExecute = false;

                string sProblemName = "p." + i + ".k_replanner.pddl";
                sChosen = problem.WriteKReplannerProblem(sBenchmarkPath + sProblemName, bsInitial);

                pKReplanner.StartInfo.Arguments = //"--no-remove-intermediate-files " +
                    sBenchmarkPath + "d.k_replanner.pddl " + sBenchmarkPath + sProblemName;

                Debug.WriteLine("Starting KReplanner");
                File.Delete(sBenchmarkPath + "KReplanner.plan.txt");
                if (RedirectShellOutput)
                {
                    pKReplanner.StartInfo.RedirectStandardOutput = true;
                    pKReplanner.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                }
                pKReplanner.Start();
                if (RedirectShellOutput)
                {
                    //string sOutput = p.StandardOutput.ReadToEnd();
                    pKReplanner.BeginOutputReadLine();
                }                 /*
                    StreamWriter swOutput = new StreamWriter(sBenchmarkPath + "CLGOutput.txt");
                    swOutput.Write(pCLG.StandardOutput.ReadToEnd());
                    swOutput.Close();
                 * */
                if (!pKReplanner.WaitForExit(1000 * 60 * 30))//30 minutes max
                {
                    pKReplanner.Kill();
                    cFailures++;
                }
                else if (!File.Exists(sBenchmarkPath + sProblemName + ".plan"))
                {
                    cFailures++;
                }
                else
                {
                    StreamReader sr = new StreamReader(sBenchmarkPath + sProblemName + ".plan");
                    List<string> lPlan = new List<string>();
                    while (!sr.EndOfStream)
                    {
                        string sLine = sr.ReadLine();
                        /*
                        string sParsedLine = sLine.Trim().ToLower().Replace("_", " ").
                            Replace("smell wumpus", "smell_wumpus").Replace("cd ", "cd_").Replace("my file", "my_file")
                            .Replace(" package ", "_package_").Replace(" truck ", "_truck_")
                            //.Replace(" airplane", "_airplane")
                            ;
                        */
                        string sParsedLine = sLine.Trim().Replace("(", "").Replace(")", "");
                        lPlan.Add(sParsedLine);
                    }
                    sr.Close();
                    int cActions = 0;
                    TimeSpan tsTime;
                    DateTime dtBeforeVerification = DateTime.Now;
                    bool bSuccess = true;
                    bSuccess = TestCLGPlan(sBenchmarkPath, domain, problem, lPlan, sChosen, out cActions, out tsTime);
                    if (!bSuccess)
                    {
                        cFailures++;
                        Debug.WriteLine("KReplanner Failed");
                    }
                    else
                    {
                        lActions.Add(cActions);
                        tsTime = dtBeforeVerification - dtStart;
                        lTimes.Add(tsTime.TotalSeconds);
                    }
                }
                Console.WriteLine("Done " + problem.Name + " execution " + i + "/" + cAttempts);
            }

            m_mCLGMutex.WaitOne();
            StreamWriter sw1 = new StreamWriter(sBenchmarkPath + @"..\KReplannerResults.txt", true);
            sw1.Write(sOutput);
            sw1.Close();
            TestBenchmarkThread.WriteResultsToFile(sBenchmarkPath + @"..\KReplannerResults.txt", lActions);
            TestBenchmarkThread.WriteResultsToFile(sBenchmarkPath + @"..\KReplannerResults.txt", lTimes);
            sw1 = new StreamWriter(sBenchmarkPath + @"..\KReplannerResults.txt", true);
            sw1.WriteLine("\t" + cFailures);
            sw1.Close();
            m_mCLGMutex.ReleaseMutex();
            Console.WriteLine("Done " + problem.Name);
        }
            void TestBenchmark(string sBenchmarkPath, string sBenchmark, int cTrials, bool bWriteResults)
            {
                StringWriter sw = new StringWriter();
                List<double> lTime = new List<double>();
                List<double> lActions = new List<double>();
                List<double> lPlanning = new List<double>();
                List<double> lObservations = new List<double>();
                int cFailure = 0;
                try
                {
                    string sPath = sBenchmarkPath + sBenchmark + @"\";
                    Parser parser = new Parser();
                    Domain domain = parser.ParseDomain(sPath + "d.pddl");
                    Debug.WriteLine("Reading domain and problem");
                    Problem problem = parser.ParseProblem(sPath + "p.pddl", domain);
                    //domain.Actions = domain.GroundAllActions(problem);
                    Debug.WriteLine("Done reading domain and problem");
                    DateTime dtStart = DateTime.Now;
                    //domain.WriteKnowledgeDomain(sPath + "Kd.pddl", problem);
                    DateTime dtEnd = DateTime.Now;
                    //Debug.WriteLine("Done writing knowledge translation. Time = " + (dtEnd - dtStart).TotalSeconds);

                    //sw.WriteLine();
                    sw.Write(sBenchmark + "\t" + DateTime.Now + "\t" +
                        (dtEnd - dtStart).TotalSeconds + "\t" + SDRPlanner.TagsCount);
                    for (int i = 0; i < cTrials; i++)
                    {
                        //int cActions = 0, cPlanning = 0;
                        //TimeSpan tsTime;
                        //OnlineReplanning(sPath, domain, problem, out cActions, out cPlanning, out tsTime);

                        //WriteKnowledgeDomain(domain, problem, i);
                        //continue;

                        DateTime dtStartTask = DateTime.Now;
                        SDRPlanner sdr = new SDRPlanner(sPath, domain, problem);
                        Thread t = new Thread(sdr.Start);
                        t.Name = "OfflinePlanningData " + domain.Name;
                        t.Start();
                        bool bFailed = false;
                        if (!t.Join(new TimeSpan(0, MaxTimePerProblem, 0)))
                        //t.Join();
                        {
                            //if (!t.Join(100))
                            t.Abort();
                            t.Join();

                            cFailure++;
                            bFailed = true;
                        }

                        sdr.TerminateFFPRocesses(t);

                        SDRPlanner.ExecutionData data = sdr.Data;

                        if (data.Failure)
                        {
                            cFailure++;
                            bFailed = true;
                        }
                        else
                        {
                            lActions.Add(data.Actions);
                            lPlanning.Add(data.Planning);
                            lTime.Add(data.Time.TotalSeconds);
                            lObservations.Add(data.Observations);
                        }
                        sw.Write(i + ": " + data.Actions + "\t" + data.Planning + "\t" + data.Time.TotalSeconds);
                        Console.WriteLine(sBenchmark + ", " + i + "/" + cTrials + ", " + Math.Round((DateTime.Now - dtStartTask).TotalMinutes) + ", failed? " + bFailed);
                    }
                }
                catch (Exception e)
                {
                    //sw.Write(e.Message);
                    Console.WriteLine(e);
                }
                if (bWriteResults)
                {
                    m_mWriteToFile.WaitOne();
                    StreamWriter swFile = new StreamWriter(sBenchmarkPath + ResultsFile, true);
                    //swFile.Write(sw.ToString());
                    swFile.Write(sBenchmark + "\t" + SDRPlanner.TagsCount);
                    swFile.Close();
                    WriteResultsToFile(sBenchmarkPath + ResultsFile, lActions);
                    WriteResultsToFile(sBenchmarkPath + ResultsFile, lPlanning);
                    WriteResultsToFile(sBenchmarkPath + ResultsFile, lObservations);
                    WriteResultsToFile(sBenchmarkPath + ResultsFile, lTime);
                    swFile = new StreamWriter(sBenchmarkPath + ResultsFile, true);
                    swFile.WriteLine("\t" + cFailure);
                    swFile.Close();
                    m_mWriteToFile.ReleaseMutex();
                }
            }