/// <summary> /// Finds the state with the lowest value in fScores /// </summary> /// <param name="set">A list of CemaStates</param> /// <param name="fScores">A dictionary of CemaStates and their fScores</param> /// <returns></returns> private CemaState FindBest(List <CemaState> set, Dictionary <string, double> fScores, bool randomBest) { CemaState lowestState = null; double lowest = double.MaxValue; int bestCount = 1; //loop through all states in the list foreach (CemaState state in set) { double value = fScores[state.idCode]; if ((value == lowest) && (randomBest)) { bestCount++; Random rnd = new Random(); if (rnd.NextDouble() < (1 / (double)bestCount)) { lowestState = state; lowest = value; } } //keep the best score if (value < lowest) { lowestState = state; lowest = value; bestCount = 1; } } return(lowestState); }
public void setSolution(CemaState cState, string solutionMt, string problemMt) { // Make the description in cState the focus prologEngine.clearKB(solutionMt); prologEngine.clearConnectionsFromMt(solutionMt); prologEngine.connectMT(solutionMt, problemMt); foreach (string moduleMt in cState.modList) { prologEngine.connectMT(solutionMt, moduleMt); } List <string> solutionMissingList = missingInMt(solutionMt); List <string> solutionViolationList = violationsInMt(problemMt); cState.missingList = solutionMissingList; cState.violationList = solutionViolationList; }
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); }
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); }