Skip to content

Commit e7f351b

Browse files
committed
Relax cost ordering
1 parent 262a478 commit e7f351b

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

Runtime/Details/NodeSet.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
namespace Activ.GOAP{
55
public class NodeSet<T> : Base{
66

7-
internal bool sorted;
8-
int capacity;
7+
float precision = 0;
8+
internal bool sorted;
9+
int capacity;
910
Func<T, float> h;
1011
HashSet<T> states = new HashSet<T>();
1112
List<Node<T>> list = new List<Node<T>>();
1213

1314
public NodeSet(T x, Func<T, float> h, bool sorted = true,
14-
int capacity = 128){
15-
this.h = h; this.sorted = sorted; this.capacity = capacity;
15+
int capacity = 128,
16+
float precision = 0){
17+
this.h = h;
18+
this.sorted = sorted;
19+
this.capacity = capacity;
20+
this.precision = precision;
1621
states.Add(Assert(x, "Initial state"));
1722
list.Add(new Node<T>(Solver<T>.INIT, x));
1823
}
@@ -28,6 +33,7 @@ public void Insert(Node<T> n){
2833
if(!states.Add(n.state)) return;
2934
if(sorted){
3035
n.value = n.cost + (h != null ? h(n.state) : 0);
36+
if(precision > 0) n.value = (int)(n.value / precision);
3137
// NOTE: In actual use, tested 4x faster than SortedSet;
3238
// Likely bottleneck with SortedSet is the API, not the
3339
// algorithm; SortedSet needs total, ordering:
@@ -39,7 +45,7 @@ public void Insert(Node<T> n){
3945
// Also getting Min/Max item is cheap, but there's no way
4046
// to combine this with a 'pop'
4147
for(int i = list.Count-1; i >= 0; i--){
42-
if(n.value < list[i].value){
48+
if(n.value <= list[i].value){
4349
list.Insert(i + 1, n);
4450
return;
4551
}

Runtime/GameAI.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public virtual void Update(){
4444
var model = Model();
4545
if(model == null) return;
4646
solver = solver ?? new Solver<T>();
47-
solver.maxNodes = config.maxNodes;
48-
solver.maxIter = config.maxIter;
47+
solver.maxNodes = config.maxNodes;
48+
solver.maxIter = config.maxIter;
49+
solver.tolerance = config.tolerance;
4950
if(handler is ActionMap m) m.verbose = verbose;
5051
handler.Effect( solver.isRunning
5152
? solver.Iterate(config.frameBudget)?.Head()

Runtime/Solver.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ public class Solver<T> : SolverStats{
1111
const string ZERO_COST_ERR = "Zero cost op is not allowed",
1212
NO_INIT = "Init state is null";
1313
//
14-
public int maxNodes = 1000,
15-
maxIter = 1000;
16-
public bool brfs = false;
14+
public int maxNodes = 1000,
15+
maxIter = 1000;
16+
public float tolerance = 0f;
17+
public bool brfs = false;
1718
public PlanningState state { get; private set; }
1819
public int fxMaxNodes { get; private set; }
1920
public int I { get; private set; }
@@ -28,7 +29,7 @@ public Node<T> Next(T s, in Goal<T> g, int iter=-1){
2829
if(s == null) throw new NullRef(NO_INIT);
2930
initialState = s;
3031
goal = g;
31-
avail = new NodeSet<T>(s, g.h, !brfs, maxNodes);
32+
avail = new NodeSet<T>(s, g.h, !brfs, maxNodes, tolerance);
3233
I = 0;
3334
return Iterate(iter);
3435
}

Runtime/SolverParams.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
namespace Activ.GOAP{
22
[System.Serializable] public class SolverParams{
33

4-
public int frameBudget = 25;
5-
public int maxNodes = 1000;
6-
public int maxIter = 1000;
4+
public int frameBudget = 25;
5+
public int maxNodes = 1000;
6+
public int maxIter = 1000;
7+
public float tolerance = 0;
78

89
}}

0 commit comments

Comments
 (0)