Ejemplo n.º 1
0
        public static void Run(string csvFile, string solversDir, string inputFile, string optionFile)
        {
            FileManager.WriteToFile(csvFile, inputFile, true, true);

            foreach (string solver in SolverEnum.GetSolvers())
            {
                if (!FileManager.SolverPresent(solversDir, solver + ".solver"))
                {
                    continue;
                }

                ASPInputProgram       inputProgram = new ASPInputProgram();
                DesktopHandler        handler      = new DesktopHandler(SolverEnum.GetService(solversDir, solver));
                List <ISet <object> > answerSets   = new List <ISet <object> >();
                int optionID = handler.AddOption(new OptionDescriptor(SolverEnum.GetOutputOption(solver)));

                if (string.IsNullOrWhiteSpace(optionFile))
                {
                    FileManager.ReadFilters(optionFile, solver).ForEach(filter => handler.AddOption(new OptionDescriptor(" " + filter)));
                }

                handler.AddProgram(inputProgram);
                inputProgram.AddFilesPath(inputFile);

                List <string> sortedOutput = OutputManager.ProcessRawOutput(solver, ((AnswerSets)(handler.StartSync())).OutputString);

                handler.RemoveOption(optionID);

                var watch = System.Diagnostics.Stopwatch.StartNew();

                foreach (AnswerSet answerSet in ((AnswerSets)(handler.StartSync())).Answersets)
                {
                    answerSets.Add(answerSet.Atoms);
                }

                watch.Stop();
                FileManager.WriteToFile(csvFile, solver + ":" + watch.ElapsedMilliseconds.ToString(), null, true);

                if (answerSets.Count > 0)
                {
                    foreach (ISet <object> answerSet in answerSets)
                    {
                        string tmp = string.Join(" ", SortFacts(answerSet));

                        if (!sortedOutput.Contains(tmp))
                        {
                            Console.WriteLine("ERROR! Original " + solver + " output does not contain:\n" + tmp + "\n");
                        }
                        ;
                    }
                }
            }

            Console.WriteLine("END");
        }
Ejemplo n.º 2
0
        public SymbolicConstant GetNextMove(InputProgram facts)
        {
            SymbolicConstant move             = new SymbolicConstant();
            string           encodingResource = @".\encodings\pacman.asp";
            //string encodingResource2 = @"encodings\min_distances_5.asp";
            //Debug.Log("DLV Started: " + numberOfSteps++);
            Handler      handler  = new DesktopHandler(new DLVDesktopService(@".\lib\dlv.exe"));
            InputProgram encoding = new ASPInputProgram();

            encoding.AddFilesPath(encodingResource);
            //InputProgram encoding2 = new ASPInputProgram();
            //encoding.AddFilesPath(encodingResource2);
            handler.AddProgram(encoding);
            //handler.AddProgram(encoding2);
            handler.AddProgram(facts);
            handler.AddOption(new OptionDescriptor("-filter=next"));
            Output o = handler.StartSync();
            //EmbaspCall++;



            AnswerSets answers = (AnswerSets)o;

            System.Random r      = new System.Random();
            int           answer = r.Next(answers.Answersets.Count);
            AnswerSet     a      = answers.Answersets[answer];

            foreach (object obj in a.Atoms)
            {
                //Debug.Log(obj.ToString());
                if (obj is Next)
                {
                    Next nextAction = (Next)obj;

                    move = nextAction.getAction();
                    return(move);
                    //Debug.Log("Next Action: " + move);
                }
            }
            return(move);
        }
