Ejemplo n.º 1
0
        public void commitSolution(GoapState cState, string solutionMt, string nowMt, string backgroundMt)
        {
            prologEngine.markKBScratchpad(solutionMt);
            planNode = cState;
            // Modules/Actions are in reverse order from now to goal so flip them
            cState.modList.Reverse();

            // Make final connections
            if (backgroundMt != null)
            {
                prologEngine.connectMT(solutionMt, backgroundMt);
            }
            foreach (string moduleMt in cState.modList)
            {
                prologEngine.connectMT(solutionMt, moduleMt);
            }

            // Post stats and planner state
            string postScript = "";

            postScript += String.Format("g({0}).\n", cState.costSoFar());
            postScript += String.Format("h({0}).\n", cState.distToGoal());
            postScript += String.Format("f({0}).\n", cState.costSoFar() + cState.distToGoal() * problemWorstCost);
            postScript += String.Format("worst({0}).\n", problemWorstCost);
            postScript += String.Format("openedNodes({0}).\n", openSet.Count);
            postScript += String.Format("closedNodes({0}).\n", closedSet.Count);
            postScript += String.Format("totalNodes({0}).\n", openSet.Count + closedSet.Count);

            if (cState.distToGoal() == 0)
            {
                postScript += "planstate(solved).\n";
            }
            else
            {
                postScript += "planstate(unsolved).\n";
            }

            prologEngine.appendKB(postScript, solutionMt);


            // post the modules used
            string modString = "";

            if (cState.modList.Count > 0)
            {
                foreach (string m in cState.modList)
                {
                    modString += m + " ";
                }
                prologEngine.appendListPredToMt("modlist", modString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("modlist([]).\n", solutionMt);
            }

            string planSequence = "";
            int    planCount    = 0;

            if (cState.modList.Count > 0)
            {
                foreach (string m in cState.modList)
                {
                    planSequence += String.Format("planraw({0}).\n", m);
                }
                foreach (string m in cState.modList)
                {
                    planSequence += String.Format("planseq({0},{1}).\n", planCount, m);
                    planCount++;
                }
                prologEngine.appendKB(planSequence, solutionMt);
            }
            else
            {
                prologEngine.appendKB("planraw(nop).\n planseq(0,nop).\n", solutionMt);
            }
            //post anything missing.

            if (cState.missingList.Count > 0)
            {
                string missingString = "";
                foreach (string m in cState.missingList)
                {
                    missingString += " " + m;
                }
                prologEngine.appendListPredToMt("missing", missingString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("missing([]).\n", solutionMt);
            }
            tickEnd = Environment.TickCount;
            int elapsed    = tickEnd - tickBegin;
            int totalNodes = openSet.Count + closedSet.Count;

            SIProlog.ConsoleWriteLine("Planning time = {0}", elapsed);
            SIProlog.ConsoleWriteLine("Planning list = '{0}'", modString);

            SIProlog.ConsoleWriteLine("Planning tials = {0}", trials);
            SIProlog.ConsoleWriteLine("TotalNodes = {0}", totalNodes);
            if (trials > 0)
            {
                SIProlog.ConsoleWriteLine("Planning ms/trials = {0}", ((double)elapsed / (double)trials));
            }
            if (totalNodes > 0)
            {
                double mspn = ((double)elapsed / (double)totalNodes);
                SIProlog.ConsoleWriteLine("Planning ms/nodes = {0}", mspn);
                if (mspn > 0)
                {
                    SIProlog.ConsoleWriteLine("Planning @ nodes/sec = {0}", 1000 / mspn);
                }
            }
            if (elapsed > 0)
            {
                SIProlog.ConsoleWriteLine("Planning trials/ms = {0}", ((double)trials / (double)elapsed));
                SIProlog.ConsoleWriteLine("Planning nodes/ms = {0}", ((double)totalNodes / (double)elapsed));
            }

            SIProlog.ConsoleWriteLine(postScript);
            prologEngine.markKBNonScratchPad(solutionMt);
        }
Ejemplo n.º 2
0
        public void commitSolution(CemaState cState, string solutionMt, string problemMt)
        {
            planNode = cState;
            // Post stats and planner state
            string postScript = "";

            postScript += String.Format("g({0}).\n", cState.costSoFar());
            postScript += String.Format("h({0}).\n", cState.distToGoal());
            postScript += String.Format("f({0}).\n", cState.costSoFar() + cState.distToGoal() * problemWorstCost);
            postScript += String.Format("worst({0}).\n", problemWorstCost);
            postScript += String.Format("openedNodes({0}).\n", openSet.Count);
            postScript += String.Format("closedNodes({0}).\n", closedSet.Count);
            postScript += String.Format("totalNodes({0}).\n", openSet.Count + closedSet.Count);

            if (cState.distToGoal() == 0)
            {
                postScript += "planstate(solved).\n";
            }
            else
            {
                postScript += "planstate(unsolved).\n";
            }

            prologEngine.appendKB(postScript, solutionMt);

            // post the modules used
            string modString = "";

            if (cState.modList.Count > 0)
            {
                foreach (string m in cState.modList)
                {
                    modString += " " + m;
                }
                prologEngine.appendListPredToMt("modlist", modString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("modlist([]).\n", solutionMt);
            }
            //post anything missing.

            if (cState.missingList.Count > 0)
            {
                string missingString = "";
                foreach (string m in cState.missingList)
                {
                    missingString += " " + m;
                }
                prologEngine.appendListPredToMt("missing", missingString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("missing([]).\n", solutionMt);
            }
            tickEnd = Environment.TickCount;
            int elapsed    = tickEnd - tickBegin;
            int totalNodes = openSet.Count + closedSet.Count;

            SIProlog.ConsoleWriteLine("Inventing time = {0}", elapsed);
            SIProlog.ConsoleWriteLine("Inventing list = {0}", modString);

            SIProlog.ConsoleWriteLine("Inventing tials = {0}", trials);
            SIProlog.ConsoleWriteLine("TotalNodes = {0}", totalNodes);
            if (trials > 0)
            {
                SIProlog.ConsoleWriteLine("Inventing ms/trials = {0}", ((double)elapsed / (double)trials));
            }
            if (totalNodes > 0)
            {
                double mspn = ((double)elapsed / (double)totalNodes);
                SIProlog.ConsoleWriteLine("Inventing ms/nodes = {0}", mspn);
                if (mspn > 0)
                {
                    SIProlog.ConsoleWriteLine("Inventing @ nodes/sec = {0}", 1000 / mspn);
                }
            }
            if (elapsed > 0)
            {
                SIProlog.ConsoleWriteLine("Inventing trials/ms = {0}", ((double)trials / (double)elapsed));
                SIProlog.ConsoleWriteLine("Inventing nodes/ms = {0}", ((double)totalNodes / (double)elapsed));
            }

            SIProlog.ConsoleWriteLine(postScript);
        }