Skip to content

Commit

Permalink
Merge pull request #13 from merschformann/merschformann/support-max-w…
Browse files Browse the repository at this point in the history
…eight

Adds support for MaxWeight on containers
  • Loading branch information
merschformann authored Nov 11, 2024
2 parents 9241226 + 11dbbf9 commit c4dc407
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 89 deletions.
161 changes: 161 additions & 0 deletions Material/Instances/Showcases/json_max_weight.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"name": "json_max_weight",
"containers": [
{
"id": 0,
"length": 600,
"width": 400,
"height": 300,
"maxWeight": 1000
},
{
"id": 1,
"length": 600,
"width": 400,
"height": 300,
"maxWeight": 1000
}
],
"pieces": [
{
"id": 0,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 304,
"width": 336,
"height": 140
}
]
},
{
"id": 1,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 122,
"width": 245,
"height": 367
}
]
},
{
"id": 2,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 392,
"width": 145,
"height": 152
}
]
},
{
"id": 3,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 271,
"width": 214,
"height": 393
}
]
},
{
"id": 4,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 351,
"width": 398,
"height": 287
}
]
},
{
"id": 5,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 351,
"width": 245,
"height": 287
}
]
},
{
"id": 6,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 294,
"width": 234,
"height": 376
}
]
},
{
"id": 7,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 241,
"width": 78,
"height": 115
}
]
},
{
"id": 8,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 154,
"width": 395,
"height": 274
}
]
},
{
"id": 9,
"weight": 500,
"cubes": [
{
"x": 0,
"y": 0,
"z": 0,
"length": 60,
"width": 183,
"height": 170
}
]
}
]
}
6 changes: 0 additions & 6 deletions SC.GUI/OptimizationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ class EvaluationFolderRunner
/// </summary>
private readonly string _exportDir;

/// <summary>
/// The export dir
/// </summary>
private bool _canceled;

/// <summary>
/// Time limit
/// </summary>
Expand Down Expand Up @@ -270,7 +265,6 @@ public void Run(object context)
public void Cancel()
{
_method.Cancel();
_canceled = true;
}

}
Expand Down
3 changes: 3 additions & 0 deletions SC.Heuristics/PrimalHeuristic/PointInsertionSkeletonLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ protected bool ContainerCheck(COSolution solution, Container container, Variable
// Check for flag rule compatibility
if (IsFlagRuleIncompatible(solution, container, piece))
return false;
// Check weight against MaxWeight
if (solution.ExploitedWeightOfContainers[container.VolatileID] + piece.Weight > container.MaxWeight)
return false;

// All checks passed, piece is compatible with container
return true;
Expand Down
102 changes: 49 additions & 53 deletions SC.Linear/LinearModelFLB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@ internal override LinearModel Transform()
LinearExpression.Sum(Instance.Containers.Select(c => _pieceIsInContainer[piece, c])) == 1,
"AssignToSingleContainer" + piece.ToIdentString());
}
// Ensure that the gross weight is not exceeded // TODO enable again!?
//foreach (var container in _instance.Containers)
//{
// model.AddConstraint(
// Expression.Sum(_instance.Pieces.Select(p => _pieceIsInContainer[p, container] * p.Weight)) <= containerGrossWeight,
// "GrossWeightCapacityLimitation" + container.ToIdentString());
//}
// Ensure that the pieces stay in the container
foreach (var piece in Instance.Pieces)
{
Expand Down Expand Up @@ -342,6 +335,13 @@ internal override LinearModel Transform()
"VirtualPieceFixPositionZ" + container.ToIdentString() + virtualPiece.ToIdentString());
}
}
// Ensure that the max weight is not exceeded
foreach (var container in Instance.Containers)
{
model.AddConstr(
LinearExpression.Sum(Instance.Pieces.Select(p => _pieceIsInContainer[p, container] * p.Weight)) <= container.MaxWeight,
"WeightCapacityLimitation" + container.ToIdentString());
}
// Ensure gravity only if desired
if (Config.HandleGravity)
{
Expand Down Expand Up @@ -420,15 +420,18 @@ internal override LinearModel Transform()
"LocatedOnGround" + piece.ToIdentString());
}
// Locate pieces on other pieces or the ground
foreach (var piece in Instance.Pieces)
if (Instance.Pieces.Count > 1)
{
model.AddConstr(
LinearExpression.Sum(_pieceTuples
.Where(t => t.Item1 == piece)
.Select(tuple => _locatedOn[tuple.Item1, tuple.Item2])) +
_locatedOnGround[piece]
== 1,
"LocatedOnAnotherPiece-" + piece.ToIdentString());
foreach (var piece in Instance.Pieces)
{
model.AddConstr(
LinearExpression.Sum(_pieceTuples
.Where(t => t.Item1 == piece)
.Select(tuple => _locatedOn[tuple.Item1, tuple.Item2])) +
_locatedOnGround[piece]
== 1,
"LocatedOnAnotherPiece-" + piece.ToIdentString());
}
}
}
// Ensure material compatibility only if desired
Expand Down Expand Up @@ -607,32 +610,28 @@ internal override void TransformIntermediateSolution()
foreach (var piece in Instance.Pieces)
{
// Get the container the piece is contained in
Container container = Instance.Containers.Where(c => _pieceIsInContainer[piece, c].CallbackValue > 0.5).FirstOrDefault();
var container = Instance.Containers.FirstOrDefault(c => _pieceIsInContainer[piece, c].CallbackValue > 0.5);

// If contained in a container add it to the solution
if (container != null)
// If not contained in a container, no need to add it to the solution
if (container == null) continue;

// Get orientation
int orientation;
if (_rotation[piece, 1, 1].CallbackValue > 0.5)
{
// Get orientation
int orientation = 0;
if (_rotation[piece, 1, 1].CallbackValue > 0.5)
if (_rotation[piece, 2, 2].CallbackValue > 0.5)
orientation = 0;
else
orientation = 4;
else
if (_rotation[piece, 1, 2].CallbackValue > 0.5)
if (_rotation[piece, 2, 1].CallbackValue > 0.5)
orientation = 1;
else
orientation = 17;
else
if (_rotation[piece, 2, 1].CallbackValue > 0.5)
orientation = 5;
else
orientation = 16;
// Add to solution
Solution.Add(container, piece, orientation, new MeshPoint() { X = _frontLeftBottomX[piece].CallbackValue, Y = _frontLeftBottomY[piece].CallbackValue, Z = _frontLeftBottomZ[piece].CallbackValue });
orientation = _rotation[piece, 2, 2].CallbackValue > 0.5 ? 0 : 4;
}
else if (_rotation[piece, 1, 2].CallbackValue > 0.5)
{
orientation = _rotation[piece, 2, 1].CallbackValue > 0.5 ? 1 : 17;
}
else
{
orientation = _rotation[piece, 2, 1].CallbackValue > 0.5 ? 5 : 16;
}

// Add to solution
Solution.Add(container, piece, orientation, new MeshPoint() { X = _frontLeftBottomX[piece].CallbackValue, Y = _frontLeftBottomY[piece].CallbackValue, Z = _frontLeftBottomZ[piece].CallbackValue });
}
}