Ejemplo n.º 3
0
        public void Run()
        {
            reason = true;
            IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor));

            while (reason)
            {
                if (!brain.executeReasonerOn.Equals("When Sensors are ready"))
                {
                    lock (brain.toLock)
                    {
                        MyDebugger.MyDebug("going to wait for pulse by brain");
                        brain.solverWaiting = true;
                        Monitor.Wait(brain.toLock);
                    }
                }
                try
                {
                    factsPath = Path.GetTempFileName();

                    using (StreamWriter fs = new StreamWriter(factsPath, true))
                    {
                        string toAppend = SensorsManager.GetSensorsMapping(brain);
                        if (!reason)
                        {
                            return;
                        }
                        fs.Write(toAppend);
                        fs.Close();
                    }
                }
                catch (Exception e)
                {
                    MyDebugger.MyDebug("CAUGHT EXECPTION!!!!");
                    MyDebugger.MyDebug(e.Message);
                    MyDebugger.MyDebug(e.StackTrace);
                }


                Handler handler = new DesktopHandler(new DLV2DesktopService(@".\lib\dlv2.exe"));
                //With DLV2DesktopService I get a Error during parsing: --> Invalid #show directive: setOnActuator/1--competition-output.
                //With DLVDesktopService the AS, obviously, are wrongly parsed
                InputProgram encoding = new ASPInputProgram();
                MyDebugger.MyDebug("adding encoding");
                encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath));
                InputProgram facts = new ASPInputProgram();
                MyDebugger.MyDebug("adding facts");
                facts.AddFilesPath(factsPath);
                handler.AddProgram(encoding);
                handler.AddProgram(facts);
                handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1 "));
                stopwatch.Restart();
                MyDebugger.MyDebug("starting sync");
                Output o = handler.StartSync();
                if (!o.ErrorsString.Equals(""))
                {
                    MyDebugger.MyDebug(o.ErrorsString + " " + o.OutputString);
                }
                AnswerSets answers = (AnswerSets)o;
                stopwatch.Stop();
                brain.asSteps++;
                brain.asTotalMS += stopwatch.ElapsedMilliseconds;
                MyDebugger.MyDebug("num of AS " + answers.Answersets.Count);
                if (answers.Answersets.Count > 0)
                {
                    lock (brain.toLock)
                    {
                        foreach (SimpleActuator actuator in brain.getActuators())
                        {
                            actuator.parse(answers.Answersets[0]);
                        }
                        brain.setActuatorsReady(true);
                    }
                }
                if (!brain.maintainFactFile)
                {
                    File.Delete(factsPath);
                }
            }
        }
        public void Run()
        {
            reason = true;
            IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor));

            //Debug.Log("mapper " + sensorMapper);
            while (reason)
            {
                //Thread.Sleep(1000);
                //Debug.Log("executing thread");
                lock (brain.toLock)
                {
                    brain.solverWaiting = true;
                    Monitor.Wait(brain.toLock);
                    try
                    {
                        stopwatch.Restart();
                        factsPath = Path.GetTempFileName();

                        using (StreamWriter fs = new StreamWriter(factsPath, true))
                        {
                            //Debug.Log("creating file "+ factsPath);
                            string toAppend = "";
                            foreach (AdvancedSensor sensor in brain.getSensors())
                            {
                                //Stopwatch temp = new Stopwatch();
                                //temp.Start();
                                toAppend += sensor.Map();
                                //temp.Stop();
                                //Debug.Log(toAppend);
                                //Debug.Log(toAppend);
                            }
                            //Debug.Lof(fs.)
                            fs.Write(toAppend);
                            fs.Close();
                            //Debug.Log("closing stream");
                        }
                        stopwatch.Stop();
                        factsSteps++;
                        factsAvgTime += stopwatch.ElapsedMilliseconds;
                    }
                    catch (Exception e)
                    {
                        UnityEngine.Debug.LogError(e.Message);
                        UnityEngine.Debug.LogError(e.StackTrace);
                    }
                }
                //Debug.Log(Path.GetFullPath(@".\lib\dlv.exe"));
                Handler      handler  = new DesktopHandler(new DLVDesktopService(@".\lib\dlv2.exe"));
                InputProgram encoding = new ASPInputProgram();
                encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath));
                InputProgram facts = new ASPInputProgram();
                facts.AddFilesPath(factsPath);
                handler.AddProgram(encoding);
                handler.AddProgram(facts);
                handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1"));
                stopwatch.Restart();
                //Debug.Log("reasoning");
                Output o = handler.StartSync();
                if (!o.ErrorsString.Equals(""))
                {
                    Debug.Log(o.ErrorsString + " " + o.OutputString);
                }
                AnswerSets answers = (AnswerSets)o;
                stopwatch.Stop();
                asSteps++;
                asAvgTime += stopwatch.ElapsedMilliseconds;
                //Debug.Log("debugging answer set");
                //Debug.Log("there are "+answers.Answersets.Count);
                //Debug.Log("error: " + answers.ErrorsString);
                if (answers.Answersets.Count > 0)
                {
                    /*string asPath = Path.GetTempFileName();
                     * using (StreamWriter fs = new StreamWriter(asPath, true))
                     * {
                     *  fs.Write(o.OutputString);
                     *  fs.Close();
                     * }*/
                    lock (brain.toLock)
                    {
                        foreach (SimpleActuator actuator in brain.getActuators())
                        {
                            Debug.Log("input fact " + factsPath);
                            Debug.Log("parsing " + actuator.actuatorName);
                            if (answers.Answersets[0].GetAnswerSet().Count > 0)
                            {
                                Debug.Log(answers.Answersets[0].GetAnswerSet()[0]);
                            }
                            actuator.parse(answers.Answersets[0]);
                        }
                        brain.setActuatorsReady(true);
                    }
                }
                if (!brain.maintainFactFile)
                {
                    File.Delete(factsPath);
                }
            }
        }