Skip to content

Commit

Permalink
Don't loop forever on RotateOptimize
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Dec 9, 2023
1 parent 15b88e4 commit 610f541
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ I want to thank [teoxoy](https://github.com/teoxoy), author of the awesome Facto

### Planner quality

| Electric pole | Add beacons | Overlap beacons | Pipe count | Pole count | Beacon count | Effect count |
| -------------------- | ----------- | --------------- | ------------------ | ------------------ | ------------------ | ------------------ |
| small-electric-pole | True | True | 43.172413793103445 | 33.39655172413793 | 79.24137931034483 | 104.93103448275862 |
| medium-electric-pole | True | True | 43.172413793103445 | 25.82758620689655 | 79.24137931034483 | 104.93103448275862 |
| substation | True | True | 43.172413793103445 | 7.896551724137931 | 79.24137931034483 | 104.93103448275862 |
| big-electric-pole | True | True | 47.36206896551724 | 34.05172413793103 | 75.24137931034483 | 100.01724137931035 |
| small-electric-pole | True | False | 41.51724137931034 | 13.568965517241379 | 5.948275862068965 | 11.017241379310345 |
| medium-electric-pole | True | False | 41.51724137931034 | 11.224137931034482 | 5.948275862068965 | 11.017241379310345 |
| substation | True | False | 41.51724137931034 | 4.241379310344827 | 5.948275862068965 | 11.017241379310345 |
| big-electric-pole | True | False | 41.793103448275865 | 10.827586206896552 | 5.9655172413793105 | 11.017241379310345 |
| small-electric-pole | False | N/A | 83.93103448275862 | 13.068965517241379 | 0 | 0 |
| medium-electric-pole | False | N/A | 83.93103448275862 | 10.293103448275861 | 0 | 0 |
| substation | False | N/A | 83.93103448275862 | 3.896551724137931 | 0 | 0 |
| big-electric-pole | False | N/A | 84.13793103448276 | 9.03448275862069 | 0 | 0 |
| Electric pole | Add beacons | Overlap beacons | Pipe count | Pole count | Beacon count | Effect count |
| -------------------- | ----------- | --------------- | ------------------ | ------------------ | ----------------- | ------------------ |
| small-electric-pole | True | True | 46.50847457627118 | 34.67796610169491 | 82.23728813559322 | 111.54237288135593 |
| medium-electric-pole | True | True | 46.50847457627118 | 26.728813559322035 | 82.23728813559322 | 111.54237288135593 |
| substation | True | True | 46.50847457627118 | 8.169491525423728 | 82.23728813559322 | 111.54237288135593 |
| big-electric-pole | True | True | 50.40677966101695 | 35.47457627118644 | 77.9322033898305 | 105.98305084745763 |
| small-electric-pole | True | False | 44.88135593220339 | 14.440677966101696 | 6.271186440677966 | 11.813559322033898 |
| medium-electric-pole | True | False | 44.88135593220339 | 11.830508474576272 | 6.271186440677966 | 11.813559322033898 |
| substation | True | False | 44.88135593220339 | 4.4576271186440675 | 6.271186440677966 | 11.813559322033898 |
| big-electric-pole | True | False | 45.152542372881356 | 11.610169491525424 | 6.288135593220339 | 11.813559322033898 |
| small-electric-pole | False | N/A | 88.79661016949153 | 13.76271186440678 | 0 | 0 |
| medium-electric-pole | False | N/A | 88.79661016949153 | 10.796610169491526 | 0 | 0 |
| substation | False | N/A | 88.79661016949153 | 4.101694915254237 | 0 | 0 |
| big-electric-pole | False | N/A | 89 | 9.830508474576272 | 0 | 0 |

### Runtime performance

Expand Down
23 changes: 20 additions & 3 deletions src/FactorioTools/OilField/Steps/RotateOptimize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ public static void Execute(Context parentContext, HashSet<Location> pipes)

var context = new ChildContext(parentContext, pipes);

// Visualizer.Show(existingPipeGrid, intersections.Select(l => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(l.X, l.Y)), Array.Empty<DelaunatorSharp.IEdge>());
// Visualizer.Show(context.ExistingPipeGrid, context.Intersections.Select(l => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(l.X, l.Y)), Array.Empty<DelaunatorSharp.IEdge>());

var modified = true;
var previousPipeCount = int.MaxValue;

while (modified)
// Some oil fields have multiple optimal configurations with the same pipe count. Allow up to 5 of these to be
// attempted before settling on one.
var allowedSamePipeCounts = 5;

while (modified && (context.Pipes.Count < previousPipeCount || allowedSamePipeCounts > 0))
{
var changedTerminal = false;
foreach (var terminals in context.CenterToTerminals.Values)
Expand Down Expand Up @@ -64,6 +69,14 @@ public static void Execute(Context parentContext, HashSet<Location> pipes)
}

modified = changedTerminal || shortenedPath;
if (previousPipeCount == context.Pipes.Count)
{
allowedSamePipeCounts--;
}
else
{
previousPipeCount = context.Pipes.Count;
}
}
}