Expand Down Expand Up @@ -660,23 +659,20 @@ internal override void TransformSolution()
if (container != null)
{
// Get orientation
int orientation = 0;
int orientation;
if (_rotation[piece, 1, 1].Value > 0.5)
if (_rotation[piece, 2, 2].Value > 0.5)
orientation = 0;
else
orientation = 4;
{
orientation = _rotation[piece, 2, 2].Value > 0.5 ? 0 : 4;
}
else if (_rotation[piece, 1, 2].Value > 0.5)
{
orientation = _rotation[piece, 2, 1].Value > 0.5 ? 1 : 17;
}
else
if (_rotation[piece, 1, 2].Value > 0.5)
if (_rotation[piece, 2, 1].Value > 0.5)
orientation = 1;
else
orientation = 17;
else
if (_rotation[piece, 2, 1].Value > 0.5)
orientation = 5;
else
orientation = 16;
{
orientation = _rotation[piece, 2, 1].Value > 0.5 ? 5 : 16;
}

// Add to solution
Solution.Add(container, piece, orientation, new MeshPoint() { X = _frontLeftBottomX[piece].Value, Y = _frontLeftBottomY[piece].Value, Z = _frontLeftBottomZ[piece].Value });
}
Expand Down
30 changes: 20 additions & 10 deletions SC.Linear/LinearModelHybrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ internal override LinearModel Transform()
foreach (var cube2 in piece2.Original.Components.OrderBy(c => c.ID))
{
model.AddConstr(
LinearExpression.Sum(_betas.Select(beta =>
LinearExpression.Sum(_betas.Select(beta =>
_sigmaPlus[beta, cube1, cube2, piece1, piece2] + _sigmaMinus[beta, cube1, cube2, piece1, piece2]))
>= 1,
"NonOverlapLink" + piece1.ToIdentString() + piece2.ToIdentString() + cube1.ToIdentString() + cube2.ToIdentString());
Expand Down Expand Up @@ -276,6 +276,13 @@ internal override LinearModel Transform()
}
}
}
// Ensure that the max weight is not exceeded
foreach (var container in Instance.Containers)
{
model.AddConstr(
LinearExpression.Sum(Instance.Pieces.Select(p => _itemIsPicked[p, container] * p.Weight)) <= container.MaxWeight,
"WeightCapacityLimitation" + container.ToIdentString());
}
// Ensure gravity-handling only if desired
if (Config.HandleGravity)
{
Expand Down Expand Up @@ -349,16 +356,19 @@ internal override LinearModel Transform()
}
}
// Ensure that one gravity requirement is met
foreach (var piece1 in Instance.Pieces)
if (Instance.Pieces.Count > 1)
{
model.AddConstr(
LinearExpression.Sum(Instance.PiecesWithVirtuals.Where(p => p != piece1).Select(piece2 =>
LinearExpression.Sum(piece1.Original.Components.Select(cube1 =>
LinearExpression.Sum(piece2.Original.Components.Select(cube2 =>
_locatedOn[cube1, cube2, piece1, piece2])))))) +
_locatedOnGround[piece1]
== 1,
"GravityEnsurance" + piece1.ToIdentString());
foreach (var piece1 in Instance.Pieces)
{
model.AddConstr(
LinearExpression.Sum(Instance.PiecesWithVirtuals.Where(p => p != piece1).Select(piece2 =>
LinearExpression.Sum(piece1.Original.Components.Select(cube1 =>
LinearExpression.Sum(piece2.Original.Components.Select(cube2 =>
_locatedOn[cube1, cube2, piece1, piece2])))))) +
_locatedOnGround[piece1]
== 1,
"GravityEnsurance" + piece1.ToIdentString());
}
}
}
// Ensure material compatibility only if desired
Expand Down
Loading

0 comments on commit c4dc407

Please sign in to comment.