public List <string> Plan() { bool bAllAgentsEqual = CheckAgentEquality(); int i = 200;// countOfLandmarks; int[] statesNum = new int[agents.Count]; for (int k = 0; k < statesNum.Length; k++) { statesNum[k] = 0; } Vertex rootVertex = new Vertex(statesNum, new List <Action>(), 0, countOfLandmarks, 0, null); //DateTime begin = DateTime.Now; int count = 0; Vertex curentVertex = null; DateTime dtStart = DateTime.Now; DateTime begin = DateTime.Now; List <Vertex> queue = new List <Vertex>(); HashSet <int[]> lVisited = new HashSet <int[]>(new ComparerArray()); HashSet <Vertex> lVisited2 = new HashSet <Vertex>(); //PriorityQueue<Vertex, double> queue2 = new PriorityQueue<Vertex, double>(Vertex.ComparerDouble); //PriorityQueue<Vertex, double> queue2 = new PriorityQueue<Vertex, double>(); //Heap heap = new Heap(1000000); //queue2.Insert(rootVertex, rootVertex.h); //heap.Insert(rootVertex); queue.Add(rootVertex); lVisited.Add(rootVertex.statesNubmber); lVisited2.Add(rootVertex); // double priority = -1 * (rootVertex.h + (double)(rootVertex.g) / 100000 ); //queue2.Enqueue(rootVertex, priority); int temp = 0; List <string> lplan = null;// new List<string>(); bool flag = true; int c = 0; int cResets = 0; double minh = 1000; int blindCounter = 0; TimeSpan tsDeadendDetection = new TimeSpan(); while (queue.Count > 0) { c++; if (c % 30 == 0) { //Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bExpanded: " + c + ", open: " + queue.Count + ", h: " + curentVertex.h + ", T: " + (int)(DateTime.Now - dtStart).TotalSeconds); Console.Write("\rExpanded: " + c + ", open: " + queue.Count + ", h: " + curentVertex.h + ", h2: " + curentVertex.h2 + ", T: " + (int)(DateTime.Now - dtStart).TotalSeconds + ", deadend = " + (int)tsDeadendDetection.TotalSeconds); if (queue.Count > 200000) { return(null); Console.WriteLine(); Console.WriteLine("Queue too large, breaking."); } } flag = true; //curentVertex = queue[0]; // List<PriorityQueueItem<Vertex,double>> v = queue2.ToList(); //curentVertex = queue2.Dequeue().Value; temp++; curentVertex = FindMin(queue);// queue[0];// heap.Remove(); // queue.Remove(curentVertex); DateTime dtBefore = DateTime.Now; bool bDeadend = curentVertex.IsDeadEnd(); tsDeadendDetection += (DateTime.Now - dtBefore); if (bDeadend) { continue; } // queue.RemoveAt(0); count++; if (false && curentVertex.BlindSearchDepth > 2) { cResets++; Vertex vLastImprovement = curentVertex; while (vLastImprovement.Parent != null && vLastImprovement.Parent.h == curentVertex.h) { vLastImprovement = vLastImprovement.Parent; } int[] newStatesIndexs = new int[agents.Count]; int k = 0; // List<State> lCurrentStartStates = new List<State>(); foreach (Agent agent in agents) { // lCurrentStartStates.Add(agent.startState); agent.ChangeInitialState(vLastImprovement.statesNubmber[k]); k++; } Distributed_Landmarks_Detection.Landmarks_Detection(agents, false); // for (int j = 0; j < agents.Count; j++) // agents[j].startState = lCurrentStartStates[j]; k = 0; foreach (Agent agent in agents) { newStatesIndexs[k] = agent.ResetStateInfo(vLastImprovement.statesNubmber[k]); k++; } curentVertex = new Vertex(newStatesIndexs, new List <Action>(), 0, countOfLandmarks, 0, null); curentVertex.History = vLastImprovement.History; curentVertex.lplan = vLastImprovement.lplan; minh = curentVertex.vectors.ElementAt(0)[0].Length; queue = new List <Vertex>(); lVisited = new HashSet <int[]>(); lVisited.Add(curentVertex.statesNubmber); lVisited2.Add(curentVertex); blindCounter = 0; } blindCounter++; // if (curentVertex.Parent != null && curentVertex.Parent.Parent != null && curentVertex.h == curentVertex.Parent.h && curentVertex.h == curentVertex.Parent.Parent.h) // Console.WriteLine("stuck!"); string isGoal = curentVertex.IsGoal(out lplan); if (isGoal.Equals("true")) { double time = ((double)((DateTime.Now.Subtract(begin)).Minutes)) * 60.0; time += ((double)((DateTime.Now.Subtract(begin)).Seconds)); time += ((double)((DateTime.Now.Subtract(begin)).Milliseconds) / 1000); //Console.WriteLine(time); //Console.WriteLine(lplan.Count); // Console.WriteLine(((double)((DateTime.Now.Subtract(begin)).Milliseconds) / 1000)); Program.times.Add(time); Program.countActions.Add(lplan.Count); Program.timeSum += time; Program.actionSum += lplan.Count; /* foreach (Action act in curentVertex.lplan) * { * sw.WriteLine(act.Name); * lplan.Add(act.Name); * } * sw.Close();*/ return(lplan); } if (!isGoal.Equals("false")) { //return null; } // Program.messages += agents.Count; int f = 0; List <Vertex> needUpDate = new List <Vertex>(); HashSet <CompoundFormula> levelPotential = new HashSet <CompoundFormula>(); int index = 1; bool con = false; List <Vertex> lExpanded = Expand(curentVertex, levelPotential, needUpDate); foreach (Vertex newVertex in lExpanded) { if (newVertex.actionNumber > 0) { f++; queue.Add(newVertex); } f++; newVertex.g += (double)index / 1000000; bool bVisited = false; if (bAllAgentsEqual) { bVisited = lVisited2.Contains(newVertex); } else { bVisited = lVisited.Contains(newVertex.statesNubmber); } if (!bVisited) { queue.Add(newVertex); lVisited.Add(newVertex.statesNubmber); lVisited2.Add(newVertex); } index++; if (minh > newVertex.h) { minh = newVertex.h; Console.WriteLine(minh); } /* if (Vertex.Comparer(newVertex, curentVertex) == -1 && !con) * { * blindCounter = 0; * }*/ } /*foreach (Vertex v in needUpDate) * { * v.publicPredicateImplications.Add(levelPotential); * }*/ // curentVertex.publicPredicateImplications.Add(levelPotential); curentVertex.canGetInParallel = levelPotential; //if (curentVertex.h + curentVertex.h2 == 0) if (curentVertex.h <= 0)// + curentVertex.h2 == 0) { curentVertex.publicPredicateImplications = new List <HashSet <CompoundFormula> >(); Vertex tmp = curentVertex; while (tmp != null) { curentVertex.publicPredicateImplications.Insert(0, tmp.canGetInParallel); tmp = tmp.Parent; } } } return(null); }
public static List <string> SolveFactored(List <Domain> lDomains, List <Problem> lProblems, ref List <Agent> m_agents, Domain joinDomain) { if (Program.highLevelPlanerType != Program.HighLevelPlanerType.ProjectionMafs) { Program.projectionVersion = Program.ProjectionVersion.NULL; } Program.Start = DateTime.Now; pdbCreationTime = 0; var lGroundedProblems = GroundProblems(lDomains, lProblems); Program.internalPlaner = Program.PlanerType.ff_toActions; BuildAgents_II buildAgents = new BuildAgents_II(lDomains, lGroundedProblems, lProblems); List <Agent> agents = buildAgents.ReturnAgents(); GetPublicPredicates(agents); List <string> lPlan = null; if (Program.highLevelPlanerType == Program.HighLevelPlanerType.Projection) { Console.WriteLine("Planning"); ShareGoals(agents); AdvancedLandmarkProjectionPlaner planner = new AdvancedLandmarkProjectionPlaner(); lPlan = planner.Plan(agents, lDomains, lProblems, joinDomain); } else { try { Program.StartHighLevelPlanning = DateTime.Now; if (Program.highLevelPlanerType == Program.HighLevelPlanerType.Landmark) { Console.WriteLine("Identifying landmarks"); bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, false); Planer_I Planner = new Planer_I(agents); Console.WriteLine("Planning"); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.PDB) { DateTime startPdbCreation = DateTime.Now; PdbPlaner.pdb = new PatternDatabase(lDomains, lProblems, agents, pdbPath); pdbCreationTime = DateTime.Now.Subtract(startPdbCreation).TotalSeconds; foreach (Agent agent in agents) { agent.InitMutex(); } Distributed_Landmarks_Detection.Reset(agents); List <Landmark> PublicAndArtificialGoals = FindPublicAndArtificialGoals(agents); PdbPlaner Planner = new PdbPlaner(agents, PublicAndArtificialGoals, PdbPlaner.pdb); Console.WriteLine("Planning"); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.ForwardHsp) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } Distributed_Landmarks_Detection.Reset(agents); PlanerHsp Planner = new PlanerHsp(agents); Console.WriteLine("Planning"); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.BackwardHsp) { bool stop = false; List <GroundedPredicate> publishPublic = new List <GroundedPredicate>(); List <GroundedPredicate> nextPublishPublic = new List <GroundedPredicate>(); foreach (Agent a in agents) { publishPublic.AddRange(a.InitBackwardHspGraph()); } bool outFlag = false; while (!stop) { stop = true; outFlag = false; foreach (Agent agent in agents) { nextPublishPublic.AddRange(agent.UpdateBackwardHspGraph(publishPublic, out outFlag)); if (outFlag) { stop = false; } } publishPublic = nextPublishPublic; } foreach (Agent agent in agents) { agent.InitMutex(); } Distributed_Landmarks_Detection.Reset(agents); // agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents); PlanerHspII Planner = new PlanerHspII(agents); Console.WriteLine("Planning"); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.LandmarkAndHsp) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, false); PlanerHspAndLandmarks Planner = new PlanerHspAndLandmarks(agents); Console.WriteLine("Planning.."); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.WeightedLandmarkAndHsp) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, false); PlanerWeightedLandmarkAndHsp Planner = new PlanerWeightedLandmarkAndHsp(agents); Console.WriteLine("Planning"); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.SophisticatedProjection) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } // agents = AdvancedLandmarkProjectionAgents.CreateProjAgents(agents, lDomains, lProblems); agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, false); PlanerHspAndLandmarks Planner = new PlanerHspAndLandmarks(agents); Console.WriteLine("Planning"); lPlan = Planner.Plan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.PDBMafs) { MapsVertex.pdb = new PatternDatabase(lDomains, lProblems, agents, pdbPath); bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } Console.WriteLine("Planning"); MapsPlanner Planner = new MapsPlanner(agents, lDomains, lProblems); Program.StartGrounding = DateTime.Now; lPlan = Planner.Plan(); } else if (Program.highLevelPlanerType == Program.HighLevelPlanerType.MafsLandmark) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, true); Console.WriteLine("Planning"); MapsPlanner Planner = new MapsPlanner(agents, lDomains, lProblems); Program.StartGrounding = DateTime.Now; lPlan = Planner.Plan(); } else if (Program.highLevelPlanerType == Program.HighLevelPlanerType.Mafsff) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, true); Console.WriteLine("Planning"); MapsPlanner Planner = new MapsPlanner(agents, lDomains, lProblems); Program.StartGrounding = DateTime.Now; lPlan = Planner.PreferableFFPlan(); } else { if (Program.highLevelPlanerType == Program.HighLevelPlanerType.ProjectionMafs) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, true); Console.WriteLine("Planning"); MapsPlanner Planner = new MapsPlanner(agents, lDomains, lProblems); Program.StartGrounding = DateTime.Now; lPlan = Planner.PreferablePlan(); } else if (Program.highLevelPlanerType == Program.HighLevelPlanerType.DistrebutedProjectionMafs) { bool stop = false; while (!stop) { stop = true; string name = ""; GroundedPredicate currentGoal = null; foreach (Agent agent in agents) { currentGoal = agent.GetGoal(); if (currentGoal != null) { stop = false; name = agent.name; break; } } if (!stop) { foreach (Agent agent in agents) { if (!agent.name.Equals(name)) { agent.ReceiveGoal(currentGoal); } } } } foreach (Agent agent in agents) { agent.InitMutex(); } agents = Distributed_Landmarks_Detection.Landmarks_Detection(agents, true); Console.WriteLine("Planning"); MapsPlanner Planner = new MapsPlanner(agents, lDomains, lProblems); Program.StartGrounding = DateTime.Now; lPlan = Planner.DistrebutedPreferablePlan(); } else { Console.WriteLine("highLevelPlanerType did not selected"); } } } } } } } } } } catch (Exception ex) { RunUtils.KillPlanners(); return(null); } } Program.End = DateTime.Now; RunUtils.KillPlanners(); m_agents = agents; return(lPlan); }
public static List <Agent> CreateProjAgents(List <Agent> m_agents, List <Domain> lDomains, List <Problem> lProblems) { agents = m_agents; map = new Dictionary <string, int>(); int j = 0; foreach (Agent agent in agents) { map.Add(agent.name, j); j++; } List <Action> allProjectionAction = new List <Action>(); List <Landmark> lGoal = new List <Landmark>(); int index = 0; Distributed_Landmarks_Detection.Reset(agents); List <Predicate> predicates = new List <Predicate>(); Domain dPublic = new Domain("PublicDomain", ""); Problem pPublic = new Problem("PublicProblem", dPublic); State publicStartState = new State(pPublic); GroundedPredicate newPrePredicate = null; newPrePredicate = new GroundedPredicate(Domain.ARTIFICIAL_PREDICATE + "StartState"); publicStartState.AddPredicate(newPrePredicate); predicates.Add(newPrePredicate); //mapActionNameToAction=new Dictionary<string,Action>(); for (int i = 0; i < agents.Count; i++) { Agent agent = agents[i]; agent.mapActionNameToAction = new Dictionary <string, Action>(); foreach (Action act in agent.publicActions) { if (!agent.mapActionNameToAction.ContainsKey(act.Name)) { agent.mapActionNameToAction.Add(act.Name, act); } } agent.initLandmarksDetect(); agent.saveInfo(); State startState = new State(lProblems[i]); startState.AddPredicate(newPrePredicate); List <Action> currentlProjAction = agent.getAdvancedProjectionPublicAction(index, predicates, startState); agent.m_actions = currentlProjAction; agent.privateActions = new List <Action>(); agent.publicActions = currentlProjAction; // agent.PublicPredicates = new HashSet<GroundedPredicate>(); //allProjectionAction.AddRange(currentlProjAction); foreach (Action act in currentlProjAction) { act.agent = agent.name; //if (!agent.mapActionNameToAction.ContainsKey(act.Name)) // agent.mapActionNameToAction.Add(act.Name, act); } foreach (Predicate pred in agent.PublicPredicates) { predicates.Add(pred); // agent.PublicPredicates.Add((GroundedPredicate)pred); } agent.Predicates = new HashSet <GroundedPredicate>(); foreach (GroundedPredicate pgp in predicates) { agent.Predicates.Add(pgp); } foreach (GroundedPredicate pgp in agent.GetPublicStartState()) { startState.AddPredicate(pgp); } index += 1000; lGoal.AddRange(agent.GetPublicGoals()); /**** I implmented this in GetPublicStartState function in Agent ****/ List <GroundedPredicate> lInitialPrivate = agent.GetPrivateStartState(); foreach (KeyValuePair <GroundedPredicate, HashSet <GroundedPredicate> > pArtificial in agent.ArtificialToPrivate) { bool bAllTrue = true; foreach (GroundedPredicate gpReal in pArtificial.Value) { if (!lInitialPrivate.Contains(gpReal)) { bAllTrue = false; } } if (bAllTrue) { if (!startState.Contains(pArtificial.Key)) { startState.AddPredicate(pArtificial.Key); } } } agent.startState = startState; } return(agents); /* dPublic.Actions = allProjectionAction; * foreach (Domain d in lDomains) * { * foreach (ParametrizedPredicate pp in d.Predicates) * dPublic.Predicates.Add(pp); * foreach (string sType in d.Types) * if (!dPublic.Types.Contains(sType)) * dPublic.Types.Add(sType); * foreach (KeyValuePair<string, string> pair in d.TypeHierarchy) * dPublic.TypeHierarchy[pair.Key] = pair.Value; * foreach (Constant c in d.Constants) * dPublic.Constants.Add(c); * } * foreach (Predicate p in predicates) * { * if (p.Name.StartsWith(Domain.ARTIFICIAL_PREDICATE)) * { * ParametrizedPredicate pp = new ParametrizedPredicate(p.Name); * dPublic.Predicates.Add(pp); * } * * } * * foreach (Predicate pInit in publicStartState.Predicates) * pPublic.AddKnown(pInit); */ /*CompoundFormula goalf = new CompoundFormula("and"); * foreach (Landmark l in lGoal) * { * CompoundFormula disjGoal = new CompoundFormula("or"); * foreach (GroundedPredicate gp in l.facts.Keys) * { * disjGoal.AddOperand(gp); * } * goalf.AddOperand(disjGoal); * } * pPublic.Goal = goalf; * * bool ans; * * highLevelplan = ExternalPlanners.FFPlan(dPublic, pPublic, publicStartState, goalf, dPublic.Actions, 20 * 60000, out ans); * //highLevelplan = ExternalPlanners.FDPlan(dPublic, pPublic, publicStartState, goalf, dPublic.Actions, 20 * 60000, out ans); * if (highLevelplan == null) * return null; * // return ManualSolve(pPublic, dPublic); * string fault; * List<string> finalPlan; * if (Grounding(out finalPlan, out fault)) * return finalPlan; * //throw new NotImplementedException(); * return null;*/ }