Expand Down Expand Up @@ -116,7 +129,7 @@ private static bool UseBestTerminal(ChildContext context, TerminalLocation origi
#if USE_SHARED_INSTANCES
var newPath = minPath == context.ParentContext.SharedInstances.LocationListA ? context.ParentContext.SharedInstances.LocationListB : context.ParentContext.SharedInstances.LocationListA;
#else
var newPath = new List<Location>();
var newPath = new List<Location>();
#endif
var result = AStar.GetShortestPath(context.ParentContext.SharedInstances, context.Grid, terminalCandidate, context.Pipes, outputList: newPath);
if (result.ReachedGoal.HasValue)
Expand Down Expand Up @@ -166,6 +179,8 @@ private static bool UseBestTerminal(ChildContext context, TerminalLocation origi
EliminateOtherTerminals(context.ParentContext, minTerminal);
}

// Console.WriteLine($"New best terminal: {minTerminal} -> {minPath.Last()}");

context.UpdateIntersectionsAndGoals();

/*
Expand Down Expand Up @@ -252,6 +267,8 @@ private static bool UseShortestPath(
Visualizer.Show(clone, originalPath.Select(l => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(l.X, l.Y)), Array.Empty<DelaunatorSharp.IEdge>());
*/

// Console.WriteLine($"Shortened path: {result.Path[0]} -> {result.Path.Last()}");

return true;
}
finally
Expand Down
3 changes: 3 additions & 0 deletions test/FactorioTools.Test/OilField/blueprints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@
0eJyU1MFuhCAQBuB3mTMHBdxdeZWm2bjupKFdkSg2NcZ3L0gPTduEv0cRP3WYfza6PRb2k3WBzEa2H91M5mmj2b647pHWXDcwGfLL4F+7/o0EhdWnFRt4oF2QdXf+IFPvz4LYBRssZ+O4WK9uGW48xQ3iD8uPc3xgdOlNCdGCVjIqsnc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/9ybQvV3Pq3vv1l17r1/tsWjOc98PHevtpKjNeBr3SjXlvbpXp+l3+Ma29vVYm92M3drkw7h8ub/15v8nHaUD1D2s4nKYfHHr9pwniY2Uu01A/cXfdMW/Ld/WtesA5gBPBOA9wbp5dWMYJwAXBuEDWblVwzTIukrVLGJcALtqCWy3jGoCzzR3n3DJuRdbOYpyt+aNFPOJF5LdriRiuPFwHomeJGSlwHlFD6sKLgBd4mBGPyBE95xE70ny/wA5N/nKe3Z3na8AjfoSAeY74oYOUB/LsLK99iOd4pfcgz87z/CEe8SPWnBd4fUG8yOsB4iVeTz1oRq7h+RMLeKh/cJ4OXeY1nEf8SJbziB/pifk94YcAfz3yw3Fe4P1SQL3XLezy+pU8SwI84ocU3wT4odGi9R7xiB9x5oH+pq2f+htAPxLUPwLnET+aeX4gf0L80E2Y8sibh/A8I94T+yvEizx/iEf8UImUB/yVhtdTxCN+6CZs4kWQv0D8iMJ5yI+S5whepIPj/RzxUP8o9T4K4BE/kuc8tL+a5wfqXyB+6KZu4iXyfJEfZb+LeMQPDb3yHurfdEZ0Pzdq/xw8VeY7H0/zgNsvAAAA//8DAFxbEYk=

0 comments on commit 610f541

Please sign in to comment.