Skip to content

Commit

Permalink
Reduces memory usage and processing time it takes to make the applica…
Browse files Browse the repository at this point in the history
…bility graph
  • Loading branch information
kris701 committed Jun 11, 2024
1 parent b09f784 commit 4cef186
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 52 deletions.
6 changes: 3 additions & 3 deletions FlashPlanner.Core/Models/TranslatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TranslatorContext
/// <summary>
/// A graph saying that when a given operator have been executed, these following operators could now be applicable
/// </summary>
public Dictionary<int, List<Operator>> ApplicabilityGraph;
public Dictionary<int, List<int>> ApplicabilityGraph;

/// <summary>
/// Main constructor
Expand All @@ -32,7 +32,7 @@ public class TranslatorContext
/// <param name="pDDL"></param>
/// <param name="factHashes"></param>
/// <param name="applicabilityGraph"></param>
public TranslatorContext(SASDecl sas, PDDLDecl pDDL, int[] factHashes, Dictionary<int, List<Operator>> applicabilityGraph)
public TranslatorContext(SASDecl sas, PDDLDecl pDDL, int[] factHashes, Dictionary<int, List<int>> applicabilityGraph)
{
SAS = sas;
PDDL = pDDL;
Expand All @@ -48,7 +48,7 @@ public TranslatorContext()
SAS = new SASDecl();
PDDL = new PDDLDecl();
FactHashes = new int[0];
ApplicabilityGraph = new Dictionary<int, List<Operator>>();
ApplicabilityGraph = new Dictionary<int, List<int>>();
}
}
}
3 changes: 2 additions & 1 deletion FlashPlanner.Core/Search/BeamS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public BeamS(IHeuristic heuristic, int beta) : base(heuristic)
{
var stateMove = ExpandBestState();
var newItems = new RefPriorityQueue<StateMove>();
foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
9 changes: 2 additions & 7 deletions FlashPlanner.Core/Search/EnforcedHillClimbingS.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using FlashPlanner.Core.Heuristics;
using FlashPlanner.Core.Models;
using FlashPlanner.Core.States;
using PDDLSharp.Models.FastDownward.Plans;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlashPlanner.Core.Search
{
Expand All @@ -30,8 +24,9 @@ public EnforcedHillClimbingS(IHeuristic heuristic) : base(heuristic)
while (!Abort && _openList.Count > 0)
{
var stateMove = ExpandBestState();
foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
3 changes: 2 additions & 1 deletion FlashPlanner.Core/Search/GreedyBFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public GreedyBFS(IHeuristic heuristic) : base(heuristic)
while (!Abort && _openList.Count > 0)
{
var stateMove = ExpandBestState();
foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
3 changes: 2 additions & 1 deletion FlashPlanner.Core/Search/GreedyBFSDHE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public GreedyBFSDHE(IHeuristic heuristic) : base(heuristic)
stateMove.hValue = Heuristic.GetValue(stateMove, stateMove.State, _context.SAS.Operators);

bool lowerFound = false;
foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
3 changes: 2 additions & 1 deletion FlashPlanner.Core/Search/GreedyBFSFocused.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public GreedyBFSFocused(IHeuristic heuristic, int numberOfMacros, int searchBudg
while (!Abort && _openList.Count > 0)
{
var stateMove = ExpandBestState();
foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
3 changes: 2 additions & 1 deletion FlashPlanner.Core/Search/GreedyBFSLazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public GreedyBFSLazy(IHeuristic heuristic) : base(heuristic)
var stateMove = ExpandBestState();
stateMove.hValue = Heuristic.GetValue(stateMove, stateMove.State, _context.SAS.Operators);

foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
3 changes: 2 additions & 1 deletion FlashPlanner.Core/Search/GreedyBFSPO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public GreedyBFSPO(IHeuristic heuristic) : base(heuristic)

var stateMove = ExpandBestState();

foreach (var op in _context.ApplicabilityGraph[stateMove.Operator])
foreach (var opID in _context.ApplicabilityGraph[stateMove.Operator])
{
var op = _context.SAS.GetOperatorByID(opID);
if (Abort) break;
if (stateMove.State.IsApplicable(op))
{
Expand Down
2 changes: 1 addition & 1 deletion FlashPlanner.Core/Translators/PDDLToSASTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public TranslatorContext Translate(PDDLDecl from)
CheckIfValid(from);

var phases = GetTranslatorPhases(from);
var result = new TranslatorContext(new SASDecl(), from, new int[0], new Dictionary<int, List<Operator>>());
var result = new TranslatorContext(new SASDecl(), from, new int[0], new Dictionary<int, List<int>>());
foreach (var phase in phases)
{
if (Abort) return new TranslatorContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FlashPlanner.Core.Models;
using FlashPlanner.Core.Models.SAS;
using FlashPlanner.Core.States;
using System.Xml.Linq;

namespace FlashPlanner.Core.Translators.Phases
{
Expand Down Expand Up @@ -33,38 +34,39 @@ public override TranslatorContext ExecutePhase(TranslatorContext from)

private TranslatorContext GenerateTotalGraph(TranslatorContext from)
{
var graphs = new Dictionary<int, List<Operator>>();
var graphs = new Dictionary<int, List<int>>();
var inits = GetInitApplicableOperators(from);
var all = from.SAS.Operators.Select(x => x.ID).ToList();
// -1 is from the initial state
graphs.Add(-1, inits.ToList());
foreach (var op in from.SAS.Operators)
graphs.Add(op.ID, from.SAS.Operators);
graphs.Add(op.ID, all);

from = new TranslatorContext(from.SAS, from.PDDL, from.FactHashes, graphs);
return from;
}

private TranslatorContext GenerateApplicabilityGraph(TranslatorContext from, Dictionary<string, List<Operator>> argGraph, Dictionary<int, List<Operator>> preGraph)
private TranslatorContext GenerateApplicabilityGraph(TranslatorContext from, Dictionary<string, List<int>> argGraph, Dictionary<int, List<int>> preGraph)
{
var graphs = new Dictionary<int, List<Operator>>();
var graphs = new Dictionary<int, List<int>>();
var inits = GetInitApplicableOperators(from);
// -1 is from the initial state
graphs.Add(-1, inits.ToList());
foreach (var op in from.SAS.Operators)
{
var possibles = new List<Operator>();
var possibles = new List<int>();
foreach (var arg in op.Arguments)
if (argGraph.ContainsKey(arg))
possibles.AddRange(argGraph[arg]);
foreach (var add in op.Add)
if (preGraph.ContainsKey(add.ID))
possibles.AddRange(preGraph[add.ID]);
possibles.AddRange(inits);
graphs.Add(op.ID, possibles.DistinctBy(x => x.ID).ToList());
graphs.Add(op.ID, possibles.Distinct().ToList());
}

foreach (var key in graphs.Keys)
graphs[key].RemoveAll(x => x.ID == key);
graphs[key].RemoveAll(x => x == key);

var total = graphs.Sum(x => x.Value.Count);
var worst = from.SAS.Operators.Count * from.SAS.Operators.Count;
Expand All @@ -74,47 +76,47 @@ private TranslatorContext GenerateApplicabilityGraph(TranslatorContext from, Dic
return from;
}

private HashSet<Operator> GetInitApplicableOperators(TranslatorContext context)
private HashSet<int> GetInitApplicableOperators(TranslatorContext context)
{
var ops = new HashSet<Operator>();
var ops = new HashSet<int>();
var initState = new SASStateSpace(context);

foreach (var op in context.SAS.Operators)
if (initState.IsApplicable(op))
ops.Add(op);
ops.Add(op.ID);

return ops;
}

private Dictionary<string, List<Operator>> GenerateOpArgumentGraph(SASDecl decl)
private Dictionary<string, List<int>> GenerateOpArgumentGraph(SASDecl decl)
{
var argGraph = new Dictionary<string, List<Operator>>();
var argGraph = new Dictionary<string, List<int>>();
foreach (var op in decl.Operators)
{
foreach (var arg in op.Arguments)
{
if (argGraph.ContainsKey(arg))
argGraph[arg].Add(op);
argGraph[arg].Add(op.ID);
else
argGraph.Add(arg, new List<Operator>() { op });
argGraph.Add(arg, new List<int>() { op.ID });
}
}
foreach (var key in argGraph.Keys)
argGraph[key] = argGraph[key].Distinct().ToList();
return argGraph;
}

private Dictionary<int, List<Operator>> GeneratePreconditionGraph(SASDecl decl)
private Dictionary<int, List<int>> GeneratePreconditionGraph(SASDecl decl)
{
var preGraph = new Dictionary<int, List<Operator>>();
var preGraph = new Dictionary<int, List<int>>();
foreach (var op in decl.Operators)
{
foreach (var pre in op.Pre)
{
if (preGraph.ContainsKey(pre.ID))
preGraph[pre.ID].Add(op);
preGraph[pre.ID].Add(op.ID);
else
preGraph.Add(pre.ID, new List<Operator>() { op });
preGraph.Add(pre.ID, new List<int>() { op.ID });
}
}
foreach (var key in preGraph.Keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private List<Operator> GetPotentiallyReachableOperators(SASDecl decl)
{
bool any = true;
bool[] covered = new bool[decl.Operators.Count];
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[decl.Facts], new Dictionary<int, List<Operator>>()));
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[decl.Facts], new Dictionary<int, List<int>>()));
var applicables = new List<Operator>();
while (any)
{
Expand Down
8 changes: 4 additions & 4 deletions FlashPlanner.Tests/Heuristics/hGoalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Can_GeneratehGoalCorrectly_NoGoals()
decl.Goal[0].ID = 0;
decl.Facts = 1;
var h = new hGoal();
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[0], new Dictionary<int, List<Operator>>()));
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[0], new Dictionary<int, List<int>>()));
var parent = new StateMove(state);

// ACT
Expand All @@ -51,7 +51,7 @@ public void Can_GeneratehGoalCorrectly_OneGoal()

var decl = new SASDecl(new List<Operator>(), goals.ToArray(), inits.ToArray(), 1);
var h = new hGoal();
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[0], new Dictionary<int, List<Operator>>()));
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[0], new Dictionary<int, List<int>>()));
var parent = new StateMove(state);

// ACT
Expand Down Expand Up @@ -80,7 +80,7 @@ public void Can_GeneratehGoalCorrectly_MultipleGoals_1()

var decl = new SASDecl(new List<Operator>(), goals.ToArray(), inits.ToArray(), 3);
var h = new hGoal();
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[3], new Dictionary<int, List<Operator>>()));
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[3], new Dictionary<int, List<int>>()));
var parent = new StateMove(state);

