Skip to content

Commit

Permalink
Adding support for iteration limit, updating expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
merschformann committed Oct 10, 2024
1 parent 8afbbbb commit 2a4f7ba
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 127 deletions.
2 changes: 1 addition & 1 deletion SC.Heuristics/Heuristic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class Heuristic : IMethod
/// <summary>
/// Indicates whether the timelimit is reached. <code>true</code> if the timelimit is exceeded, <code>false</code> otherwise.
/// </summary>
public bool TimeUp { get { return (TimeSpan.MaxValue == Config.TimeLimit) ? false : DateTime.Now > TimeStart + Config.TimeLimit; } }
public bool TimeUp { get { return TimeSpan.MaxValue != Config.TimeLimit && DateTime.Now > TimeStart + Config.TimeLimit; } }

/// <summary>
/// The overall available volume across all containers
Expand Down
5 changes: 4 additions & 1 deletion SC.Heuristics/PrimalHeuristic/ALNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ protected override void Solve()
currentIteration++;
currentIntervalIteration++;

} while (currentIteration - lastImprovement < Config.StagnationDistance && !Cancelled && !TimeUp);
} while (currentIteration - lastImprovement < Config.StagnationDistance &&
!Cancelled &&
!TimeUp &&
IterationsReached(currentIteration));
}

#endregion
Expand Down
5 changes: 5 additions & 0 deletions SC.Heuristics/PrimalHeuristic/PointInsertionSkeletonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public abstract partial class PointInsertionSkeleton : Heuristic
/// Checks whether a solution is available. (Since an empty solution is always valid, a valid solution is always available)
/// </summary>
public override bool HasSolution { get { return true; } }

