添加日志

This commit is contained in:
admin 2025-04-23 13:08:05 +08:00
parent f32634f97e
commit e8f3c527e6

57
main.go
View File

@ -37,6 +37,7 @@ type Layer struct {
} }
func main() { func main() {
fmt.Println("集装箱装箱系统已启动,监听端口 :8080")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html") http.ServeFile(w, r, "index.html")
}) })
@ -45,7 +46,16 @@ func main() {
Container Container `json:"container"` Container Container `json:"container"`
Box Box `json:"box"` Box Box `json:"box"`
} }
json.NewDecoder(r.Body).Decode(&data) err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, "参数解析失败", http.StatusBadRequest)
fmt.Printf("⚠️ 错误:参数解析失败 - %v\n", err)
return
}
fmt.Printf("🚀 收到请求:\n集装箱参数长%fmm × 宽%fmm × 高%fmm承重%fkg\n纸箱参数长%fmm × 宽%fmm × 高%fmm重量%fkg\n",
data.Container.Length, data.Container.Width, data.Container.Height, data.Container.WeightLimit,
data.Box.Length, data.Box.Width, data.Box.Height, data.Box.Weight)
layout, strategy, count := optimizePacking(data.Container, data.Box) layout, strategy, count := optimizePacking(data.Container, data.Box)
layerMap := make(map[float64][]Placement) layerMap := make(map[float64][]Placement)
for _, pos := range layout { for _, pos := range layout {
@ -62,9 +72,7 @@ func main() {
Count int `json:"count"` Count int `json:"count"`
Layers []Layer `json:"layers"` Layers []Layer `json:"layers"`
Strategy string `json:"strategy"` Strategy string `json:"strategy"`
Density float64 `json:"density"`
SpaceUtilization float64 `json:"spaceUtilization"` SpaceUtilization float64 `json:"spaceUtilization"`
UsedVolume float64 `json:"usedVolume"`
TotalWeight float64 `json:"totalWeight"` TotalWeight float64 `json:"totalWeight"`
BoxLength float64 `json:"boxLength"` BoxLength float64 `json:"boxLength"`
BoxWidth float64 `json:"boxWidth"` BoxWidth float64 `json:"boxWidth"`
@ -73,21 +81,23 @@ func main() {
Count: count, Count: count,
Layers: layers, Layers: layers,
Strategy: strategy, Strategy: strategy,
Density: calculateDensity(data.Container, data.Box, count),
SpaceUtilization: calculateDensity(data.Container, data.Box, count), SpaceUtilization: calculateDensity(data.Container, data.Box, count),
UsedVolume: float64(count) * data.Box.Length * data.Box.Width * data.Box.Height,
TotalWeight: float64(count) * data.Box.Weight, TotalWeight: float64(count) * data.Box.Weight,
BoxLength: data.Box.Length, BoxLength: data.Box.Length,
BoxWidth: data.Box.Width, BoxWidth: data.Box.Width,
BoxHeight: data.Box.Height, BoxHeight: data.Box.Height,
} }
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
fmt.Printf("✅ 返回结果:\n最优装箱数%d\n策略%s\n空间利用率%.2f%%\n", count, strategy, response.SpaceUtilization)
}) })
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }
func optimizePacking(con Container, box Box) ([]Placement, string, int) { func optimizePacking(con Container, box Box) ([]Placement, string, int) {
fmt.Printf("📦 开始装箱优化\n容器尺寸长%fmm × 宽%fmm × 高%fmm\n纸箱尺寸长%fmm × 宽%fmm × 高%fmm\n",
con.Length, con.Width, con.Height, box.Length, box.Width, box.Height)
rotations := generateRotations(con, box) rotations := generateRotations(con, box)
fmt.Printf("🔧 生成有效旋转方式:共%d种\n", len(rotations))
type candidate struct { type candidate struct {
layout []Placement layout []Placement
count int count int
@ -95,6 +105,7 @@ func optimizePacking(con Container, box Box) ([]Placement, string, int) {
} }
var candidates []candidate var candidates []candidate
for _, r := range rotations { for _, r := range rotations {
fmt.Printf("正在尝试旋转方式:%f×%f×%f\n", r.Length, r.Width, r.Height)
for _, strategy := range []string{"XY", "XZ", "YX", "YZ", "ZX", "ZY"} { for _, strategy := range []string{"XY", "XZ", "YX", "YZ", "ZX", "ZY"} {
var xCount, yCount, zCount float64 var xCount, yCount, zCount float64
switch strategy { switch strategy {
@ -130,15 +141,19 @@ func optimizePacking(con Container, box Box) ([]Placement, string, int) {
actualCount = maxCountByWeight actualCount = maxCountByWeight
} }
if actualCount > 0 { if actualCount > 0 {
layout := generateLayout(r, xCount, yCount, zCount, strategy)
candidates = append(candidates, candidate{ candidates = append(candidates, candidate{
layout: generateLayout(r, xCount, yCount, zCount, strategy)[:actualCount], layout: layout[:actualCount],
count: actualCount, count: actualCount,
strategy: strategy, strategy: strategy,
}) })
fmt.Printf("💡 新增方案:策略[%s] 可装载%d箱尺寸%dx%dx%d\n",
strategy, actualCount, xCount, yCount, zCount)
} }
} }
} }
if len(candidates) == 0 { if len(candidates) == 0 {
fmt.Printf("❗️ 未找到有效装箱方案\n")
return nil, "", 0 return nil, "", 0
} }
maxCount := 0 maxCount := 0
@ -151,22 +166,22 @@ func optimizePacking(con Container, box Box) ([]Placement, string, int) {
finalStrategy = c.strategy finalStrategy = c.strategy
} }
} }
fmt.Printf("🏆 最优方案:策略[%s] 可装载%d箱\n", finalStrategy, maxCount)
return finalLayout, finalStrategy, maxCount return finalLayout, finalStrategy, maxCount
} }
func generateRotations(con Container, box Box) []Box { func generateRotations(con Container, box Box) []Box {
validRotations := make([]Box, 0) validRotations := make([]Box, 0)
for _, r := range []Box{ rotations := []Box{
{box.Length, box.Width, box.Height, box.Weight}, {Length: box.Length, Width: box.Width, Height: box.Height, Weight: box.Weight},
{box.Length, box.Height, box.Width, box.Weight}, {Length: box.Length, Width: box.Height, Height: box.Width, Weight: box.Weight},
{box.Width, box.Length, box.Height, box.Weight}, {Length: box.Width, Width: box.Length, Height: box.Height, Weight: box.Weight},
{box.Width, box.Height, box.Length, box.Weight}, {Length: box.Width, Width: box.Height, Height: box.Length, Weight: box.Weight},
{box.Height, box.Length, box.Width, box.Weight}, {Length: box.Height, Width: box.Length, Height: box.Width, Weight: box.Weight},
{box.Height, box.Width, box.Length, box.Weight}, {Length: box.Height, Width: box.Width, Height: box.Length, Weight: box.Weight},
} { }
if r.Length <= con.Length && for _, r := range rotations {
r.Width <= con.Width && if r.Length <= con.Length && r.Width <= con.Width && r.Height <= con.Height {
r.Height <= con.Height {
validRotations = append(validRotations, r) validRotations = append(validRotations, r)
} }
} }
@ -174,6 +189,7 @@ func generateRotations(con Container, box Box) []Box {
} }
func generateLayout(r Box, xCount, yCount, zCount float64, strategy string) []Placement { func generateLayout(r Box, xCount, yCount, zCount float64, strategy string) []Placement {
fmt.Printf("正在生成布局:策略[%s](尺寸:%dx%dx%d\n", strategy, xCount, yCount, zCount)
var layout []Placement var layout []Placement
switch strategy { switch strategy {
case "XY": case "XY":
@ -273,7 +289,7 @@ func generateLayout(r Box, xCount, yCount, zCount float64, strategy string) []Pl
} }
} }
default: default:
fmt.Println("无效排列策略") fmt.Printf("❗️ 无效策略:%s\n", strategy)
} }
return layout return layout
} }
@ -281,5 +297,8 @@ func generateLayout(r Box, xCount, yCount, zCount float64, strategy string) []Pl
func calculateDensity(con Container, box Box, count int) float64 { func calculateDensity(con Container, box Box, count int) float64 {
containerVolume := con.Length * con.Width * con.Height containerVolume := con.Length * con.Width * con.Height
boxVolume := float64(count) * box.Length * box.Width * box.Height boxVolume := float64(count) * box.Length * box.Width * box.Height
return (boxVolume / containerVolume) * 100 density := (boxVolume / containerVolume) * 100
fmt.Printf("📊 体积利用率计算:%.2f%%(箱子总容积%fmm³ / 集装箱容积%fmm³\n",
density, boxVolume, containerVolume)
return density
} }