// ACT
Expand Down Expand Up @@ -112,7 +112,7 @@ public void Can_GeneratehGoalCorrectly_MultipleGoals_2()
var decl = new SASDecl(new List<Operator>(), goals.ToArray(), inits.ToArray(), 3);

var h = new hGoal();
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[0], new Dictionary<int, List<Operator>>()));
var state = new SASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[0], new Dictionary<int, List<int>>()));
var parent = new StateMove(state);

// ACT
Expand Down
14 changes: 7 additions & 7 deletions FlashPlanner.Tests/Models/BitMaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public void Can_SetEntireArrayToZero()
}

[TestMethod]
[DataRow(0,0)]
[DataRow(1,0,true)]
[DataRow(1,1,true,false)]
[DataRow(2,1,true,false,true)]
[DataRow(4,1,true,true,true,false,true)]
[DataRow(4,2,true,true,true,false,true, false)]
[DataRow(0, 0)]
[DataRow(1, 0, true)]
[DataRow(1, 1, true, false)]
[DataRow(2, 1, true, false, true)]
[DataRow(4, 1, true, true, true, false, true)]
[DataRow(4, 2, true, true, true, false, true, false)]
public void Can_SetSomeValues(int expectedTrue, int expectedFalse, params bool[] targets)
{
// ARRANGE
Expand Down Expand Up @@ -119,7 +119,7 @@ public void Can_EqualsIfTheSame(params bool[] targets)
// ARRANGE
var mask1 = new BitMask(targets.Length);
var mask2 = new BitMask(targets.Length);
for(int i = 0; i < targets.Length; i++)
for (int i = 0; i < targets.Length; i++)
{
mask1[i] = targets[i];
mask2[i] = targets[i];
Expand Down
6 changes: 3 additions & 3 deletions FlashPlanner.Tests/RelaxedPlanningGraphs/OperatorRPGTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void Cant_GenerateGraph_IfNoApplicableActions_1()
goal.Add(new Fact("abc"));
goal.ElementAt(0).ID = 0;
var decl = new SASDecl(new List<Operator>(), goal.ToArray(), new Fact[0], 1);
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[1], new Dictionary<int, List<Operator>>()));
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[1], new Dictionary<int, List<int>>()));
var generator = new OperatorRPG();

