Enhance path finding using path cost

This commit is contained in:
2026-08-11 02:01:08 +02:00
parent f0accc8df8
commit 03c91fb55a
3 changed files with 49 additions and 34 deletions
+23 -6
View File
@@ -87,19 +87,19 @@ namespace PamhagenSysCtrl.Helpers {
CreatePipe(MP1, x1);
CreatePipe(MP2, x2);
var v1 = CreateValve(1, mw1X, mpY + 70);
var v2 = CreateValve(2, mw2X, mpY + 70);
var v3 = CreateValve(3, mw2X + 30, mpY + 70);
var v4 = CreateValve(4, mw1X - 30, mpY + 70);
var v1 = CreateValve(1, mw1X, mpY + 80);
var v2 = CreateValve(2, mw2X, mpY + 80);
var v3 = CreateValve(3, mw2X + 30, mpY + 80);
var v4 = CreateValve(4, mw1X - 30, mpY + 80);
CreatePipe(x1, v1);
CreatePipe(x1, v4);
CreatePipe(x2, v2);
CreatePipe(x2, v3);
var x3 = new PipeJoin(mw1X, mpY + 100);
var x3 = new PipeJoin(mw1X, ltg1Y);
var x4 = new PipeJoin(x1X, ltg1Y);
var x5 = new PipeJoin(mw2X, mpY + 130);
var x5 = new PipeJoin(mw1X - 30, ltg2Y);
var x6 = new PipeJoin(x1X - 30, ltg2Y);
CreatePipe(v1, x3, true);
@@ -413,6 +413,23 @@ namespace PamhagenSysCtrl.Helpers {
return new($"Tank {n}", x, y, capacity, () => App.Plant?.TankFillLevels[n - 1] ?? FillState.Unknown);
}
private int PathCost(Path p) {
return
1000 * p.Hops.Count(h => h.Node is Valve) +
(p.Hops.Any(h => h.Node is Valve v && v.Nr == 15) && !p.Hops.Any(h => h.Node is Valve v && (v.Nr == 16 || v.Nr == 20)) ? 10 : 0) +
(p.Hops.Any(h => h.Node is Valve v && v.Nr == 11) && !p.Hops.Any(h => h.Node is Valve v && (v.Nr == 14 || v.Nr == 21)) ? 10 : 0) +
(p.Hops.Any(h => h.Node is Valve v && v.Nr == 6) ? 10 : 0) +
p.Bends;
}
public Path? GetFreePath(IEnumerable<INode> points, bool overrideStart = false) {
if (overrideStart) {
return GetPath(points, (n) => n is not ISink && (n is not Valve v || !v.LockedForPaths.Any(p => p.Start != points.First())), PathCost);
} else {
return GetPath(points, (n) => n is not ISink && (n is not Valve v || !v.IsLocked), PathCost);
}
}
public void TraverseSubgraph(INode start, Func<INode, bool> invalid, HashSet<IEdge>? _visited = null, Action<INode, bool, IEdge>? edgeAction = null, Action<Valve>? valveAction = null, bool reverse = false) {
var visited = _visited ?? [];
if (start is Valve v) {