/// <summary>
/// Indicates whether the maximum number of iterations is reached.
/// </summary>
public bool IterationsReached(int current) => Config.IterationLimit >= 0 && current >= Config.IterationLimit;
}
}
7 changes: 6 additions & 1 deletion SC.Heuristics/PrimalHeuristic/PointInsertionSkeletonLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2249,8 +2249,13 @@ protected COSolution GASP(COSolution solution, List<Container> containers, List<
// Init solution
COSolution localSolution = Instance.CreateSolution(Config.Tetris, Config.MeritType, true);



// Start improvement
while (currentIteration - lastImprovement < Config.StagnationDistance && !Cancelled && !TimeUp)
while (currentIteration - lastImprovement < Config.StagnationDistance &&
!Cancelled &&
!TimeUp &&
!IterationsReached(currentIteration))
{
// Break if all pieces packed (can only break when minimization of container count is impossible)
if (solution.NumberOfContainersInUse == 1 && solution.NumberOfPiecesPacked == Instance.Pieces.Count)
Expand Down
7 changes: 6 additions & 1 deletion SC.ObjectModel/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Configuration(MethodType method, bool handleTetris)
{
// Set some default time-limit to not accidentally cause infinite runs
TimeLimit = TimeSpan.FromSeconds(10);

// Base settings
Type = method;
HandleGravity = true;
Expand Down Expand Up @@ -256,6 +256,11 @@ public Configuration(MethodType method, bool handleTetris)
/// </summary>
public double TimeLimitInSeconds { get { return TimeLimit.TotalSeconds; } set { TimeLimit = TimeSpan.FromSeconds(value); } }

/// <summary>
/// The maximum number of iterations for the solution process. A negative value indicates no limit.
/// </summary>
public int IterationLimit { get; set; } = -1;

/// <summary>
/// Indicates whether to respect the gravity contraint or not
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion SC.Tests/Golden.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public void GoldenInputs(string inputFile)
var instance = Instance.ReadJson(File.ReadAllText(inputFile));

// Run calculation
var result = Executor.Execute(instance, new Configuration() { TimeLimitInSeconds = 1 }, Console.WriteLine);
var result = Executor.Execute(instance, new Configuration()
{
TimeLimitInSeconds = 1,
ThreadLimit = 1,
IterationLimit = 100,
}, Console.WriteLine);

var update = Environment.GetEnvironmentVariable("UPDATE") == "1";
if (update)
Expand Down
166 changes: 83 additions & 83 deletions SC.Tests/data/sample_contained.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,12 @@
"height": 300,
"assignments": [
{
"piece": 0,
"piece": 3,
"position": {
"x": 0,
"y": 0,
"z": 0,
"a": 0,
"b": 0,
"c": 180
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 304,
"width": 336,
"height": 140
}
]
},
{
"piece": 9,
"position": {
"x": 304,
"y": 0,
"z": 0,
"a": 270,
"a": 90,
"b": 0,
"c": 0
},
Expand All @@ -42,51 +21,51 @@
"x": 0,
"y": 0,
"z": 0,
"length": 60,
"width": 170,
"height": 183
"length": 271,
"width": 393,
"height": 214
}
]
},
{
"piece": 6,
"piece": 5,
"position": {
"x": 364,
"x": 271,
"y": 0,
"z": 0,
"a": 0,
"b": 90,
"b": 0,
"c": 90
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 234,
"width": 376,
"height": 294
"length": 245,
"width": 351,
"height": 287
}
]
},
{
"piece": 8,
"piece": 7,
"position": {
"x": 0,
"x": 516,
"y": 0,
"z": 140,
"z": 0,
"a": 0,
"b": 270,
"c": 0
"b": 0,
"c": 90
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 274,
"width": 395,
"height": 154
"length": 78,
"width": 241,
"height": 115
}
]
}
Expand All @@ -99,33 +78,33 @@
"height": 300,
"assignments": [
{
"piece": 5,
"piece": 4,
"position": {
"x": 0,
"y": 0,
"z": 0,
"a": 0,
"b": 0,
"c": 180
"c": 0
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 351,
"width": 245,
"width": 398,
"height": 287
}
]
},
{
"piece": 1,
"piece": 2,
"position": {
"x": 0,
"y": 245,
"x": 351,
"y": 0,
"z": 0,
"a": 90,
"a": 0,
"b": 0,
"c": 90
},
Expand All @@ -134,80 +113,101 @@
"x": 0,
"y": 0,
"z": 0,
"length": 367,
"width": 122,
"height": 245
"length": 145,
"width": 392,
"height": 152
}
]
},
{
"piece": 7,
"piece": 1,
"position": {
"x": 351,
"y": 0,
"z": 0,
"z": 152,
"a": 0,
"b": 90,
"c": 270
"c": 90
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 78,
"width": 115,
"height": 241
"length": 245,
"width": 367,
"height": 122
}
]
}
]
},
{
"id": 2,
"length": 500,
"width": 400,
"height": 300,
"assignments": [
{
"piece": 0,
"position": {
"x": 0,
"y": 0,
"z": 0,
"a": 0,
"b": 0,
"c": 0
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 304,
"width": 336,
"height": 140
}
]
},
{
"piece": 2,
"piece": 8,
"position": {
"x": 429,
"x": 304,
"y": 0,
"z": 0,
"a": 0,
"b": 0,
"c": 270
"c": 0
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 145,
"width": 392,
"height": 152
"length": 154,
"width": 395,
"height": 274
}
]
}
]
},
{
"id": 2,
"length": 500,
"width": 400,
"height": 300,
"assignments": [
},
{
"piece": 4,
"piece": 9,
"position": {
"x": 0,
"y": 0,
"y": 336,
"z": 0,
"a": 180,
"a": 0,
"b": 0,
"c": 270
"c": 90
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 398,
"width": 351,
"height": 287
"length": 183,
"width": 60,
"height": 170
}
]
}
Expand All @@ -220,23 +220,23 @@
"height": 250,
"assignments": [
{
"piece": 3,
"piece": 6,
"position": {
"x": 0,
"y": 0,
"z": 0,
"a": 90,
"b": 0,
"c": 180
"c": 0
},
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 271,
"width": 393,
"height": 214
"length": 294,
"width": 376,
"height": 234
}
]
}
Expand Down
Loading

0 comments on commit 2a4f7ba

Please sign in to comment.