// ACT
Expand All @@ -159,7 +159,7 @@ public void Cant_GenerateGraph_IfNoApplicableActions_2()
goal.Add(new Fact("abc"));
goal.ElementAt(0).ID = 0;
var decl = new SASDecl(new List<Operator>(), goal.ToArray(), new Fact[0], 2);
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[2], new Dictionary<int, List<Operator>>()));
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[2], new Dictionary<int, List<int>>()));

var actions = new List<Operator>()
{
Expand Down Expand Up @@ -188,7 +188,7 @@ public void Cant_GenerateGraph_IfActionDoesNothing()
goal.Add(new Fact("abc"));
goal.ElementAt(0).ID = 0;
var decl = new SASDecl(new List<Operator>(), goal.ToArray(), new Fact[0], 1);
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[1], new Dictionary<int, List<Operator>>()));
var state = new RelaxedSASStateSpace(new TranslatorContext(decl, new PDDLDecl(), new int[1], new Dictionary<int, List<int>>()));

var operators = new List<Operator>()
{
Expand Down
2 changes: 1 addition & 1 deletion FlashPlanner/FlashPlanner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<PropertyGroup>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<Version>1.2.13</Version>
<Version>1.2.14</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion FlashPlanner/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"FlashPlanner.CLI": {
"commandName": "Project",
"commandLineArgs": "--domain ../../../../Dependencies/FocusedMetaActionsData/Benchmarks/gripper/domain.pddl --problem ../../../../Dependencies/FocusedMetaActionsData/Benchmarks/gripper/testing/p01.pddl --search \"ehc(hFF())\" --validate\""
"commandLineArgs": "--domain ../../../../Dependencies/FocusedMetaActionsData/Benchmarks/scanalyzer/domain.pddl --problem ../../../../Dependencies/FocusedMetaActionsData/Benchmarks/scanalyzer/testing/p05.pddl --search \"greedy(hFF())\" --validate\""
}
}
}

0 comments on commit 4cef186

Please sign in to comment.