public List <PartiallySpecifiedState> GetInitialStates() { List <PartiallySpecifiedState> lpssInitialPossibleStates = new List <PartiallySpecifiedState>(); BeliefState bsInitial = GetInitialBelief();//, bsCurrent = bsInitial, bsNext = null; PartiallySpecifiedState pssInitial = bsInitial.GetPartiallySpecifiedState(); lpssInitialPossibleStates.Add(pssInitial); foreach (var hiddenItems in Hidden) { if (hiddenItems is CompoundFormula) { List <PartiallySpecifiedState> stateAdditions = new List <PartiallySpecifiedState>(); foreach (var item in hiddenItems.Operands) { foreach (var pssCurrentCheckedState in lpssInitialPossibleStates) { PartiallySpecifiedState pssNew = pssCurrentCheckedState.Clone(); pssNew.AddObserved(item); stateAdditions.Add(pssNew); } } lpssInitialPossibleStates = stateAdditions; } else { throw new NotImplementedException(); } } return(lpssInitialPossibleStates); }
public bool ValidatePlan(PartiallySpecifiedState pssCurrent) { if (pssCurrent == null) { return(false); } if (pssCurrent.IsGoalState()) { return(true); } if (Action == null) { return(false); } Formula fObserved = null; PartiallySpecifiedState psTrueState, psFalseState; pssCurrent.ApplyOffline(Action, out fObserved, out psTrueState, out psFalseState); if (Action.Observe == null) { return(SingleChild.ValidatePlan(psTrueState)); } bool bTrueOk = TrueObservationChild.ValidatePlan(psTrueState); bool bFalseOk = FalseObservationChild.ValidatePlan(psFalseState); return(bTrueOk && bFalseOk); }
private List <PartiallySpecifiedState> SearchAllStates(Domain domain, List <PartiallySpecifiedState> lInitialStates) { Console.WriteLine("Scanning all possible states, using BFS algorithm"); Formula fObserved = null; PartiallySpecifiedState fTrueObserved = null; PartiallySpecifiedState fFalseObserved = null; Stack <PartiallySpecifiedState> OpenListStack = new Stack <PartiallySpecifiedState>(lInitialStates); Dictionary <string, PartiallySpecifiedState> OpenList = new Dictionary <string, PartiallySpecifiedState>(); foreach (PartiallySpecifiedState pss in OpenListStack) { OpenList.Add(pss.ToString(), pss); } Dictionary <string, PartiallySpecifiedState> ClosedList = new Dictionary <string, PartiallySpecifiedState>(); List <IMAP.Action> groundedActions = domain.GroundAllActions(domain.Actions, domain.GroundAllPredicates(null), false, false); int iterationCounter = 0; while (OpenListStack.Count != 0) { if (iterationCounter++ % 100 == 0) { Console.WriteLine("Seaching: (OL=" + OpenListStack.Count + "/CL=" + ClosedList.Count + ")"); } PartiallySpecifiedState selectedState = OpenListStack.Pop(); OpenList.Remove(selectedState.ToString()); ClosedList.Add(selectedState.ToString(), selectedState); foreach (var a in groundedActions) { selectedState.ApplyOffline(a, out fObserved, out fTrueObserved, out fFalseObserved); if (fTrueObserved != null && fTrueObserved.GetPositivePredicates().Count != selectedState.GetPositivePredicates().Count) { continue; } if (fTrueObserved != null) { if (ClosedList.ContainsKey(fTrueObserved.ToString()) || OpenList.ContainsKey(fTrueObserved.ToString())) { continue; } OpenListStack.Push(fTrueObserved); OpenList.Add(fTrueObserved.ToString(), fTrueObserved); } } } return(ClosedList.Values.ToList()); }
private static List <Action> GenerateJointActionsListForSingleInitialState(PartiallySpecifiedState initialState, Dictionary <Constant, PlanResult> plans) { foreach (var agentPlan in plans) { Constant agent = agentPlan.Key; PlanResult planResult = agentPlan.Value; ConditionalPlanTreeNode cptn = planResult.Plan; List <Action> actions = new List <Action>(); cptn.GetActionUsed(ref actions); return(actions); } return(null); }
public static List <string> BattleshipHeuristicII(PartiallySpecifiedState pssCurrent, Domain d) { List <string> lActions = new List <string>(); if (lPlaces == null) { lPlaces = new List <int>(); for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { lPlaces.Add(iX * 1000 + iY); } } for (int i = 0; i < lPlaces.Count; i++) { int iRandom = RandomGenerator.Next(lPlaces.Count); int iAux = lPlaces[iRandom]; lPlaces[iRandom] = lPlaces[i]; lPlaces[i] = iAux; } iCurrent = 0; } bool bUnknown = false; int iSkipped = 0; while (!bUnknown) { int iChosen = lPlaces[iCurrent]; iCurrent++; int iChosenX = iChosen / 1000; int iChosenY = iChosen % 1000; GroundedPredicate gp = GetPredicate("water-at", iChosenX, iChosenY); if (!pssCurrent.Observed.Contains(gp)) { bUnknown = true; lActions.Add("shoot p-" + iChosenX + " p-" + iChosenY); } else { iSkipped++; } } return(lActions); }
public MemoryStream WriteSimpleProblem(string sProblemFile, PartiallySpecifiedState sCurrent, CompoundFormula cfGoal) { MemoryStream msProblem = new MemoryStream(); StreamWriter sw = new StreamWriter(msProblem); sw.WriteLine("(define (problem K" + Name + ")"); sw.WriteLine("(:domain K" + Domain.Name + ")"); sw.WriteLine("(:init"); //ff doesn't like the and (and"); string sP = ""; foreach (GroundedPredicate gp in sCurrent.Observed) { if (!gp.Negation) { sP = "(" + gp.Name; foreach (Constant c in gp.Constants) { sP += " " + c.Name; } sw.WriteLine("\t" + sP + ")"); } } sw.WriteLine(")"); sw.WriteLine("(:goal " + cfGoal + ")"); //sw.WriteLine("))"); sw.WriteLine(")"); sw.Flush(); if (SDRPlanner.UseFilesForPlanners) { msProblem.Position = 0; StreamReader sr = new StreamReader(msProblem); StreamWriter swFile = new StreamWriter(sProblemFile); swFile.Write(sr.ReadToEnd()); swFile.Close(); } return(msProblem); }
private static bool CheckPlansOnSingleInitialState(PartiallySpecifiedState initialState, Dictionary <Constant, PlanResult> plans, List <Predicate> goals) { List <Action> actionsList = GenerateJointActionsListForSingleInitialState(initialState, plans); List <Predicate> collectedPredicate = new List <Predicate>(); PartiallySpecifiedState currentState = initialState.Clone(); foreach (var action in actionsList) { currentState.ApplyOffline(action, out Formula obs, out PartiallySpecifiedState psTrue, out PartiallySpecifiedState psFalse); currentState = psTrue; } if (currentState.IsGoalState()) { return(true); } else { return(false); } }
public static List <string> LargeWumpusHeuristic(PartiallySpecifiedState pssCurrent, Domain d) { List <string> lActions = new List <string>(); GroundedPredicate gpAtX = null, gpAtY = null; foreach (GroundedPredicate gp in pssCurrent.Observed) { if (!gp.Negation) { if (gp.Name == "at-x") { gpAtX = gp; } if (gp.Name == "at-y") { gpAtY = gp; } } } string sX = gpAtX.Constants[0].Name; string sY = gpAtY.Constants[0].Name; int iX = int.Parse(sX.Split('-')[1]); int iY = int.Parse(sY.Split('-')[1]); VisitedLocations.Add(iX * 1000 + iY); GroundedPredicate gpAlive = new GroundedPredicate("alive"); if (pssCurrent.Hidden.Contains(gpAlive)) { lActions.Add("check-alive_" + sX + "_" + sY); } GroundedPredicate gpStench = new GroundedPredicate("stench"); gpStench.AddConstant(gpAtX.Constants[0]); gpStench.AddConstant(gpAtY.Constants[0]); if (pssCurrent.Hidden.Contains(gpStench)) { lActions.Add("smell-wumpus " + sX + " " + sY); } GroundedPredicate gpBreeze = new GroundedPredicate("breeze"); gpBreeze.AddConstant(gpAtX.Constants[0]); gpBreeze.AddConstant(gpAtY.Constants[0]); if (pssCurrent.Hidden.Contains(gpBreeze)) { lActions.Add("feel-breeze " + sX + " " + sY); } GroundedPredicate gpGold = new GroundedPredicate("gold-at"); gpGold.AddConstant(gpAtX.Constants[0]); gpGold.AddConstant(gpAtY.Constants[0]); if (pssCurrent.Hidden.Contains(gpGold)) { lActions.Add("observe-gold " + sX + " " + sY); } if (lActions.Count == 0) { List <string> lNotVisited = new List <string>(); List <string> lSafe = new List <string>(); if (iX > 1) { if (!VisitedLocations.Contains((iX - 1) * 1000 + iY)) { lNotVisited.Add("move-left"); } if (pssCurrent.Observed.Contains(GetSafe(iX - 1, iY))) { lSafe.Add("move-left"); } } if (iX < Size) { if (!VisitedLocations.Contains((iX + 1) * 1000 + iY)) { lNotVisited.Add("move-right"); } if (pssCurrent.Observed.Contains(GetSafe(iX + 1, iY))) { lSafe.Add("move-right"); } } if (iY > 1) { if (!VisitedLocations.Contains(iX * 1000 + (iY - 1))) { lNotVisited.Add("move-up"); } if (pssCurrent.Observed.Contains(GetSafe(iX, iY - 1))) { lSafe.Add("move-up"); } } if (iY < Size) { if (!VisitedLocations.Contains(iX * 1000 + (iY + 1))) { lNotVisited.Add("move-down"); } if (pssCurrent.Observed.Contains(GetSafe(iX, iY + 1))) { lSafe.Add("move-down"); } } List <string> lSafeAndNotVisited = new List <string>(lSafe.Intersect(lNotVisited)); if (lSafeAndNotVisited.Count > 0) { int idx = RandomGenerator.Next(lSafeAndNotVisited.Count); lActions.Add(lSafeAndNotVisited[idx]); } else if (lSafe.Count > 0) { int idx = RandomGenerator.Next(lSafe.Count); lActions.Add(lSafe[idx]); } else { int idx = RandomGenerator.Next(4); if (idx == 0) { lActions.Add("move-down"); } if (idx == 1) { lActions.Add("move-up"); } if (idx == 2) { lActions.Add("move-left"); } if (idx == 3) { lActions.Add("move-right"); } } } return(lActions); }
public static List <string> MasterMindHeuristic(PartiallySpecifiedState pssCurrent, Domain domain) { List <Constant> lColors = new List <Constant>(); List <Constant> lPegs = new List <Constant>(); List <Constant> lValues = new List <Constant>(); foreach (Constant c in domain.Constants) { if (c.Type.ToLower() == "color") { lColors.Add(c); } if (c.Type.ToLower() == "peg") { lPegs.Add(c); } if (c.Type.ToLower() == "value") { lValues.Add(c); } } //DateTime dtStart = DateTime.Now; //BeliefState bs = pssCurrent.m_bsInitialBelief; //State s = bs.ChooseState(true); //List<Predicate> l = bs.RunSatSolver(); //Console.WriteLine((DateTime.Now - dtStart).TotalSeconds); Permute(lColors); List <Constant> lGuessColors = new List <Constant>(); for (int iPeg = 0; iPeg < lPegs.Count; iPeg++) { /* * //foreach (GroundedPredicate gp in s.Predicates) * foreach (GroundedPredicate gp in l) * { * if (gp.ToString().StartsWith("(on p" + iPeg) && gp.Negation == false) * { * lGuessColors.Add(gp.Constants[1]); * } * * * } */ foreach (Constant cColor in lColors) { GroundedPredicate gp = new GroundedPredicate("on"); gp.AddConstant(lPegs[iPeg]); gp.AddConstant(cColor); if (!pssCurrent.Observed.Contains(gp.Negate())) { lGuessColors.Add(cColor); break; } } lColors.Remove(lGuessColors.Last()); } if (lGuessColors.Count == lPegs.Count) { List <string> lActions = new List <string>(); string sGuess = "guess-all"; foreach (Constant c in lGuessColors) { sGuess += " " + c.Name; } lActions.Add(sGuess); lActions.Add("evaluate-guess"); foreach (Constant cValue in lValues) { lActions.Add("observe-LocationHits " + cValue.Name); lActions.Add("observe-ColorHits " + cValue.Name); } return(lActions); } return(null); }
public static List <string> MineSweeperHeuristic(PartiallySpecifiedState pssCurrent, Domain d, Formula fObserve) { int[,] aBoard; aBoard = new int[Size, Size]; List <string> lActions = new List <string>(); List <int> lUnknown = new List <int>(); for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { GroundedPredicate gp = GetPredicate("opened", iX, iY); if (pssCurrent.Observed.Contains(gp)) { aBoard[iX, iY] = 1;//opened } else { gp = GetPredicate("mine-at", iX, iY); if (pssCurrent.Observed.Contains(gp)) { gp = GetPredicate("flagged", iX, iY); if (pssCurrent.Observed.Contains(gp)) { aBoard[iX, iY] = 4;//flagged mine } else { aBoard[iX, iY] = 2;//unflagged mine } } else if (pssCurrent.Observed.Contains(gp.Negate())) { aBoard[iX, iY] = 3;//no mine } } } } List <int> lCandidates = new List <int>(); for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { if (aBoard[iX, iY] == 0 || aBoard[iX, iY] == 3) { lCandidates.Add((iX) * 1000 + iY); } else if (aBoard[iX, iY] == 2) { lActions.Add("flag p" + iX + "-" + iY); } } } if (lCandidates.Count > 0) { int iChosen = lCandidates[RandomGenerator.Next(lCandidates.Count)]; int iChosenX = iChosen / 1000; int iChosenY = iChosen % 1000; lActions.Add("open-cell-" + iChosenX + "-" + iChosenY); //lActions.Add("open-cell p" + iChosenX + "-" + iChosenY); lActions.Add("observe-dead"); for (int i = 0; i < 9; i++) { lActions.Add("observe-mine-count p" + iChosenX + "-" + iChosenY + " v" + i); } Shootings++; } /* * else if (lUnknown.Count > 0) * { * int iChosen = lUnknown[RandomGenerator.Next(lUnknown.Count)]; * int iChosenX = iChosen / 1000; * int iChosenY = iChosen % 1000; * lActions.Add("shoot p-" + iChosenX + " p-" + iChosenY); * Shootings++; * } */ return(lActions); }
public static List <string> BattleshipHeuristic(PartiallySpecifiedState pssCurrent, Domain d, Formula fObserve) { int[,] aBoard; aBoard = new int[Size, Size]; List <string> lActions = new List <string>(); List <int> lUnknown = new List <int>(); //List<GroundedPredicate>[] aShips = new List<GroundedPredicate>[ShipCount]; /* * for (int iShip = 0; iShip < ShipCount; iShip++) * { * aShips[iShip] = new List<GroundedPredicate>(); * } */ for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { GroundedPredicate gp = GetPredicate("water-at", iX, iY); if (!pssCurrent.Observed.Contains(gp)) { gp = GetPredicate("hit", iX, iY); if (!pssCurrent.Observed.Contains(gp)) { lUnknown.Add(iX * 1000 + iY); } else { aBoard[iX, iY] = 2; } } else { aBoard[iX, iY] = 1; } } } for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { if (aBoard[iX, iY] == 2) { if (iX == 0 || aBoard[iX - 1, iY] == 1) { int i = 0; while (iX + i < Size && aBoard[iX + i, iY] == 2) { i++; } if (i > 1 && (iX + i == Size || aBoard[iX + i, iY] == 1)) { lActions.Add("drownship-" + i + "-" + iX + "-" + iY + "-H"); return(lActions); } } if (iY == 0 || aBoard[iX, iY - 1] == 1) { int i = 0; while (iY + i < Size && aBoard[iX, iY + i] == 2) { i++; } if (i > 1 && (iY == Size || aBoard[iX, iY + i] == 1)) { lActions.Add("drownship-" + i + "-" + iX + "-" + iY + "-V"); return(lActions); } } } } } List <int> lCandidates = new List <int>(); for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { if (aBoard[iX, iY] == 2)//a hit ship { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (iX + i >= 0 && iX + i < Size && iY + j >= 0 && iY + j < Size) { if (aBoard[iX + i, iY + j] == 0) { lCandidates.Add((iX + i) * 1000 + iY + j); } } } } } } } if (false && lCandidates.Count > 0) { int iChosen = lCandidates[RandomGenerator.Next(lCandidates.Count)]; int iChosenX = iChosen / 1000; int iChosenY = iChosen % 1000; lActions.Add("shoot p-" + iChosenX + " p-" + iChosenY); Shootings++; } else if (lUnknown.Count > 0) { int iChosen = lUnknown[RandomGenerator.Next(lUnknown.Count)]; int iChosenX = iChosen / 1000; int iChosenY = iChosen % 1000; lActions.Add("shoot p-" + iChosenX + " p-" + iChosenY); Shootings++; } if (lActions.Count == 0) { for (int iX = 0; iX < Size; iX++) { for (int iY = 0; iY < Size; iY++) { Console.Write(aBoard[iX, iY]); } Console.WriteLine(); } } return(lActions); }
public static List <string> RockSampleHeuristic(PartiallySpecifiedState pssCurrent, Domain domain) { List <string> lActions = new List <string>(); int iX = -1, iY = 1; int iSize = 0; foreach (GroundedPredicate p in pssCurrent.Observed) { if (p.Name.Contains("agent-at")) { string[] a = p.Constants[0].Name.Substring(1).Split('-'); if (!p.Negation) { iX = int.Parse(a[0]); iY = int.Parse(a[1]); } if (int.Parse(a[0]) > iSize) { iSize = int.Parse(a[0]); } } } int iXNew = iX, iYNew = iY; if (iX < iSize - 1 && iY % 2 == 1) { iXNew++; } else if (iX > 1 && iY % 2 == 0) { iXNew--; } else { iYNew++; } lActions.Add("move p" + iX + "-" + iY + " p" + iXNew + "-" + iYNew); bool bValidSensorH1 = false; bool bValidSensorH0 = false; foreach (Action a in domain.Actions) { if (a.Name == "activate-sensor-at-p" + iXNew + "-" + iYNew + "-h1") { bValidSensorH1 = true; } if (a.Name == "activate-sensor-at-p" + iXNew + "-" + iYNew + "-h0") { bValidSensorH0 = true; } } if (bValidSensorH1) { lActions.Add("raise-antena"); lActions.Add("activate-sensor-at-p" + iXNew + "-" + iYNew + "-h1"); lActions.Add("observe-sensor"); lActions.Add("lower-antena"); } if (bValidSensorH0) { lActions.Add("activate-sensor-at-p" + iXNew + "-" + iYNew + "-h0"); lActions.Add("observe-sensor"); } return(lActions); }