修复超重问题

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