public void setSolution(GoapState cState, string solutionMt, string nowMt, string backgroundMt) { // Make the description in cState the focus prologEngine.markKBScratchpad(solutionMt); prologEngine.clearKB(solutionMt); prologEngine.clearConnectionsFromMt(solutionMt); if (backgroundMt != null) { prologEngine.connectMT(solutionMt, backgroundMt); } // foreach (string moduleMt in cState.modList) // { // prologEngine.connectMT(solutionMt, moduleMt); // } string goalCode = ""; foreach (string p in cState.missingList) { goalCode += String.Format("precond({0}).\n", p); } prologEngine.appendKB(goalCode, solutionMt); List <string> solutionMissingList = missingInMt(solutionMt, nowMt); //List<string> solutionViolationList = violationsInMt(problemMt); cState.missingList = solutionMissingList; //cState.violationList = solutionViolationList; }
static void testProlog3() { // we can do a graph of connected kb's // the query is from a point in the graph and the system gathers the KB // Wonder if we "logicRank" the graph in someway so we inference over what's relevant // Also what kind of connected graphs can we concoct ? // Maybe we want to have prove walk the KB graph? // Should the KB be constructed depth-first or breath first ? // (It may not matter so long as the items of a given predicate appear in one MT) // probably should have classic optomizations like first term indexing for speed // Should work on built-ins. (Done except for eval) (Dmiles did eval) prologEngine.connectMT("sense1MT", "baseKB"); prologEngine.connectMT("sense2MT", "baseKB"); prologEngine.connectMT("spindleMT", "sense1MT"); prologEngine.connectMT("spindleMT", "sense2MT"); prologEngine.insertKB("canSense(X):-sense(X).\n", "baseKB"); prologEngine.appendKB("genls(A,B):-genls(A,X),genls(X,B).\n", "baseKB"); prologEngine.appendKB("isa(A,B):-isa(A,X),genls(X,B).\n", "baseKB"); prologEngine.insertKB("sense(dog).\nsense(cat).\n", "sense1MT"); prologEngine.insertKB("sense(boy).\nsense(girl).\n", "sense2MT"); prologEngine.appendKB("isa(john,boy).\ngenls(boy,human).\ngenls(human,mammal).\n", "sense2MT"); prologEngine.askQuery("canSense(WHAT1)", "sense1MT"); prologEngine.askQuery("canSense(WHAT2)", "sense2MT"); prologEngine.askQuery("canSense(WHAT3)", "spindleMT"); prologEngine.askQuery("isa(john,JOHN_IS)", "spindleMT"); //prologEngine.KBGraph.PrintToConsole(); }
public bool constructSolution(string problemMt, string moduleMt, string solutionMt) { tickBegin = Environment.TickCount; // CEMA prologEngine.connectMT(solutionMt, problemMt); List <Dictionary <string, string> > bingingsList = new List <Dictionary <string, string> >(); List <string> totalModuleList = new List <string>(); // Collect Module List string query = "module(MODMT)"; prologEngine.askQuery(query, moduleMt, out bingingsList); foreach (Dictionary <string, string> bindings in bingingsList) { foreach (string k in bindings.Keys) { if (k == "MODMT") { totalModuleList.Add(bindings[k]); } } } // Find worst cost // h(n)*problemWorstCost should be admissible for A* problemWorstCost = -1; if (worstWeighting) { string costQuery = "cost(COST)"; prologEngine.askQuery(costQuery, moduleMt, out bingingsList); foreach (Dictionary <string, string> bindings in bingingsList) { foreach (string k in bindings.Keys) { if (k == "COST") { double newCost = double.Parse(bindings[k].Trim()); if (newCost > problemWorstCost) { problemWorstCost = newCost; } } } } if (problemWorstCost == -1) { problemWorstCost = 1; } } else { problemWorstCost = 1; } List <string> missingList = missingInMt(problemMt); List <string> violationList = violationsInMt(problemMt); CemaState start = new CemaState(new List <string>(), missingList); // get initial Eval setSolution(start, solutionMt, problemMt); if ((missingList.Count == 0) && (violationList.Count == 0)) { commitSolution(start, solutionMt, problemMt); return(true); // nothing is missing so done } closedSet = new List <CemaState>(); openSet = new List <CemaState>(); //cost expended so far Dictionary <string, double> gScores = new Dictionary <string, double>(); //Estimate how far to go Dictionary <string, double> hScores = new Dictionary <string, double>(); //combined f(n) = g(n)+h(n) Dictionary <string, double> fScores = new Dictionary <string, double>(); gScores.Add(start.idCode, 0); hScores.Add(start.idCode, start.distToGoal() * problemWorstCost); fScores.Add(start.idCode, (gScores[start.idCode] + hScores[start.idCode])); openSet.Add(start); trials = 0; while (openSet.Count != 0) { trials++; if (trials > limitTrials) { break; } //we look for the node within the openSet with the lowest f score. CemaState bestState = this.FindBest(openSet, fScores, nondeterministic); setSolution(bestState, solutionMt, problemMt); // if goal then we're done if (bestState.distToGoal() == 0) { // return with the solutionMt already connected commitSolution(bestState, solutionMt, problemMt); return(true); } openSet.Remove(bestState); closedSet.Add(bestState); // Not the final solution and too expensive if (bestState.totalCost > limitCost) { continue; } // get the list of modules we have not used List <string> validModules = bestState.validNextMods(totalModuleList); foreach (string nextModule in validModules) { // only consider those that provide something missing if (!isRelevantMt(nextModule, bestState.missingList)) { continue; } double nextCost = getModuleCost(nextModule); // Ok nextModule is relevant so clone bestState and extend List <string> nextModList = new List <string> (); foreach (string m in bestState.modList) { nextModList.Add(m); } nextModList.Add(nextModule); CemaState nextState = new CemaState(nextModList, null); nextState.totalCost = bestState.totalCost + nextCost; // measure the quality of the next state setSolution(nextState, solutionMt, problemMt); //skip if it has been examined if (closedSet.Contains(nextState)) { continue; } if (!openSet.Contains(nextState)) { openSet.Add(nextState); gScores[nextState.idCode] = nextState.costSoFar(); hScores[nextState.idCode] = nextState.distToGoal() * problemWorstCost; fScores[nextState.idCode] = (gScores[nextState.idCode] + hScores[nextState.idCode]); } } openSet.Sort(); } // an impossible task appently commitSolution(start, solutionMt, problemMt); return(false); }