stream_app/main.go

305 lines
9.2 KiB
Go
Raw Permalink Normal View History

2025-04-18 13:20:01 +08:00
package main
import (
"encoding/json"
2025-04-21 14:12:26 +08:00
"fmt"
2025-04-18 13:20:01 +08:00
"math"
"net/http"
)
type Container struct {
2025-04-21 14:12:26 +08:00
Length float64 `json:"length"`
Width float64 `json:"width"`
Height float64 `json:"height"`
WeightLimit float64 `json:"weightLimit"`
2025-04-18 13:20:01 +08:00
}
type Box struct {
Length float64 `json:"length"`
Width float64 `json:"width"`
Height float64 `json:"height"`
2025-04-21 14:12:26 +08:00
Weight float64 `json:"weight"`
2025-04-18 13:20:01 +08:00
}
type Placement struct {
2025-04-22 18:59:06 +08:00
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
2025-04-21 14:12:26 +08:00
RotationX float64 `json:"rotationX"`
RotationY float64 `json:"rotationY"`
RotationZ float64 `json:"rotationZ"`
BoxNumber int `json:"boxNumber"`
2025-04-18 13:20:01 +08:00
}
2025-04-21 14:12:26 +08:00
type Layer struct {
Count int `json:"count"`
Layout []Placement `json:"layout"`
2025-04-18 14:03:36 +08:00
}
2025-04-18 13:20:01 +08:00
func main() {
2025-04-23 13:08:05 +08:00
fmt.Println("集装箱装箱系统已启动,监听端口 :8080")
2025-04-18 13:20:01 +08:00
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.HandleFunc("/calculate", func(w http.ResponseWriter, r *http.Request) {
var data struct {
Container Container `json:"container"`
Box Box `json:"box"`
}
2025-04-23 13:08:05 +08:00
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)
2025-04-21 14:12:26 +08:00
layout, strategy, count := optimizePacking(data.Container, data.Box)
layerMap := make(map[float64][]Placement)
for _, pos := range layout {
layerMap[pos.Z] = append(layerMap[pos.Z], pos)
}
var layers []Layer
2025-04-22 18:59:06 +08:00
for _, layer := range layerMap {
2025-04-21 14:12:26 +08:00
layers = append(layers, Layer{
Count: len(layer),
Layout: layer,
})
}
response := struct {
Count int `json:"count"`
Layers []Layer `json:"layers"`
Strategy string `json:"strategy"`
SpaceUtilization float64 `json:"spaceUtilization"`
TotalWeight float64 `json:"totalWeight"`
BoxLength float64 `json:"boxLength"`
BoxWidth float64 `json:"boxWidth"`
BoxHeight float64 `json:"boxHeight"`
}{
2025-04-18 14:03:36 +08:00
Count: count,
2025-04-21 14:12:26 +08:00
Layers: layers,
2025-04-18 14:03:36 +08:00
Strategy: strategy,
2025-04-22 18:59:06 +08:00
SpaceUtilization: calculateDensity(data.Container, data.Box, count),
2025-04-21 14:12:26 +08:00
TotalWeight: float64(count) * data.Box.Weight,
BoxLength: data.Box.Length,
BoxWidth: data.Box.Width,
BoxHeight: data.Box.Height,
2025-04-18 13:20:01 +08:00
}
json.NewEncoder(w).Encode(response)
2025-04-23 13:08:05 +08:00
fmt.Printf("✅ 返回结果:\n最优装箱数%d\n策略%s\n空间利用率%.2f%%\n", count, strategy, response.SpaceUtilization)
2025-04-18 13:20:01 +08:00
})
http.ListenAndServe(":8080", nil)
}
2025-04-21 14:12:26 +08:00
func optimizePacking(con Container, box Box) ([]Placement, string, int) {
2025-04-23 13:08:05 +08:00
fmt.Printf("📦 开始装箱优化\n容器尺寸长%fmm × 宽%fmm × 高%fmm\n纸箱尺寸长%fmm × 宽%fmm × 高%fmm\n",
con.Length, con.Width, con.Height, box.Length, box.Width, box.Height)
2025-04-21 14:12:26 +08:00
rotations := generateRotations(con, box)
2025-04-23 13:08:05 +08:00
fmt.Printf("🔧 生成有效旋转方式:共%d种\n", len(rotations))
2025-04-21 14:12:26 +08:00
type candidate struct {
layout []Placement
count int
strategy string
}
var candidates []candidate
2025-04-18 13:20:01 +08:00
for _, r := range rotations {
2025-04-23 13:08:05 +08:00
fmt.Printf("正在尝试旋转方式:%f×%f×%f\n", r.Length, r.Width, r.Height)
2025-04-18 13:20:01 +08:00
for _, strategy := range []string{"XY", "XZ", "YX", "YZ", "ZX", "ZY"} {
var xCount, yCount, zCount float64
switch strategy {
case "XY":
xCount = math.Floor(con.Length / r.Length)
yCount = math.Floor(con.Width / r.Width)
zCount = math.Floor(con.Height / r.Height)
case "XZ":
xCount = math.Floor(con.Length / r.Length)
zCount = math.Floor(con.Height / r.Height)
yCount = math.Floor(con.Width / r.Width)
case "YX":
yCount = math.Floor(con.Width / r.Length)
xCount = math.Floor(con.Length / r.Width)
zCount = math.Floor(con.Height / r.Height)
case "YZ":
yCount = math.Floor(con.Width / r.Length)
zCount = math.Floor(con.Height / r.Height)
xCount = math.Floor(con.Length / r.Width)
case "ZX":
zCount = math.Floor(con.Height / r.Length)
xCount = math.Floor(con.Length / r.Width)
yCount = math.Floor(con.Width / r.Height)
case "ZY":
zCount = math.Floor(con.Height / r.Length)
yCount = math.Floor(con.Width / r.Height)
xCount = math.Floor(con.Length / r.Width)
}
2025-04-21 14:13:49 +08:00
totalByVolume := int(xCount * yCount * zCount)
maxCountByWeight := int(math.Floor(con.WeightLimit / box.Weight))
actualCount := totalByVolume
if actualCount > maxCountByWeight {
actualCount = maxCountByWeight
}
if actualCount > 0 {
2025-04-23 13:08:05 +08:00
layout := generateLayout(r, xCount, yCount, zCount, strategy)
2025-04-21 14:12:26 +08:00
candidates = append(candidates, candidate{
2025-04-23 13:08:05 +08:00
layout: layout[:actualCount],
2025-04-21 14:13:49 +08:00
count: actualCount,
2025-04-21 14:12:26 +08:00
strategy: strategy,
})
2025-04-23 13:08:05 +08:00
fmt.Printf("💡 新增方案:策略[%s] 可装载%d箱尺寸%dx%dx%d\n",
strategy, actualCount, xCount, yCount, zCount)
2025-04-18 13:20:01 +08:00
}
}
}
2025-04-21 14:13:49 +08:00
if len(candidates) == 0 {
2025-04-23 13:08:05 +08:00
fmt.Printf("❗️ 未找到有效装箱方案\n")
2025-04-21 14:13:49 +08:00
return nil, "", 0
}
maxCount := 0
2025-04-21 14:12:26 +08:00
var finalLayout []Placement
var finalStrategy string
for _, c := range candidates {
2025-04-21 14:13:49 +08:00
if c.count > maxCount {
maxCount = c.count
2025-04-21 14:12:26 +08:00
finalLayout = c.layout
finalStrategy = c.strategy
}
}
2025-04-23 13:08:05 +08:00
fmt.Printf("🏆 最优方案:策略[%s] 可装载%d箱\n", finalStrategy, maxCount)
2025-04-21 14:13:49 +08:00
return finalLayout, finalStrategy, maxCount
2025-04-18 13:20:01 +08:00
}
2025-04-21 14:12:26 +08:00
func generateRotations(con Container, box Box) []Box {
validRotations := make([]Box, 0)
2025-04-23 13:08:05 +08:00
rotations := []Box{
{Length: box.Length, Width: box.Width, Height: box.Height, Weight: box.Weight},
{Length: box.Length, Width: box.Height, Height: box.Width, Weight: box.Weight},
{Length: box.Width, Width: box.Length, Height: box.Height, Weight: box.Weight},
{Length: box.Width, Width: box.Height, Height: box.Length, Weight: box.Weight},
{Length: box.Height, Width: box.Length, Height: box.Width, Weight: box.Weight},
{Length: box.Height, Width: box.Width, Height: box.Length, Weight: box.Weight},
}
for _, r := range rotations {
if r.Length <= con.Length && r.Width <= con.Width && r.Height <= con.Height {
2025-04-21 14:12:26 +08:00
validRotations = append(validRotations, r)
}
2025-04-18 13:20:01 +08:00
}
2025-04-21 14:12:26 +08:00
return validRotations
2025-04-18 13:20:01 +08:00
}
func generateLayout(r Box, xCount, yCount, zCount float64, strategy string) []Placement {
2025-04-23 13:08:05 +08:00
fmt.Printf("正在生成布局:策略[%s](尺寸:%dx%dx%d\n", strategy, xCount, yCount, zCount)
2025-04-18 13:20:01 +08:00
var layout []Placement
switch strategy {
case "XY":
for x := 0.0; x < xCount; x++ {
for y := 0.0; y < yCount; y++ {
for z := 0.0; z < zCount; z++ {
layout = append(layout, Placement{
2025-04-21 14:12:26 +08:00
X: x * r.Length,
2025-04-22 18:59:06 +08:00
Y: y * r.Width,
Z: z * r.Height,
2025-04-21 14:12:26 +08:00
RotationX: 0.0,
RotationY: 0.0,
RotationZ: 0.0,
2025-04-22 18:59:06 +08:00
BoxNumber: len(layout),
2025-04-18 13:20:01 +08:00
})
}
}
}
case "XZ":
for x := 0.0; x < xCount; x++ {
for z := 0.0; z < zCount; z++ {
for y := 0.0; y < yCount; y++ {
layout = append(layout, Placement{
2025-04-21 14:12:26 +08:00
X: x * r.Length,
2025-04-22 18:59:06 +08:00
Y: y * r.Width,
Z: z * r.Height,
2025-04-21 14:12:26 +08:00
RotationX: 0.0,
RotationY: 0.0,
RotationZ: 0.0,
2025-04-22 18:59:06 +08:00
BoxNumber: len(layout),
2025-04-18 13:20:01 +08:00
})
}
}
}
case "YX":
for y := 0.0; y < yCount; y++ {
for x := 0.0; x < xCount; x++ {
for z := 0.0; z < zCount; z++ {
layout = append(layout, Placement{
2025-04-21 14:12:26 +08:00
X: x * r.Width,
2025-04-22 18:59:06 +08:00
Y: y * r.Length,
Z: z * r.Height,
2025-04-21 14:12:26 +08:00
RotationX: 0.0,
RotationY: 0.0,
RotationZ: 0.0,
2025-04-22 18:59:06 +08:00
BoxNumber: len(layout),
2025-04-18 13:20:01 +08:00
})
}
}
}
case "YZ":
for y := 0.0; y < yCount; y++ {
for z := 0.0; z < zCount; z++ {
for x := 0.0; x < xCount; x++ {
layout = append(layout, Placement{
2025-04-21 14:12:26 +08:00
X: x * r.Width,
2025-04-22 18:59:06 +08:00
Y: y * r.Length,
Z: z * r.Height,
2025-04-21 14:12:26 +08:00
RotationX: 0.0,
RotationY: 0.0,
RotationZ: 0.0,
2025-04-22 18:59:06 +08:00
BoxNumber: len(layout),
2025-04-18 13:20:01 +08:00
})
}
}
}
case "ZX":
for z := 0.0; z < zCount; z++ {
for x := 0.0; x < xCount; x++ {
for y := 0.0; y < yCount; y++ {
layout = append(layout, Placement{
2025-04-21 14:12:26 +08:00
X: x * r.Width,
2025-04-22 18:59:06 +08:00
Y: y * r.Height,
Z: z * r.Length,
2025-04-21 14:12:26 +08:00
RotationX: 0.0,
RotationY: 0.0,
RotationZ: 0.0,
2025-04-22 18:59:06 +08:00
BoxNumber: len(layout),
2025-04-18 13:20:01 +08:00
})
}
}
}
case "ZY":
for z := 0.0; z < zCount; z++ {
for y := 0.0; y < yCount; y++ {
for x := 0.0; x < xCount; x++ {
layout = append(layout, Placement{
2025-04-21 14:12:26 +08:00
X: x * r.Width,
2025-04-22 18:59:06 +08:00
Y: y * r.Height,
Z: z * r.Length,
2025-04-21 14:12:26 +08:00
RotationX: 0.0,
RotationY: 0.0,
RotationZ: 0.0,
2025-04-22 18:59:06 +08:00
BoxNumber: len(layout),
2025-04-18 13:20:01 +08:00
})
}
}
}
2025-04-21 14:12:26 +08:00
default:
2025-04-23 13:08:05 +08:00
fmt.Printf("❗️ 无效策略:%s\n", strategy)
2025-04-18 13:20:01 +08:00
}
return layout
}
2025-04-21 14:12:26 +08:00
func calculateDensity(con Container, box Box, count int) float64 {
containerVolume := con.Length * con.Width * con.Height
boxVolume := float64(count) * box.Length * box.Width * box.Height
2025-04-23 13:08:05 +08:00
density := (boxVolume / containerVolume) * 100
fmt.Printf("📊 体积利用率计算:%.2f%%(箱子总容积%fmm³ / 集装箱容积%fmm³\n",
density, boxVolume, containerVolume)
return density
2025-04-21 14:12:26 +08:00
}