修复超重问题

This commit is contained in:
admin 2025-04-21 14:13:49 +08:00
parent ae1d3459d9
commit 7247083a24
2 changed files with 17 additions and 16 deletions

33
main.go
View File

@ -97,7 +97,6 @@ func optimizePacking(con Container, box Box) ([]Placement, string, int) {
type candidate struct {
layout []Placement
count int
weight float64
strategy string
}
@ -133,38 +132,40 @@ func optimizePacking(con Container, box Box) ([]Placement, string, int) {
xCount = math.Floor(con.Length / r.Width)
}
total := int(xCount * yCount * zCount)
totalWeight := float64(total) * box.Weight
totalByVolume := int(xCount * yCount * zCount)
maxCountByWeight := int(math.Floor(con.WeightLimit / box.Weight))
actualCount := totalByVolume
if actualCount > maxCountByWeight {
actualCount = maxCountByWeight
}
if total > 0 {
fmt.Printf("Rotation: %v | Strategy: %s | Total: %d | Weight: %.2f kg\n", r, strategy, total, totalWeight)
if actualCount > 0 {
candidates = append(candidates, candidate{
layout: generateLayout(r, xCount, yCount, zCount, strategy),
count: total,
weight: totalWeight,
layout: generateLayout(r, xCount, yCount, zCount, strategy)[:actualCount],
count: actualCount,
strategy: strategy,
})
}
}
}
maxCountUnderLimit := 0
if len(candidates) == 0 {
return nil, "", 0
}
maxCount := 0
var finalLayout []Placement
var finalStrategy string
for _, c := range candidates {
if c.count > maxCountUnderLimit && c.weight <= con.WeightLimit {
maxCountUnderLimit = c.count
if c.count > maxCount {
maxCount = c.count
finalLayout = c.layout
finalStrategy = c.strategy
}
}
if maxCountUnderLimit == 0 {
return candidates[0].layout, candidates[0].strategy, candidates[0].count
}
return finalLayout, finalStrategy, maxCountUnderLimit
return finalLayout, finalStrategy, maxCount
}
func generateRotations(con Container, box Box) []Box {